Sha256: 76fcda5cb47d254e1a84907c3d7049f72d1ec4045da5537db096c089bb170052

Contents?: true

Size: 1.71 KB

Versions: 62

Compression:

Stored size: 1.71 KB

Contents

module Bmg
  module Sql
    class Processor
      class Merge < Processor

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

        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

        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
        alias :on_union      :on_nonjoin_exp
        alias :on_except     :on_nonjoin_exp
        alias :on_intersect  :on_nonjoin_exp
        alias :on_select_exp :on_nonjoin_exp

      private

        def modifier
          @all ? builder.all : builder.distinct
        end

      end # class Merge
    end # class Processor
  end # module Sql
end # module Bmg

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
bmg-0.17.2 lib/bmg/sql/processor/merge.rb
bmg-0.16.7 lib/bmg/sql/processor/merge.rb
bmg-0.17.1 lib/bmg/sql/processor/merge.rb
bmg-0.17.0 lib/bmg/sql/processor/merge.rb
bmg-0.16.6 lib/bmg/sql/processor/merge.rb
bmg-0.16.5 lib/bmg/sql/processor/merge.rb
bmg-0.16.4 lib/bmg/sql/processor/merge.rb
bmg-0.16.3 lib/bmg/sql/processor/merge.rb
bmg-0.16.2 lib/bmg/sql/processor/merge.rb
bmg-0.16.1 lib/bmg/sql/processor/merge.rb
bmg-0.16.0 lib/bmg/sql/processor/merge.rb
bmg-0.16.0.pre.rc2 lib/bmg/sql/processor/merge.rb
bmg-0.16.0.pre.rc1 lib/bmg/sql/processor/merge.rb
bmg-0.15.0 lib/bmg/sql/processor/merge.rb
bmg-0.14.6 lib/bmg/sql/processor/merge.rb
bmg-0.14.5 lib/bmg/sql/processor/merge.rb
bmg-0.14.4 lib/bmg/sql/processor/merge.rb
bmg-0.14.3 lib/bmg/sql/processor/merge.rb
bmg-0.14.2 lib/bmg/sql/processor/merge.rb
bmg-0.14.1 lib/bmg/sql/processor/merge.rb