Module: Vertica

Defined in:
lib/vertica.rb,
lib/vertica/version.rb,
lib/vertica/protocol/message.rb,
lib/vertica/protocol/frontend/bind.rb,
lib/vertica/protocol/frontend/sync.rb,
lib/vertica/protocol/frontend/close.rb,
lib/vertica/protocol/frontend/flush.rb,
lib/vertica/protocol/frontend/parse.rb,
lib/vertica/protocol/frontend/query.rb,
lib/vertica/protocol/backend/no_data.rb,
lib/vertica/protocol/backend/unknown.rb,
lib/vertica/protocol/backend/data_row.rb,
lib/vertica/protocol/frontend/execute.rb,
lib/vertica/protocol/frontend/startup.rb,
lib/vertica/protocol/frontend/describe.rb,
lib/vertica/protocol/frontend/password.rb,
lib/vertica/protocol/frontend/copy_data.rb,
lib/vertica/protocol/frontend/copy_done.rb,
lib/vertica/protocol/frontend/copy_fail.rb,
lib/vertica/protocol/frontend/terminate.rb,
lib/vertica/protocol/frontend/ssl_request.rb,
lib/vertica/protocol/backend/bind_complete.rb,
lib/vertica/protocol/backend/authentication.rb,
lib/vertica/protocol/backend/close_complete.rb,
lib/vertica/protocol/backend/error_response.rb,
lib/vertica/protocol/backend/parse_complete.rb,
lib/vertica/protocol/backend/notice_response.rb,
lib/vertica/protocol/backend/ready_for_query.rb,
lib/vertica/protocol/backend/row_description.rb,
lib/vertica/protocol/frontend/cancel_request.rb,
lib/vertica/protocol/backend/backend_key_data.rb,
lib/vertica/protocol/backend/command_complete.rb,
lib/vertica/protocol/backend/copy_in_response.rb,
lib/vertica/protocol/backend/parameter_status.rb,
lib/vertica/protocol/backend/portal_suspended.rb,
lib/vertica/protocol/backend/empty_query_response.rb,
lib/vertica/protocol/backend/parameter_description.rb

Overview

Main module for this library. It contains the Vertica.connect method to return a Connection instance, and methods to quote values (Vertica.quote) and identifiers (Vertica.quote_identifier) to safely include those in SQL strings to prevent SQL injection.

Defined Under Namespace

Modules: Protocol Classes: Column, Connection, DataType, Error, Query, Result, Row, RowDescription

Constant Summary collapse

PROTOCOL_VERSION =

The protocol version (3.0.0) implemented in this library.

3 << 16
VERSION =

The version of the package. We adhere to semantic versioning. To release a new version, update this constant, commit to master, and run rake release

"1.0.3"

Class Method Summary collapse

Class Method Details

.connect(**kwargs) ⇒ Vertica::Connection

Opens a new connection to a Vertica database.

Parameters:

  • host (String)

    The hostname to connect to. E.g. localhost

  • port (Integer)

    The port to connect to. Defaults to 5433.

  • username (String)

    The username for the session.

  • password (String)

    The password for the session.

  • interruptable (true, false)

    Whether to make this session interruptible. Setting this to true allows you to interrupt sessions and queries, but requires running a query during startup in order to obtain the session id.

  • ssl (OpenSSL::SSL::SSLContext, Boolean)

    Set this to an OpenSSL::SSL::SSLContext instance to require the connection to be encrypted using SSL/TLS. true will use the default SSL options. Not every server has support for SSL encryption. In that case you'll have to leave this to false.

  • read_timeout (Integer)

    The number of seconds to wait for data on the connection. You should set this to a sufficiently high value when executing complicated queries that require a long time to be evaluated.

  • role (Array<String>, :all, :none, :default)

    A list of additional roles to enable for the session. See the Vertica documentation for SET ROLE.

  • timezone (String)

    The timezone to use for the session. See the Vertica documentation for SET TIME ZONE.

  • search_path (Array<String>)

    A list of schemas to use as search path. See the Vertica documentation for SET SEARCH_PATH.

  • autocommit (Boolean)

    Enable autocommit on the session. See the Vertica documentation for more information.

  • debug (Boolean)

    Setting this to true will log all the communication between client and server to STDOUT. Useful when developing this library.

Returns:



18
19
20
# File 'lib/vertica.rb', line 18

def self.connect(**kwargs)
  Vertica::Connection.new(**kwargs)
end

.quote(value) ⇒ String

Properly quotes a value for safe usage in SQL queries.

This method has quoting rules for common types. Any other object will be converted to a string using +:to_s+ and then quoted as a string.

Parameters:

  • value (Object)

    The value to quote.

Returns:

  • (String)

    The quoted value that can be safely included in SQL queries.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vertica.rb', line 29

def self.quote(value)
  case value
    when nil        then 'NULL'
    when false      then 'FALSE'
    when true       then 'TRUE'
    when DateTime   then value.strftime("'%Y-%m-%dT%H:%M:%S.%6N%z'::timestamptz")
    when Time       then value.strftime("'%Y-%m-%dT%H:%M:%S.%6N%z'::timestamptz")
    when Date       then value.strftime("'%Y-%m-%d'::date")
    when String     then "'#{value.gsub(/'/, "''")}'"
    when BigDecimal then value.to_s('F')
    when Numeric    then value.to_s
    when Array      then value.map { |v| self.quote(v) }.join(', ')
    else self.quote(value.to_s)
  end
end

.quote_identifier(identifier) ⇒ String

Quotes an identifier for safe use within SQL queries, using double quotes.

Parameters:

  • identifier (:to_s)

    The identifier to quote.

Returns:

  • (String)

    The quoted identifier that can be safely included in SQL queries.



48
49
50
# File 'lib/vertica.rb', line 48

def self.quote_identifier(identifier)
  "\"#{identifier.to_s.gsub(/"/, '""')}\""
end