Class: Praxis::Mapper::Query::Sequel
- Defined in:
- lib/praxis-mapper/query/sequel.rb
Overview
Sequel-centric query class
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
#contexts, #identity_map, #model, #statistics, #where
Instance Method Summary collapse
-
#_execute(ds = nil) ⇒ Array
Executes this SQL statement.
-
#_multi_get(identity, values) ⇒ Array
Executes a ‘SELECT’ statement.
- #dataset ⇒ Object
- #describe ⇒ Object
-
#initialize(identity_map, model, &block) ⇒ Sequel
constructor
A new instance of Sequel.
-
#raw(sql_text) ⇒ Object
Constructs a raw SQL statement.
-
#sql ⇒ String
Raw or assembled SQL statement.
- #to_records(rows) ⇒ Object
Methods inherited from Base
#apply_selector, #connection, #context, #default_select, #execute, #limit, #load, #multi_get, #select, #track, #tracked_associations
Constructor Details
#initialize(identity_map, model, &block) ⇒ Sequel
Returns a new instance of Sequel.
10 11 12 |
# File 'lib/praxis-mapper/query/sequel.rb', line 10 def initialize(identity_map, model, &block) super end |
Instance Method Details
#_execute(ds = nil) ⇒ Array
Executes this SQL statement. Does not perform any validation of the statement before execution.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/praxis-mapper/query/sequel.rb', line 54 def _execute(ds=nil) Praxis::Mapper.logger.debug "SQL:\n#{self.describe}\n" self.statistics[:datastore_interactions] += 1 start_time = Time.now rows = if @raw_query unless ds.nil? warn 'WARNING: Query::Sequel#_execute ignoring passed dataset due to previously-specified raw SQL' end connection.run(@raw_query).to_a else (ds || self.dataset).to_a end self.statistics[:datastore_interaction_time] += (Time.now - start_time) return rows end |
#_multi_get(identity, values) ⇒ Array
Executes a ‘SELECT’ statement.
45 46 47 48 |
# File 'lib/praxis-mapper/query/sequel.rb', line 45 def _multi_get(identity, values) ds = self.dataset.where(identity => values) _execute(ds) end |
#dataset ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/praxis-mapper/query/sequel.rb', line 14 def dataset ds = connection[model.table_name.to_sym] # TODO: support column aliases if @select && @select != true ds = ds.select(*@select.keys) end if @where ds = ds.where(@where) end if @limit ds = ds.limit(@limit) end ds end |
#describe ⇒ Object
73 74 75 |
# File 'lib/praxis-mapper/query/sequel.rb', line 73 def describe self.sql end |
#raw(sql_text) ⇒ Object
Constructs a raw SQL statement. No validation is performed here (security risk?).
82 83 84 |
# File 'lib/praxis-mapper/query/sequel.rb', line 82 def raw(sql_text) @raw_query = sql_text end |
#sql ⇒ String
Returns raw or assembled SQL statement.
87 88 89 90 91 92 93 |
# File 'lib/praxis-mapper/query/sequel.rb', line 87 def sql if @raw_query @raw_query else dataset.sql end end |
#to_records(rows) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/praxis-mapper/query/sequel.rb', line 95 def to_records(rows) if model < ::Sequel::Model rows.collect do |row| m = model.call(row) m._query = self m end else super end end |