Class: Vertica::Query

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, sql, options = {}) ⇒ Query

Returns a new instance of Query.



6
7
8
9
10
11
12
# File 'lib/vertica/query.rb', line 6

def initialize(connection, sql, options = {})
  @connection, @sql = connection, sql
  
  @row_style    = options[:row_style] || @connection.row_style || :hash
  @row_handler  = options[:row_handler] 
  @copy_handler = options[:copy_handler]
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



3
4
5
# File 'lib/vertica/query.rb', line 3

def connection
  @connection
end

#copy_handlerObject

Returns the value of attribute copy_handler.



4
5
6
# File 'lib/vertica/query.rb', line 4

def copy_handler
  @copy_handler
end

#row_handlerObject

Returns the value of attribute row_handler.



4
5
6
# File 'lib/vertica/query.rb', line 4

def row_handler
  @row_handler
end

#row_styleObject

Returns the value of attribute row_style.



4
5
6
# File 'lib/vertica/query.rb', line 4

def row_style
  @row_style
end

#sqlObject (readonly)

Returns the value of attribute sql.



3
4
5
# File 'lib/vertica/query.rb', line 3

def sql
  @sql
end

Instance Method Details

#runObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vertica/query.rb', line 15

def run
  @connection.write Vertica::Messages::Query.new(@sql)
  result, error = nil, nil
  begin
    case message = @connection.read_message
    when Vertica::Messages::ErrorResponse
      error = Vertica::Error::QueryError.from_error_response(message, @sql)
    when Vertica::Messages::EmptyQueryResponse
      error = Vertica::Error::EmptyQueryError.new("A SQL string was expected, but the given string was blank or only contained SQL comments.")
    when Vertica::Messages::CopyInResponse
      handle_copy_from_stdin
    when Vertica::Messages::RowDescription, Vertica::Messages::CommandComplete
      result = retreive_result(message, Vertica::Result.new(row_style))
    else
      @connection.process_message(message)
    end
  end until message.kind_of?(Vertica::Messages::ReadyForQuery)
  
  raise error unless error.nil?
  return result
end

#write(data) ⇒ Object Also known as: <<



37
38
39
40
# File 'lib/vertica/query.rb', line 37

def write(data)
  @connection.write Vertica::Messages::CopyData.new(data)
  return self
end