diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-03-13 22:39:51 -0700 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-03-13 22:39:51 -0700 |
commit | c823c2c2cc3e5d0a709d67e585e73fde0f11512f (patch) | |
tree | fc16865073d5d4ff32c909962b11f6c3204f9e9f | |
parent | b1acebaaf0823c093853ade5700bbf5117b4f31a (diff) | |
download | rails-c823c2c2cc3e5d0a709d67e585e73fde0f11512f.tar.gz rails-c823c2c2cc3e5d0a709d67e585e73fde0f11512f.tar.bz2 rails-c823c2c2cc3e5d0a709d67e585e73fde0f11512f.zip |
renamed sql formatting strategies to correspond with sql grammar rule names in the mysql bnf
-rw-r--r-- | lib/active_relation/primitives/attribute.rb | 2 | ||||
-rw-r--r-- | lib/active_relation/primitives/value.rb | 2 | ||||
-rw-r--r-- | lib/active_relation/relations/join.rb | 2 | ||||
-rw-r--r-- | lib/active_relation/relations/relation.rb | 18 | ||||
-rw-r--r-- | lib/active_relation/sql.rb | 27 |
5 files changed, 25 insertions, 26 deletions
diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb index bbea7b4554..0fed676727 100644 --- a/lib/active_relation/primitives/attribute.rb +++ b/lib/active_relation/primitives/attribute.rb @@ -111,7 +111,7 @@ module ActiveRelation end include Expressions - def to_sql(strategy = Sql::Predicate.new(engine)) + def to_sql(strategy = Sql::WhereCondition.new(engine)) strategy.attribute prefix, name, self.alias end diff --git a/lib/active_relation/primitives/value.rb b/lib/active_relation/primitives/value.rb index ce9497cf34..096c876ecd 100644 --- a/lib/active_relation/primitives/value.rb +++ b/lib/active_relation/primitives/value.rb @@ -6,7 +6,7 @@ module ActiveRelation @value, @relation = value, relation end - def to_sql(strategy = Sql::Predicate.new(relation.engine)) + def to_sql(strategy = Sql::WhereCondition.new(relation.engine)) strategy.value value end diff --git a/lib/active_relation/relations/join.rb b/lib/active_relation/relations/join.rb index 043b237de7..4a57795280 100644 --- a/lib/active_relation/relations/join.rb +++ b/lib/active_relation/relations/join.rb @@ -61,7 +61,7 @@ module ActiveRelation delegate :engine, :to => :relation def table_sql - relation.aggregation?? relation.to_sql(Sql::Aggregation.new(engine)) : relation.send(:table_sql) + relation.aggregation?? relation.to_sql(Sql::TableReference.new(engine)) : relation.send(:table_sql) end def selects diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index ee2e00aa21..9847cf2b7c 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -102,16 +102,16 @@ module ActiveRelation false end - def to_sql(strategy = Sql::Relation.new(engine)) + def to_sql(strategy = Sql::SelectStatement.new(engine)) strategy.select [ - "SELECT #{attributes.collect{ |a| a.to_sql(Sql::Projection.new(engine)) }.join(', ')}", + "SELECT #{attributes.collect{ |a| a.to_sql(Sql::SelectExpression.new(engine)) }.join(', ')}", "FROM #{table_sql}", - (joins unless joins.blank? ), - ("WHERE #{selects.collect{|s| s.to_sql(Sql::Selection.new(engine))}.join("\n\tAND ")}" unless selects.blank? ), - ("ORDER BY #{orders.collect(&:to_sql)}" unless orders.blank? ), - ("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank? ), - ("LIMIT #{limit}" unless limit.blank? ), - ("OFFSET #{offset}" unless offset.blank? ) + (joins unless joins.blank? ), + ("WHERE #{selects.collect{|s| s.to_sql(Sql::WhereClause.new(engine))}.join("\n\tAND ")}" unless selects.blank? ), + ("ORDER BY #{orders.collect(&:to_sql)}" unless orders.blank? ), + ("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank? ), + ("LIMIT #{limit}" unless limit.blank? ), + ("OFFSET #{offset}" unless offset.blank? ) ].compact.join("\n"), self.alias end alias_method :to_s, :to_sql @@ -133,7 +133,7 @@ module ActiveRelation end def format(object) - object.to_sql(Sql::Predicate.new(engine)) + object.to_sql(Sql::WhereCondition.new(engine)) end def attributes; [] end diff --git a/lib/active_relation/sql.rb b/lib/active_relation/sql.rb index fb2177a55b..b38e6dc392 100644 --- a/lib/active_relation/sql.rb +++ b/lib/active_relation/sql.rb @@ -4,7 +4,6 @@ module ActiveRelation delegate :quote_table_name, :quote_column_name, :quote, :to => :engine end - # unit test me!!! class Formatter attr_reader :engine include Quoting @@ -14,7 +13,7 @@ module ActiveRelation end end - class Projection < Formatter + class SelectExpression < 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 +23,13 @@ module ActiveRelation end end - class Predicate < Formatter + class WhereClause < Formatter + def value(value) + value + end + end + + class WhereCondition < Formatter def attribute(relation_name, attribute_name, aliaz) "#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}" end @@ -38,25 +43,19 @@ module ActiveRelation end end - class Selection < Formatter - def value(value) - value - end - end - - class Relation < Formatter + class SelectStatement < Formatter def select(select_sql, aliaz) select_sql end end - class Aggregation < Formatter + class TableReference < Formatter def select(select_sql, aliaz) - "(#{select_sql}) AS #{engine.quote_table_name(aliaz)}" + "(#{select_sql}) AS #{quote_table_name(aliaz)}" end end - class Attribute < Predicate + class Attribute < WhereCondition def initialize(attribute) @attribute, @engine = attribute, attribute.engine end @@ -66,7 +65,7 @@ module ActiveRelation end end - class Value < Predicate + class Value < WhereCondition end end end
\ No newline at end of file |