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
- #busy? ⇒ Boolean
- #cancel ⇒ Object
- #close ⇒ 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) ⇒ 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 28 29 30 |
# 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] ||= 30 @row_style = @options[:row_style] ? @options[:row_style] : :hash unless [:skip_startup] startup_connection initialize_connection end 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
#busy? ⇒ Boolean
67 68 69 |
# File 'lib/vertica/connection.rb', line 67 def busy? !ready_for_query? end |
#cancel ⇒ Object
97 98 99 100 101 102 |
# File 'lib/vertica/connection.rb', line 97 def cancel conn = self.class.new(.merge(:skip_startup => true)) conn.write Vertica::Messages::CancelRequest.new(backend_pid, backend_key) conn.write Vertica::Messages::Flush.new conn.socket.close end |
#close ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/vertica/connection.rb', line 81 def close write Vertica::Messages::Terminate.new socket.close @socket = nil rescue Errno::ENOTCONN # the backend closed the socket already ensure reset_values end |
#closed? ⇒ Boolean
63 64 65 |
# File 'lib/vertica/connection.rb', line 63 def closed? !opened? end |
#copy(sql, source = nil, &block) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/vertica/connection.rb', line 155 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_job_lock(job) end |
#inspect ⇒ Object
167 168 169 170 |
# File 'lib/vertica/connection.rb', line 167 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
104 105 106 107 108 109 110 |
# File 'lib/vertica/connection.rb', line 104 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
112 113 114 |
# File 'lib/vertica/connection.rb', line 112 def interruptable? !session_id.nil? end |
#on_notice(&block) ⇒ Object
32 33 34 |
# File 'lib/vertica/connection.rb', line 32 def on_notice(&block) @notice_handler = block end |
#opened? ⇒ Boolean
59 60 61 |
# File 'lib/vertica/connection.rb', line 59 def opened? @socket && @backend_pid && @transaction_status end |
#process_message(message) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/vertica/connection.rb', line 130 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 @current_job = nil else raise Vertica::Error::MessageError, "Unhandled message: #{.inspect}" end end |
#query(sql, options = {}, &block) ⇒ Object
149 150 151 152 153 |
# File 'lib/vertica/connection.rb', line 149 def query(sql, = {}, &block) job = Vertica::Query.new(self, sql, { :row_style => @row_style }.merge()) job.row_handler = block if block_given? run_with_job_lock(job) end |
#read_message ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/vertica/connection.rb', line 116 def ready = IO.select([socket], nil, nil, @options[:read_timeout]) if ready 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 else raise Errno::ETIMEDOUT end end |
#ready_for_query? ⇒ Boolean
71 72 73 |
# File 'lib/vertica/connection.rb', line 71 def ready_for_query? @current_job.nil? end |
#reset_connection ⇒ Object
90 91 92 93 94 95 |
# File 'lib/vertica/connection.rb', line 90 def reset_connection close startup_connection initialize_connection end |
#socket ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/vertica/connection.rb', line 36 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
55 56 57 |
# File 'lib/vertica/connection.rb', line 55 def ssl? Object.const_defined?('OpenSSL') && socket.kind_of?(OpenSSL::SSL::SSLSocket) end |
#write(message) ⇒ Object
75 76 77 78 79 |
# File 'lib/vertica/connection.rb', line 75 def write() raise ArgumentError, "invalid message: (#{.inspect})" unless .respond_to?(:to_bytes) puts "=> #{.inspect}" if @debug socket.write .to_bytes end |