Sha256: 68de9b983700e13ee0f7e3c598e40b229169e7774462ba07c11ba1e277e2dce5

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

require 'abstract_unit'


module Arel
  module Nodes
    class SubSelect < Node
       attr_accessor :select, :as

      def initialize(select, as = 'subquery')
        @select = select
        @as = as
      end
    end
  end

  module Visitors
    class ToSql
      def visit_Arel_Nodes_SubSelect o
        "(#{visit(o.select)}) AS #{o.as}"
      end
    end
  end

  class SelectManager
    def from table, as = nil
      table = case table
        when Arel::SelectManager
          Nodes::SubSelect.new(table.ast, as)
        when String
          Nodes::SqlLiteral.new(table)
        else
          table
        end

      # FIXME: this is a hack to support
      # test_with_two_tables_in_from_without_getting_double_quoted
      # from the AR tests.
      if @ctx.froms
        source = @ctx.froms

        if Nodes::SqlLiteral === table && Nodes::Join === source
          source.left = table
          table = source
        end
      end
      @ctx.froms = table
      self
    end
  end
end



#class TestArel < Test::Unit::TestCase
#  def test_engine
#    assert_not_nil(Arel::Table)
#    assert_not_nil(Arel::Table.engine)
#  end
#end

visitor = Arel::Visitors::ToSql.new(Arel::Table.engine)

tariffs = Arel::Table.new(:tariffs)
product_tariffs = Arel::Table.new(:product_tariffs)
subquery = tariffs.join(product_tariffs).on(tariffs[:tariff_id].eq(product_tariffs[:tariff_id]).and(tariffs[:start_date].eq(product_tariffs[:tariff_start_date])))

projection = ['tariff_id', 'start_date'].map do |key|
  visitor.accept(tariffs[key])
end.join(', ')

subquery.project("DISTINCT #{projection}")

table = Arel::Table.new('test').project('count(*)')
query = table.from(subquery, "foobar")

puts "***********"
puts query.to_sql
a = query.to_a
puts a

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
composite_primary_keys-3.1.6 test/test_arel.rb
composite_primary_keys-3.1.5 test/test_arel.rb