diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-05-04 18:15:38 -0700 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-05-04 18:15:38 -0700 |
commit | 6b28dcfa7e03909b69b922e26eb6d3ad8eb3f061 (patch) | |
tree | b05e941417382e1851df6559f870ab48584e61a0 /lib/arel/sql/formatters.rb | |
parent | 8f87072b1fc2b92328aa45078e535e9575b0b6cf (diff) | |
download | rails-6b28dcfa7e03909b69b922e26eb6d3ad8eb3f061.tar.gz rails-6b28dcfa7e03909b69b922e26eb6d3ad8eb3f061.tar.bz2 rails-6b28dcfa7e03909b69b922e26eb6d3ad8eb3f061.zip |
cleanup
Diffstat (limited to 'lib/arel/sql/formatters.rb')
-rw-r--r-- | lib/arel/sql/formatters.rb | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/lib/arel/sql/formatters.rb b/lib/arel/sql/formatters.rb new file mode 100644 index 0000000000..96bab2495c --- /dev/null +++ b/lib/arel/sql/formatters.rb @@ -0,0 +1,102 @@ +module Arel + module Sql + class Formatter + attr_reader :environment + delegate :christener, :engine, :to => :environment + delegate :name_for, :to => :christener + delegate :quote_table_name, :quote_column_name, :quote, :to => :engine + + def initialize(environment) + @environment = environment + end + end + + class SelectClause < Formatter + def attribute(attribute) + "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" + (attribute.alias ? " AS #{quote(attribute.alias.to_s)}" : "") + end + + def expression(expression) + "#{expression.function_sql}(#{expression.attribute.to_sql(self)})" + (expression.alias ? " AS #{quote_column_name(expression.alias)}" : '') + end + + def select(select_sql, name) + "(#{select_sql}) AS #{quote_table_name(name.to_s)}" + end + + def value(value) + value + end + end + + class PassThrough < Formatter + def value(value) + value + end + end + + class WhereClause < PassThrough + end + + class OrderClause < PassThrough + def attribute(attribute) + "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" + end + end + + class WhereCondition < Formatter + def attribute(attribute) + "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" + end + + def expression(expression) + "#{expression.function_sql}(#{expression.attribute.to_sql(self)})" + end + + def value(value) + value.to_sql(self) + end + + def scalar(value, column = nil) + quote(value, column) + end + + def select(select_sql, name) + "(#{select_sql})" + end + end + + class SelectStatement < Formatter + def select(select_sql, name) + select_sql + end + end + + class TableReference < Formatter + def select(select_sql, name) + "(#{select_sql}) AS #{quote_table_name(name)}" + end + + def table(table) + quote_table_name(table.name) + (table.name != name_for(table) ? " AS " + engine.quote_table_name(name_for(table)) : '') + end + end + + class Attribute < WhereCondition + def scalar(scalar) + quote(scalar, environment.column) + end + + def array(array) + "(" + array.collect { |e| e.to_sql(self) }.join(', ') + ")" + end + + def range(left, right) + "#{left} AND #{right}" + end + end + + class Value < WhereCondition + end + end +end
\ No newline at end of file |