Class: Mohair::Select
- Inherits:
-
Object
- Object
- Mohair::Select
- Defined in:
- lib/mohair/sql/select.rb
Instance Method Summary collapse
- #bucket ⇒ Object
- #build_columns(items) ⇒ Object
-
#initialize(tree) ⇒ Select
constructor
A new instance of Select.
- #mapper ⇒ Object
- #maybe_column(item) ⇒ Object
- #reducer ⇒ Object
Constructor Details
#initialize(tree) ⇒ Select
Returns a new instance of Select.
5 6 7 8 9 10 11 12 13 |
# File 'lib/mohair/sql/select.rb', line 5 def initialize tree @select = build_columns tree[:select] @from = From.new tree[:from] @where = Where.new tree[:where] if tree[:group_by] then @group_by = Group.new tree[:group_by] end @agg = false end |
Instance Method Details
#bucket ⇒ Object
41 42 43 |
# File 'lib/mohair/sql/select.rb', line 41 def bucket @from.bucket end |
#build_columns(items) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/mohair/sql/select.rb', line 15 def build_columns items reqs = [] if items.class == Array then items.each do |i| reqs << maybe_column(i[:item]) end elsif items.class == Hash then reqs << maybe_column(items[:item]) elsif items.to_s == "*" then reqs = :all end reqs end |
#mapper ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mohair/sql/select.rb', line 45 def mapper where = @where.to_js ## don't do 'select * from table group by col' if @select == :all then ERB.new(GetAllMapper).result(binding) elsif @group_by and @group_by.col then select = [] col = @group_by.col @select.each do |c| select << c.to_map_js end ERB.new(GroupByMapperTemplate).result(binding) else select = [] @select.each do |c| select << c.to_map_js end ERB.new(MapperTemplate).result(binding) end end |
#maybe_column(item) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mohair/sql/select.rb', line 29 def maybe_column item if item.class == Hash then if item[:function] then Function.new item else raise item end else Column.new item end end |
#reducer ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/mohair/sql/select.rb', line 66 def reducer if @select == :all then return nil end if @group_by and @group_by.col then col = @group_by.col return ERB.new(GroupByReducerTemplate).result(binding) end @select.each do |c| if c.is_agg? then agg_init, agg_fun = c.to_reduce_js return ERB.new(ReducerTemplate).result(binding) end end return nil end |