Class: SQLKnit::SQL::Join

Inherits:
Object
  • Object
show all
Defined in:
lib/sql/join.rb

Constant Summary collapse

TableAbbrSymbol =
':'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(head_table_name, table_name, opts = {}) ⇒ Join

Returns a new instance of Join.



12
13
14
15
16
17
18
# File 'lib/sql/join.rb', line 12

def initialize head_table_name, table_name, opts = {}
  @head_table_name = head_table_name
  @join_table_name = table_name
  @on_conditions = []
  @type = opts[:type] || 'join'
  @statement_chains = []
end

Instance Attribute Details

#head_table_nameObject (readonly)

Returns the value of attribute head_table_name.



7
8
9
# File 'lib/sql/join.rb', line 7

def head_table_name
  @head_table_name
end

#join_table_nameObject (readonly)

Returns the value of attribute join_table_name.



7
8
9
# File 'lib/sql/join.rb', line 7

def join_table_name
  @join_table_name
end

#on_conditionsObject (readonly)

Returns the value of attribute on_conditions.



9
10
11
# File 'lib/sql/join.rb', line 9

def on_conditions
  @on_conditions
end

#on_table_nameObject (readonly)

Returns the value of attribute on_table_name.



7
8
9
# File 'lib/sql/join.rb', line 7

def on_table_name
  @on_table_name
end

#statement_chainsObject (readonly)

Returns the value of attribute statement_chains.



10
11
12
# File 'lib/sql/join.rb', line 10

def statement_chains
  @statement_chains
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/sql/join.rb', line 8

def type
  @type
end

Instance Method Details

#on(text = nil, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sql/join.rb', line 20

def on text = nil, &block

  if text.include? TableAbbrSymbol
    join_text = text.gsub(TableAbbrSymbol, join_table_name.to_s)
  else
    join_text = text
  end
  
  on_condition = OnCondition.new join_table_name
  on_condition.add_text join_text
  
  on_conditions << on_condition
  on_condition.instance_eval &block if block_given?
end

#to_statementObject



35
36
37
38
# File 'lib/sql/join.rb', line 35

def to_statement
  statement = on_conditions.map(&:to_statement).join("\n")
  [type, statement].join(" ")
end