1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

postgresql visitor is working

This commit is contained in:
Aaron Patterson 2014-04-08 17:16:21 -07:00
parent 93036b168b
commit b2fb1d3381
4 changed files with 31 additions and 27 deletions

View file

@ -3,24 +3,25 @@ module Arel
class PostgreSQL < Arel::Visitors::ToSql class PostgreSQL < Arel::Visitors::ToSql
private private
def visit_Arel_Nodes_Matches o def visit_Arel_Nodes_Matches o, collector
"#{visit o.left} ILIKE #{visit o.right}" infix_value o, collector, ' ILIKE '
end end
def visit_Arel_Nodes_DoesNotMatch o def visit_Arel_Nodes_DoesNotMatch o, collector
"#{visit o.left} NOT ILIKE #{visit o.right}" infix_value o, collector, ' NOT ILIKE '
end end
def visit_Arel_Nodes_Regexp o def visit_Arel_Nodes_Regexp o, collector
"#{visit o.left} ~ #{visit o.right}" infix_value o, collector, ' ~ '
end end
def visit_Arel_Nodes_NotRegexp o def visit_Arel_Nodes_NotRegexp o, collector
"#{visit o.left} !~ #{visit o.right}" infix_value o, collector, ' !~ '
end end
def visit_Arel_Nodes_DistinctOn o def visit_Arel_Nodes_DistinctOn o, collector
"DISTINCT ON ( #{visit o.expr} )" collector << "DISTINCT ON ( "
visit(o.expr, collector) << " )"
end end
end end
end end

View file

@ -275,8 +275,8 @@ module Arel
visit o.expr visit o.expr
end end
def visit_Arel_Nodes_Distinct o def visit_Arel_Nodes_Distinct o, collector
DISTINCT collector << DISTINCT
end end
def visit_Arel_Nodes_DistinctOn o, collector def visit_Arel_Nodes_DistinctOn o, collector

View file

@ -9,16 +9,20 @@ module Arel
@attr = @table[:id] @attr = @table[:id]
end end
def compile node
@visitor.accept(node, Collectors::SQLString.new).value
end
describe 'locking' do describe 'locking' do
it 'defaults to FOR UPDATE' do it 'defaults to FOR UPDATE' do
@visitor.accept(Nodes::Lock.new(Arel.sql('FOR UPDATE'))).must_be_like %{ compile(Nodes::Lock.new(Arel.sql('FOR UPDATE'))).must_be_like %{
FOR UPDATE FOR UPDATE
} }
end end
it 'allows a custom string to be used as a lock' do it 'allows a custom string to be used as a lock' do
node = Nodes::Lock.new(Arel.sql('FOR SHARE')) node = Nodes::Lock.new(Arel.sql('FOR SHARE'))
@visitor.accept(node).must_be_like %{ compile(node).must_be_like %{
FOR SHARE FOR SHARE
} }
end end
@ -29,7 +33,7 @@ module Arel
sc.limit = Nodes::Limit.new(Nodes.build_quoted("omg")) sc.limit = Nodes::Limit.new(Nodes.build_quoted("omg"))
sc.cores.first.projections << Arel.sql('DISTINCT ON') sc.cores.first.projections << Arel.sql('DISTINCT ON')
sc.orders << Arel.sql("xyz") sc.orders << Arel.sql("xyz")
sql = @visitor.accept(sc) sql = compile(sc)
assert_match(/LIMIT 'omg'/, sql) assert_match(/LIMIT 'omg'/, sql)
assert_equal 1, sql.scan(/LIMIT/).length, 'should have one limit' assert_equal 1, sql.scan(/LIMIT/).length, 'should have one limit'
end end
@ -37,19 +41,19 @@ module Arel
it 'should support DISTINCT ON' do it 'should support DISTINCT ON' do
core = Arel::Nodes::SelectCore.new core = Arel::Nodes::SelectCore.new
core.set_quantifier = Arel::Nodes::DistinctOn.new(Arel.sql('aaron')) core.set_quantifier = Arel::Nodes::DistinctOn.new(Arel.sql('aaron'))
assert_match 'DISTINCT ON ( aaron )', @visitor.accept(core) assert_match 'DISTINCT ON ( aaron )', compile(core)
end end
it 'should support DISTINCT' do it 'should support DISTINCT' do
core = Arel::Nodes::SelectCore.new core = Arel::Nodes::SelectCore.new
core.set_quantifier = Arel::Nodes::Distinct.new core.set_quantifier = Arel::Nodes::Distinct.new
assert_equal 'SELECT DISTINCT', @visitor.accept(core) assert_equal 'SELECT DISTINCT', compile(core)
end end
describe "Nodes::Matches" do describe "Nodes::Matches" do
it "should know how to visit" do it "should know how to visit" do
node = @table[:name].matches('foo%') node = @table[:name].matches('foo%')
@visitor.accept(node).must_be_like %{ compile(node).must_be_like %{
"users"."name" ILIKE 'foo%' "users"."name" ILIKE 'foo%'
} }
end end
@ -57,7 +61,7 @@ module Arel
it 'can handle subqueries' do it 'can handle subqueries' do
subquery = @table.project(:id).where(@table[:name].matches('foo%')) subquery = @table.project(:id).where(@table[:name].matches('foo%'))
node = @attr.in subquery 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" ILIKE 'foo%') "users"."id" IN (SELECT id FROM "users" WHERE "users"."name" ILIKE 'foo%')
} }
end end
@ -66,7 +70,7 @@ module Arel
describe "Nodes::DoesNotMatch" do describe "Nodes::DoesNotMatch" do
it "should know how to visit" do it "should know how to visit" do
node = @table[:name].does_not_match('foo%') node = @table[:name].does_not_match('foo%')
@visitor.accept(node).must_be_like %{ compile(node).must_be_like %{
"users"."name" NOT ILIKE 'foo%' "users"."name" NOT ILIKE 'foo%'
} }
end end
@ -74,7 +78,7 @@ module Arel
it 'can handle subqueries' do it 'can handle subqueries' do
subquery = @table.project(:id).where(@table[:name].does_not_match('foo%')) subquery = @table.project(:id).where(@table[:name].does_not_match('foo%'))
node = @attr.in subquery 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 ILIKE 'foo%') "users"."id" IN (SELECT id FROM "users" WHERE "users"."name" NOT ILIKE 'foo%')
} }
end end
@ -83,7 +87,7 @@ module Arel
describe "Nodes::Regexp" do describe "Nodes::Regexp" do
it "should know how to visit" do it "should know how to visit" do
node = Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo%')) node = Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo%'))
@visitor.accept(node).must_be_like %{ compile(node).must_be_like %{
"users"."name" ~ 'foo%' "users"."name" ~ 'foo%'
} }
end end
@ -91,7 +95,7 @@ module Arel
it 'can handle subqueries' do it 'can handle subqueries' do
subquery = @table.project(:id).where(Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo%'))) subquery = @table.project(:id).where(Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo%')))
node = @attr.in subquery 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" ~ 'foo%') "users"."id" IN (SELECT id FROM "users" WHERE "users"."name" ~ 'foo%')
} }
end end
@ -100,7 +104,7 @@ module Arel
describe "Nodes::NotRegexp" do describe "Nodes::NotRegexp" do
it "should know how to visit" do it "should know how to visit" do
node = Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo%')) node = Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo%'))
@visitor.accept(node).must_be_like %{ compile(node).must_be_like %{
"users"."name" !~ 'foo%' "users"."name" !~ 'foo%'
} }
end end
@ -108,7 +112,7 @@ module Arel
it 'can handle subqueries' do it 'can handle subqueries' do
subquery = @table.project(:id).where(Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo%'))) subquery = @table.project(:id).where(Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo%')))
node = @attr.in subquery 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" !~ 'foo%') "users"."id" IN (SELECT id FROM "users" WHERE "users"."name" !~ 'foo%')
} }
end end

View file

@ -6,7 +6,6 @@ module Arel
before do before do
@conn = FakeRecord::Base.new @conn = FakeRecord::Base.new
@visitor = ToSql.new @conn.connection @visitor = ToSql.new @conn.connection
@collector = Collectors::SQLString.new
@table = Table.new(:users) @table = Table.new(:users)
@attr = @table[:id] @attr = @table[:id]
end end
@ -33,7 +32,7 @@ module Arel
end end
}.new }.new
viz.accept(@table, @collector) viz.accept(@table, Collectors::SQLString.new)
assert visited, 'hello method was called' assert visited, 'hello method was called'
end end