Class: Lore::Clause

Inherits:
String show all
Defined in:
lib/lore/clause.rb

Overview

Clause objects are responsible for operands on Model attributes, as in WHERE parts of a query. Model klass methods named like one if its (possibly inherited) field names return a preconfigured Clause object on that field.

Example:

(Car.num_seats > 100).to_sql  --> "public.vehicle.num_seats > '100'"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from String

#empty?, #lore_escape

Constructor Details

#initialize(field_name = '', left_side = '', value_string = '', plan = {}) ⇒ Clause

Returns a new instance of Clause.



159
160
161
162
163
164
165
166
167
168
# File 'lib/lore/clause.rb', line 159

def initialize(field_name='', left_side='', value_string='', plan={})
  if field_name.instance_of?(TrueClass) then
    return (Clause.new('1') == '1')
  elsif field_name.instance_of?(FalseClass) then
    return (Clause.new('1') == '0')
  end
  @value_string  = value_string
  @left_side     = left_side
  @field_name    = field_name
end

Instance Attribute Details

#field_nameObject (readonly)

{{{



157
158
159
# File 'lib/lore/clause.rb', line 157

def field_name
  @field_name
end

#left_sideObject (readonly)

{{{



157
158
159
# File 'lib/lore/clause.rb', line 157

def left_side
  @left_side
end

#planObject (readonly)

{{{



157
158
159
# File 'lib/lore/clause.rb', line 157

def plan
  @plan
end

#value_stringObject (readonly)

{{{



157
158
159
# File 'lib/lore/clause.rb', line 157

def value_string
  @value_string
end

Class Method Details

.for(accessor) ⇒ Object



170
171
172
# File 'lib/lore/clause.rb', line 170

def self.for(accessor)
  Clause_Parser.new(accessor)
end

Instance Method Details

#&(value) ⇒ Object Also known as: and



306
307
308
309
310
311
312
313
314
315
316
# File 'lib/lore/clause.rb', line 306

def &(value) 
  return unless value
  return Clause.new('1') == '1' if value.instance_of?(TrueClass)
  return Clause.new('1') == '0' if value.instance_of?(FalseClass)
  if @left_side.gsub(' ','') != '' then
    @value_string = " AND #{value.left_side}"
  else 
    @value_string = value.left_side
  end
  Clause.new(@field_name, "(#{@left_side+@value_string})", '', @plan)
end

#+(value) ⇒ Object



213
214
215
216
217
218
219
220
221
# File 'lib/lore/clause.rb', line 213

def +(value)
  if value.instance_of? String then 
    value = '\''+value.lore_escape+'\'::text'
  else 
    value = value.to_s.lore_escape
  end
  @value_string = @field_name.to_s + '+'+value
  return Clause.new(@value_string, @left_side.to_s+@value_string.to_s)
end

#-(value) ⇒ Object



222
223
224
225
226
227
228
229
230
# File 'lib/lore/clause.rb', line 222

def -(value)
  if value.instance_of? String then 
    value = '\''+value.lore_escape+'\'::text'
  else 
    value = value.to_s.lore_escape
  end
  @value_string = @field_name + '-'+ value
  return Clause.new(@value_string, @left_side+@value_string)
end

#<(value) ⇒ Object



242
243
244
245
# File 'lib/lore/clause.rb', line 242

def <(value) 
  @value_string = @field_name + ' < ' << Lore.parse_field_value(value)
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#<=(value) ⇒ Object



246
247
248
249
# File 'lib/lore/clause.rb', line 246

def <=(value) 
  @value_string = @field_name + ' <= ' << Lore.parse_field_value(value)
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#<=>(value) ⇒ Object Also known as: is_not



289
290
291
292
293
294
295
296
# File 'lib/lore/clause.rb', line 289

def <=>(value) 
  if(value != :NULL)
    @value_string = @field_name << ' != ' << Lore.parse_field_value(value)
  else 
    @value_string = @field_name << ' NOT NULL '
  end
  Clause.new(@field_name, @left_side+@value_string, '', @plan)
end

#==(value) ⇒ Object Also known as: is



273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/lore/clause.rb', line 273

def ==(value) 
  if value.instance_of? Clause then
    value = value.field_name
  else 
    value = value.to_s.lore_escape
    value = '\'' << value << '\''
  end
  if(value != :NULL)
    @value_string = @field_name << ' = ' << value
  else
    @value_string = @field_name << ' IS NULL' 
  end
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#>(value) ⇒ Object



250
251
252
253
# File 'lib/lore/clause.rb', line 250

def >(value) 
  @value_string = @field_name + ' > ' << Lore.parse_field_value(value)
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#>=(value) ⇒ Object



254
255
256
257
# File 'lib/lore/clause.rb', line 254

def >=(value) 
  @value_string = @field_name + ' >= ' << Lore.parse_field_value(value)
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#between(range_begin, range_end) ⇒ Object



208
209
210
211
# File 'lib/lore/clause.rb', line 208

def between(range_begin, range_end)
  @value_string = @field_name << " BETWEEN #{range_begin} AND #{range_end} "
  Clause.new(@value_string, @left_side+@value_string)
end

#has_element(element) ⇒ Object



319
320
321
322
323
324
# File 'lib/lore/clause.rb', line 319

def has_element(element)
  element = element.to_s if element.kind_of? Clause
  element = '\''+element.lore_escape+'\'' if element.kind_of? String
  @value_string = element + ' = ANY ('  << @field_name+ ')'
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#has_element_ilike(element) ⇒ Object

This query requires a custom operator, defined by:

create function irlike(text,text) returns bool as 'select $2 ilike $1' language sql strict immutable; 
create operator ~~~~ (procedure = irlike, leftarg = text, rightarg = text, commutator = ~~);


343
344
345
346
347
348
# File 'lib/lore/clause.rb', line 343

def has_element_ilike(element)
  element = element.to_s if element.kind_of? Clause
  element = '\'' + element.lore_escape + '\'' if element.kind_of? String
  @value_string = element + ' ~~~~ ANY (' << @field_name+ ')'
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#has_element_like(element) ⇒ Object

This query requires a custom operator, defined by:

create function rlike(text,text) returns bool as 'select $2 like $1' language sql strict immutable; 
create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);


331
332
333
334
335
336
# File 'lib/lore/clause.rb', line 331

def has_element_like(element)
  element = element.to_s if element.kind_of? Clause
  element = '\''+element.lore_escape+'\'' if element.kind_of? String
  @value_string = element + ' ~~~ ANY ('  << @field_name+ ')'
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#ilike(value) ⇒ Object



263
264
265
266
267
# File 'lib/lore/clause.rb', line 263

def ilike(value) 
  value = value.to_s.lore_escape
  @value_string = @field_name + ' ILIKE ' << Lore.parse_field_value(value)
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#in(nested_query_string) ⇒ Object

Check for Refined_Select needed for functionality of Refined_Select. Example:

User.all.with(User.user_id.in( Admin.all(:user_id) ))


194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/lore/clause.rb', line 194

def in(nested_query_string)
  if(nested_query_string.instance_of? Refined_Select) then
    nested_query_string = nested_query_string.to_select
  elsif nested_query_string.instance_of? Array then
    nested_query_string = nested_query_string.join(',')
    nested_query_string = 'NULL' if nested_query_string.length == 0
  elsif nested_query_string.instance_of? Range then
    return between(nested_query_string.first, nested_query_string.last)
  end
  @value_string = @field_name << ' IN (' << "\n" << nested_query_string.to_s << ') '
  Clause.new(@value_string, @left_side+@value_string)

end

#inspectObject



362
363
364
# File 'lib/lore/clause.rb', line 362

def inspect
  return 'Clause('+to_s+')'
end

#is_not_nullObject



237
238
239
240
# File 'lib/lore/clause.rb', line 237

def is_not_null()
  @value_string = @field_name + ' IS NOT NULL'
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#is_nullObject



232
233
234
235
# File 'lib/lore/clause.rb', line 232

def is_null()
  @value_string = @field_name + ' IS NULL'
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#like(value) ⇒ Object



258
259
260
261
262
# File 'lib/lore/clause.rb', line 258

def like(value) 
  value = value.to_s.lore_escape
  @value_string = @field_name + ' LIKE ' << Lore.parse_field_value(value)
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#not_in(nested_query_string) ⇒ Object

Check for Refined_Select needed for functionality of Refined_Select. Example:

User.all.with(User.user_id.in( Admin.all(:user_id) ))


179
180
181
182
183
184
185
186
187
# File 'lib/lore/clause.rb', line 179

def not_in(nested_query_string)
  if(nested_query_string.instance_of? Refined_Select) then
    nested_query_string.to_inner_select
  else
    nested_query_string = nested_query_string.join(',') if nested_query_string.is_a?(Array)
    @value_string = @field_name << ' NOT IN (' << "\n" << nested_query_string.to_s << ') '
    Clause.new(@value_string, @left_side+@value_string)
  end
end

#posreg_like(value) ⇒ Object



268
269
270
271
272
# File 'lib/lore/clause.rb', line 268

def posreg_like(value) 
  value = value.to_s.lore_escape
  @value_string = @field_name + ' ~* ' << Lore.parse_field_value(value)
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#tagObject



358
359
360
# File 'lib/lore/clause.rb', line 358

def tag 
  @field_name.gsub('.','--')
end

#to_sObject

Important to return @field_name here, as Table_Accessor.attribute should return schema.table_name.attribute. See Table_Accessor.load_attribute_fields



354
355
356
# File 'lib/lore/clause.rb', line 354

def to_s
  @field_name.to_s
end

#to_sqlObject



366
367
368
# File 'lib/lore/clause.rb', line 366

def to_sql
  @left_side + @value_string
end

#|(value) ⇒ Object Also known as: or



299
300
301
302
303
304
# File 'lib/lore/clause.rb', line 299

def |(value) 
  return Clause.new('1') == '1' if value.instance_of?(TrueClass)
  return Clause.new('1') == '0' if value.instance_of?(FalseClass)
  @value_string = " OR #{value.left_side}"
  Clause.new(@value_string, "(#{@left_side+@value_string})", '', @plan)
end