Class: Bmg::Sequel::Translator
- Inherits:
-
Sexpr::Processor
- Object
- Sexpr::Processor
- Bmg::Sequel::Translator
- Includes:
- Predicate::ToSequel::Methods
- Defined in:
- lib/bmg/sequel/translator.rb
Instance Attribute Summary collapse
-
#sequel_db ⇒ Object
readonly
Returns the value of attribute sequel_db.
Instance Method Summary collapse
-
#initialize(sequel_db) ⇒ Translator
constructor
A new instance of Translator.
- #on_column_name(sexpr) ⇒ Object
- #on_cross_join(sexpr) ⇒ Object
- #on_exists(sexpr) ⇒ Object
- #on_from_clause(sexpr) ⇒ Object
- #on_inner_join(sexpr) ⇒ Object
- #on_limit_clause(sexpr) ⇒ Object
- #on_native_table_as(sexpr) ⇒ Object
- #on_offset_clause(sexpr) ⇒ Object
- #on_opaque(sexpr) ⇒ Object
- #on_order_by_clause(sexpr) ⇒ Object
- #on_order_by_term(sexpr) ⇒ Object
- #on_qualified_name(sexpr) ⇒ Object
- #on_select_exp(sexpr) ⇒ Object
- #on_select_item(sexpr) ⇒ Object
- #on_select_list(sexpr) ⇒ Object
- #on_select_star(sexpr) ⇒ Object
- #on_set_operator(sexpr) ⇒ Object (also: #on_union, #on_intersect, #on_except)
- #on_subquery_as(sexpr) ⇒ Object
- #on_table_as(sexpr) ⇒ Object
- #on_table_name(sexpr) ⇒ Object
- #on_with_exp(sexpr) ⇒ Object
- #on_with_spec(sexpr) ⇒ Object
Constructor Details
#initialize(sequel_db) ⇒ Translator
Returns a new instance of Translator.
6 7 8 |
# File 'lib/bmg/sequel/translator.rb', line 6 def initialize(sequel_db) @sequel_db = sequel_db end |
Instance Attribute Details
#sequel_db ⇒ Object (readonly)
Returns the value of attribute sequel_db.
9 10 11 |
# File 'lib/bmg/sequel/translator.rb', line 9 def sequel_db @sequel_db end |
Instance Method Details
#on_column_name(sexpr) ⇒ Object
82 83 84 |
# File 'lib/bmg/sequel/translator.rb', line 82 def on_column_name(sexpr) ::Sequel.expr(sexpr.last.to_sym) end |
#on_cross_join(sexpr) ⇒ Object
94 95 96 97 |
# File 'lib/bmg/sequel/translator.rb', line 94 def on_cross_join(sexpr) left, right = apply(sexpr.left), apply(sexpr.right) dataset(left).cross_join(right) end |
#on_exists(sexpr) ⇒ Object
141 142 143 |
# File 'lib/bmg/sequel/translator.rb', line 141 def on_exists(sexpr) apply(sexpr.last).exists end |
#on_from_clause(sexpr) ⇒ Object
86 87 88 |
# File 'lib/bmg/sequel/translator.rb', line 86 def on_from_clause(sexpr) apply(sexpr.table_spec) end |
#on_inner_join(sexpr) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/bmg/sequel/translator.rb', line 99 def on_inner_join(sexpr) left, right = apply(sexpr.left), apply(sexpr.right) = {qualify: false, table_alias: false} dataset(left).join_table(:inner, right, nil, ){|*args| apply(sexpr.predicate) } end |
#on_limit_clause(sexpr) ⇒ Object
127 128 129 |
# File 'lib/bmg/sequel/translator.rb', line 127 def on_limit_clause(sexpr) sexpr.last end |
#on_native_table_as(sexpr) ⇒ Object
115 116 117 |
# File 'lib/bmg/sequel/translator.rb', line 115 def on_native_table_as(sexpr) sexpr[1].from_self(:alias => sexpr.as_name) end |
#on_offset_clause(sexpr) ⇒ Object
131 132 133 |
# File 'lib/bmg/sequel/translator.rb', line 131 def on_offset_clause(sexpr) sexpr.last end |
#on_opaque(sexpr) ⇒ Object
137 138 139 |
# File 'lib/bmg/sequel/translator.rb', line 137 def on_opaque(sexpr) apply(sexpr.last) end |
#on_order_by_clause(sexpr) ⇒ Object
119 120 121 |
# File 'lib/bmg/sequel/translator.rb', line 119 def on_order_by_clause(sexpr) sexpr.sexpr_body.map{|c| apply(c)} end |
#on_order_by_term(sexpr) ⇒ Object
123 124 125 |
# File 'lib/bmg/sequel/translator.rb', line 123 def on_order_by_term(sexpr) ::Sequel.send(sexpr.direction, apply(sexpr.qualified_name)) end |
#on_qualified_name(sexpr) ⇒ Object
78 79 80 |
# File 'lib/bmg/sequel/translator.rb', line 78 def on_qualified_name(sexpr) apply(sexpr.last).qualify(sexpr.qualifier) end |
#on_select_exp(sexpr) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bmg/sequel/translator.rb', line 39 def on_select_exp(sexpr) dataset = sequel_db.select(1) dataset = dataset(apply(sexpr.from_clause)) if sexpr.from_clause # selection = apply(sexpr.select_list) predicate = apply(sexpr.predicate) if sexpr.predicate order = apply(sexpr.order_by_clause) if sexpr.order_by_clause limit = apply(sexpr.limit_clause) if sexpr.limit_clause offset = apply(sexpr.offset_clause) if sexpr.offset_clause # dataset = dataset.select(*selection) dataset = dataset.distinct if sexpr.distinct? dataset = dataset.where(predicate) if predicate dataset = dataset.order_by(*order) if order dataset = dataset.limit(limit, offset == 0 ? nil : offset) if limit or offset dataset end |
#on_select_item(sexpr) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/bmg/sequel/translator.rb', line 65 def on_select_item(sexpr) left = apply(sexpr.left) right = apply(sexpr.right) case kind = sexpr.left.first when :qualified_name left.column == right.value ? left : ::Sequel.as(left, right) when :literal ::Sequel.as(left, right) else raise NotImplementedError, "Unexpected select item `#{kind}`" end end |
#on_select_list(sexpr) ⇒ Object
57 58 59 |
# File 'lib/bmg/sequel/translator.rb', line 57 def on_select_list(sexpr) sexpr.sexpr_body.map{|c| apply(c) } end |
#on_select_star(sexpr) ⇒ Object
61 62 63 |
# File 'lib/bmg/sequel/translator.rb', line 61 def on_select_star(sexpr) ::Sequel.lit('*') end |
#on_set_operator(sexpr) ⇒ Object Also known as: on_union, on_intersect, on_except
30 31 32 33 34 |
# File 'lib/bmg/sequel/translator.rb', line 30 def on_set_operator(sexpr) sexpr.tail_exprs.inject(apply(sexpr.head_expr)) do |left,right| left.send(sexpr.first, apply(right), all: sexpr.all?, from_self: false) end end |
#on_subquery_as(sexpr) ⇒ Object
111 112 113 |
# File 'lib/bmg/sequel/translator.rb', line 111 def on_subquery_as(sexpr) ::Sequel.as(apply(sexpr.subquery), ::Sequel.identifier(sexpr.as_name)) end |
#on_table_as(sexpr) ⇒ Object
107 108 109 |
# File 'lib/bmg/sequel/translator.rb', line 107 def on_table_as(sexpr) ::Sequel.as(::Sequel.expr(sexpr.table_name.to_sym), ::Sequel.identifier(sexpr.as_name)) end |
#on_table_name(sexpr) ⇒ Object
90 91 92 |
# File 'lib/bmg/sequel/translator.rb', line 90 def on_table_name(sexpr) ::Sequel.expr(sexpr.last.to_sym) end |
#on_with_exp(sexpr) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/bmg/sequel/translator.rb', line 11 def on_with_exp(sexpr) if sequel_db.select(1).supports_cte? dataset = apply(sexpr.select_exp) apply(sexpr.with_spec).each_pair do |name,subquery| dataset = dataset.with(name, subquery) end dataset else apply(Sql::Processor::Flatten.new(Sql::Builder.new).call(sexpr)) end end |
#on_with_spec(sexpr) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/bmg/sequel/translator.rb', line 23 def on_with_spec(sexpr) sexpr.each_with_object({}){|child,hash| next if child == :with_spec hash[apply(child.table_name)] = apply(child.subquery) } end |