Class: Praxis::Mapper::Query::Sequel

Inherits:
Base
  • Object
show all
Defined in:
lib/praxis-mapper/query/sequel.rb

Overview

Sequel-centric query class

Constant Summary

Constants inherited from Base

Base::MULTI_GET_BATCH_SIZE

Instance Attribute Summary

Attributes inherited from Base

#contexts, #identity_map, #model, #statistics, #where

Instance Method Summary collapse

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.

Returns:

  • (Array)

    result-set



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.

Examples:

numeric key

_multi_get(:id, [1, 2])

string key

_multi_get(:uid, ['foo', 'bar'])

composite key (possibly a combination of numeric and string keys)

_multi_get([:cloud_id, :account_id], [['foo1', 'bar1'], ['foo2', 'bar2']])

Parameters:

  • identity (Symbol|Array)

    a simple or composite key for this model

  • values (Array)

    list of identifier values (ideally a sorted set)

Returns:

  • (Array)

    SQL result set



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

#datasetObject



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

#describeObject

See Also:



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?).

Parameters:

  • sql_text

    a custom SQL query



82
83
84
# File 'lib/praxis-mapper/query/sequel.rb', line 82

def raw(sql_text)
  @raw_query = sql_text
end

#sqlString

Returns raw or assembled SQL statement.

Returns:

  • (String)

    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