Class: OccamsRecord::RawQuery

Inherits:
Object
  • Object
show all
Includes:
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

Instance Method Summary collapse

Methods included from Batches

#find_each, #find_in_batches

Methods included from EagerLoaders::Builder

#eager_load

Constructor Details

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

Initialize a new query.

Parameters:

  • sql (String)

    The SELECT statement to run. Binds should use Ruby’s named string substitution.

  • binds (Hash)

    Bind values (Symbol keys)

  • use (Array<Module>) (defaults to: nil)

    optional Module to include in the result class (single or array)

  • eager_loaders (OccamsRecord::EagerLoaders::Base) (defaults to: [])
  • query_logger (Array) (defaults to: nil)

    (optional) an array into which all queries will be inserted for logging/debug purposes



64
65
66
67
68
69
70
71
72
# File 'lib/occams-record/raw_query.rb', line 64

def initialize(sql, binds, use: nil, eager_loaders: [], query_logger: nil)
  @sql = sql
  @binds = binds
  @use = use
  @eager_loaders = eager_loaders
  @query_logger = query_logger
  @model = nil
  @conn = @model&.connection || ActiveRecord::Base.connection
end

Instance Attribute Details

#bindsHash (readonly)

Returns:

  • (Hash)


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

def binds
  @binds
end

#sqlString (readonly)

Returns:

  • (String)


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

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)

Returns:

  • (Enumerable)


120
121
122
123
124
125
126
# File 'lib/occams-record/raw_query.rb', line 120

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

#firstOccamsRecord::Results::Row

Run the query and return the first result (which could be nil). IMPORTANT you MUST add LIMIT 1 yourself!



109
110
111
# File 'lib/occams-record/raw_query.rb', line 109

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.

Parameters:

  • klass (ActiveRecord::Base)

Returns:



84
85
86
87
# File 'lib/occams-record/raw_query.rb', line 84

def model(klass)
  @model = klass
  self
end

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

Run the query and return the results.

Returns:



94
95
96
97
98
99
100
# File 'lib/occams-record/raw_query.rb', line 94

def run
  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