Class: Lore::Clause_Parser

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

Overview

parses / builds WHERE, GROUP BY, ORDER BY, LIMIT, … part of the query:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_accessor) ⇒ Clause_Parser

{{{



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/lore/clause.rb', line 312

def initialize(base_accessor)

  @clause            = Hash.new
  @clause[:limit]    = ''
  @clause[:offset]   = ''
  @clause[:group_by] = ''
  @clause[:order_by] = ''
  @clause[:join]     = ''
  @clause[:as]       = ''
  @clause[:set]      = ''
  @clause[:filter]   = ''
  @clause[:where]    = 't'
  @clause[:joined]   = []
  @base_accessor = base_accessor

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(absolute_field_name) ⇒ Object



510
511
512
513
514
515
516
517
518
519
# File 'lib/lore/clause.rb', line 510

def method_missing(absolute_field_name)
  if absolute_field_name.instance_of? Clause then
    field_name = absolute_field_name.field_name.to_s
  else 
    field_name = absolute_field_name.to_s
  end
  
# return Clause.new("#{@base_table}.#{field_name}")
  return Clause.new(field_name)
end

Class Method Details

.for(accessor) ⇒ Object

def



329
330
331
# File 'lib/lore/clause.rb', line 329

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

Instance Method Details

#[](absolute_field_name) ⇒ Object

def



491
492
493
494
495
496
497
498
499
# File 'lib/lore/clause.rb', line 491

def [](absolute_field_name)
  if absolute_field_name.instance_of? Clause then
    field_name = absolute_field_name.field_name.to_s
  else 
    field_name = absolute_field_name.to_s
  end
  
  return Clause.new(field_name)
end

#add_as(string) ⇒ Object



431
432
433
# File 'lib/lore/clause.rb', line 431

def add_as(string)
  @clause[:as] << string
end

#add_join(join) ⇒ Object

For usage in class Join only



416
417
418
# File 'lib/lore/clause.rb', line 416

def add_join(join)
  @clause[:final_join] = join.implicit_joins
end

#append_join(join) ⇒ Object



426
427
428
429
# File 'lib/lore/clause.rb', line 426

def append_join(join)
  @clause[:join] << join.string 
  @clause[:join] << join.implicit_joins
end

#as_partObject



340
341
342
# File 'lib/lore/clause.rb', line 340

def as_part
  @clause[:as]
end

#filter_partObject



350
351
352
# File 'lib/lore/clause.rb', line 350

def filter_part
  return @clause[:order_by] << @clause[:limit] << @clause[:offset] << @clause[:group_by] << @clause[:having]
end

#group_by(*absolute_field_names) ⇒ Object



467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/lore/clause.rb', line 467

def group_by(*absolute_field_names)
  absolute_field_names.map! { |field|
    if field.instance_of? Clause then
      field = field.field_name.to_s
    else 
      field = field.to_s
    end
  }
  @clause[:group_by] = ' GROUP BY ' << absolute_field_names.join(',')
  p @clause
  return self
end

#having(having_clause) ⇒ Object

def



462
463
464
465
# File 'lib/lore/clause.rb', line 462

def having(having_clause)
  @clause[:having] = ' HAVING ' << having_clause.to_sql
  return self
end

#having_partObject



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

def having_part
  @clause[:having] 
end

#idObject



521
522
523
# File 'lib/lore/clause.rb', line 521

def id()
  return Clause.new("#{@base_accessor.table_name}.id")
end

#join(join_klass) ⇒ Object



435
436
437
438
439
440
441
# File 'lib/lore/clause.rb', line 435

def join(join_klass)
  # this Join instance also will update this Clause_Parser's 
  # as_part (so passing self is crucial): 
  @clause[:joined] << join_klass
  j = Join.new(self, @base_accessor, join_klass)
  return j
end

#join_partObject



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

def join_part
  @clause[:join] 
end

#left_join(join_klass) ⇒ Object



442
443
444
445
446
447
# File 'lib/lore/clause.rb', line 442

def left_join(join_klass)
  # this Join instance also will update this Clause_Parser's 
  # as_part (so passing self is crucial): 
  j = Join.new(self, @base_accessor, join_klass, :left)
  return j
end

#limit(limit_val, offset_val = 0) ⇒ Object



455
456
457
458
459
460
# File 'lib/lore/clause.rb', line 455

def limit(limit_val, offset_val=0)
  @clause[:limit] = ' LIMIT ' << limit_val.to_s
  @clause[:offset] = ' OFFSET ' << offset_val.to_s

  return self
end

#max(absolute_field_name) ⇒ Object

def



501
502
503
504
505
506
507
508
# File 'lib/lore/clause.rb', line 501

def max(absolute_field_name)
  if absolute_field_name.instance_of? Clause then
    field_name = absolute_field_name.field_name.to_s
  else 
    field_name = absolute_field_name.to_s
  end
  return Clause.new("max(#{field_name})")
end

#order_by(order_field, dir = :asc) ⇒ Object

def



480
481
482
483
484
485
486
487
488
489
# File 'lib/lore/clause.rb', line 480

def order_by(order_field, dir=:asc)
  (dir == :desc)? dir_s = 'DESC' : dir_s = 'ASC'
  if @clause[:order_by]  == '' then
    @clause[:order_by] = ' ORDER BY ' 
  else
    @clause[:order_by] << ', '
  end
  @clause[:order_by] << order_field.to_s + ' ' << dir_s
  return self
end

#partsObject



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

def parts
  @clause
end

#performObject



525
526
527
# File 'lib/lore/clause.rb', line 525

def perform
  
end

#plan_argsObject



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

def plan_args
  @clause[:plan_args]
end

#plan_nameObject



370
371
372
# File 'lib/lore/clause.rb', line 370

def plan_name
  @clause[:plan_name]
end

#plan_typesObject



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

def plan_types
  @clause[:plan_types]
end

#plan_valuesObject



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

def plan_values
  @clause[:plan_values]
end

#prepend_join(join) ⇒ Object

TODO: Check if this is ever needed at all. Currently unused and untested.



421
422
423
424
425
# File 'lib/lore/clause.rb', line 421

def prepend_join(join)
  @clause[:join] = join.string << @clause[:join]
  @clause[:join] << join.implicit_joins

end

#right_join(join_klass) ⇒ Object



448
449
450
451
452
453
# File 'lib/lore/clause.rb', line 448

def right_join(join_klass)
  # this Join instance also will update this Clause_Parser's 
  # as_part (so passing self is crucial): 
  j = Join.new(self, @base_accessor, join_klass, :right)
  return j
end

#set(attrib_value_hash) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/lore/clause.rb', line 374

def set(attrib_value_hash)

  attrib_value_hash.each_pair { |attr_name, value|

    attr_name = attr_name.to_s unless attr_name.instance_of? String
    if value.instance_of? Clause then
      value = value.to_s.lore_escape
    else
      value = '\'' << value.to_s.lore_escape+'\'' 
    end
    # Postgres disallows full attribute names in UPDATE 
    # queries, so use field name only:
    Lore.log { 'Set Attrib: ' << attr_name.to_s }
    Lore.log { 'Set Value: ' << value.to_s }
    @clause[:set] << attr_name.to_s.split('.').at(-1).to_s + ' = ' << value.to_s + ','
  }
  return self

end

#set_partObject



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

def set_part
  @clause[:set].chomp!(',')
  return ' SET ' << @clause[:set]
end

#to_sqlObject



394
395
396
397
398
399
400
401
# File 'lib/lore/clause.rb', line 394

def to_sql
  clause = ''
  clause << @clause[:join].to_s 
  clause << @clause[:where].to_s 
  clause << @clause[:group_by].to_s 
  clause << @clause[:order_by].to_s 
  clause << @clause[:limit].to_s 
end

#where(where_clause) ⇒ Object

def



403
404
405
406
407
408
409
410
411
412
413
# File 'lib/lore/clause.rb', line 403

def where(where_clause)
  if where_clause.instance_of? Clause then
    where_clause = where_clause.to_sql
  elsif where_clause.instance_of? TrueClass then
    where_clause = '\'t\''
  elsif where_clause.instance_of? FalseClass then
    where_clause = '\'f\''
  end
  @clause[:where] = ' WHERE ' << where_clause.to_s
  return self
end

#where_partObject



337
338
339
# File 'lib/lore/clause.rb', line 337

def where_part
  @clause[:where]
end