Class: Mohair::Select

Inherits:
Object
  • Object
show all
Defined in:
lib/mohair/sql/select.rb

Instance Method Summary collapse

Constructor Details

#initialize(tree) ⇒ Select

Returns a new instance of Select.



5
6
7
8
9
10
11
# 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]
  @group_by = Group.new tree[:group_by]
  @agg = false
end

Instance Method Details

#bucketObject



39
40
41
# File 'lib/mohair/sql/select.rb', line 39

def bucket
  @from.bucket
end

#build_columns(items) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mohair/sql/select.rb', line 13

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

#mapperObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mohair/sql/select.rb', line 43

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



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mohair/sql/select.rb', line 27

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

#reducerObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mohair/sql/select.rb', line 64

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