Class: Qdsl::Select

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns, &block) ⇒ Select

Returns a new instance of Select.



19
20
21
22
23
# File 'lib/select.rb', line 19

def initialize(columns, &block)
  @columns = columns
  @block = block
  @inner_joins = []
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



17
18
19
# File 'lib/select.rb', line 17

def source
  @source
end

Instance Method Details

#add_inner_join(source, predicate) ⇒ Object



47
48
49
50
# File 'lib/select.rb', line 47

def add_inner_join(source, predicate)
  @inner_joins << InnerJoin.new(source, predicate)
  self
end

#create_queryObject



30
31
32
33
34
# File 'lib/select.rb', line 30

def create_query
  # Clone columns?
  column_names = @columns.collect(&:name)
  SelectQuery.new(self, column_names)
end

#from(source) ⇒ Object



25
26
27
28
# File 'lib/select.rb', line 25

def from(source)
  @source = source.create_query
  self
end

#inner_join(source) ⇒ Object



43
44
45
# File 'lib/select.rb', line 43

def inner_join(source)
  InnerJoinBuilder.new(self, source.create_query)
end

#render(context = nil, depth = 0) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/select.rb', line 52

def render(context = nil, depth = 0)
  context ||= Context.new

  indent = '  ' * depth

  unknown_columns = @columns.select { |x| !@source.column_names.include?(x.name) }
  raise "One or more unknown columns: #{unknown_columns.collect(&:name).join(', ')}" unless unknown_columns.empty?

  columns = @columns.collect { |x| x.with_source(@source) }

  sources = [@source] + @inner_joins.collect(&:source)

  extra_columns = if @block
    proxies = sources.collect { |x| FunkyProxy.new(x) }
    [*@block.call(proxies.size == 1 ? proxies.first : proxies)]
  else
    []
  end

  ids = sources.inject({}) { |acc, x| acc[x] = context.id; acc }

  column_results = (columns + extra_columns).collect { |x| x.render(context, ids[x.source]) }

  fragments = []

  fragments << "#{indent}SELECT #{column_results.join(', ')}\n"
  fragments << "#{indent}FROM #{@source.render(context, depth + 1, ids[@source])}\n"

  @inner_joins.each do |inner_join|
    predicate_result = inner_join.predicate.render(context, ids)
    fragments << "#{indent}INNER JOIN #{inner_join.source.render(context, depth + 1, ids[inner_join.source])}\n"
    fragments << "#{indent}ON #{predicate_result}\n"
  end

  if @where
    predicate_result = @where.render(context, ids)
    fragments << "#{indent}WHERE #{predicate_result}"
  end

  fragments.join
end

#whereObject



36
37
38
39
40
41
# File 'lib/select.rb', line 36

def where
  sources = [@source] + @inner_joins.collect(&:source)
  proxies = sources.collect { |x| FunkyProxy.new(x) }
  @where = yield(proxies.size == 1 ? proxies.first : proxies)
  self
end