diff options
-rw-r--r-- | lib/arel/collectors/sql_string.rb | 28 | ||||
-rw-r--r-- | lib/arel/nodes/node.rb | 6 | ||||
-rw-r--r-- | lib/arel/tree_manager.rb | 6 | ||||
-rw-r--r-- | lib/arel/visitors/bind_substitute.rb | 9 | ||||
-rw-r--r-- | lib/arel/visitors/bind_visitor.rb | 12 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 346 | ||||
-rw-r--r-- | lib/arel/visitors/visitor.rb | 8 | ||||
-rw-r--r-- | test/helper.rb | 9 | ||||
-rw-r--r-- | test/visitors/test_bind_visitor.rb | 22 | ||||
-rw-r--r-- | test/visitors/test_to_sql.rb | 161 |
10 files changed, 382 insertions, 225 deletions
diff --git a/lib/arel/collectors/sql_string.rb b/lib/arel/collectors/sql_string.rb new file mode 100644 index 0000000000..824b0a1712 --- /dev/null +++ b/lib/arel/collectors/sql_string.rb @@ -0,0 +1,28 @@ +# encoding: utf-8 + +module Arel + module Collectors + class SQLString + def initialize + @str = '' + end + + def value + @str + end + + def << str + @str << str + self + end + + def start; self; end + def finish; self; end + + def add_bind bind + self << bind + self + end + end + end +end diff --git a/lib/arel/nodes/node.rb b/lib/arel/nodes/node.rb index 36e7628612..239c4fd766 100644 --- a/lib/arel/nodes/node.rb +++ b/lib/arel/nodes/node.rb @@ -1,3 +1,5 @@ +require 'arel/collectors/sql_string' + module Arel module Nodes ### @@ -42,7 +44,9 @@ module Arel # # Maybe we should just use `Table.engine`? :'( def to_sql engine = Table.engine - engine.connection.visitor.accept self + collector = Arel::Collectors::SQLString.new + collector = engine.connection.visitor.accept self, collector + collector.value end # Iterate through AST, nodes will be yielded depth-first diff --git a/lib/arel/tree_manager.rb b/lib/arel/tree_manager.rb index 1adb230991..87887800d0 100644 --- a/lib/arel/tree_manager.rb +++ b/lib/arel/tree_manager.rb @@ -1,3 +1,5 @@ +require 'arel/collectors/sql_string' + module Arel class TreeManager include Arel::FactoryMethods @@ -21,7 +23,9 @@ module Arel end def to_sql - visitor.accept @ast + collector = Arel::Collectors::SQLString.new + collector = visitor.accept @ast, collector + collector.value end def initialize_copy other diff --git a/lib/arel/visitors/bind_substitute.rb b/lib/arel/visitors/bind_substitute.rb new file mode 100644 index 0000000000..0503a9c986 --- /dev/null +++ b/lib/arel/visitors/bind_substitute.rb @@ -0,0 +1,9 @@ +module Arel + module Visitors + class BindSubstitute + def initialize delegte + @delegate = delegate + end + end + end +end diff --git a/lib/arel/visitors/bind_visitor.rb b/lib/arel/visitors/bind_visitor.rb index 5cb251ffde..c316c8d702 100644 --- a/lib/arel/visitors/bind_visitor.rb +++ b/lib/arel/visitors/bind_visitor.rb @@ -6,24 +6,26 @@ module Arel super end - def accept node, &block + def accept node, collector, &block @block = block if block_given? super end private - def visit_Arel_Nodes_Assignment o + def visit_Arel_Nodes_Assignment o, collector if o.right.is_a? Arel::Nodes::BindParam - "#{visit o.left} = #{visit o.right}" + collector = visit o.left, collector + collector << " = " + visit o.right, collector else super end end - def visit_Arel_Nodes_BindParam o + def visit_Arel_Nodes_BindParam o, collector if @block - @block.call + @block.call(collector) else super end diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 84034829a5..d8883073d0 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -116,20 +116,20 @@ module Arel o.alias ? " AS #{visit o.alias}" : ''}" end - def visit_Arel_Nodes_Casted o - quoted o.val, o.attribute + def visit_Arel_Nodes_Casted o, collector + collector << quoted(o.val, o.attribute).to_s end - def visit_Arel_Nodes_Quoted o - quoted o.expr, nil + def visit_Arel_Nodes_Quoted o, collector + collector << quoted(o.expr, nil).to_s end - def visit_Arel_Nodes_True o - "TRUE" + def visit_Arel_Nodes_True o, collector + collector << "TRUE" end - def visit_Arel_Nodes_False o - "FALSE" + def visit_Arel_Nodes_False o, collector + collector << "FALSE" end def table_exists? name @@ -160,81 +160,93 @@ module Arel }.join ', '})" end - def visit_Arel_Nodes_SelectStatement o - str = '' - + def visit_Arel_Nodes_SelectStatement o, collector if o.with - str << visit(o.with) - str << SPACE + collector = visit o.with, collector + collector << SPACE end - o.cores.each { |x| str << visit_Arel_Nodes_SelectCore(x) } + collector = o.cores.inject(collector) { |c,x| + visit_Arel_Nodes_SelectCore(x, c) + } unless o.orders.empty? - str << SPACE - str << ORDER_BY + collector << SPACE + collector << ORDER_BY len = o.orders.length - 1 o.orders.each_with_index { |x, i| - str << visit(x) - str << COMMA unless len == i + collector = visit(x, collector) + collector << COMMA unless len == i } end - str << " #{visit(o.limit)}" if o.limit - str << " #{visit(o.offset)}" if o.offset - str << " #{visit(o.lock)}" if o.lock + collector = maybe_visit o.limit, collector + collector = maybe_visit o.offset, collector + collector = maybe_visit o.lock, collector - str.strip! - str + collector end - def visit_Arel_Nodes_SelectCore o - str = "SELECT" + def visit_Arel_Nodes_SelectCore o, collector + collector << "SELECT" + + if o.top + collector << " " + collector = visit o.top, collector + end - str << " #{visit(o.top)}" if o.top - str << " #{visit(o.set_quantifier)}" if o.set_quantifier + if o.set_quantifier + collector << " " + collector = visit o.set_quantifier, collector + end unless o.projections.empty? - str << SPACE + collector << SPACE len = o.projections.length - 1 o.projections.each_with_index do |x, i| - str << visit(x) - str << COMMA unless len == i + collector = visit(x, collector) + collector << COMMA unless len == i end end - str << " FROM #{visit(o.source)}" if o.source && !o.source.empty? + if o.source && !o.source.empty? + collector << " FROM " + collector = visit o.source, collector + end unless o.wheres.empty? - str << WHERE + collector << WHERE len = o.wheres.length - 1 o.wheres.each_with_index do |x, i| - str << visit(x) - str << AND unless len == i + collector = visit(x, collector) + collector << AND unless len == i end end unless o.groups.empty? - str << GROUP_BY + collector << GROUP_BY len = o.groups.length - 1 o.groups.each_with_index do |x, i| - str << visit(x) - str << COMMA unless len == i + collector = visit(x, collector) + collector << COMMA unless len == i end end - str << " #{visit(o.having)}" if o.having + if o.having + collector << " " + collector = visit(o.having, collector) + end unless o.windows.empty? - str << WINDOW + collector << WINDOW len = o.windows.length - 1 o.windows.each_with_index do |x, i| - str << visit(x) - str << COMMA unless len == i + collector = visit(x, collector) + collector << COMMA unless len == i end end - str + collector end def visit_Arel_Nodes_Bin o @@ -245,7 +257,7 @@ module Arel DISTINCT end - def visit_Arel_Nodes_DistinctOn o + def visit_Arel_Nodes_DistinctOn o, collector raise NotImplementedError, 'DISTINCT ON not implemented for this db' end @@ -334,8 +346,9 @@ module Arel "OFFSET #{visit o.expr}" end - def visit_Arel_Nodes_Limit o - "LIMIT #{visit o.expr}" + def visit_Arel_Nodes_Limit o, collector + collector << "LIMIT " + visit o.expr, collector end # FIXME: this does nothing on most databases, but does on MSSQL @@ -347,107 +360,127 @@ module Arel visit o.expr end - def visit_Arel_Nodes_Grouping o - "(#{visit o.expr})" + def visit_Arel_Nodes_Grouping o, collector + collector << "(" + visit(o.expr, collector) << ")" end - def visit_Arel_SelectManager o - "(#{o.to_sql.rstrip})" + def visit_Arel_SelectManager o, collector + collector << "(#{o.to_sql.rstrip})" end def visit_Arel_Nodes_Ascending o "#{visit o.expr} ASC" end - def visit_Arel_Nodes_Descending o - "#{visit o.expr} DESC" + def visit_Arel_Nodes_Descending o, collector + visit(o.expr, collector) << " DESC" end def visit_Arel_Nodes_Group o visit o.expr end - def visit_Arel_Nodes_NamedFunction o - "#{o.name}(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| - visit x - }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" + def visit_Arel_Nodes_NamedFunction o, collector + collector << o.name + collector << "(" + collector << "DISTINCT " if o.distinct + collector = inject_join(o.expressions, collector, ", ") << ")" + if o.alias + collector << " AS " + visit o.alias, collector + else + collector + end end def visit_Arel_Nodes_Extract o "EXTRACT(#{o.field.to_s.upcase} FROM #{visit o.expr})#{o.alias ? " AS #{visit o.alias}" : ''}" end - def visit_Arel_Nodes_Count o - "COUNT(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| - visit x - }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" + def visit_Arel_Nodes_Count o, collector + aggregate "COUNT", o, collector end - def visit_Arel_Nodes_Sum o - "SUM(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| - visit x}.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" + def visit_Arel_Nodes_Sum o, collector + aggregate "SUM", o, collector end - def visit_Arel_Nodes_Max o - "MAX(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| - visit x}.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" + def visit_Arel_Nodes_Max o, collector + aggregate "MAX", o, collector end - def visit_Arel_Nodes_Min o - "MIN(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| - visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" + def visit_Arel_Nodes_Min o, collector + aggregate "MIN", o, collector end - def visit_Arel_Nodes_Avg o - "AVG(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| - visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" + def visit_Arel_Nodes_Avg o, collector + aggregate "AVG", o, collector end def visit_Arel_Nodes_TableAlias o "#{visit o.relation} #{quote_table_name o.name}" end - def visit_Arel_Nodes_Between o - "#{visit o.left} BETWEEN #{visit o.right}" + def visit_Arel_Nodes_Between o, collector + collector = visit o.left, collector + collector << " BETWEEN " + visit o.right, collector end - def visit_Arel_Nodes_GreaterThanOrEqual o - "#{visit o.left} >= #{visit o.right}" + def visit_Arel_Nodes_GreaterThanOrEqual o, collector + collector = visit o.left, collector + collector << " >= " + visit o.right, collector end - def visit_Arel_Nodes_GreaterThan o - "#{visit o.left} > #{visit o.right}" + def visit_Arel_Nodes_GreaterThan o, collector + collector = visit o.left, collector + collector << " > " + visit o.right, collector end - def visit_Arel_Nodes_LessThanOrEqual o - "#{visit o.left} <= #{visit o.right}" + def visit_Arel_Nodes_LessThanOrEqual o, collector + collector = visit o.left, collector + collector << " <= " + visit o.right, collector end - def visit_Arel_Nodes_LessThan o - "#{visit o.left} < #{visit o.right}" + def visit_Arel_Nodes_LessThan o, collector + collector = visit o.left, collector + collector << " < " + visit o.right, collector end - def visit_Arel_Nodes_Matches o - "#{visit o.left} LIKE #{visit o.right}" + def visit_Arel_Nodes_Matches o, collector + collector = visit o.left, collector + collector << " LIKE " + visit o.right, collector end - def visit_Arel_Nodes_DoesNotMatch o - "#{visit o.left} NOT LIKE #{visit o.right}" + def visit_Arel_Nodes_DoesNotMatch o, collector + collector = visit o.left, collector + collector << " NOT LIKE " + visit o.right, collector end - def visit_Arel_Nodes_Regexp o - raise NotImplementedError, '~ not implemented for this db' + def visit_Arel_Nodes_JoinSource o, collector + if o.left + collector = visit o.left, collector + collector << " " + end + if o.right.any? + collector = inject_join o.right, collector, ' ' + end + collector end - def visit_Arel_Nodes_NotRegexp o - raise NotImplementedError, '!~ not implemented for this db' + def visit_Arel_Nodes_Regexp o, collector + raise NotImplementedError, '~ not implemented for this db' end - def visit_Arel_Nodes_JoinSource o - [ - (visit(o.left) if o.left), - o.right.map { |j| visit j }.join(' ') - ].compact.join ' ' + def visit_Arel_Nodes_NotRegexp o, collector + raise NotImplementedError, '!~ not implemented for this db' end def visit_Arel_Nodes_StringJoin o @@ -479,83 +512,103 @@ module Arel "ON #{visit o.expr}" end - def visit_Arel_Nodes_Not o - "NOT (#{visit o.expr})" + def visit_Arel_Nodes_Not o, collector + collector << "NOT (" + visit(o.expr, collector) << ")" end - def visit_Arel_Table o + def visit_Arel_Table o, collector if o.table_alias - "#{quote_table_name o.name} #{quote_table_name o.table_alias}" + collector << "#{quote_table_name o.name} #{quote_table_name o.table_alias}" else - quote_table_name o.name + collector << quote_table_name(o.name) end end - def visit_Arel_Nodes_In o + def visit_Arel_Nodes_In o, collector if Array === o.right && o.right.empty? - '1=0' + collector << '1=0' else - "#{visit o.left} IN (#{visit o.right})" + collector = visit o.left, collector + collector << " IN (" + visit(o.right, collector) << ")" end end - def visit_Arel_Nodes_NotIn o + def visit_Arel_Nodes_NotIn o, collector if Array === o.right && o.right.empty? - '1=1' + collector << '1=1' else - "#{visit o.left} NOT IN (#{visit o.right})" + collector = visit o.left, collector + collector << " NOT IN (" + collector = visit o.right, collector + collector << ")" end end - def visit_Arel_Nodes_And o - o.children.map { |x| visit x}.join ' AND ' + def visit_Arel_Nodes_And o, collector + inject_join o.children, collector, " AND " end - def visit_Arel_Nodes_Or o - "#{visit o.left} OR #{visit o.right}" + def visit_Arel_Nodes_Or o, collector + collector = visit o.left, collector + collector << " OR " + visit o.right, collector end - def visit_Arel_Nodes_Assignment o + def visit_Arel_Nodes_Assignment o, collector case o.right when Arel::Nodes::UnqualifiedColumn, Arel::Attributes::Attribute - "#{visit o.left} = #{visit o.right}" + collector = visit o.left, collector + collector << " = " + visit o.right, collector else - right = quote(o.right, column_for(o.left)) - "#{visit o.left} = #{right}" + collector = visit o.left, collector + collector << " = " + collector << quote(o.right, column_for(o.left)).to_s end end - def visit_Arel_Nodes_Equality o + def visit_Arel_Nodes_Equality o, collector right = o.right + collector = visit o.left, collector + if right.nil? - "#{visit o.left} IS NULL" + collector << " IS NULL" else - "#{visit o.left} = #{visit right}" + collector << " = " + visit right, collector end end - def visit_Arel_Nodes_NotEqual o + def visit_Arel_Nodes_NotEqual o, collector right = o.right + collector = visit o.left, collector + if right.nil? - "#{visit o.left} IS NOT NULL" + collector << " IS NOT NULL" else - "#{visit o.left} != #{visit right}" + collector << " != " + visit right, collector end end - def visit_Arel_Nodes_As o - "#{visit o.left} AS #{visit o.right}" + def visit_Arel_Nodes_As o, collector + collector = visit o.left, collector + collector << " AS " + visit o.right, collector end - def visit_Arel_Nodes_UnqualifiedColumn o - "#{quote_column_name o.name}" + def visit_Arel_Nodes_UnqualifiedColumn o, collector + collector << "#{quote_column_name o.name}" + collector end - def visit_Arel_Attributes_Attribute o + def visit_Arel_Attributes_Attribute o, collector join_name = o.relation.table_alias || o.relation.name - "#{quote_table_name join_name}.#{quote_column_name o.name}" + collector << "#{quote_table_name join_name}.#{quote_column_name o.name}" end alias :visit_Arel_Attributes_Integer :visit_Arel_Attributes_Attribute alias :visit_Arel_Attributes_Float :visit_Arel_Attributes_Attribute @@ -564,7 +617,7 @@ module Arel alias :visit_Arel_Attributes_Time :visit_Arel_Attributes_Attribute alias :visit_Arel_Attributes_Boolean :visit_Arel_Attributes_Attribute - def literal o; o end + def literal o, collector; collector << o.to_s; end alias :visit_Arel_Nodes_BindParam :literal alias :visit_Arel_Nodes_SqlLiteral :literal @@ -594,8 +647,10 @@ module Arel alias :visit_Time :unsupported alias :visit_TrueClass :unsupported - def visit_Arel_Nodes_InfixOperation o - "#{visit o.left} #{o.operator} #{visit o.right}" + def visit_Arel_Nodes_InfixOperation o, collector + collector = visit o.left, collector + collector << " #{o.operator} " + visit o.right, collector end alias :visit_Arel_Nodes_Addition :visit_Arel_Nodes_InfixOperation @@ -603,8 +658,8 @@ module Arel alias :visit_Arel_Nodes_Multiplication :visit_Arel_Nodes_InfixOperation alias :visit_Arel_Nodes_Division :visit_Arel_Nodes_InfixOperation - def visit_Array o - o.map { |x| visit x }.join(', ') + def visit_Array o, collector + inject_join o, collector, ", " end def quote value, column = nil @@ -620,6 +675,37 @@ module Arel def quote_column_name name @quoted_columns[name] ||= Arel::Nodes::SqlLiteral === name ? name : @connection.quote_column_name(name) end + + def maybe_visit thing, collector + return collector unless thing + collector << " " + visit thing, collector + end + + def inject_join list, collector, join_str + len = list.length - 1 + list.each_with_index.inject(collector) { |c, (x,i)| + if i == len + visit x, c + else + visit(x, c) << join_str + end + } + end + + def aggregate name, o, collector + collector << "#{name}(" + if o.distinct + collector << "DISTINCT " + end + collector = inject_join(o.expressions, collector, ", ") << ")" + if o.alias + collector << " AS " + visit o.alias, collector + else + collector + end + end end end end diff --git a/lib/arel/visitors/visitor.rb b/lib/arel/visitors/visitor.rb index 0730c15794..2d4b472ea3 100644 --- a/lib/arel/visitors/visitor.rb +++ b/lib/arel/visitors/visitor.rb @@ -1,8 +1,8 @@ module Arel module Visitors class Visitor - def accept object - visit object + def accept object, collector + visit object, collector end private @@ -18,8 +18,8 @@ module Arel DISPATCH[self.class] end - def visit object - send dispatch[object.class], object + def visit object, collector + send dispatch[object.class], object, collector rescue NoMethodError => e raise e if respond_to?(dispatch[object.class], true) superklass = object.class.ancestors.find { |klass| diff --git a/test/helper.rb b/test/helper.rb index 1292c09a08..6e8ac836fc 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -11,3 +11,12 @@ class Object gsub(/\s+/, ' ').strip.must_equal other.gsub(/\s+/, ' ').strip end end + +module Arel + class Test < MiniTest::Test + def assert_like expected, actual + assert_equal expected.gsub(/\s+/, ' ').strip, + actual.gsub(/\s+/, ' ').strip + end + end +end diff --git a/test/visitors/test_bind_visitor.rb b/test/visitors/test_bind_visitor.rb index 2bfd03c861..4563fddfd1 100644 --- a/test/visitors/test_bind_visitor.rb +++ b/test/visitors/test_bind_visitor.rb @@ -4,8 +4,14 @@ require 'support/fake_record' module Arel module Visitors - class TestBindVisitor < Minitest::Test - + class TestBindVisitor < Arel::Test + attr_reader :collector + + def setup + @collector = Collectors::SQLString.new + super + end + ## # Tests visit_Arel_Nodes_Assignment correctly # substitutes binds with values from block @@ -19,8 +25,12 @@ module Arel }.new Table.engine.connection assignment = um.ast.values[0] - actual = visitor.accept(assignment) { "replace" } - actual.must_be_like "\"name\" = replace" + actual = visitor.accept(assignment, collector) { |collector| + collector << "replace" + } + assert actual + value = actual.value + assert_like "\"name\" = replace", value end def test_visitor_yields_on_binds @@ -33,7 +43,7 @@ module Arel bp = Nodes::BindParam.new 'omg' called = false - visitor.accept(bp) { called = true } + visitor.accept(bp, collector) { |collector| called = true } assert called end @@ -49,7 +59,7 @@ module Arel called = false assert_raises(TypeError) { - visitor.accept(bp) { called = true } + visitor.accept(bp, collector) { called = true } } refute called end diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb index 644951d71c..b6d91046b8 100644 --- a/test/visitors/test_to_sql.rb +++ b/test/visitors/test_to_sql.rb @@ -6,20 +6,25 @@ module Arel before do @conn = FakeRecord::Base.new @visitor = ToSql.new @conn.connection + @collector = Collectors::SQLString.new @table = Table.new(:users) @attr = @table[:id] end + def compile node + @visitor.accept(node, Collectors::SQLString.new).value + end + it 'works with BindParams' do node = Nodes::BindParam.new 'omg' - sql = @visitor.accept node + sql = compile node sql.must_be_like 'omg' end it 'can define a dispatch method' do visited = false viz = Class.new(Arel::Visitors::Visitor) { - define_method(:hello) do |node| + define_method(:hello) do |node, c| visited = true end @@ -28,75 +33,75 @@ module Arel end }.new - viz.accept(@table) + viz.accept(@table, @collector) assert visited, 'hello method was called' end it 'should not quote sql literals' do node = @table[Arel.star] - sql = @visitor.accept node + sql = compile node sql.must_be_like '"users".*' end it 'should visit named functions' do function = Nodes::NamedFunction.new('omg', [Arel.star]) - assert_equal 'omg(*)', @visitor.accept(function) + assert_equal 'omg(*)', compile(function) end it 'should chain predications on named functions' do function = Nodes::NamedFunction.new('omg', [Arel.star]) - sql = @visitor.accept(function.eq(2)) + sql = compile(function.eq(2)) sql.must_be_like %{ omg(*) = 2 } end it 'should visit built-in functions' do function = Nodes::Count.new([Arel.star]) - assert_equal 'COUNT(*)', @visitor.accept(function) + assert_equal 'COUNT(*)', compile(function) function = Nodes::Sum.new([Arel.star]) - assert_equal 'SUM(*)', @visitor.accept(function) + assert_equal 'SUM(*)', compile(function) function = Nodes::Max.new([Arel.star]) - assert_equal 'MAX(*)', @visitor.accept(function) + assert_equal 'MAX(*)', compile(function) function = Nodes::Min.new([Arel.star]) - assert_equal 'MIN(*)', @visitor.accept(function) + assert_equal 'MIN(*)', compile(function) function = Nodes::Avg.new([Arel.star]) - assert_equal 'AVG(*)', @visitor.accept(function) + assert_equal 'AVG(*)', compile(function) end it 'should visit built-in functions operating on distinct values' do function = Nodes::Count.new([Arel.star]) function.distinct = true - assert_equal 'COUNT(DISTINCT *)', @visitor.accept(function) + assert_equal 'COUNT(DISTINCT *)', compile(function) function = Nodes::Sum.new([Arel.star]) function.distinct = true - assert_equal 'SUM(DISTINCT *)', @visitor.accept(function) + assert_equal 'SUM(DISTINCT *)', compile(function) function = Nodes::Max.new([Arel.star]) function.distinct = true - assert_equal 'MAX(DISTINCT *)', @visitor.accept(function) + assert_equal 'MAX(DISTINCT *)', compile(function) function = Nodes::Min.new([Arel.star]) function.distinct = true - assert_equal 'MIN(DISTINCT *)', @visitor.accept(function) + assert_equal 'MIN(DISTINCT *)', compile(function) function = Nodes::Avg.new([Arel.star]) function.distinct = true - assert_equal 'AVG(DISTINCT *)', @visitor.accept(function) + assert_equal 'AVG(DISTINCT *)', compile(function) end it 'works with lists' do function = Nodes::NamedFunction.new('omg', [Arel.star, Arel.star]) - assert_equal 'omg(*, *)', @visitor.accept(function) + assert_equal 'omg(*, *)', compile(function) end describe 'Nodes::Equality' do it "should escape strings" do test = Table.new(:users)[:name].eq 'Aaron Patterson' - @visitor.accept(test).must_be_like %{ + compile(test).must_be_like %{ "users"."name" = 'Aaron Patterson' } end @@ -104,19 +109,19 @@ module Arel it 'should handle false' do table = Table.new(:users) val = Nodes.build_quoted(false, table[:active]) - sql = @visitor.accept Nodes::Equality.new(val, val) + sql = compile Nodes::Equality.new(val, val) sql.must_be_like %{ 'f' = 'f' } end it 'should use the column to quote' do table = Table.new(:users) val = Nodes.build_quoted('1-fooo', table[:id]) - sql = @visitor.accept Nodes::Equality.new(table[:id], val) + sql = compile Nodes::Equality.new(table[:id], val) sql.must_be_like %{ "users"."id" = 1 } end it 'should handle nil' do - sql = @visitor.accept Nodes::Equality.new(@table[:name], nil) + sql = compile Nodes::Equality.new(@table[:name], nil) sql.must_be_like %{ "users"."name" IS NULL } end end @@ -124,13 +129,13 @@ module Arel describe 'Nodes::NotEqual' do it 'should handle false' do val = Nodes.build_quoted(false, @table[:active]) - sql = @visitor.accept Nodes::NotEqual.new(@table[:active], val) + sql = compile Nodes::NotEqual.new(@table[:active], val) sql.must_be_like %{ "users"."active" != 'f' } end it 'should handle nil' do val = Nodes.build_quoted(nil, @table[:active]) - sql = @visitor.accept Nodes::NotEqual.new(@table[:name], val) + sql = compile Nodes::NotEqual.new(@table[:name], val) sql.must_be_like %{ "users"."name" IS NOT NULL } end end @@ -141,19 +146,19 @@ module Arel Class.new(Class.new(String)).new(":'("), ].each do |obj| val = Nodes.build_quoted(obj, @table[:active]) - sql = @visitor.accept Nodes::NotEqual.new(@table[:name], val) + sql = compile Nodes::NotEqual.new(@table[:name], val) sql.must_be_like %{ "users"."name" != ':\\'(' } end end it "should visit_Class" do - @visitor.accept(Nodes.build_quoted(DateTime)).must_equal "'DateTime'" + compile(Nodes.build_quoted(DateTime)).must_equal "'DateTime'" end it "should escape LIMIT" do sc = Arel::Nodes::SelectStatement.new sc.limit = Arel::Nodes::Limit.new(Nodes.build_quoted("omg")) - assert_match(/LIMIT 'omg'/, @visitor.accept(sc)) + assert_match(/LIMIT 'omg'/, compile(sc)) end it "should visit_DateTime" do @@ -168,7 +173,7 @@ module Arel dt = DateTime.now table = Table.new(:users) test = table[:created_at].eq dt - sql = @visitor.accept test + sql = compile test assert_equal "created_at", called_with.name sql.must_be_like %{"users"."created_at" = '#{dt.strftime("%Y-%m-%d %H:%M:%S")}'} @@ -176,37 +181,37 @@ module Arel it "should visit_Float" do test = Table.new(:users)[:name].eq 2.14 - sql = @visitor.accept test + sql = compile test sql.must_be_like %{"users"."name" = 2.14} end it "should visit_Not" do - sql = @visitor.accept Nodes::Not.new(Arel.sql("foo")) + sql = compile Nodes::Not.new(Arel.sql("foo")) sql.must_be_like "NOT (foo)" end it "should apply Not to the whole expression" do node = Nodes::And.new [@attr.eq(10), @attr.eq(11)] - sql = @visitor.accept Nodes::Not.new(node) + sql = compile Nodes::Not.new(node) sql.must_be_like %{NOT ("users"."id" = 10 AND "users"."id" = 11)} end it "should visit_As" do as = Nodes::As.new(Arel.sql("foo"), Arel.sql("bar")) - sql = @visitor.accept as + sql = compile as sql.must_be_like "foo AS bar" end it "should visit_Bignum" do - @visitor.accept 8787878092 + compile 8787878092 end it "should visit_Hash" do - @visitor.accept(Nodes.build_quoted({:a => 1})) + compile(Nodes.build_quoted({:a => 1})) end it "should visit_BigDecimal" do - @visitor.accept Nodes.build_quoted(BigDecimal.new('2.14')) + compile Nodes.build_quoted(BigDecimal.new('2.14')) end it "should visit_Date" do @@ -221,31 +226,31 @@ module Arel dt = Date.today table = Table.new(:users) test = table[:created_at].eq dt - sql = @visitor.accept test + sql = compile test assert_equal "created_at", called_with.name sql.must_be_like %{"users"."created_at" = '#{dt.strftime("%Y-%m-%d")}'} end it "should visit_NilClass" do - @visitor.accept(Nodes.build_quoted(nil)).must_be_like "NULL" + compile(Nodes.build_quoted(nil)).must_be_like "NULL" end it "should visit_Arel_SelectManager, which is a subquery" do mgr = Table.new(:foo).project(:bar) - @visitor.accept(mgr).must_be_like '(SELECT bar FROM "foo")' + compile(mgr).must_be_like '(SELECT bar FROM "foo")' end it "should visit_Arel_Nodes_And" do node = Nodes::And.new [@attr.eq(10), @attr.eq(11)] - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" = 10 AND "users"."id" = 11 } end it "should visit_Arel_Nodes_Or" do node = Nodes::Or.new @attr.eq(10), @attr.eq(11) - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" = 10 OR "users"."id" = 11 } end @@ -256,25 +261,25 @@ module Arel Nodes::UnqualifiedColumn.new(column), Nodes::UnqualifiedColumn.new(column) ) - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "id" = "id" } end it "should visit visit_Arel_Attributes_Time" do attr = Attributes::Time.new(@attr.relation, @attr.name) - @visitor.accept attr + compile attr end it "should visit_TrueClass" do test = Table.new(:users)[:bool].eq(true) - @visitor.accept(test).must_be_like %{ "users"."bool" = 't' } + compile(test).must_be_like %{ "users"."bool" = 't' } end describe "Nodes::Matches" do it "should know how to visit" do node = @table[:name].matches('foo%') - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."name" LIKE 'foo%' } end @@ -282,7 +287,7 @@ module Arel it 'can handle subqueries' do subquery = @table.project(:id).where(@table[:name].matches('foo%')) node = @attr.in subquery - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" IN (SELECT id FROM "users" WHERE "users"."name" LIKE 'foo%') } end @@ -291,7 +296,7 @@ module Arel describe "Nodes::DoesNotMatch" do it "should know how to visit" do node = @table[:name].does_not_match('foo%') - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."name" NOT LIKE 'foo%' } end @@ -299,7 +304,7 @@ module Arel it 'can handle subqueries' do subquery = @table.project(:id).where(@table[:name].does_not_match('foo%')) node = @attr.in subquery - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" IN (SELECT id FROM "users" WHERE "users"."name" NOT LIKE 'foo%') } end @@ -308,7 +313,7 @@ module Arel describe "Nodes::Ordering" do it "should know how to visit" do node = @attr.desc - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" DESC } end @@ -317,52 +322,52 @@ module Arel describe "Nodes::In" do it "should know how to visit" do node = @attr.in [1, 2, 3] - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" IN (1, 2, 3) } end it "should return 1=0 when empty right which is always false" do node = @attr.in [] - @visitor.accept(node).must_equal '1=0' + compile(node).must_equal '1=0' end it 'can handle two dot ranges' do node = @attr.in 1..3 - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" BETWEEN 1 AND 3 } end it 'can handle three dot ranges' do node = @attr.in 1...3 - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" >= 1 AND "users"."id" < 3 } end it 'can handle ranges bounded by infinity' do node = @attr.in 1..Float::INFINITY - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" >= 1 } node = @attr.in(-Float::INFINITY..3) - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" <= 3 } node = @attr.in(-Float::INFINITY...3) - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" < 3 } node = @attr.in(-Float::INFINITY..Float::INFINITY) - @visitor.accept(node).must_be_like %{1=1} + compile(node).must_be_like %{1=1} end it 'can handle subqueries' do table = Table.new(:users) subquery = table.project(:id).where(table[:name].eq('Aaron')) node = @attr.in subquery - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" IN (SELECT id FROM "users" WHERE "users"."name" = 'Aaron') } end @@ -383,29 +388,29 @@ module Arel visitor.expected = Table.engine.connection.columns(:users).find { |x| x.name == 'name' } - visitor.accept(in_node).must_equal %("users"."name" IN ('a', 'b', 'c')) + visitor.accept(in_node, Collectors::SQLString.new).value.must_equal %("users"."name" IN ('a', 'b', 'c')) end end describe "Nodes::InfixOperation" do it "should handle Multiplication" do node = Arel::Attributes::Decimal.new(Table.new(:products), :price) * Arel::Attributes::Decimal.new(Table.new(:currency_rates), :rate) - @visitor.accept(node).must_equal %("products"."price" * "currency_rates"."rate") + compile(node).must_equal %("products"."price" * "currency_rates"."rate") end it "should handle Division" do node = Arel::Attributes::Decimal.new(Table.new(:products), :price) / 5 - @visitor.accept(node).must_equal %("products"."price" / 5) + compile(node).must_equal %("products"."price" / 5) end it "should handle Addition" do node = Arel::Attributes::Decimal.new(Table.new(:products), :price) + 6 - @visitor.accept(node).must_equal %(("products"."price" + 6)) + compile(node).must_equal %(("products"."price" + 6)) end it "should handle Subtraction" do node = Arel::Attributes::Decimal.new(Table.new(:products), :price) - 7 - @visitor.accept(node).must_equal %(("products"."price" - 7)) + compile(node).must_equal %(("products"."price" - 7)) end it "should handle arbitrary operators" do @@ -414,59 +419,59 @@ module Arel Arel::Attributes::String.new(Table.new(:products), :name), Arel::Attributes::String.new(Table.new(:products), :name) ) - @visitor.accept(node).must_equal %("products"."name" || "products"."name") + compile(node).must_equal %("products"."name" || "products"."name") end end describe "Nodes::NotIn" do it "should know how to visit" do node = @attr.not_in [1, 2, 3] - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" NOT IN (1, 2, 3) } end it "should return 1=1 when empty right which is always true" do node = @attr.not_in [] - @visitor.accept(node).must_equal '1=1' + compile(node).must_equal '1=1' end it 'can handle two dot ranges' do node = @attr.not_in 1..3 - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" < 1 OR "users"."id" > 3 } end it 'can handle three dot ranges' do node = @attr.not_in 1...3 - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" < 1 OR "users"."id" >= 3 } end it 'can handle ranges bounded by infinity' do node = @attr.not_in 1..Float::INFINITY - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" < 1 } node = @attr.not_in(-Float::INFINITY..3) - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" > 3 } node = @attr.not_in(-Float::INFINITY...3) - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" >= 3 } node = @attr.not_in(-Float::INFINITY..Float::INFINITY) - @visitor.accept(node).must_be_like %{1=0} + compile(node).must_be_like %{1=0} end it 'can handle subqueries' do table = Table.new(:users) subquery = table.project(:id).where(table[:name].eq('Aaron')) node = @attr.not_in subquery - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" NOT IN (SELECT id FROM "users" WHERE "users"."name" = 'Aaron') } end @@ -487,21 +492,21 @@ module Arel visitor.expected = Table.engine.connection.columns(:users).find { |x| x.name == 'name' } - visitor.accept(in_node).must_equal %("users"."name" NOT IN ('a', 'b', 'c')) + compile(in_node).must_equal %("users"."name" NOT IN ('a', 'b', 'c')) end end describe 'Constants' do it "should handle true" do test = Table.new(:users).create_true - @visitor.accept(test).must_be_like %{ + compile(test).must_be_like %{ TRUE } end it "should handle false" do test = Table.new(:users).create_false - @visitor.accept(test).must_be_like %{ + compile(test).must_be_like %{ FALSE } end @@ -510,7 +515,7 @@ module Arel describe 'TableAlias' do it "should use the underlying table for checking columns" do test = Table.new(:users).alias('zomgusers')[:id].eq '3' - @visitor.accept(test).must_be_like %{ + compile(test).must_be_like %{ "zomgusers"."id" = 3 } end @@ -522,7 +527,7 @@ module Arel core.set_quantifier = Arel::Nodes::DistinctOn.new(Arel.sql('aaron')) assert_raises(NotImplementedError) do - @visitor.accept(core) + compile(core) end end end @@ -532,7 +537,7 @@ module Arel node = Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo%')) assert_raises(NotImplementedError) do - @visitor.accept(node) + compile(node) end end end @@ -542,7 +547,7 @@ module Arel node = Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo%')) assert_raises(NotImplementedError) do - @visitor.accept(node) + compile(node) end end end |