Class: Lore::Join
- Inherits:
-
Object
- Object
- Lore::Join
- 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
- #implicit_joins ⇒ Object
-
#initialize(clause, base_klass, join_klass, type = :natural) ⇒ Join
constructor
{{{.
- #on(clause) {|@clause_parser| ... } ⇒ Object
- #plan_name ⇒ Object
- #string ⇒ Object
-
#using(key) {|@clause_parser| ... } ⇒ Object
Will transform into ON clause, as USING drops duplicate fields.
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_joins ⇒ Object
64 65 66 |
# File 'lib/lore/clause.rb', line 64 def implicit_joins @implicit_joins end |
#on(clause) {|@clause_parser| ... } ⇒ Object
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_name ⇒ Object
68 69 70 |
# File 'lib/lore/clause.rb', line 68 def plan_name @plan_name end |
#string ⇒ Object
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
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 |