diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-02-24 22:19:32 -0800 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-02-24 22:19:32 -0800 |
commit | 92db013ba3ee4d0a9d92281e614d05f064c22e15 (patch) | |
tree | 23585e771ee1a2b1682f66a67d6b6891fab49f24 /lib | |
parent | ecd072d21951573f59e9515b868224d3732dbdfa (diff) | |
download | rails-92db013ba3ee4d0a9d92281e614d05f064c22e15.tar.gz rails-92db013ba3ee4d0a9d92281e614d05f064c22e15.tar.bz2 rails-92db013ba3ee4d0a9d92281e614d05f064c22e15.zip |
quoting issues
Diffstat (limited to 'lib')
-rw-r--r-- | lib/active_relation/extensions.rb | 1 | ||||
-rw-r--r-- | lib/active_relation/extensions/base.rb | 48 | ||||
-rw-r--r-- | lib/active_relation/extensions/object.rb | 6 | ||||
-rw-r--r-- | lib/active_relation/predicates.rb | 5 | ||||
-rw-r--r-- | lib/active_relation/primitives/attribute.rb | 18 | ||||
-rw-r--r-- | lib/active_relation/primitives/expression.rb | 2 | ||||
-rw-r--r-- | lib/active_relation/relations/compound.rb | 2 | ||||
-rw-r--r-- | lib/active_relation/relations/join.rb | 2 | ||||
-rw-r--r-- | lib/active_relation/relations/relation.rb | 4 | ||||
-rw-r--r-- | lib/active_relation/relations/table.rb | 17 | ||||
-rw-r--r-- | lib/active_relation/sql.rb | 23 |
11 files changed, 58 insertions, 70 deletions
diff --git a/lib/active_relation/extensions.rb b/lib/active_relation/extensions.rb index 3751088483..8a024947ed 100644 --- a/lib/active_relation/extensions.rb +++ b/lib/active_relation/extensions.rb @@ -1,4 +1,3 @@ require 'active_relation/extensions/object' require 'active_relation/extensions/array' -require 'active_relation/extensions/base' require 'active_relation/extensions/hash' diff --git a/lib/active_relation/extensions/base.rb b/lib/active_relation/extensions/base.rb deleted file mode 100644 index c1b823f9a7..0000000000 --- a/lib/active_relation/extensions/base.rb +++ /dev/null @@ -1,48 +0,0 @@ -class ActiveRecord::Base - class << self - def cache - @identity_map ||= IdentityMap.new - end - - def relation - @relation ||= ActiveRelation::Table.new(table_name) - end - end - - class IdentityMap - def initialize - @map = {} - end - - def get(record, &block) - @map[record] ||= yield - end - end -end - -# all of the below disables normal AR behavior. It's rather destructive and purely for demonstration purposes (see scratch_spec). -class ActiveRecord::Associations::BelongsToAssociation - def instantiate(record, joins = []) - @target = proxy_reflection.klass.instantiate(record, joins) - loaded - end - - # this basically disables belongs_to from loading themselves - def reload - @target = 'hack' - end -end - -class ActiveRecord::Associations::AssociationCollection - def instantiate(record, joins = []) - @target << proxy_reflection.klass.instantiate(record, joins) - loaded # technically, this isn't true. doesn't matter though - end -end - -class ActiveRecord::Associations::HasManyThroughAssociation - def instantiate(record, joins = []) - @target << proxy_reflection.klass.instantiate(record, joins) - loaded # again, not really true. - end -end
\ No newline at end of file diff --git a/lib/active_relation/extensions/object.rb b/lib/active_relation/extensions/object.rb index 35ffa9c661..0cc87bf262 100644 --- a/lib/active_relation/extensions/object.rb +++ b/lib/active_relation/extensions/object.rb @@ -7,7 +7,11 @@ class Object self end - def to_sql(strategy = ActiveRelation::Sql::Scalar.new) + def to_sql(strategy = self.strategy) strategy.scalar self end + + def strategy + ActiveRelation::Sql::Scalar.new + end end
\ No newline at end of file diff --git a/lib/active_relation/predicates.rb b/lib/active_relation/predicates.rb index 7d0618f42e..2a36e65042 100644 --- a/lib/active_relation/predicates.rb +++ b/lib/active_relation/predicates.rb @@ -6,7 +6,6 @@ module ActiveRelation end class Binary < Predicate - # rename "operand21", "operand22" attr_reader :operand1, :operand2 def initialize(operand1, operand2) @@ -25,8 +24,8 @@ module ActiveRelation descend(&:qualify) end - def to_sql(strategy = Sql::Predicate.new) - "#{operand1.to_sql(strategy)} #{predicate_sql} #{operand2.to_sql(strategy)}" + def to_sql(strategy = nil) + "#{operand1.to_sql(operand2.strategy)} #{predicate_sql} #{operand2.to_sql(operand1.strategy)}" end def descend diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb index e55dd0bdc4..75f19605c7 100644 --- a/lib/active_relation/primitives/attribute.rb +++ b/lib/active_relation/primitives/attribute.rb @@ -2,8 +2,8 @@ module ActiveRelation class Attribute attr_reader :relation, :name, :alias, :ancestor - def initialize(relation, name, aliaz = nil, ancestor = nil) - @relation, @name, @alias, @ancestor = relation, name, aliaz, ancestor + def initialize(relation, name, options = {}) + @relation, @name, @alias, @ancestor, @column = relation, name, options[:alias], options[:ancestor] end def alias_or_name @@ -12,11 +12,11 @@ module ActiveRelation module Transformations def as(aliaz = nil) - Attribute.new(relation, name, aliaz, self) + Attribute.new(relation, name, :alias => aliaz, :ancestor => self) end def bind(new_relation) - relation == new_relation ? self : Attribute.new(new_relation, name, @alias, self) + relation == new_relation ? self : Attribute.new(new_relation, name, :alias => @alias, :ancestor => self) end def qualify @@ -32,6 +32,10 @@ module ActiveRelation def qualified_name "#{prefix}.#{name}" end + + def column + relation.column_for(self) + end def ==(other) self.class == other.class and @@ -112,10 +116,14 @@ module ActiveRelation end include Expressions - def to_sql(strategy = Sql::Predicate.new) + def to_sql(strategy = self.strategy) strategy.attribute prefix, name, self.alias end + def strategy + Sql::Attribute.new(self) + end + private def prefix relation.prefix_for(self) diff --git a/lib/active_relation/primitives/expression.rb b/lib/active_relation/primitives/expression.rb index 35c1b5ff65..11aa558977 100644 --- a/lib/active_relation/primitives/expression.rb +++ b/lib/active_relation/primitives/expression.rb @@ -20,7 +20,7 @@ module ActiveRelation end def to_attribute - Attribute.new(relation, @alias, nil, self) + Attribute.new(relation, @alias, :ancestor => self) end end include Transformations diff --git a/lib/active_relation/relations/compound.rb b/lib/active_relation/relations/compound.rb index 094c6e29dd..115315a76a 100644 --- a/lib/active_relation/relations/compound.rb +++ b/lib/active_relation/relations/compound.rb @@ -2,7 +2,7 @@ module ActiveRelation class Compound < Relation attr_reader :relation delegate :joins, :selects, :orders, :groupings, :table_sql, :inserts, :limit, - :offset, :name, :alias, :aggregation?, :alias?, :prefix_for, + :offset, :name, :alias, :aggregation?, :alias?, :prefix_for, :column_for, :to => :relation def attributes diff --git a/lib/active_relation/relations/join.rb b/lib/active_relation/relations/join.rb index 70c85bb1e0..8e0dcb2a4b 100644 --- a/lib/active_relation/relations/join.rb +++ b/lib/active_relation/relations/join.rb @@ -38,7 +38,7 @@ module ActiveRelation join_sql, externalize(relation2).table_sql, "ON", - predicates.collect { |p| p.bind(self).to_sql(Sql::Predicate.new) }.join(' AND ') + predicates.collect { |p| p.bind(self).to_sql }.join(' AND ') ].join(" ") [relation1.joins, relation2.joins, this_join].compact.join(" ") end diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index 1d5e194923..7536390aca 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -104,12 +104,12 @@ module ActiveRelation false end - def to_sql(strategy = Sql::Select.new) + def to_sql(strategy = Sql::Relation.new) strategy.select [ "SELECT #{attributes.collect{ |a| a.to_sql(Sql::Projection.new) }.join(', ')}", "FROM #{table_sql}", (joins unless joins.blank?), - ("WHERE #{selects.collect{|s| s.to_sql(Sql::Predicate.new)}.join("\n\tAND ")}" unless selects.blank?), + ("WHERE #{selects.collect{|s| s.to_sql(Sql::Selection.new)}.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.to_sql}" unless limit.blank?), diff --git a/lib/active_relation/relations/table.rb b/lib/active_relation/relations/table.rb index d0fc2b2475..5720be30e3 100644 --- a/lib/active_relation/relations/table.rb +++ b/lib/active_relation/relations/table.rb @@ -3,11 +3,11 @@ module ActiveRelation attr_reader :name def initialize(name) - @name = name + @name = name.to_s end def attributes - @attributes ||= connection.columns(name, "#{name} Columns").collect do |column| + @attributes ||= columns.collect do |column| Attribute.new(self, column.name.to_sym) end end @@ -19,7 +19,20 @@ module ActiveRelation def prefix_for(attribute) self[attribute] and name end + + def column_for(attribute) + self[attribute] and columns.detect { |c| c.name == attribute.name } + end + + def ==(other) + self.class == other.class and + name == other.name + end + def columns + @columns ||= connection.columns(name, "#{name} Columns") + end + protected def table_sql "#{quote_table_name(name)}" diff --git a/lib/active_relation/sql.rb b/lib/active_relation/sql.rb index 85bcb4107b..49ced3cbaa 100644 --- a/lib/active_relation/sql.rb +++ b/lib/active_relation/sql.rb @@ -27,8 +27,8 @@ module ActiveRelation "#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}" end - def scalar(scalar) - scalar + def scalar(scalar, column = nil) + quote(scalar, column) end def select(select_sql, aliaz) @@ -36,7 +36,13 @@ module ActiveRelation end end - class Select < Strategy + class Selection < Strategy + def scalar(scalar) + scalar + end + end + + class Relation < Strategy def select(select_sql, aliaz) select_sql end @@ -48,10 +54,17 @@ module ActiveRelation end end - class Scalar < Strategy + class Attribute < Predicate + def initialize(attribute) + @attribute = attribute + end + def scalar(scalar) - quote(scalar) + quote(scalar, @attribute.column) end end + + class Scalar < Predicate + end end end
\ No newline at end of file |