diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-03-11 23:39:40 -0700 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-03-11 23:39:40 -0700 |
commit | 8a62a733c107a33edee8cabc908c3ebc3b280e8b (patch) | |
tree | 30d088f5ef2798ee164fb6fe4ea253cef20b3f19 | |
parent | 12ef6a5ad15078d2f634d3c6cdfc453848f90122 (diff) | |
download | rails-8a62a733c107a33edee8cabc908c3ebc3b280e8b.tar.gz rails-8a62a733c107a33edee8cabc908c3ebc3b280e8b.tar.bz2 rails-8a62a733c107a33edee8cabc908c3ebc3b280e8b.zip |
renamed strategy method to format
- strategy (the method on scalar and attribute) is a complex double-dispatching
scheme to format (to_sql) a scalar in the light of the particular attribute;
that is, it casts strings to integers if the column is int, etc.
-rw-r--r-- | TODO | 3 | ||||
-rw-r--r-- | lib/active_relation/predicates.rb | 2 | ||||
-rw-r--r-- | lib/active_relation/primitives/attribute.rb | 8 | ||||
-rw-r--r-- | lib/active_relation/primitives/scalar.rb | 6 | ||||
-rw-r--r-- | lib/active_relation/relations/relation.rb | 4 | ||||
-rw-r--r-- | lib/active_relation/sql.rb | 14 |
6 files changed, 21 insertions, 16 deletions
@@ -1,6 +1,5 @@ todo: - clarify distinction between engine and connection: an engine is a connection pool, plus the quoting operations -- #relation is now on scalar, attribute and relation; you must admit it's name is confusing given that e.g., relation already has a strategy (Sql::Relation) ... should it be called predicate strategy? operand1.to_sql(operand2.predicate) maybe prefer operand1.cast(operand2) or project or in light of - clean up integration tests in join - i find the aggregate stuff too integrationy; unit testing would be better - test relation, table reset - cache expiry on write @@ -46,3 +45,5 @@ done: - hash custom matcher - make session engine stuff follow laws of demeter - currently doing some odd method chaining? rethink who is responsible for what - session just calls execute, passing in a connection; by default it gets a connection from the relation. +- #strategy is now on scalar, attribute and relation; you must admit it's name is confusing given that e.g., relation already has a strategy (Sql::Relation) ... should it be called predicate strategy? operand1.to_sql(operand2.predicate) maybe prefer operand1.cast(operand2) or project or in light of + - renamed to #format: operand1.format(operand2) diff --git a/lib/active_relation/predicates.rb b/lib/active_relation/predicates.rb index ba926a86e5..12ddd1b48d 100644 --- a/lib/active_relation/predicates.rb +++ b/lib/active_relation/predicates.rb @@ -25,7 +25,7 @@ module ActiveRelation end def to_sql(strategy = nil) - "#{operand1.to_sql(operand2.strategy)} #{predicate_sql} #{operand2.to_sql(operand1.strategy)}" + "#{operand2.format(operand1)} #{predicate_sql} #{operand1.format(operand2)}" end def descend diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb index 20677f824b..bbea7b4554 100644 --- a/lib/active_relation/primitives/attribute.rb +++ b/lib/active_relation/primitives/attribute.rb @@ -111,15 +111,19 @@ module ActiveRelation end include Expressions - def to_sql(strategy = self.strategy) + def to_sql(strategy = Sql::Predicate.new(engine)) strategy.attribute prefix, name, self.alias end + def format(object) + object.to_sql(strategy) + end + + private def strategy Sql::Attribute.new(self) end - private def prefix relation.prefix_for(self) end diff --git a/lib/active_relation/primitives/scalar.rb b/lib/active_relation/primitives/scalar.rb index fa88404ee3..d428541a50 100644 --- a/lib/active_relation/primitives/scalar.rb +++ b/lib/active_relation/primitives/scalar.rb @@ -6,12 +6,12 @@ module ActiveRelation @value, @relation = value, relation end - def to_sql(strategy = self.strategy) + def to_sql(strategy = Sql::Predicate.new(relation.engine)) strategy.scalar value end - def strategy - ActiveRelation::Sql::Scalar.new(relation.engine) + def format(object) + object.to_sql(Sql::Scalar.new(relation.engine)) end def ==(other) diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index 69935a7be0..ee2e00aa21 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -132,8 +132,8 @@ module ActiveRelation self end - def strategy - Sql::Predicate.new(engine) + def format(object) + object.to_sql(Sql::Predicate.new(engine)) end def attributes; [] end diff --git a/lib/active_relation/sql.rb b/lib/active_relation/sql.rb index 99cfc66383..ff00223ce7 100644 --- a/lib/active_relation/sql.rb +++ b/lib/active_relation/sql.rb @@ -4,8 +4,8 @@ module ActiveRelation delegate :quote_table_name, :quote_column_name, :quote, :to => :engine end - # module Formatting Context / Strategy # unit test me!!! - class Strategy + # unit test me!!! + class Formatter attr_reader :engine include Quoting @@ -14,7 +14,7 @@ module ActiveRelation end end - class Projection < Strategy + class Projection < Formatter def attribute(relation_name, attribute_name, aliaz) "#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}" + (aliaz ? " AS #{quote(aliaz.to_s)}" : "") end @@ -24,7 +24,7 @@ module ActiveRelation end end - class Predicate < Strategy + class Predicate < Formatter def attribute(relation_name, attribute_name, aliaz) "#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}" end @@ -38,19 +38,19 @@ module ActiveRelation end end - class Selection < Strategy + class Selection < Formatter def scalar(scalar) scalar end end - class Relation < Strategy + class Relation < Formatter def select(select_sql, aliaz) select_sql end end - class Aggregation < Strategy + class Aggregation < Formatter def select(select_sql, aliaz) "(#{select_sql}) AS #{engine.quote_table_name(aliaz)}" end |