Class: Bmg::Sql::Processor::Merge

Inherits:
Bmg::Sql::Processor show all
Defined in:
lib/bmg/sql/processor/merge.rb

Constant Summary

Constants inherited from Bmg::Sql::Processor

UnexpectedError

Instance Attribute Summary

Attributes inherited from Bmg::Sql::Processor

#builder

Instance Method Summary collapse

Methods inherited from Bmg::Sql::Processor

#on_set_operator

Constructor Details

#initialize(kind, all, right, builder) ⇒ Merge

Returns a new instance of Merge.



6
7
8
9
10
11
# File 'lib/bmg/sql/processor/merge.rb', line 6

def initialize(kind, all, right, builder)
  super(builder)
  @kind = kind
  @all = all
  @right = right
end

Instance Method Details

#on_nonjoin_exp(sexpr) ⇒ Object Also known as: on_union, on_except, on_intersect, on_select_exp



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bmg/sql/processor/merge.rb', line 25

def on_nonjoin_exp(sexpr)
  left_attrs  = sexpr.to_attr_list.map(&:to_s).sort
  right_attrs = @right.to_attr_list.map(&:to_s).sort
  unless left_attrs == right_attrs
    raise "Operands are not union compatible: #{left_attrs.inspect} vs. #{right_attrs.inspect}"
  end

  reordered = Reorder.new(sexpr.to_attr_list, builder).call(@right)
  if @right.with_exp?
    [ :with_exp,
      reordered.with_spec,
      [ @kind, modifier, sexpr, reordered.select_exp ] ]
  elsif sexpr.first == @kind && sexpr.set_quantifier == modifier
    sexpr.dup + [ reordered ]
  else
    [ @kind, modifier, sexpr, reordered ]
  end
end

#on_with_exp(sexpr) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bmg/sql/processor/merge.rb', line 13

def on_with_exp(sexpr)
  if @right.with_exp?
    reordered = Reorder.new(sexpr.to_attr_list, builder).call(@right)
    main = [ @kind, modifier, sexpr.select_exp, reordered.select_exp ]
    merge_with_exps(sexpr, reordered, main)
  else
    [ :with_exp,
      sexpr.with_spec,
      apply(sexpr.last) ]
  end
end