diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel.rb | 1 | ||||
-rw-r--r-- | lib/arel/algebra/attributes/attribute.rb | 12 | ||||
-rw-r--r-- | lib/arel/algebra/expression.rb | 33 | ||||
-rw-r--r-- | lib/arel/algebra/ordering.rb | 13 | ||||
-rw-r--r-- | lib/arel/algebra/value.rb | 24 | ||||
-rw-r--r-- | lib/arel/engines/memory/primitives.rb | 14 | ||||
-rw-r--r-- | lib/arel/engines/sql.rb | 1 | ||||
-rw-r--r-- | lib/arel/engines/sql/primitives.rb | 97 | ||||
-rw-r--r-- | lib/arel/sql_literal.rb | 13 |
9 files changed, 90 insertions, 118 deletions
diff --git a/lib/arel.rb b/lib/arel.rb index ae4240bf8a..30b3faadf9 100644 --- a/lib/arel.rb +++ b/lib/arel.rb @@ -5,6 +5,7 @@ require 'active_support/core_ext/object/blank' module Arel require 'arel/algebra' + require 'arel/sql_literal' require 'arel/engines' require 'arel/version' diff --git a/lib/arel/algebra/attributes/attribute.rb b/lib/arel/algebra/attributes/attribute.rb index 9206b228c8..8a7993f284 100644 --- a/lib/arel/algebra/attributes/attribute.rb +++ b/lib/arel/algebra/attributes/attribute.rb @@ -270,5 +270,17 @@ module Arel end end include Types + + def column + original_relation.column_for(self) + end + + def format(object) + object.to_sql(Sql::Attribute.new(self)) + end + + def to_sql(formatter = Sql::WhereCondition.new(relation)) + formatter.attribute self + end end end diff --git a/lib/arel/algebra/expression.rb b/lib/arel/algebra/expression.rb index 183de3c005..5a93ca9735 100644 --- a/lib/arel/algebra/expression.rb +++ b/lib/arel/algebra/expression.rb @@ -16,6 +16,10 @@ module Arel true end + def to_sql(formatter = Sql::SelectClause.new(relation)) + formatter.expression self + end + module Transformations def as(aliaz) self.class.new(attribute, aliaz, self) @@ -32,11 +36,28 @@ module Arel include Transformations end - class Count < Expression; end - class Distinct < Expression; end - class Sum < Expression; end - class Maximum < Expression; end - class Minimum < Expression; end - class Average < Expression; end + class Count < Expression + def function_sql; 'COUNT' end + end + + class Distinct < Expression + def function_sql; 'DISTINCT' end + end + + class Sum < Expression + def function_sql; 'SUM' end + end + + class Maximum < Expression + def function_sql; 'MAX' end + end + + class Minimum < Expression + def function_sql; 'MIN' end + end + + class Average < Expression + def function_sql; 'AVG' end + end end diff --git a/lib/arel/algebra/ordering.rb b/lib/arel/algebra/ordering.rb index 984327aaf7..8ed3e1e6e6 100644 --- a/lib/arel/algebra/ordering.rb +++ b/lib/arel/algebra/ordering.rb @@ -13,10 +13,23 @@ module Arel def == other super || (self.class === other && attribute == other.attribute) end + + def eval(row1, row2) + (attribute.eval(row1) <=> attribute.eval(row2)) * direction + end + + def to_sql(formatter = Sql::OrderClause.new(relation)) + formatter.ordering self + end end class Ascending < Ordering + def direction; 1 end + def direction_sql; 'ASC' end end + class Descending < Ordering + def direction_sql; 'DESC' end + def direction; -1 end end end diff --git a/lib/arel/algebra/value.rb b/lib/arel/algebra/value.rb index d9fc2e061c..90787fa5ea 100644 --- a/lib/arel/algebra/value.rb +++ b/lib/arel/algebra/value.rb @@ -21,5 +21,29 @@ module Arel def to_ordering self end + + def inclusion_predicate_sql + value.inclusion_predicate_sql + end + + def exclusion_predicate_sql + value.exclusion_predicate_sql + end + + def equality_predicate_sql + value.equality_predicate_sql + end + + def inequality_predicate_sql + value.inequality_predicate_sql + end + + def to_sql(formatter = Sql::WhereCondition.new(relation)) + formatter.value value + end + + def format(object) + object.to_sql(Sql::Value.new(relation)) + end end end diff --git a/lib/arel/engines/memory/primitives.rb b/lib/arel/engines/memory/primitives.rb index 935b34f5ee..0c9471fb49 100644 --- a/lib/arel/engines/memory/primitives.rb +++ b/lib/arel/engines/memory/primitives.rb @@ -10,18 +10,4 @@ module Arel value end end - - class Ordering - def eval(row1, row2) - (attribute.eval(row1) <=> attribute.eval(row2)) * direction - end - end - - class Descending < Ordering - def direction; -1 end - end - - class Ascending < Ordering - def direction; 1 end - end end diff --git a/lib/arel/engines/sql.rb b/lib/arel/engines/sql.rb index 686078826c..438d028606 100644 --- a/lib/arel/engines/sql.rb +++ b/lib/arel/engines/sql.rb @@ -1,7 +1,6 @@ require 'arel/engines/sql/attributes' require 'arel/engines/sql/engine' require 'arel/engines/sql/relations' -require 'arel/engines/sql/primitives' require 'arel/engines/sql/formatters' require 'arel/engines/sql/core_extensions' require 'arel/engines/sql/christener' diff --git a/lib/arel/engines/sql/primitives.rb b/lib/arel/engines/sql/primitives.rb deleted file mode 100644 index 1b215d2b54..0000000000 --- a/lib/arel/engines/sql/primitives.rb +++ /dev/null @@ -1,97 +0,0 @@ -module Arel - class SqlLiteral < String - def relation - nil - end - - def to_sql(formatter = nil) - self - end - - include Attribute::Expressions - end - - class Attribute - def column - original_relation.column_for(self) - end - - def format(object) - object.to_sql(Sql::Attribute.new(self)) - end - - def to_sql(formatter = Sql::WhereCondition.new(relation)) - formatter.attribute self - end - end - - class Value - def inclusion_predicate_sql - value.inclusion_predicate_sql - end - - def exclusion_predicate_sql - value.exclusion_predicate_sql - end - - def equality_predicate_sql - value.equality_predicate_sql - end - - def inequality_predicate_sql - value.inequality_predicate_sql - end - - def to_sql(formatter = Sql::WhereCondition.new(relation)) - formatter.value value - end - - def format(object) - object.to_sql(Sql::Value.new(relation)) - end - end - - class Ordering - def to_sql(formatter = Sql::OrderClause.new(relation)) - formatter.ordering self - end - end - - class Ascending < Ordering - def direction_sql; 'ASC' end - end - - class Descending < Ordering - def direction_sql; 'DESC' end - end - - class Expression < Attribute - def to_sql(formatter = Sql::SelectClause.new(relation)) - formatter.expression self - end - end - - class Count < Expression - def function_sql; 'COUNT' end - end - - class Distinct < Expression - def function_sql; 'DISTINCT' end - end - - class Sum < Expression - def function_sql; 'SUM' end - end - - class Maximum < Expression - def function_sql; 'MAX' end - end - - class Minimum < Expression - def function_sql; 'MIN' end - end - - class Average < Expression - def function_sql; 'AVG' end - end -end diff --git a/lib/arel/sql_literal.rb b/lib/arel/sql_literal.rb new file mode 100644 index 0000000000..a98a9a058c --- /dev/null +++ b/lib/arel/sql_literal.rb @@ -0,0 +1,13 @@ +module Arel + class SqlLiteral < String + def relation + nil + end + + def to_sql(formatter = nil) + self + end + + include Attribute::Expressions + end +end |