From 2e6fbf4f75c8e33380623b52e86ca8b90037712f Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 18:47:45 -0400 Subject: Extracting #build_query method for creating SQL from parts --- lib/arel/engines/sql/relations/relation.rb | 8 +++--- .../engines/sql/relations/utilities/compound.rb | 4 +++ lib/arel/engines/sql/relations/writes.rb | 29 ++++++++++++---------- 3 files changed, 25 insertions(+), 16 deletions(-) (limited to 'lib/arel/engines/sql') diff --git a/lib/arel/engines/sql/relations/relation.rb b/lib/arel/engines/sql/relations/relation.rb index c8a4d952fe..4cfb83a601 100644 --- a/lib/arel/engines/sql/relations/relation.rb +++ b/lib/arel/engines/sql/relations/relation.rb @@ -5,7 +5,7 @@ module Arel end def select_sql - [ + build_query \ "SELECT #{select_clauses.join(', ')}", "FROM #{table_sql(Sql::TableReference.new(self))}", (joins(self) unless joins(self).blank? ), @@ -14,10 +14,8 @@ module Arel ("ORDER BY #{order_clauses.join(', ')}" unless orders.blank? ), ("LIMIT #{taken}" unless taken.blank? ), ("OFFSET #{skipped}" unless skipped.blank? ) - ].compact.join("\n") end - def inclusion_predicate_sql "IN" end @@ -28,6 +26,10 @@ module Arel protected + def build_query(*parts) + parts.compact.join("\n") + end + def select_clauses attributes.collect { |a| a.to_sql(Sql::SelectClause.new(self)) } end diff --git a/lib/arel/engines/sql/relations/utilities/compound.rb b/lib/arel/engines/sql/relations/utilities/compound.rb index b63a829c67..f8ce4033fd 100644 --- a/lib/arel/engines/sql/relations/utilities/compound.rb +++ b/lib/arel/engines/sql/relations/utilities/compound.rb @@ -1,6 +1,10 @@ module Arel class Compound < Relation delegate :table, :table_sql, :to => :relation + + def build_query(*parts) + parts.compact.join("\n") + end end end diff --git a/lib/arel/engines/sql/relations/writes.rb b/lib/arel/engines/sql/relations/writes.rb index 4d753f5fca..f1a9bfd2ac 100644 --- a/lib/arel/engines/sql/relations/writes.rb +++ b/lib/arel/engines/sql/relations/writes.rb @@ -1,36 +1,39 @@ module Arel class Deletion < Compound def to_sql(formatter = nil) - [ + build_query \ "DELETE", "FROM #{table_sql}", - ("WHERE #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank? ), - ("LIMIT #{taken}" unless taken.blank? ), - ].compact.join("\n") + ("WHERE #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank? ), + ("LIMIT #{taken}" unless taken.blank? ) end end class Insert < Compound def to_sql(formatter = nil) - [ + build_query \ "INSERT", "INTO #{table_sql}", "(#{record.keys.collect { |key| engine.quote_column_name(key.name) }.join(', ')})", "VALUES (#{record.collect { |key, value| key.format(value) }.join(', ')})" - ].join("\n") end end class Update < Compound def to_sql(formatter = nil) - [ + build_query \ "UPDATE #{table_sql} SET", - assignments.collect do |attribute, value| - "#{engine.quote_column_name(attribute.name)} = #{attribute.format(value)}" - end.join(",\n"), - ("WHERE #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank? ), - ("LIMIT #{taken}" unless taken.blank? ) - ].join("\n") + assignment_sql, + ("WHERE #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank? ), + ("LIMIT #{taken}" unless taken.blank? ) + end + + protected + + def assignment_sql + assignments.collect do |attribute, value| + "#{engine.quote_column_name(attribute.name)} = #{attribute.format(value)}" + end.join(",\n") end end end -- cgit v1.2.3