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/arel/sql/formatters.rb | |
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/arel/sql/formatters.rb')
-rw-r--r-- | lib/arel/sql/formatters.rb | 14 |
1 files changed, 11 insertions, 3 deletions
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 |