Class: OccamsRecord::RawQuery

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Batches, EagerLoaders::Builder, Measureable
Defined in:
lib/occams-record/raw_query.rb

Overview

Represents a raw SQL query to be run and eager associations to be loaded. Use OccamsRecord.sql to create your queries instead of instantiating objects directly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Measureable

#measure

Methods included from EagerLoaders::Builder

#eager_load, #eager_load_many, #eager_load_one, #nest

Methods included from Batches

#find_each, #find_in_batches

Constructor Details

#initialize(sql, binds, use: nil, eager_loaders: nil, query_logger: nil, measurements: nil) ⇒ RawQuery

Initialize a new query.



68
69
70
71
72
73
74
# File 'lib/occams-record/raw_query.rb', line 68

def initialize(sql, binds, use: nil, eager_loaders: nil, query_logger: nil, measurements: nil)
  @sql = sql
  @binds = binds
  @use = use
  @eager_loaders = eager_loaders || EagerLoaders::Context.new
  @query_logger, @measurements = query_logger, measurements
end

Instance Attribute Details

#bindsHash (readonly)



51
52
53
# File 'lib/occams-record/raw_query.rb', line 51

def binds
  @binds
end

#sqlString (readonly)



49
50
51
# File 'lib/occams-record/raw_query.rb', line 49

def sql
  @sql
end

Instance Method Details

#each {|OccansR::Results::Row| ... } ⇒ Enumerable

If you pass a block, each result row will be yielded to it. If you don’t, an Enumerable will be returned.

Yields:

  • (OccansR::Results::Row)


123
124
125
126
127
128
129
# File 'lib/occams-record/raw_query.rb', line 123

def each
  if block_given?
    to_a.each { |row| yield row }
  else
    to_a.each
  end
end

#model(klass) ⇒ OccamsRecord::RawQuery

Specify the model to be used to load eager associations. Normally this would be the main table you’re SELECTing from.

NOTE Some database adapters, notably SQLite’s, require that the model always be specified, even if you aren’t doing eager loading.



86
87
88
89
# File 'lib/occams-record/raw_query.rb', line 86

def model(klass)
  @eager_loaders.model = klass
  self
end

#runArray<OccamsRecord::Results::Row> Also known as: to_a

Run the query and return the results.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/occams-record/raw_query.rb', line 96

def run
  _escaped_sql = escaped_sql
  @query_logger << _escaped_sql if @query_logger
  result = if measure?
             record_start_time!
             measure!(table_name, _escaped_sql) {
               conn.exec_query _escaped_sql
             }
           else
             conn.exec_query _escaped_sql
           end
  row_class = OccamsRecord::Results.klass(result.columns, result.column_types, @eager_loaders.names, model: @eager_loaders.model, modules: @use)
  rows = result.rows.map { |row| row_class.new row }
  @eager_loaders.run!(rows, query_logger: @query_logger, measurements: @measurements)
  yield_measurements!
  rows
end