Class: OccamsRecord::RawQuery
- Inherits:
-
Object
- Object
- OccamsRecord::RawQuery
- Includes:
- Enumerable, Batches, EagerLoaders::Builder
- 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
- #binds ⇒ Hash readonly
- #sql ⇒ String readonly
Instance Method Summary collapse
-
#each {|OccansR::Results::Row| ... } ⇒ Enumerable
If you pass a block, each result row will be yielded to it.
-
#first ⇒ OccamsRecord::Results::Row
Run the query and return the first result (which could be nil).
-
#initialize(sql, binds, use: nil, eager_loaders: [], query_logger: nil, &eval_block) ⇒ RawQuery
constructor
Initialize a new query.
-
#model(klass) ⇒ OccamsRecord::RawQuery
Specify the model to be used to load eager associations.
-
#run ⇒ Array<OccamsRecord::Results::Row>
(also: #to_a)
Run the query and return the results.
Methods included from EagerLoaders::Builder
#eager_load, #eager_load_many, #eager_load_one
Methods included from Batches
Constructor Details
#initialize(sql, binds, use: nil, eager_loaders: [], query_logger: nil, &eval_block) ⇒ RawQuery
Initialize a new query.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/occams-record/raw_query.rb', line 66 def initialize(sql, binds, use: nil, eager_loaders: [], query_logger: nil, &eval_block) @sql = sql @binds = binds @use = use @eager_loaders = eager_loaders @query_logger = query_logger @model = nil @conn = @model&.connection || ActiveRecord::Base.connection instance_eval(&eval_block) if eval_block end |
Instance Attribute Details
#binds ⇒ Hash (readonly)
51 52 53 |
# File 'lib/occams-record/raw_query.rb', line 51 def binds @binds end |
#sql ⇒ String (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.
125 126 127 128 129 130 131 |
# File 'lib/occams-record/raw_query.rb', line 125 def each if block_given? to_a.each { |row| yield row } else to_a.each end end |
#first ⇒ OccamsRecord::Results::Row
Run the query and return the first result (which could be nil). IMPORTANT you MUST add LIMIT 1 yourself!
114 115 116 |
# File 'lib/occams-record/raw_query.rb', line 114 def first run[0] 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.
87 88 89 90 |
# File 'lib/occams-record/raw_query.rb', line 87 def model(klass) @model = klass self end |
#run ⇒ Array<OccamsRecord::Results::Row> Also known as: to_a
Run the query and return the results.
97 98 99 100 101 102 103 104 105 |
# File 'lib/occams-record/raw_query.rb', line 97 def run _escaped_sql = escaped_sql @query_logger << _escaped_sql if @query_logger result = @conn.exec_query _escaped_sql row_class = OccamsRecord::Results.klass(result.columns, result.column_types, @eager_loaders.map(&:name), model: @model, modules: @use) rows = result.rows.map { |row| row_class.new row } eager_load! rows rows end |