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
137 138 139 |
# File 'lib/bmg/sql/builder.rb', line 137 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
147 148 149 |
# File 'lib/bmg/sql/builder.rb', line 147 def exists(subquery) Predicate::Grammar.sexpr [ :exists, subquery ] end |
#from_clause(table, qualifier) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# 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 ::Sequel::SQL::QualifiedIdentifier 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
183 184 185 |
# File 'lib/bmg/sql/builder.rb', line 183 def from_self(sexpr) Processor::FromSelf.new(self).call(sexpr) end |
#group_by_clause(attrlist, &desaliaser) ⇒ Object
151 152 153 154 155 156 |
# File 'lib/bmg/sql/builder.rb', line 151 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
189 190 191 |
# File 'lib/bmg/sql/builder.rb', line 189 def last_qualifier "t#{@next_qualifier}" end |
#limit_clause(limit) ⇒ Object
168 169 170 |
# File 'lib/bmg/sql/builder.rb', line 168 def limit_clause(limit) [:limit_clause, limit] end |
#literal(lit) ⇒ Object
178 179 180 |
# File 'lib/bmg/sql/builder.rb', line 178 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
119 120 121 122 123 |
# File 'lib/bmg/sql/builder.rb', line 119 def native_table_as(table, qualifier) [ :native_table_as, table, range_var_name(qualifier) ] end |
#next_qualifier! ⇒ Object
193 194 195 |
# File 'lib/bmg/sql/builder.rb', line 193 def next_qualifier! "t#{@next_qualifier += 1}" end |
#offset_clause(limit) ⇒ Object
173 174 175 |
# File 'lib/bmg/sql/builder.rb', line 173 def offset_clause(limit) [:offset_clause, limit] end |
#order_by_clause(ordering, &desaliaser) ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/bmg/sql/builder.rb', line 159 def order_by_clause(ordering, &desaliaser) ordering.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
125 126 127 128 129 |
# File 'lib/bmg/sql/builder.rb', line 125 def qualified_name(qualifier, name) [:qualified_name, range_var_name(qualifier), column_name(name) ] end |
#range_var_name(qualifier) ⇒ Object
132 133 134 |
# File 'lib/bmg/sql/builder.rb', line 132 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
112 113 114 115 116 |
# File 'lib/bmg/sql/builder.rb', line 112 def subquery_as(subquery, qualifier) [:subquery_as, subquery, range_var_name(qualifier) ] end |
#table_as(table, qualifier) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/bmg/sql/builder.rb', line 100 def table_as(table, qualifier) table = case table when String, Symbol then table_name(table) when ::Sequel::SQL::QualifiedIdentifier then table_name(table) else table end [:table_as, table, range_var_name(qualifier) ] end |
#table_name(name) ⇒ Object
142 143 144 |
# File 'lib/bmg/sql/builder.rb', line 142 def table_name(name) [:table_name, name] end |