Class: Lore::Refined_Select

Inherits:
Refined_Query show all
Defined in:
lib/lore/query_shortcuts.rb

Overview

class

Instance Method Summary collapse

Methods inherited from Refined_Query

#having, #method_missing, #with

Constructor Details

#initialize(accessor, statements = {}) ⇒ Refined_Select

Returns a new instance of Refined_Select.



71
72
73
# File 'lib/lore/query_shortcuts.rb', line 71

def initialize(accessor, statements={})
  super(accessor, statements)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Lore::Refined_Query

Instance Method Details

#each(&block) ⇒ Object

Handy wrapper for

<request>.entities.each { |e| ... }


172
173
174
# File 'lib/lore/query_shortcuts.rb', line 172

def each(&block)
  entities.each &block
end

#entitiesObject Also known as: perform

Sends request defined by previous method calls to self. Examples:

Car.find(10).with(Car.name.like '%BMW%').entities  # -> Array of 10 instances of model klass 'Car'

Before calling #entities, the request isn’t sent, but defined only. Therefore you safely can pass a query (Clause) object to other methods, like in this example:

def filter_cars(clause)
  query.with((Car.name != '') & clause).limit(10).entities
end

filder = Car.find(10).with(Car.name.like '%Audi%').limit(20) # Request is not sent yet
filter_cars(filter) # Request will be sent here
# -> Car.find(10).with(Car.name.like '%Audi%').with(Car.name != '').limit(10).entities
# -> Car.find(10).with((Car.name.like '%Audi%') & (Car.name != '')).limit(10).entities

There are other methods not defining but executing a request: #entity, #each, #to_i, #to_s



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/lore/query_shortcuts.rb', line 222

def entities
  if @what.nil? then
    result = @accessor.select { |entity|
      entity.where(@condition)
      entity.limit(@limit, @offset) unless @limit.nil?
      @order_by.each { |o|  
        entity.order_by(o[0], o[1]) 
      }
      entity.group_by(@group_by) unless @group_by.nil?
      entity.having(@having) unless @having.nil?
      entity
    }
  else
    result = Array.new
    @accessor.select_values(@what) { |entity|
      entity.where(@condition)
      entity.limit(@limit, @offset) unless @limit.nil?
      @order_by.each { |o|  
        entity.order_by(o[0], o[1]) 
      }
      entity.group_by(@group_by) unless @group_by.nil?
      entity.having(@having) unless @having.nil?
      entity
    }.each { |row|
      if row.kind_of? Hash then
        result << row.values['value'] 
      elsif row.kind_of? String then
        result << row
      end
    }
  end

  return result
end

#entityObject

Same as #entities.first. Useful when requesting one row or a single attribute only.



98
99
100
# File 'lib/lore/query_shortcuts.rb', line 98

def entity
  entities.first
end

#firstObject



102
103
104
# File 'lib/lore/query_shortcuts.rb', line 102

def first
  entities.first
end

#limit(lim, off = 0) ⇒ Object

Limit part of a query.

Something.all_with(Something.name == 'foo').limit(10,123).entities

Is same as

Something.find(10).with(Something.name == 'foo').offset(123).entities

Is same as

Something.find(10,123).with(Something.name == 'foo').entities

And results in

SELECT * FROM something WHERE something.name = 'foo' LIMIT 10,123


164
165
166
167
168
# File 'lib/lore/query_shortcuts.rb', line 164

def limit(lim, off=0)
  @limit = lim
  @offset = off
  return self
end

#offset(off) ⇒ Object

Offset part of a query.

Something.find(10).with(Something.name == 'foo').offset(123).entities

Results in

SELECT * FROM something WHERE something.name = 'foo' LIMIT 10,123


148
149
150
151
# File 'lib/lore/query_shortcuts.rb', line 148

def offset(off)
  @offset = off
  return self
end

#ordered_by(attrib, dir = :asc) ⇒ Object Also known as: order_by, sort_by

Adds order statement to query. Direction is either :desc or :asc. Example:

User.find(0..10).ordered_by(:surname, :asc)
User.find(20).ordered_by([:surname, :forename], :desc)

Aliases are #order_by and #sort_by.



135
136
137
138
# File 'lib/lore/query_shortcuts.rb', line 135

def ordered_by(attrib, dir=:asc)
  @order_by << [attrib, dir]
  return self
end

#to_iObject

When requesting a single value, #to_i can be used to retreive the result as integer instead of calling #entity:

i1 = User.all(User.id).with(User.name.like '%Christina%').to_i
i2 = User.all(User.id).with(User.name.like '%Christina%').entity.to_i
assert_equal i1, i2


123
124
125
# File 'lib/lore/query_shortcuts.rb', line 123

def to_i
  entities.first.to_i
end

#to_sObject

When requesting a single value, #to_i can be used to retreive the result as string instead of calling #entity:

s1 = User.all(User.name).with(User.name.like '%Christina%').to_s
s2 = User.all(User.name).with(User.name.like '%Christina%').entity
assert_equal s1, s2


113
114
115
# File 'lib/lore/query_shortcuts.rb', line 113

def to_s
  entities.first.to_s
end

#to_selectObject

Returns Clause instance containing this select statement. This is needed for all Clause methods expecting an inner select. Example:

inner = Car.all(Car.car_id).with(Car.seats > 2)
Car.all.where(Car.car_id).in(inner)  # method 'in' calls inner.to_select

Full example:

Car.all.where(Car.car_id).in( 
  Manufacturer_Car.values_of(Manufacturer_Car.car_id).limit(10).sort_by(Manufacturer_Car.name)
)


189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/lore/query_shortcuts.rb', line 189

def to_select
  @accessor.select(@what) { |entity|
    entity.where(@condition)
    entity.limit(@limit, @offset) unless @limit.nil?
    @order_by.each { |o|  
      entity.order_by(o[0], o[1]) 
    }
    entity.group_by(@group_by) unless @group_by.nil?
    entity.having(@having) unless @having.nil?
    entity
  }
end

#values_of(*what) ⇒ Object Also known as: value_of

Examples:

Initiates request for single attribute values instead model instances. Returns value as string, array or two-dimensional array, depending from parameters passed:

Car.find(2).values_of(Car.name, Car.id).ordered_by(Car.name, :asc).entities 
-> [ ['BMW318i', '3'], ['BMW Mini', '5'] ]
Car.find(2).values_of(Car.name).ordered_by(Car.name, :asc).entities   
-> [ 'BMW318i', 'BMW Mini']
Car.find(1).value_of(Car.name, Car.type).with(Car.name.ilike '%BMW%').entity 
-> ['BMW318i', 'Convertible']
Car.find(1).value_of(Car.name).with(Car.name.ilike '%BMW%').entity 
-> 'BMW318i'


90
91
92
93
# File 'lib/lore/query_shortcuts.rb', line 90

def values_of(*what)
  @what = what
  return self
end