class SQLKnit::SQL::From

Attributes

current_chain[R]
current_join_type[R]
current_table_name[R]
statement_chains[R]

Public Class Methods

new() click to toggle source
# File lib/sql/from.rb, line 8
def initialize
  @statement_chains = Hash.new {|hash, key| hash[key] = []}
end

Public Instance Methods

add_table(table_name) click to toggle source
# 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) click to toggle source
# 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_name) click to toggle source
# 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
Also aliased as: join_chain
join_chain(table_name)
Alias for: join
left_join(*table_names) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/sql/from.rb, line 17
def text str
  statement_chains << str if not statement_chains.include? str
end
to_statement() click to toggle source
# 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

Private Instance Methods

switch_join_type(type) click to toggle source
# File lib/sql/from.rb, line 74
def switch_join_type type
  @current_join_type = type
end
switch_to(table_name) click to toggle source
# File lib/sql/from.rb, line 78
def switch_to table_name
  @current_table_name = table_name
  @current_chain = @statement_chains[table_name]
end