diff options
author | Bryan Helmkamp <bryan@brynary.com> | 2009-05-17 18:47:45 -0400 |
---|---|---|
committer | Bryan Helmkamp <bryan@brynary.com> | 2009-05-17 18:47:45 -0400 |
commit | 2e6fbf4f75c8e33380623b52e86ca8b90037712f (patch) | |
tree | 78d222d35a4025e4be86240fb7c5267e7abfe38f /lib | |
parent | 982f100d49095614df320d449f66f69aa827a763 (diff) | |
download | rails-2e6fbf4f75c8e33380623b52e86ca8b90037712f.tar.gz rails-2e6fbf4f75c8e33380623b52e86ca8b90037712f.tar.bz2 rails-2e6fbf4f75c8e33380623b52e86ca8b90037712f.zip |
Extracting #build_query method for creating SQL from parts
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/engines/sql/relations/relation.rb | 8 | ||||
-rw-r--r-- | lib/arel/engines/sql/relations/utilities/compound.rb | 4 | ||||
-rw-r--r-- | lib/arel/engines/sql/relations/writes.rb | 29 |
3 files changed, 25 insertions, 16 deletions
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 |