From b339caca2f3c7306c3944c5fc5d8dde17ae2deb8 Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Sun, 27 Dec 2009 18:04:35 -0300 Subject: Added lock to Arel, allowing a locking read if required. --- lib/arel/algebra/relations.rb | 1 + lib/arel/algebra/relations/operations/lock.rb | 12 ++++++++++++ lib/arel/algebra/relations/relation.rb | 5 +++++ lib/arel/algebra/relations/utilities/compound.rb | 2 +- lib/arel/engines/sql/relations/relation.rb | 6 ++++-- 5 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 lib/arel/algebra/relations/operations/lock.rb (limited to 'lib') diff --git a/lib/arel/algebra/relations.rb b/lib/arel/algebra/relations.rb index cee308299b..951b69d970 100644 --- a/lib/arel/algebra/relations.rb +++ b/lib/arel/algebra/relations.rb @@ -13,3 +13,4 @@ require 'arel/algebra/relations/operations/project' require 'arel/algebra/relations/operations/where' require 'arel/algebra/relations/operations/skip' require 'arel/algebra/relations/operations/take' +require 'arel/algebra/relations/operations/lock' diff --git a/lib/arel/algebra/relations/operations/lock.rb b/lib/arel/algebra/relations/operations/lock.rb new file mode 100644 index 0000000000..0d6c12e65b --- /dev/null +++ b/lib/arel/algebra/relations/operations/lock.rb @@ -0,0 +1,12 @@ +module Arel + class Lock < Compound + attributes :relation, :locked + deriving :initialize, :== + + def initialize(relation, locked, &block) + @relation = relation + @locked = locked.blank? ? " FOR UPDATE" : locked + end + end +end + diff --git a/lib/arel/algebra/relations/relation.rb b/lib/arel/algebra/relations/relation.rb index 2ce3fdcce8..b24e7c24d0 100644 --- a/lib/arel/algebra/relations/relation.rb +++ b/lib/arel/algebra/relations/relation.rb @@ -51,6 +51,10 @@ module Arel OPERATION end + def lock(locking = nil) + Lock.new(self, locking) + end + def alias Alias.new(self) end @@ -131,6 +135,7 @@ module Arel def taken; nil end def skipped; nil end def sources; [] end + def locked; [] end end include DefaultOperations end diff --git a/lib/arel/algebra/relations/utilities/compound.rb b/lib/arel/algebra/relations/utilities/compound.rb index 06bfce4ac0..9967472d88 100644 --- a/lib/arel/algebra/relations/utilities/compound.rb +++ b/lib/arel/algebra/relations/utilities/compound.rb @@ -2,7 +2,7 @@ module Arel class Compound < Relation attr_reader :relation delegate :joins, :join?, :inserts, :taken, :skipped, :name, :externalizable?, - :column_for, :engine, :sources, + :column_for, :engine, :sources, :locked, :to => :relation [:attributes, :wheres, :groupings, :orders].each do |operation_name| diff --git a/lib/arel/engines/sql/relations/relation.rb b/lib/arel/engines/sql/relations/relation.rb index 448cf7951d..8b71aa8c04 100644 --- a/lib/arel/engines/sql/relations/relation.rb +++ b/lib/arel/engines/sql/relations/relation.rb @@ -16,7 +16,8 @@ module Arel "FROM #{from_clauses}", (joins(self) unless joins(self).blank? ), ("WHERE #{where_clauses.join("\n\tAND ")}" unless wheres.blank? ), - ("GROUP BY #{group_clauses.join(', ')}" unless groupings.blank? ) + ("GROUP BY #{group_clauses.join(', ')}" unless groupings.blank? ), + ("#{locked}" unless locked.blank? ) build_query \ "SELECT * FROM (#{query}) AS id_list", @@ -33,7 +34,8 @@ module Arel ("GROUP BY #{group_clauses.join(', ')}" unless groupings.blank? ), ("ORDER BY #{order_clauses.join(', ')}" unless orders.blank? ), ("LIMIT #{taken}" unless taken.blank? ), - ("OFFSET #{skipped}" unless skipped.blank? ) + ("OFFSET #{skipped}" unless skipped.blank? ), + ("#{locked}" unless locked.blank? ) end end -- cgit v1.2.3