Class: SQL::Maker::SelectSet

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/sql/maker/select_set.rb

Constant Summary collapse

FNOP =
%w[union union_all intersect intersect_all except except_all]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#array_wrap, #bind_param, bind_param, #croak, included, #parse_args, #quote_identifier, quote_identifier

Constructor Details

#initialize(args = {}) ⇒ SelectSet

Returns a new instance of SelectSet.



23
24
25
26
27
28
29
30
31
# File 'lib/sql/maker/select_set.rb', line 23

def initialize(args = {})
  croak("Missing mandatory parameter 'operator' for SQL::Maker::SelectSet.new") unless args[:operator]
  @new_line = args[:new_line] || "\n"
  @operator = args[:operator]
  @quote_char = args[:quote_char]
  @name_sep = args[:name_sep]
  @statements = []
  @order_by = []
end

Instance Attribute Details

#name_sepObject

Returns the value of attribute name_sep.



21
22
23
# File 'lib/sql/maker/select_set.rb', line 21

def name_sep
  @name_sep
end

#new_lineObject

Returns the value of attribute new_line.



21
22
23
# File 'lib/sql/maker/select_set.rb', line 21

def new_line
  @new_line
end

#operatorObject

Returns the value of attribute operator.



21
22
23
# File 'lib/sql/maker/select_set.rb', line 21

def operator
  @operator
end

#order_byObject

Returns the value of attribute order_by.



21
22
23
# File 'lib/sql/maker/select_set.rb', line 21

def order_by
  @order_by
end

#quote_charObject

Returns the value of attribute quote_char.



21
22
23
# File 'lib/sql/maker/select_set.rb', line 21

def quote_char
  @quote_char
end

#statementsObject

Returns the value of attribute statements.



21
22
23
# File 'lib/sql/maker/select_set.rb', line 21

def statements
  @statements
end

Instance Method Details

#_quote(label) ⇒ Object



51
52
53
# File 'lib/sql/maker/select_set.rb', line 51

def _quote(label)
  SQL::Maker::Util.quote_identifier(label, self.quote_char, self.name_sep)
end

#add_order_by(*args) ⇒ Object



73
74
75
76
77
# File 'lib/sql/maker/select_set.rb', line 73

def add_order_by(*args)
  col, type = parse_args(*args)
  self.order_by += [[col, type]]
  self # method chain
end

#add_statement(statement) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/sql/maker/select_set.rb', line 33

def add_statement(statement)
  unless statement.respond_to?(:as_sql)
    croak( "'statement' doesn't have 'as_sql' method.")
  end
  self.statements.push statement
  self # method chain
end

#as_sqlObject Also known as: to_s



55
56
57
58
59
60
61
62
# File 'lib/sql/maker/select_set.rb', line 55

def as_sql
  new_line = self.new_line
  operator = self.operator

  sql = self.statements.map {|st| st.as_sql }.join(new_line + operator + new_line)
  sql += ' ' + self.as_sql_order_by unless self.order_by.empty?
  sql
end

#as_sql_order_byObject



41
42
43
44
45
46
47
48
49
# File 'lib/sql/maker/select_set.rb', line 41

def as_sql_order_by
  attrs = self.order_by
  return '' if attrs.empty?

  return 'ORDER BY ' + attrs.map {|e|
    col, type = e
    type ? self._quote(col) + " #{type}" : self._quote(col)
  }.join(', ')
end

#bindObject



65
66
67
68
69
70
71
# File 'lib/sql/maker/select_set.rb', line 65

def bind
  bind = []
  self.statements.each do |select|
    bind += select.bind
  end
  bind
end