Class: Veritas::Adapter::DataObjects::Statement

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Immutable
Defined in:
lib/veritas/adapter/data_objects/statement.rb

Overview

Executes generated SQL statements

Instance Method Summary collapse

Constructor Details

#initialize(connection, relation, visitor = SQL::Generator::Relation) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a statement

Parameters:

  • connection (::DataObjects::Connection)

    the database connection

  • relation (Relation)

    the relation to generate the SQL from

  • visitor (#visit) (defaults to: SQL::Generator::Relation)

    optional object to visit the relation and generate SQL with



23
24
25
26
27
# File 'lib/veritas/adapter/data_objects/statement.rb', line 23

def initialize(connection, relation, visitor = SQL::Generator::Relation)
  @connection = connection
  @relation   = relation
  @visitor    = visitor
end

Instance Method Details

#each {|row| ... } ⇒ self

Iterate over each row in the results

Examples:

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

Yields:

  • (row)

Yield Parameters:

  • row (Array)

    each row in the results

Returns:

  • (self)


43
44
45
46
47
# File 'lib/veritas/adapter/data_objects/statement.rb', line 43

def each
  return to_enum unless block_given?
  each_row { |row| yield row }
  self
end

#to_sString

Return the SQL query

Examples:

statement.to_s  # => SQL representation of the relation

Returns:

  • (String)


57
58
59
# File 'lib/veritas/adapter/data_objects/statement.rb', line 57

def to_s
  @visitor.visit(@relation).to_sql.freeze
end