Class: Vertica::Connection
- Inherits:
-
Object
- Object
- Vertica::Connection
- Defined in:
- lib/vertica/connection.rb
Instance Attribute Summary collapse
-
#backend_key ⇒ Object
readonly
Returns the value of attribute backend_key.
-
#backend_pid ⇒ Object
readonly
Returns the value of attribute backend_pid.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#notice_handler ⇒ Object
readonly
Returns the value of attribute notice_handler.
-
#notices ⇒ Object
readonly
Returns the value of attribute notices.
-
#options ⇒ Object
Returns the value of attribute options.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#row_style ⇒ Object
Returns the value of attribute row_style.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
-
#transaction_status ⇒ Object
readonly
Returns the value of attribute transaction_status.
Class Method Summary collapse
Instance Method Summary collapse
- #boot_connection ⇒ Object
- #busy? ⇒ Boolean
- #cancel ⇒ Object
- #close ⇒ Object
- #close_socket ⇒ Object
- #closed? ⇒ Boolean
- #copy(sql, source = nil, &block) ⇒ Object
-
#initialize(options = {}) ⇒ Connection
constructor
Opens a connectio the a Vertica server.
- #inspect ⇒ Object
- #interrupt ⇒ Object
- #interruptable? ⇒ Boolean
- #on_notice(&block) ⇒ Object
- #opened? ⇒ Boolean
- #process_message(message) ⇒ Object
- #query(sql, options = {}, &block) ⇒ Object
- #read_message ⇒ Object
- #ready_for_query? ⇒ Boolean
- #reset_connection ⇒ Object
- #socket ⇒ Object
- #ssl? ⇒ Boolean
- #write_message(message) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Connection
Opens a connectio the a Vertica server
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/vertica/connection.rb', line 15 def initialize( = {}) reset_values @options = {} .each { |key, value| @options[key.to_s.to_sym] = value if value} @options[:port] ||= 5433 @options[:read_timeout] ||= 600 @row_style = @options[:row_style] ? @options[:row_style] : :hash boot_connection unless [:skip_startup] end |
Instance Attribute Details
#backend_key ⇒ Object (readonly)
Returns the value of attribute backend_key.
5 6 7 |
# File 'lib/vertica/connection.rb', line 5 def backend_key @backend_key end |
#backend_pid ⇒ Object (readonly)
Returns the value of attribute backend_pid.
5 6 7 |
# File 'lib/vertica/connection.rb', line 5 def backend_pid @backend_pid end |
#debug ⇒ Object
Returns the value of attribute debug.
7 8 9 |
# File 'lib/vertica/connection.rb', line 7 def debug @debug end |
#notice_handler ⇒ Object (readonly)
Returns the value of attribute notice_handler.
5 6 7 |
# File 'lib/vertica/connection.rb', line 5 def notice_handler @notice_handler end |
#notices ⇒ Object (readonly)
Returns the value of attribute notices.
5 6 7 |
# File 'lib/vertica/connection.rb', line 5 def notices @notices end |
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/vertica/connection.rb', line 7 def @options end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
5 6 7 |
# File 'lib/vertica/connection.rb', line 5 def parameters @parameters end |
#row_style ⇒ Object
Returns the value of attribute row_style.
7 8 9 |
# File 'lib/vertica/connection.rb', line 7 def row_style @row_style end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
5 6 7 |
# File 'lib/vertica/connection.rb', line 5 def session_id @session_id end |
#transaction_status ⇒ Object (readonly)
Returns the value of attribute transaction_status.
5 6 7 |
# File 'lib/vertica/connection.rb', line 5 def transaction_status @transaction_status end |
Class Method Details
.cancel(existing_conn) ⇒ Object
9 10 11 |
# File 'lib/vertica/connection.rb', line 9 def self.cancel(existing_conn) existing_conn.cancel end |
Instance Method Details
#boot_connection ⇒ Object
99 100 101 102 |
# File 'lib/vertica/connection.rb', line 99 def boot_connection startup_connection initialize_connection end |
#busy? ⇒ Boolean
64 65 66 |
# File 'lib/vertica/connection.rb', line 64 def busy? @mutex.locked? end |
#cancel ⇒ Object
104 105 106 107 108 109 |
# File 'lib/vertica/connection.rb', line 104 def cancel conn = self.class.new(.merge(:skip_startup => true)) conn. Vertica::Messages::CancelRequest.new(backend_pid, backend_key) conn. Vertica::Messages::Flush.new conn.socket.close end |
#close ⇒ Object
80 81 82 83 84 |
# File 'lib/vertica/connection.rb', line 80 def close Vertica::Messages::Terminate.new ensure close_socket end |
#close_socket ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/vertica/connection.rb', line 86 def close_socket socket.close @socket = nil rescue SystemCallError, IOError ensure reset_values end |
#closed? ⇒ Boolean
60 61 62 |
# File 'lib/vertica/connection.rb', line 60 def closed? !opened? end |
#copy(sql, source = nil, &block) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/vertica/connection.rb', line 160 def copy(sql, source = nil, &block) job = Vertica::Query.new(self, sql, :row_style => @row_style) if block_given? job.copy_handler = block elsif source && File.exists?(source.to_s) job.copy_handler = lambda { |data| file_copy_handler(source, data) } elsif source.respond_to?(:read) && source.respond_to?(:eof?) job.copy_handler = lambda { |data| io_copy_handler(source, data) } end run_with_mutex(job) end |
#inspect ⇒ Object
172 173 174 175 |
# File 'lib/vertica/connection.rb', line 172 def inspect = @options.reject{ |name, _| name == :password } "#<Vertica::Connection:#{object_id} @parameters=#{@parameters.inspect} @backend_pid=#{@backend_pid}, @backend_key=#{@backend_key}, @transaction_status=#{@transaction_status}, @socket=#{@socket}, @options=#{.inspect}, @row_style=#{@row_style}>" end |
#interrupt ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/vertica/connection.rb', line 111 def interrupt raise Vertica::Error::InterruptImpossible, "Session cannopt be interrupted because the session ID is not known!" if session_id.nil? conn = self.class.new(.merge(:interruptable => false, :role => nil, :search_path => nil)) response = conn.query("SELECT CLOSE_SESSION(#{Vertica.quote(session_id)})").the_value conn.close return response end |
#interruptable? ⇒ Boolean
119 120 121 |
# File 'lib/vertica/connection.rb', line 119 def interruptable? !session_id.nil? end |
#on_notice(&block) ⇒ Object
29 30 31 |
# File 'lib/vertica/connection.rb', line 29 def on_notice(&block) @notice_handler = block end |
#opened? ⇒ Boolean
56 57 58 |
# File 'lib/vertica/connection.rb', line 56 def opened? @socket && @backend_pid && @transaction_status end |
#process_message(message) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/vertica/connection.rb', line 135 def () case when Vertica::Messages::ErrorResponse raise Vertica::Error::ConnectionError.new(.) when Vertica::Messages::NoticeResponse @notice_handler.call() if @notice_handler when Vertica::Messages::BackendKeyData @backend_pid = .pid @backend_key = .key when Vertica::Messages::ParameterStatus @parameters[.name] = .value when Vertica::Messages::ReadyForQuery @transaction_status = .transaction_status @mutex.unlock else raise Vertica::Error::MessageError, "Unhandled message: #{.inspect}" end end |
#query(sql, options = {}, &block) ⇒ Object
154 155 156 157 158 |
# File 'lib/vertica/connection.rb', line 154 def query(sql, = {}, &block) job = Vertica::Query.new(self, sql, { :row_style => @row_style }.merge()) job.row_handler = block if block_given? run_with_mutex(job) end |
#read_message ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/vertica/connection.rb', line 123 def type = read_bytes(1) size = read_bytes(4).unpack('N').first raise Vertica::Error::MessageError.new("Bad message size: #{size}.") unless size >= 4 = Vertica::Messages::BackendMessage.factory type, read_bytes(size - 4) puts "<= #{.inspect}" if @debug return rescue SystemCallError, IOError => e close_socket raise Vertica::Error::ConnectionError.new(e.) end |
#ready_for_query? ⇒ Boolean
68 69 70 |
# File 'lib/vertica/connection.rb', line 68 def ready_for_query? !busy? end |
#reset_connection ⇒ Object
94 95 96 97 |
# File 'lib/vertica/connection.rb', line 94 def reset_connection close boot_connection end |
#socket ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/vertica/connection.rb', line 33 def socket @socket ||= begin raw_socket = TCPSocket.new(@options[:host], @options[:port].to_i) if @options[:ssl] require 'openssl' raw_socket.write Vertica::Messages::SslRequest.new.to_bytes if raw_socket.read(1) == 'S' raw_socket = OpenSSL::SSL::SSLSocket.new(raw_socket, OpenSSL::SSL::SSLContext.new) raw_socket.sync = true raw_socket.connect else raise Vertica::Error::SSLNotSupported.new("SSL requested but server doesn't support it.") end end raw_socket end end |
#ssl? ⇒ Boolean
52 53 54 |
# File 'lib/vertica/connection.rb', line 52 def ssl? Object.const_defined?('OpenSSL') && @socket.kind_of?(OpenSSL::SSL::SSLSocket) end |
#write_message(message) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/vertica/connection.rb', line 72 def () puts "=> #{.inspect}" if @debug write_bytes .to_bytes rescue SystemCallError, IOError => e close_socket raise Vertica::Error::ConnectionError.new(e.) end |