Class: Vertica::Connection
- Inherits:
-
Object
- Object
- Vertica::Connection
- Defined in:
- lib/vertica/connection.rb
Overview
A client for a Vertica server, which allows you to run queries against it.
Use connect to establish a connection. Then, the #query method will allow you
to run SQL queries against the database. For COPY FROM STDIN
queries, use the #copy method
instead. You can use #interrupt to interrupt long running queries. #close will close the
connection to the server.
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
The connection options provided to the constructor.
-
#parameters ⇒ Hash<String, String>
readonly
Connection parameters as provided by the server.
-
#transaction_status ⇒ :no_transaction, ...
readonly
The current transaction state of the session.
Instance Method Summary collapse
-
#busy? ⇒ Boolean
Returns true iff the connection is in use.
-
#cancel
Cancels the current query.
-
#close
Closes the connection to the Vertica server.
-
#closed? ⇒ Boolean
Returns false iff the connection to the server is opened.
-
#copy(sql, source: nil) {|io| ... } ⇒ String
Loads data into Vertica using a
COPY table FROM STDIN
query. -
#initialize(host: nil, port: 5433, username: nil, password: nil, database: nil, interruptable: false, ssl: false, read_timeout: 600, debug: false, role: nil, search_path: nil, timezone: nil, autocommit: false, skip_startup: false, skip_initialize: false, user: nil) ⇒ Connection
constructor
Creates a connection the a Vertica server.
-
#inspect ⇒ String
Returns a user-consumable string representation of this row.
-
#interrupt
Interrupts this session to the Vertica server, which will cancel the running query.
-
#interruptable? ⇒ Boolean
Returns true iff the connection can be interrupted.
-
#on_notice(&block)
Installs a hanlder for notices that may be sent from the server to the client.
-
#opened? ⇒ Boolean
Returns true iff the connection to the server is opened.
-
#query(sql, &block) ⇒ Object
Runs a SQL query against the database.
-
#ready_for_query? ⇒ Boolean
Returns true iff the connection is ready to handle queries.
-
#ssl? ⇒ Boolean
Returns true iff the connection is encrypted.
Constructor Details
#initialize(host: nil, port: 5433, username: nil, password: nil, database: nil, interruptable: false, ssl: false, read_timeout: 600, debug: false, role: nil, search_path: nil, timezone: nil, autocommit: false, skip_startup: false, skip_initialize: false, user: nil) ⇒ Connection
Creates a connection the a Vertica server.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/vertica/connection.rb', line 53 def initialize(host: nil, port: 5433, username: nil, password: nil, database: nil, interruptable: false, ssl: false, read_timeout: 600, debug: false, role: nil, search_path: nil, timezone: nil, autocommit: false, skip_startup: false, skip_initialize: false, user: nil) reset_state @notice_handler = nil = { host: host, port: port.to_i, username: username || user, password: password, database: database, debug: debug, ssl: ssl, interruptable: interruptable, read_timeout: read_timeout, role: role, search_path: search_path, timezone: timezone, autocommit: autocommit, } boot_connection(skip_initialize: skip_initialize) unless skip_startup end |
Instance Attribute Details
#options ⇒ Hash (readonly)
The connection options provided to the constructor. See #initialize.
25 26 27 |
# File 'lib/vertica/connection.rb', line 25 def end |
#parameters ⇒ Hash<String, String> (readonly)
Connection parameters as provided by the server.
25 26 27 |
# File 'lib/vertica/connection.rb', line 25 def parameters @parameters end |
#transaction_status ⇒ :no_transaction, ... (readonly)
The current transaction state of the session. This will be updated after every query.
25 26 27 |
# File 'lib/vertica/connection.rb', line 25 def transaction_status @transaction_status end |
Instance Method Details
#busy? ⇒ Boolean
Returns true iff the connection is in use.
94 95 96 |
# File 'lib/vertica/connection.rb', line 94 def busy? @mutex.locked? end |
#cancel
Vertica's protocol is based on the PostgreSQL protocol. This method to cancel sessions
in PostgreSQL is accepted by the Vertica server, but I haven't actually observed queries
actually being cancelled when using this method. Vertica provides an alternative method, by
running SELECT CLOSE_SESSION(session_id)
as a query on a different connection. See #interrupt.
This method returns an undefined value.
Cancels the current query.
215 216 217 218 219 220 |
# File 'lib/vertica/connection.rb', line 215 def cancel conn = self.class.new(skip_startup: true, **) conn.(Vertica::Protocol::CancelRequest.new(backend_pid, backend_key)) conn.(Vertica::Protocol::Flush.new) conn.close_socket end |
#close
This method returns an undefined value.
Closes the connection to the Vertica server.
200 201 202 203 204 |
# File 'lib/vertica/connection.rb', line 200 def close (Vertica::Protocol::Terminate.new) ensure close_socket end |
#closed? ⇒ Boolean
Even if the connection is closed, it will be opened automatically if you use it.
Returns false iff the connection to the server is opened.
89 90 91 |
# File 'lib/vertica/connection.rb', line 89 def closed? !opened? end |
#copy(sql, source: nil) {|io| ... } ⇒ String
Loads data into Vertica using a COPY table FROM STDIN
query.
179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/vertica/connection.rb', line 179 def copy(sql, source: nil, &block) copy_handler = if block_given? block elsif source && File.exist?(source.to_s) lambda { |data| file_copy_handler(source, data) } elsif source.respond_to?(:read) && source.respond_to?(:eof?) lambda { |data| io_copy_handler(source, data) } end run_in_mutex(Vertica::Query.new(self, sql, copy_handler: copy_handler)) end |
#inspect ⇒ String
Returns a user-consumable string representation of this row.
193 194 195 196 |
# File 'lib/vertica/connection.rb', line 193 def inspect = .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=#{safe_options.inspect}>" end |
#interrupt
This method returns an undefined value.
Interrupts this session to the Vertica server, which will cancel the running query.
You'll have to call this method in a separate thread. It will open up a separate connection, and run
SELECT CLOSE_SESSION(current_session_id)
to close the current session. In order to be able to do this
the client needs to know its session ID. You'll have to pass interruptable: true
as a connection
parameter (see #initialize) to make sure the connection will request its session id, by running
SELECT session_id FROM v_monitor.current_session
right after the connection is opened.
234 235 236 237 238 239 240 |
# File 'lib/vertica/connection.rb', line 234 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(skip_initialize: true, **) conn.query("SELECT CLOSE_SESSION(#{Vertica.quote(session_id)})").the_value ensure conn.close if conn end |
#interruptable? ⇒ Boolean
Returns true iff the connection can be interrupted.
Connections can only be interrupted if the session ID is known, so it can
run SELECT CLOSE_SESSION(session_id)
using a separate connection. By passing
interruptable: true
as a connection parameter (see #initialize), the connection
will discover its session id before you can use it, allowing it to be interrupted.
112 113 114 |
# File 'lib/vertica/connection.rb', line 112 def interruptable? !session_id.nil? end |
#on_notice(&block)
This method returns an undefined value.
Installs a hanlder for notices that may be sent from the server to the client.
You can only install one handler; if you call this method again it will replace the previous handler.
248 249 250 |
# File 'lib/vertica/connection.rb', line 248 def on_notice(&block) @notice_handler = block end |
#opened? ⇒ Boolean
The connection will be opened automatically if you use it.
Returns true iff the connection to the server is opened.
83 84 85 |
# File 'lib/vertica/connection.rb', line 83 def opened? @socket && @backend_pid && @transaction_status end |
#query(sql) ⇒ Vertica::Result #query(sql) {|row| ... } ⇒ String
Runs a SQL query against the database.
145 146 147 |
# File 'lib/vertica/connection.rb', line 145 def query(sql, &block) run_in_mutex(Vertica::Query.new(self, sql, row_handler: block)) end |
#ready_for_query? ⇒ Boolean
Returns true iff the connection is ready to handle queries.
99 100 101 |
# File 'lib/vertica/connection.rb', line 99 def ready_for_query? !busy? end |
#ssl? ⇒ Boolean
Returns true iff the connection is encrypted.
77 78 79 |
# File 'lib/vertica/connection.rb', line 77 def ssl? Object.const_defined?('OpenSSL') && @socket.kind_of?(OpenSSL::SSL::SSLSocket) end |