Class: Lore::Join

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

Overview

Usage:

Some_Klass.select { |k|
  k.join(Other_Klass).on(Some_Klass.foo == Other_Klass.bar) { |o|
    k.where(
      (o['foo'] == 1) & (o['bar'] <=> 2)
    )
    k.limit(10)
  }

Instance Method Summary collapse

Constructor Details

#initialize(clause, base_klass, join_klass, type = :natural) ⇒ Join

{{{



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lore/clause.rb', line 38

def initialize(clause, base_klass, join_klass, type=:natural)
  @type = type
  @clause_parser = clause
  @base_klass = base_klass
  @join_klass = join_klass
  @string = ''
  # By joining with another klass, new attributes are added 
  # we have to include in the AS part of the query: 
  new_attributes = ''
   #  join_klass.get_attributes.each { |attr_set|
   #    table = attr_set[0]
   #    attr_set[1].each { |attrib_name|
   #      new_attributes += ', ' << "\n"
   #      new_attributes += table + '.' << attrib_name 
   #      new_attributes += ' AS '
   #      new_attributes += '"' << table << '.' << attrib_name << '" ' 
   #    }
   #  }
  implicit_joins = ''
  @clause_parser.add_as(new_attributes)

  @implicit_joins = Table_Selector.build_joined_query(join_klass)
  
  # @clause_parser.add_join(self)
end

Instance Method Details

#implicit_joinsObject



64
65
66
# File 'lib/lore/clause.rb', line 64

def implicit_joins
  @implicit_joins
end

#on(clause) {|@clause_parser| ... } ⇒ Object

Yields:

  • (@clause_parser)


90
91
92
93
94
95
96
97
98
# File 'lib/lore/clause.rb', line 90

def on(clause, &block)
  if @type == :natural then cmd = 'JOIN '
  elsif @type == :left then cmd = 'LEFT JOIN '
  elsif @type == :right then cmd = 'RIGHT JOIN '
  end
  @string = cmd << @join_klass.table_name << ' ON (' << clause.to_sql << ') ' 
  @clause_parser.append_join(self)
  yield @clause_parser # use extended clause parser for inner block argument
end

#plan_nameObject



68
69
70
# File 'lib/lore/clause.rb', line 68

def plan_name
  @plan_name
end

#stringObject



72
73
74
# File 'lib/lore/clause.rb', line 72

def string
  @string
end

#using(key) {|@clause_parser| ... } ⇒ Object

Will transform into ON clause, as USING drops duplicate fields

Yields:

  • (@clause_parser)


77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lore/clause.rb', line 77

def using(key, &block)
  if @type == :natural then cmd = 'JOIN '
  elsif @type == :left then cmd = 'LEFT JOIN '
  elsif @type == :right then cmd = 'RIGHT JOIN '
  end
  key = key.to_s
  using_string =  "#{@base_klass.table_name}.#{key} = "
  using_string << "#{@join_klass.table_name}.#{key}"
  @string = "\n" << cmd << @join_klass.table_name << ' ON (' << using_string << ') '
  @clause_parser.append_join(self)
  yield @clause_parser # use extended clause parser for inner block argument
end