Class: Lore::Join

Inherits:
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

{{{



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/lore/clause.rb', line 92

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 = ''
  implicit_joins = ''
  @clause_parser.add_as(new_attributes)

  @implicit_joins = Table_Select.build_joined_query(join_klass)
end

Instance Method Details

#implicit_joinsObject



107
108
109
# File 'lib/lore/clause.rb', line 107

def implicit_joins
  @implicit_joins
end

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

Yields:

  • (@clause_parser)


133
134
135
136
137
138
139
140
141
# File 'lib/lore/clause.rb', line 133

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



111
112
113
# File 'lib/lore/clause.rb', line 111

def plan_name
  @plan_name
end

#stringObject



115
116
117
# File 'lib/lore/clause.rb', line 115

def string
  @string
end

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

Will transform into ON clause, as USING drops duplicate fields

Yields:

  • (@clause_parser)


120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/lore/clause.rb', line 120

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