Class: SQLKnit::SQL::From

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFrom

Returns a new instance of From.



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

def initialize
  @statement_chains = Hash.new {|hash, key| hash[key] = []}
end

Instance Attribute Details

#current_chainObject (readonly)

Returns the value of attribute current_chain.



5
6
7
# File 'lib/sql/from.rb', line 5

def current_chain
  @current_chain
end

#current_join_typeObject (readonly)

Returns the value of attribute current_join_type.



6
7
8
# File 'lib/sql/from.rb', line 6

def current_join_type
  @current_join_type
end

#current_table_nameObject (readonly)

Returns the value of attribute current_table_name.



5
6
7
# File 'lib/sql/from.rb', line 5

def current_table_name
  @current_table_name
end

#statement_chainsObject (readonly)

Returns the value of attribute statement_chains.



5
6
7
# File 'lib/sql/from.rb', line 5

def statement_chains
  @statement_chains
end

Instance Method Details

#add_table(table_name) ⇒ Object



68
69
70
# File 'lib/sql/from.rb', line 68

def add_table table_name
  statement_chains[table_name] if not statement_chains.has_key? table_name
end

#contextlize(table_name, &block) ⇒ Object



12
13
14
15
# File 'lib/sql/from.rb', line 12

def contextlize table_name, &block
  switch_to table_name
  instance_eval &block if block_given?
end

#join(*table_names) ⇒ Object



21
22
23
24
25
26
# File 'lib/sql/from.rb', line 21

def join table_name
  opts = {type: current_join_type}
  join = SQL::Join.new(current_table_name, table_name, opts)
  current_chain << join
  join
end

#join_chainObject



28
29
30
31
32
33
# File 'lib/sql/from.rb', line 28

def join table_name
  opts = {type: current_join_type}
  join = SQL::Join.new(current_table_name, table_name, opts)
  current_chain << join
  join
end

#left_join(*table_names) ⇒ Object



46
47
48
49
# File 'lib/sql/from.rb', line 46

def left_join *table_names
  switch_join_type 'left join'
  join table_names
end

#pairelize_table_names(table_names) ⇒ Object



51
52
53
54
# File 'lib/sql/from.rb', line 51

def pairelize_table_names table_names
  last_index = table_names.length - 1
  (0..(last_index-1)).map {|i| table_names[i..i+1] }
end

#text(str) ⇒ Object



17
18
19
# File 'lib/sql/from.rb', line 17

def text str
  statement_chains << str if not statement_chains.include? str
end

#to_statementObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sql/from.rb', line 56

def to_statement
  statement = statement_chains.map {|table_name, joins|
    if joins.size > 0
    "#{table_name} #{joins.map(&:to_statement).join("\n")}"
    else
      table_name.to_s
    end
  }.join(",\n")

  ["from", statement].join(" ")
end