Browse files

refactoring custom lock logic to use sql literals

  • Loading branch information...
1 parent fa79262 commit 7601b90e8c56dde016fe65ba19f622938ad61d57 @tenderlove tenderlove committed Feb 21, 2011
View
12 lib/arel/select_manager.rb
@@ -34,9 +34,15 @@ def where_clauses
@ctx.wheres.map { |c| to_sql.accept c }
end
- 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?
+ def lock locking = Arel.sql('FOR UPDATE')
+ case locking
+ when true
+ locking = Arel.sql('FOR UPDATE')
+ when Arel::Nodes::SqlLiteral
+ when String
+ locking = Arel.sql locking
+ end
+
@ast.lock = Nodes::Lock.new(locking)
self
end
View
6 lib/arel/visitors/mysql.rb
@@ -3,11 +3,7 @@ module Visitors
class MySQL < Arel::Visitors::ToSql
private
def visit_Arel_Nodes_Lock o
- if o.expr.is_a?(String)
- o.expr
- else
- "FOR UPDATE"
- end
+ visit o.expr
end
###
View
6 lib/arel/visitors/postgresql.rb
@@ -3,11 +3,7 @@ module Visitors
class PostgreSQL < Arel::Visitors::ToSql
private
def visit_Arel_Nodes_Lock o
- if o.expr.is_a?(String)
- o.expr
- else
- "FOR UPDATE"
- end
+ visit o.expr
end
def visit_Arel_Nodes_SelectStatement o
View
4 test/visitors/test_mysql.rb
@@ -31,12 +31,12 @@ module Visitors
describe 'locking' do
it 'defaults to FOR UPDATE when locking' do
- node = Nodes::Lock.new true
+ node = Nodes::Lock.new(Arel.sql('FOR UPDATE'))
@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')
+ node = Nodes::Lock.new(Arel.sql('LOCK IN SHARE MODE'))
@visitor.accept(node).must_be_like "LOCK IN SHARE MODE"
end
end
View
4 test/visitors/test_postgres.rb
@@ -9,13 +9,13 @@ module Visitors
describe 'locking' do
it 'defaults to FOR UPDATE' do
- @visitor.accept(Nodes::Lock.new(true)).must_be_like %{
+ @visitor.accept(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('FOR SHARE')
+ node = Nodes::Lock.new(Arel.sql('FOR SHARE'))
@visitor.accept(node).must_be_like %{
FOR SHARE
}

0 comments on commit 7601b90

Please sign in to comment.