class Axiom::Adapter::DataObjects::Statement

Executes generated SQL statements

Public Class Methods

new(connection, relation, visitor = SQL::Generator::Relation) click to toggle source

Initialize a statement

@param [::DataObjects::Connection] connection

the database connection

@param [Relation] relation

the relation to generate the SQL from

@param [#visit] visitor

optional object to visit the relation and generate SQL with

@return [undefined]

@api private

# File lib/axiom/adapter/data_objects/statement.rb, line 23
def initialize(connection, relation, visitor = SQL::Generator::Relation)
  @connection = connection
  @relation   = relation
  @visitor    = visitor
end

Public Instance Methods

each() { |row| ... } click to toggle source

Iterate over each row in the results

@example

statement = Statement.new(connection, relation, visitor)
statement.each { |row| ... }

@yield [row]

@yieldparam [Array] row

each row in the results

@return [self]

@api public

# File lib/axiom/adapter/data_objects/statement.rb, line 43
def each
  return to_enum unless block_given?
  each_row { |row| yield row }
  self
end
to_s() click to toggle source

Return the SQL query

@example

statement.to_s  # => SQL representation of the relation

@return [String]

@api public

# File lib/axiom/adapter/data_objects/statement.rb, line 57
def to_s
  @visitor.visit(@relation).to_sql.freeze
end

Private Instance Methods

column_types() click to toggle source

Return the list of types for each column

@return [Array<Class>]

@api private

# File lib/axiom/adapter/data_objects/statement.rb, line 96
def column_types
  @relation.header.map { |attribute| attribute.type.primitive }
end
command() click to toggle source

Return the command for the SQL query and column types

@return [::DataObjects::Command]

@api private

# File lib/axiom/adapter/data_objects/statement.rb, line 85
def command
  command = @connection.create_command(to_s)
  command.set_types(column_types)
  command
end
each_row() { |values while next!| ... } click to toggle source

Yield each row in the result

@yield [row]

@yieldparam [Array] row

each row in the results

@return [undefined]

@api private

# File lib/axiom/adapter/data_objects/statement.rb, line 73
def each_row
  reader = command.execute_reader
  yield reader.values while reader.next!
ensure
  reader.close if reader
end