提交 6c65b017 编写于 作者: J Jesse Storimer

Allow database specific locking clauses to be used

上级 dae7a245
module Arel
module Nodes
class Lock < Arel::Nodes::Node
attr_reader :locking
def initialize locking = true
@locking = locking
end
end
end
end
......@@ -40,7 +40,7 @@ def where_clauses
def lock locking = true
# FIXME: do we even need to store this? If locking is +false+ shouldn't
# we just remove the node from the AST?
@ast.lock = Nodes::Lock.new
@ast.lock = Nodes::Lock.new(locking)
self
end
......
......@@ -3,7 +3,11 @@ module Visitors
class MySQL < Arel::Visitors::ToSql
private
def visit_Arel_Nodes_Lock o
"FOR UPDATE"
if o.locking.is_a?(String)
o.locking
else
"FOR UPDATE"
end
end
###
......
......@@ -3,7 +3,11 @@ module Visitors
class PostgreSQL < Arel::Visitors::ToSql
private
def visit_Arel_Nodes_Lock o
"FOR UPDATE"
if o.locking.is_a?(String)
o.locking
else
"FOR UPDATE"
end
end
def visit_Arel_Nodes_SelectStatement o
......
......@@ -29,11 +29,16 @@ module Visitors
sql.must_be_like "SELECT FROM DUAL"
end
it 'uses FOR UPDATE when locking' do
stmt = Nodes::SelectStatement.new
stmt.lock = Nodes::Lock.new
sql = @visitor.accept(stmt)
sql.must_be_like "SELECT FROM DUAL FOR UPDATE"
describe 'locking' do
it 'defaults to FOR UPDATE when locking' do
node = Nodes::Lock.new
@visitor.accept(node).must_be_like "FOR UPDATE"
end
it 'allows a custom string to be used as a lock' do
node = Nodes::Lock.new('LOCK IN SHARE MODE')
@visitor.accept(node).must_be_like "LOCK IN SHARE MODE"
end
end
end
end
......
......@@ -7,10 +7,19 @@ module Visitors
@visitor = PostgreSQL.new Table.engine
end
it 'should produce a lock value' do
@visitor.accept(Nodes::Lock.new).must_be_like %{
FOR UPDATE
}
describe 'locking' do
it 'defaults to FOR UPDATE' do
@visitor.accept(Nodes::Lock.new).must_be_like %{
FOR UPDATE
}
end
it 'allows a custom string to be used as a lock' do
node = Nodes::Lock.new('FOR SHARE')
@visitor.accept(node).must_be_like %{
FOR SHARE
}
end
end
it "should escape LIMIT" do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册