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:
parent
93036b168b
commit
b2fb1d3381
4 changed files with 31 additions and 27 deletions
|
@ -3,24 +3,25 @@ module Arel
|
|||
class PostgreSQL < Arel::Visitors::ToSql
|
||||
private
|
||||
|
||||
def visit_Arel_Nodes_Matches o
|
||||
"#{visit o.left} ILIKE #{visit o.right}"
|
||||
def visit_Arel_Nodes_Matches o, collector
|
||||
infix_value o, collector, ' ILIKE '
|
||||
end
|
||||
|
||||
def visit_Arel_Nodes_DoesNotMatch o
|
||||
"#{visit o.left} NOT ILIKE #{visit o.right}"
|
||||
def visit_Arel_Nodes_DoesNotMatch o, collector
|
||||
infix_value o, collector, ' NOT ILIKE '
|
||||
end
|
||||
|
||||
def visit_Arel_Nodes_Regexp o
|
||||
"#{visit o.left} ~ #{visit o.right}"
|
||||
def visit_Arel_Nodes_Regexp o, collector
|
||||
infix_value o, collector, ' ~ '
|
||||
end
|
||||
|
||||
def visit_Arel_Nodes_NotRegexp o
|
||||
"#{visit o.left} !~ #{visit o.right}"
|
||||
def visit_Arel_Nodes_NotRegexp o, collector
|
||||
infix_value o, collector, ' !~ '
|
||||
end
|
||||
|
||||
def visit_Arel_Nodes_DistinctOn o
|
||||
"DISTINCT ON ( #{visit o.expr} )"
|
||||
def visit_Arel_Nodes_DistinctOn o, collector
|
||||
collector << "DISTINCT ON ( "
|
||||
visit(o.expr, collector) << " )"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -275,8 +275,8 @@ module Arel
|
|||
visit o.expr
|
||||
end
|
||||
|
||||
def visit_Arel_Nodes_Distinct o
|
||||
DISTINCT
|
||||
def visit_Arel_Nodes_Distinct o, collector
|
||||
collector << DISTINCT
|
||||
end
|
||||
|
||||
def visit_Arel_Nodes_DistinctOn o, collector
|
||||
|
|
|
@ -9,16 +9,20 @@ module Arel
|
|||
@attr = @table[:id]
|
||||
end
|
||||
|
||||
def compile node
|
||||
@visitor.accept(node, Collectors::SQLString.new).value
|
||||
end
|
||||
|
||||
describe 'locking' 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
|
||||
}
|
||||
end
|
||||
|
||||
it 'allows a custom string to be used as a lock' do
|
||||
node = Nodes::Lock.new(Arel.sql('FOR SHARE'))
|
||||
@visitor.accept(node).must_be_like %{
|
||||
compile(node).must_be_like %{
|
||||
FOR SHARE
|
||||
}
|
||||
end
|
||||
|
@ -29,7 +33,7 @@ module Arel
|
|||
sc.limit = Nodes::Limit.new(Nodes.build_quoted("omg"))
|
||||
sc.cores.first.projections << Arel.sql('DISTINCT ON')
|
||||
sc.orders << Arel.sql("xyz")
|
||||
sql = @visitor.accept(sc)
|
||||
sql = compile(sc)
|
||||
assert_match(/LIMIT 'omg'/, sql)
|
||||
assert_equal 1, sql.scan(/LIMIT/).length, 'should have one limit'
|
||||
end
|
||||
|
@ -37,19 +41,19 @@ module Arel
|
|||
it 'should support DISTINCT ON' do
|
||||
core = Arel::Nodes::SelectCore.new
|
||||
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
|
||||
|
||||
it 'should support DISTINCT' do
|
||||
core = Arel::Nodes::SelectCore.new
|
||||
core.set_quantifier = Arel::Nodes::Distinct.new
|
||||
assert_equal 'SELECT DISTINCT', @visitor.accept(core)
|
||||
assert_equal 'SELECT DISTINCT', compile(core)
|
||||
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" ILIKE 'foo%'
|
||||
}
|
||||
end
|
||||
|
@ -57,7 +61,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" ILIKE 'foo%')
|
||||
}
|
||||
end
|
||||
|
@ -66,7 +70,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 ILIKE 'foo%'
|
||||
}
|
||||
end
|
||||
|
@ -74,7 +78,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 ILIKE 'foo%')
|
||||
}
|
||||
end
|
||||
|
@ -83,7 +87,7 @@ module Arel
|
|||
describe "Nodes::Regexp" do
|
||||
it "should know how to visit" do
|
||||
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%'
|
||||
}
|
||||
end
|
||||
|
@ -91,7 +95,7 @@ module Arel
|
|||
it 'can handle subqueries' do
|
||||
subquery = @table.project(:id).where(Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('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" ~ 'foo%')
|
||||
}
|
||||
end
|
||||
|
@ -100,7 +104,7 @@ module Arel
|
|||
describe "Nodes::NotRegexp" do
|
||||
it "should know how to visit" do
|
||||
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%'
|
||||
}
|
||||
end
|
||||
|
@ -108,7 +112,7 @@ module Arel
|
|||
it 'can handle subqueries' do
|
||||
subquery = @table.project(:id).where(Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('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" !~ 'foo%')
|
||||
}
|
||||
end
|
||||
|
|
|
@ -6,7 +6,6 @@ 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
|
||||
|
@ -33,7 +32,7 @@ module Arel
|
|||
end
|
||||
}.new
|
||||
|
||||
viz.accept(@table, @collector)
|
||||
viz.accept(@table, Collectors::SQLString.new)
|
||||
assert visited, 'hello method was called'
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue