Class: Lore::Clause_Parser

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

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_accessor) ⇒ Clause_Parser

Returns a new instance of Clause_Parser.



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/lore/clause.rb', line 379

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]   = []
  @unions = false
  @base_accessor = base_accessor

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(absolute_field_name) ⇒ Object



583
584
585
586
587
588
589
590
591
592
# File 'lib/lore/clause.rb', line 583

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

Instance Attribute Details

#unionsObject (readonly)

{{{



377
378
379
# File 'lib/lore/clause.rb', line 377

def unions
  @unions
end

Class Method Details

.for(accessor) ⇒ Object

def



397
398
399
# File 'lib/lore/clause.rb', line 397

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

Instance Method Details

#[](absolute_field_name) ⇒ Object

def



564
565
566
567
568
569
570
571
572
# File 'lib/lore/clause.rb', line 564

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



500
501
502
# File 'lib/lore/clause.rb', line 500

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

#add_join(join) ⇒ Object

For usage in class Join only



485
486
487
# File 'lib/lore/clause.rb', line 485

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

#append_join(join) ⇒ Object



495
496
497
498
# File 'lib/lore/clause.rb', line 495

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

#as_partObject



408
409
410
# File 'lib/lore/clause.rb', line 408

def as_part
  @clause[:as]
end

#filter_partObject



418
419
420
# File 'lib/lore/clause.rb', line 418

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

#group_by(*absolute_field_names) ⇒ Object



541
542
543
544
545
546
547
548
549
550
551
# File 'lib/lore/clause.rb', line 541

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(',')
  return self
end

#having(having_clause) ⇒ Object

def



536
537
538
539
# File 'lib/lore/clause.rb', line 536

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

#having_partObject



414
415
416
# File 'lib/lore/clause.rb', line 414

def having_part
  @clause[:having] 
end

#idObject



594
595
596
# File 'lib/lore/clause.rb', line 594

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

#join(join_klass) ⇒ Object



504
505
506
507
508
509
510
# File 'lib/lore/clause.rb', line 504

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



411
412
413
# File 'lib/lore/clause.rb', line 411

def join_part
  @clause[:join] 
end

#left_join(join_klass) ⇒ Object



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

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



530
531
532
533
534
# File 'lib/lore/clause.rb', line 530

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



574
575
576
577
578
579
580
581
# File 'lib/lore/clause.rb', line 574

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



553
554
555
556
557
558
559
560
561
562
# File 'lib/lore/clause.rb', line 553

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



401
402
403
# File 'lib/lore/clause.rb', line 401

def parts
  @clause
end

#performObject



598
599
# File 'lib/lore/clause.rb', line 598

def perform
end

#plan_argsObject



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

def plan_args
  @clause[:plan_args]
end

#plan_nameObject



438
439
440
# File 'lib/lore/clause.rb', line 438

def plan_name
  @clause[:plan_name]
end

#plan_typesObject



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

def plan_types
  @clause[:plan_types]
end

#plan_valuesObject



434
435
436
# File 'lib/lore/clause.rb', line 434

def plan_values
  @clause[:plan_values]
end

#prepend_join(join) ⇒ Object

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



490
491
492
493
494
# File 'lib/lore/clause.rb', line 490

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

end

#right_join(join_klass) ⇒ Object



517
518
519
520
521
522
# File 'lib/lore/clause.rb', line 517

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



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/lore/clause.rb', line 442

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



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

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

#to_sqlObject



462
463
464
465
466
467
468
469
470
# File 'lib/lore/clause.rb', line 462

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 
  return clause
end

#union(select_query) ⇒ Object



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

def union(select_query)
  @unions ||= []
  @unions << select_query
  return self
end

#where(where_clause) ⇒ Object

def



472
473
474
475
476
477
478
479
480
481
482
# File 'lib/lore/clause.rb', line 472

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] = "\nWHERE #{where_clause.to_s}"
  return self
end

#where_partObject



405
406
407
# File 'lib/lore/clause.rb', line 405

def where_part
  @clause[:where]
end