diff options
-rw-r--r-- | lib/arel/visitors/informix.rb | 60 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 3 | ||||
-rw-r--r-- | test/visitors/test_informix.rb | 16 |
3 files changed, 50 insertions, 29 deletions
diff --git a/lib/arel/visitors/informix.rb b/lib/arel/visitors/informix.rb index 244c68bed3..8de05f60f6 100644 --- a/lib/arel/visitors/informix.rb +++ b/lib/arel/visitors/informix.rb @@ -2,30 +2,48 @@ module Arel module Visitors class Informix < Arel::Visitors::ToSql private - def visit_Arel_Nodes_SelectStatement o - [ - "SELECT", - (visit(o.offset) if o.offset), - (visit(o.limit) if o.limit), - o.cores.map { |x| visit_Arel_Nodes_SelectCore x }.join, - ("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?), - (visit(o.lock) if o.lock), - ].compact.join ' ' + def visit_Arel_Nodes_SelectStatement o, collector + collector << "SELECT " + collector = maybe_visit o.offset, collector + collector = maybe_visit o.limit, collector + collector = o.cores.inject(collector) { |c,x| + visit_Arel_Nodes_SelectCore x, c + } + if o.orders.any? + collector << "ORDER BY " + collector = inject_join o.orders, collector, ", " + end + collector = maybe_visit o.lock, collector end - def visit_Arel_Nodes_SelectCore o - [ - "#{o.projections.map { |x| visit x }.join ', '}", - ("FROM #{visit(o.source)}" if o.source && !o.source.empty?), - ("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.empty?), - ("GROUP BY #{o.groups.map { |x| visit x }.join ', ' }" unless o.groups.empty?), - (visit(o.having) if o.having), - ].compact.join ' ' + def visit_Arel_Nodes_SelectCore o, collector + collector = inject_join o.projections, collector, ", " + froms = false + if o.source && !o.source.empty? + froms = true + collector << " FROM " + collector = visit o.source, collector + end + + if o.wheres.any? + collector << " WHERE " + collector = inject_join o.wheres, collector, " AND " + end + + if o.groups.any? + collector << "GROUP BY " + collector = inject_join o.groups, collector, ", " + end + + maybe_visit o.having, collector end - def visit_Arel_Nodes_Offset o - "SKIP #{visit o.expr}" + def visit_Arel_Nodes_Offset o, collector + collector << "SKIP " + visit o.expr, collector end - def visit_Arel_Nodes_Limit o - "LIMIT #{visit o.expr}" + def visit_Arel_Nodes_Limit o, collector + collector << "LIMIT " + visit o.expr, collector + collector << " " end end end diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 8e254d504b..ee72d864e1 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -107,8 +107,7 @@ module Arel end unless wheres.empty? - collector << " " if values - collector << "WHERE " + collector << " WHERE " collector = inject_join wheres, collector, " AND " end diff --git a/test/visitors/test_informix.rb b/test/visitors/test_informix.rb index e4c2e81296..6d94282b77 100644 --- a/test/visitors/test_informix.rb +++ b/test/visitors/test_informix.rb @@ -7,10 +7,14 @@ module Arel @visitor = Informix.new Table.engine.connection end + def compile node + @visitor.accept(node, Collectors::SQLString.new).value + end + it 'uses LIMIT n to limit results' do stmt = Nodes::SelectStatement.new stmt.limit = Nodes::Limit.new(1) - sql = @visitor.accept(stmt) + sql = compile(stmt) sql.must_be_like "SELECT LIMIT 1" end @@ -20,14 +24,14 @@ module Arel stmt.relation = table stmt.limit = Nodes::Limit.new(Nodes.build_quoted(1)) stmt.key = table[:id] - sql = @visitor.accept(stmt) - sql.must_be_like "UPDATE \"users\" WHERE \"users\".\"id\" IN (SELECT LIMIT 1 \"users\".\"id\" FROM \"users\" )" + sql = compile(stmt) + sql.must_be_like "UPDATE \"users\" WHERE \"users\".\"id\" IN (SELECT LIMIT 1 \"users\".\"id\" FROM \"users\")" end it 'uses SKIP n to jump results' do stmt = Nodes::SelectStatement.new stmt.offset = Nodes::Offset.new(10) - sql = @visitor.accept(stmt) + sql = compile(stmt) sql.must_be_like "SELECT SKIP 10" end @@ -35,7 +39,7 @@ module Arel stmt = Nodes::SelectStatement.new stmt.limit = Nodes::Limit.new(1) stmt.offset = Nodes::Offset.new(1) - sql = @visitor.accept(stmt) + sql = compile(stmt) sql.must_be_like "SELECT SKIP 1 LIMIT 1" end @@ -45,7 +49,7 @@ module Arel core.source = Nodes::JoinSource.new(table, [table.create_join(Table.new(:comments))]) stmt = Nodes::SelectStatement.new([core]) - sql = @visitor.accept(stmt) + sql = compile(stmt) sql.must_be_like 'SELECT FROM "posts" INNER JOIN "comments"' end |