Class: Bmg::Sql::Builder
- Inherits:
-
Object
- Object
- Bmg::Sql::Builder
- Defined in:
- lib/bmg/sql/builder.rb
Constant Summary collapse
- DISTINCT =
Grammar.sexpr([:set_quantifier, "distinct"])
- ALL =
Grammar.sexpr([:set_quantifier, "all"])
- IS_TABLE_DEE =
Grammar.sexpr([:select_list, [:select_item, [:literal, true], [:column_name, "is_table_dee"]]])
Class Method Summary collapse
Instance Method Summary collapse
- #all ⇒ Object
- #column_name(name) ⇒ Object
- #distinct ⇒ Object
- #exists(subquery) ⇒ Object
- #from_clause(table, qualifier) ⇒ Object
- #from_self(sexpr) ⇒ Object
- #group_by_clause(attrlist, &desaliaser) ⇒ Object
-
#initialize(start = 0) ⇒ Builder
constructor
A new instance of Builder.
- #is_table_dee ⇒ Object
- #last_qualifier ⇒ Object
- #limit_clause(limit) ⇒ Object
- #literal(lit) ⇒ Object
- #name_intro(name, sexpr) ⇒ Object
- #native_table_as(table, qualifier) ⇒ Object
- #next_qualifier! ⇒ Object
- #offset_clause(limit) ⇒ Object
- #order_by_clause(ordering, &desaliaser) ⇒ Object
- #qualified_name(qualifier, name) ⇒ Object
- #range_var_name(qualifier) ⇒ Object
- #select_all(heading, name, qualifier = next_qualifier!) ) ⇒ Object
- #select_is_table_dee(subquery) ⇒ Object
- #select_item(qualifier, name, as = name) ⇒ Object
- #select_list(attrs, qualifier) ⇒ Object
- #select_literal_item(literal, as) ⇒ Object
- #select_star(qualifier = next_qualifier!) ) ⇒ Object
- #select_star_from(name, qualifier = next_qualifier!) ) ⇒ Object
- #subquery_as(subquery, qualifier) ⇒ Object
- #table_as(table, qualifier) ⇒ Object
- #table_name(name) ⇒ Object
Constructor Details
#initialize(start = 0) ⇒ Builder
Returns a new instance of Builder.
19 20 21 |
# File 'lib/bmg/sql/builder.rb', line 19 def initialize(start = 0) @next_qualifier = (start || 0) end |
Class Method Details
.builder(meth) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/bmg/sql/builder.rb', line 12 def self.builder(meth) old = instance_method(meth) define_method(meth) do |*args, &bl| Grammar.sexpr(old.bind(self).call(*args, &bl)) end end |
Instance Method Details
#all ⇒ Object
28 29 30 |
# File 'lib/bmg/sql/builder.rb', line 28 def all ALL end |
#column_name(name) ⇒ Object
135 136 137 |
# File 'lib/bmg/sql/builder.rb', line 135 def column_name(name) [:column_name, name] end |
#distinct ⇒ Object
23 24 25 |
# File 'lib/bmg/sql/builder.rb', line 23 def distinct DISTINCT end |
#exists(subquery) ⇒ Object
145 146 147 |
# File 'lib/bmg/sql/builder.rb', line 145 def exists(subquery) Predicate::Grammar.sexpr [ :exists, subquery ] end |
#from_clause(table, qualifier) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/bmg/sql/builder.rb', line 89 def from_clause(table, qualifier) arg = case table when String, Symbol then table_as(table, qualifier) when Expr then subquery_as(table, qualifier) else native_table_as(table, qualifier) end [ :from_clause, arg ] end |
#from_self(sexpr) ⇒ Object
181 182 183 |
# File 'lib/bmg/sql/builder.rb', line 181 def from_self(sexpr) Processor::FromSelf.new(self).call(sexpr) end |
#group_by_clause(attrlist, &desaliaser) ⇒ Object
149 150 151 152 153 154 |
# File 'lib/bmg/sql/builder.rb', line 149 def group_by_clause(attrlist, &desaliaser) attrlist.map{|name| name = name.to_s (desaliaser && desaliaser[name]) || column_name(name) }.unshift(:group_by_clause) end |
#is_table_dee ⇒ Object
59 60 61 |
# File 'lib/bmg/sql/builder.rb', line 59 def is_table_dee IS_TABLE_DEE end |
#last_qualifier ⇒ Object
187 188 189 |
# File 'lib/bmg/sql/builder.rb', line 187 def last_qualifier "t#{@next_qualifier}" end |
#limit_clause(limit) ⇒ Object
166 167 168 |
# File 'lib/bmg/sql/builder.rb', line 166 def limit_clause(limit) [:limit_clause, limit] end |
#literal(lit) ⇒ Object
176 177 178 |
# File 'lib/bmg/sql/builder.rb', line 176 def literal(lit) [:literal, lit] end |
#name_intro(name, sexpr) ⇒ Object
33 34 35 |
# File 'lib/bmg/sql/builder.rb', line 33 def name_intro(name, sexpr) [:name_intro, table_name(name), sexpr] end |
#native_table_as(table, qualifier) ⇒ Object
117 118 119 120 121 |
# File 'lib/bmg/sql/builder.rb', line 117 def native_table_as(table, qualifier) [ :native_table_as, table, range_var_name(qualifier) ] end |
#next_qualifier! ⇒ Object
191 192 193 |
# File 'lib/bmg/sql/builder.rb', line 191 def next_qualifier! "t#{@next_qualifier += 1}" end |
#offset_clause(limit) ⇒ Object
171 172 173 |
# File 'lib/bmg/sql/builder.rb', line 171 def offset_clause(limit) [:offset_clause, limit] end |
#order_by_clause(ordering, &desaliaser) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/bmg/sql/builder.rb', line 157 def order_by_clause(ordering, &desaliaser) ordering.to_a.map{|(name,direction)| name = name.to_s name = (desaliaser && desaliaser[name]) || column_name(name) [:order_by_term, name, direction ? direction.to_s : "asc"] }.unshift(:order_by_clause) end |
#qualified_name(qualifier, name) ⇒ Object
123 124 125 126 127 |
# File 'lib/bmg/sql/builder.rb', line 123 def qualified_name(qualifier, name) [:qualified_name, range_var_name(qualifier), column_name(name) ] end |
#range_var_name(qualifier) ⇒ Object
130 131 132 |
# File 'lib/bmg/sql/builder.rb', line 130 def range_var_name(qualifier) [:range_var_name, qualifier] end |
#select_all(heading, name, qualifier = next_qualifier!) ) ⇒ Object
46 47 48 49 50 |
# File 'lib/bmg/sql/builder.rb', line 46 def select_all(heading, name, qualifier = next_qualifier!) [ :select_exp, all, select_list(heading, qualifier), from_clause(name, qualifier) ] end |
#select_is_table_dee(subquery) ⇒ Object
53 54 55 56 |
# File 'lib/bmg/sql/builder.rb', line 53 def select_is_table_dee(subquery) [ :select_exp, all, is_table_dee, [:where_clause, exists(subquery)] ] end |
#select_item(qualifier, name, as = name) ⇒ Object
77 78 79 80 81 |
# File 'lib/bmg/sql/builder.rb', line 77 def select_item(qualifier, name, as = name) [:select_item, qualified_name(qualifier, name.to_s), column_name(as.to_s)] end |
#select_list(attrs, qualifier) ⇒ Object
64 65 66 67 |
# File 'lib/bmg/sql/builder.rb', line 64 def select_list(attrs, qualifier) attrs = attrs.to_attr_list if attrs.respond_to?(:to_attr_list) attrs.map{|a| select_item(qualifier, a) }.unshift(:select_list) end |
#select_literal_item(literal, as) ⇒ Object
70 71 72 73 74 |
# File 'lib/bmg/sql/builder.rb', line 70 def select_literal_item(literal, as) [:select_item, literal(literal), column_name(as.to_s)] end |
#select_star(qualifier = next_qualifier!) ) ⇒ Object
84 85 86 |
# File 'lib/bmg/sql/builder.rb', line 84 def select_star(qualifier = next_qualifier!) [:select_star, [:range_var_name, qualifier]] end |
#select_star_from(name, qualifier = next_qualifier!) ) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/bmg/sql/builder.rb', line 38 def select_star_from(name, qualifier = next_qualifier!) [ :select_exp, all, select_star(qualifier), from_clause(name, qualifier) ] end |
#subquery_as(subquery, qualifier) ⇒ Object
110 111 112 113 114 |
# File 'lib/bmg/sql/builder.rb', line 110 def subquery_as(subquery, qualifier) [:subquery_as, subquery, range_var_name(qualifier) ] end |
#table_as(table, qualifier) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/bmg/sql/builder.rb', line 99 def table_as(table, qualifier) table = case table when String, Symbol then table_name(table) else table end [:table_as, table, range_var_name(qualifier) ] end |
#table_name(name) ⇒ Object
140 141 142 |
# File 'lib/bmg/sql/builder.rb', line 140 def table_name(name) [:table_name, name] end |