diff options
author | Bryan Helmkamp <bryan@brynary.com> | 2009-05-17 18:40:24 -0400 |
---|---|---|
committer | Bryan Helmkamp <bryan@brynary.com> | 2009-05-17 18:40:24 -0400 |
commit | 982f100d49095614df320d449f66f69aa827a763 (patch) | |
tree | 5c55391123147dc161b12f2f4033ab4c2d546ac2 /lib | |
parent | 9560650fa79212b33d6ab349ff83b4f617d395b7 (diff) | |
download | rails-982f100d49095614df320d449f66f69aa827a763.tar.gz rails-982f100d49095614df320d449f66f69aa827a763.tar.bz2 rails-982f100d49095614df320d449f66f69aa827a763.zip |
Refactor #select_sql. Extract methods to generate clauses
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/engines/sql/relations/relation.rb | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/lib/arel/engines/sql/relations/relation.rb b/lib/arel/engines/sql/relations/relation.rb index 93c146352c..c8a4d952fe 100644 --- a/lib/arel/engines/sql/relations/relation.rb +++ b/lib/arel/engines/sql/relations/relation.rb @@ -6,17 +6,18 @@ module Arel def select_sql [ - "SELECT #{attributes.collect { |a| a.to_sql(Sql::SelectClause.new(self)) }.join(', ')}", + "SELECT #{select_clauses.join(', ')}", "FROM #{table_sql(Sql::TableReference.new(self))}", - (joins(self) unless joins(self).blank? ), - ("WHERE #{wheres .collect { |w| w.to_sql(Sql::WhereClause.new(self)) }.join("\n\tAND ")}" unless wheres.blank? ), - ("GROUP BY #{groupings.collect { |g| g.to_sql(Sql::GroupClause.new(self)) }.join(', ')}" unless groupings.blank? ), - ("ORDER BY #{orders .collect { |o| o.to_sql(Sql::OrderClause.new(self)) }.join(', ')}" unless orders.blank? ), - ("LIMIT #{taken}" unless taken.blank? ), - ("OFFSET #{skipped}" unless skipped.blank? ) + (joins(self) unless joins(self).blank? ), + ("WHERE #{where_clauses.join("\n\tAND ")}" unless wheres.blank? ), + ("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? ) ].compact.join("\n") end + def inclusion_predicate_sql "IN" end @@ -24,5 +25,24 @@ module Arel def christener @christener ||= Sql::Christener.new end + + protected + + def select_clauses + attributes.collect { |a| a.to_sql(Sql::SelectClause.new(self)) } + end + + def where_clauses + wheres.collect { |w| w.to_sql(Sql::WhereClause.new(self)) } + end + + def group_clauses + groupings.collect { |g| g.to_sql(Sql::GroupClause.new(self)) } + end + + def order_clauses + orders.collect { |o| o.to_sql(Sql::OrderClause.new(self)) } + end + end end |