diff options
author | Emilio Tagua <miloops@gmail.com> | 2009-04-29 19:38:39 -0300 |
---|---|---|
committer | Emilio Tagua <miloops@gmail.com> | 2009-04-29 19:38:39 -0300 |
commit | b4b73d9520a2ff27021b530ccd3dcd96973ce5fe (patch) | |
tree | 5a72648ea38f5ca0d739119d54617596d3e2b139 /lib | |
parent | 5e4bfb1ce4144785feaeffc6210740e3e4ce5770 (diff) | |
download | rails-b4b73d9520a2ff27021b530ccd3dcd96973ce5fe.tar.gz rails-b4b73d9520a2ff27021b530ccd3dcd96973ce5fe.tar.bz2 rails-b4b73d9520a2ff27021b530ccd3dcd96973ce5fe.zip |
Added DISTINCT support. Modified when to quote or not columns and tables.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/primitives/attribute.rb | 4 | ||||
-rw-r--r-- | lib/arel/primitives/value.rb | 6 | ||||
-rw-r--r-- | lib/arel/relations/relation.rb | 9 | ||||
-rw-r--r-- | lib/arel/sql/formatters.rb | 14 |
4 files changed, 26 insertions, 7 deletions
diff --git a/lib/arel/primitives/attribute.rb b/lib/arel/primitives/attribute.rb index 7021e9f9ed..6cb558d8ce 100644 --- a/lib/arel/primitives/attribute.rb +++ b/lib/arel/primitives/attribute.rb @@ -120,8 +120,8 @@ module Arel include Predications module Expressions - def count - Expression.new(self, "COUNT") + def count(distinct = false) + distinct ? Expression.new(self, "DISTINCT").count : Expression.new(self, "COUNT") end def sum diff --git a/lib/arel/primitives/value.rb b/lib/arel/primitives/value.rb index 36a7fb7c71..9c6e518a95 100644 --- a/lib/arel/primitives/value.rb +++ b/lib/arel/primitives/value.rb @@ -6,7 +6,11 @@ module Arel def to_sql(formatter = Sql::WhereCondition.new(relation)) - formatter.value value + if value =~ /^\(.*\)$/ + value + else + formatter.value value + end end def format(object) diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index 986bc3fbeb..50c46aa2ed 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -1,5 +1,7 @@ module Arel class Relation + attr_reader :count + def session Session.new end @@ -12,6 +14,11 @@ module Arel engine.select_values self.to_sql end + def count + @count = "COUNT(*) AS count_all" + engine.select_value self.to_sql + end + def to_sql(formatter = Sql::SelectStatement.new(self)) formatter.select select_sql, self end @@ -19,7 +26,7 @@ module Arel def select_sql [ - "SELECT #{attributes.collect { |a| a.to_sql(Sql::SelectClause.new(self)) }.join(', ')}", + "SELECT #{@count} #{attributes.collect { |a| a.to_sql(Sql::SelectClause.new(self)) }.join(', ') unless @count}", "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? ), diff --git a/lib/arel/sql/formatters.rb b/lib/arel/sql/formatters.rb index f105fbea72..aa10952d04 100644 --- a/lib/arel/sql/formatters.rb +++ b/lib/arel/sql/formatters.rb @@ -15,14 +15,18 @@ module Arel def attribute(attribute) # FIXME this should check that the column exists if attribute.name.to_s =~ /^\w*$/ - "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" + (attribute.alias ? " AS #{quote(attribute.alias.to_s)}" : "") + "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" + (attribute.alias ? " AS #{quote(attribute.alias.to_s)}" : "") else attribute.name.to_s + (attribute.alias ? " AS #{quote(attribute.alias.to_s)}" : "") end end def expression(expression) - "#{expression.function_sql}(#{expression.attribute.to_sql(self)})" + (expression.alias ? " AS #{quote_column_name(expression.alias)}" : '') + if expression.function_sql == "DISTINCT" + "#{expression.function_sql} #{expression.attribute.to_sql(self)}" + (expression.alias ? " AS #{quote_column_name(expression.alias)}" : '') + else + "#{expression.function_sql}(#{expression.attribute.to_sql(self)})" + (expression.alias ? " AS #{quote_column_name(expression.alias)}" : '') + end end def select(select_sql, table) @@ -89,7 +93,11 @@ module Arel end def table(table) - quote_table_name(table.name) + (table.name != name_for(table) ? " AS " + quote_table_name(name_for(table)) : '') + if table.name =~ /^(\w|-)*$/ + quote_table_name(table.name) + (table.name != name_for(table) ? " AS " + quote_table_name(name_for(table)) : '') + else + table.name + (table.name != name_for(table) ? " AS " + (name_for(table)) : '') + end end end |