Class: Vertica::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/vertica/query.rb

Overview

Note:

This class is for internal use only, you should never interact with this class directly.

The Query class handles the state of the connection while a SQL query is being executed. The connection should call #run and it will block until the query has been handled by the connection, after which control will be given back to the Connection instance.

Instance Method Summary collapse

Constructor Details

#initialize(connection, sql, row_handler: nil, copy_handler: nil) ⇒ Query

Instantiates a new query

Parameters:

  • connection (Vertica::Connection)

    The connection to use for the query

  • sql (String)

    The SQL statement to execute.

  • row_handler (Proc, nil) (defaults to: nil)

    Callback that will be called for every row that is returned. If no handler is provided, all rows will be buffered so a Result can be returned.

  • copy_handler (Proc, nil) (defaults to: nil)

    Callback that will be called when the connection is ready to receive data for a COPY ... FROM STDIN statement.



19
20
21
22
23
24
25
# File 'lib/vertica/query.rb', line 19

def initialize(connection, sql, row_handler: nil, copy_handler: nil)
  @connection, @sql = connection, sql
  @buffer = row_handler.nil? && copy_handler.nil? ? [] : nil
  @row_handler = row_handler || lambda { |row| buffer_row(row) }
  @copy_handler = copy_handler
  @row_description, @error = nil, nil
end

Instance Method Details

#inspectString

Returns a user-consumable string representation of this query instance.

Returns:

  • (String)

    Returns a user-consumable string representation of this query instance.



46
47
48
# File 'lib/vertica/query.rb', line 46

def inspect
  "#<Vertica::Query:#{object_id} sql=#{@sql.inspect}>"
end

#runString, Vertica::Result

Sends the query to the server, and processes the results.

Returns:

  • (String)

    For an unbuffered query, the type of SQL command will be return as a string (e.g. "SELECT" or "COPY").

  • (Vertica::Result)

    For a buffered query, this method will return a Result instance

Raises:



34
35
36
37
38
39
40
41
42
43
# File 'lib/vertica/query.rb', line 34

def run
  @connection.write_message(Vertica::Protocol::Query.new(@sql))

  begin
    process_message(message = @connection.read_message)
  end until message.kind_of?(Vertica::Protocol::ReadyForQuery)

  raise @error unless @error.nil?
  return @result
end