diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-03-16 15:40:15 -0700 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-03-16 15:40:15 -0700 |
commit | 1a6a3a1c6aa2f75333edef9100951407c4f76f1f (patch) | |
tree | 9e7e524c63e8eeabbdd6ae74ed83cc3469bfca93 /lib/active_relation | |
parent | 7f01134d1bdca70dcc9b16cf433894e7c8236815 (diff) | |
download | rails-1a6a3a1c6aa2f75333edef9100951407c4f76f1f.tar.gz rails-1a6a3a1c6aa2f75333edef9100951407c4f76f1f.tar.bz2 rails-1a6a3a1c6aa2f75333edef9100951407c4f76f1f.zip |
properly quoting array values
Diffstat (limited to 'lib/active_relation')
-rw-r--r-- | lib/active_relation/extensions/array.rb | 4 | ||||
-rw-r--r-- | lib/active_relation/extensions/hash.rb | 8 | ||||
-rw-r--r-- | lib/active_relation/extensions/object.rb | 4 | ||||
-rw-r--r-- | lib/active_relation/primitives/attribute.rb | 4 | ||||
-rw-r--r-- | lib/active_relation/relations/aggregation.rb | 2 | ||||
-rw-r--r-- | lib/active_relation/relations/compound.rb | 9 | ||||
-rw-r--r-- | lib/active_relation/relations/insertion.rb | 4 | ||||
-rw-r--r-- | lib/active_relation/relations/join.rb | 5 | ||||
-rw-r--r-- | lib/active_relation/relations/relation.rb | 8 | ||||
-rw-r--r-- | lib/active_relation/relations/writing.rb | 1 | ||||
-rw-r--r-- | lib/active_relation/sql.rb | 12 |
11 files changed, 39 insertions, 22 deletions
diff --git a/lib/active_relation/extensions/array.rb b/lib/active_relation/extensions/array.rb index 4bd20d8121..3b4246fb1c 100644 --- a/lib/active_relation/extensions/array.rb +++ b/lib/active_relation/extensions/array.rb @@ -3,7 +3,7 @@ class Array Hash[*flatten] end - def to_sql(strategy = nil) - "(#{collect(&:to_sql).join(', ')})" + def to_sql(strategy = Sql::SelectExpression.new) + strategy.array self end end
\ No newline at end of file diff --git a/lib/active_relation/extensions/hash.rb b/lib/active_relation/extensions/hash.rb index a33ace5738..ae3e48146f 100644 --- a/lib/active_relation/extensions/hash.rb +++ b/lib/active_relation/extensions/hash.rb @@ -8,4 +8,12 @@ class Hash descendent.merge(yield(key) => yield(value)) end end + + def to_sql(strategy = nil) + '(' + + inject([]) do |values, (key, value)| + values << key.format(value) + end.join(', ') + + ')' + 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 90eb73f0b0..2682ed99fe 100644 --- a/lib/active_relation/extensions/object.rb +++ b/lib/active_relation/extensions/object.rb @@ -3,6 +3,10 @@ class Object ActiveRelation::Value.new(self, relation) end + def to_sql(strategy = nil) + strategy.scalar self + end + def metaclass class << self self diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb index 0fed676727..f2c484cc59 100644 --- a/lib/active_relation/primitives/attribute.rb +++ b/lib/active_relation/primitives/attribute.rb @@ -52,11 +52,11 @@ module ActiveRelation end def history - [self] + (ancestor ? [ancestor, ancestor.send(:history)].flatten : []) + [self] + (ancestor ? [ancestor, ancestor.history].flatten : []) end def =~(other) - !(history & other.send(:history)).empty? + !(history & other.history).empty? end end include Congruence diff --git a/lib/active_relation/relations/aggregation.rb b/lib/active_relation/relations/aggregation.rb index d3a026820d..38f1f2dda8 100644 --- a/lib/active_relation/relations/aggregation.rb +++ b/lib/active_relation/relations/aggregation.rb @@ -12,7 +12,7 @@ module ActiveRelation groupings == other.groupings and expressions == other.expressions end - + def attributes expressions.collect { |e| e.bind(self) } end diff --git a/lib/active_relation/relations/compound.rb b/lib/active_relation/relations/compound.rb index 04f8ed5a2f..5778fe9b22 100644 --- a/lib/active_relation/relations/compound.rb +++ b/lib/active_relation/relations/compound.rb @@ -1,11 +1,12 @@ module ActiveRelation class Compound < Relation - abstract :==, :descend - attr_reader :relation + + hash_on :relation + delegate :joins, :selects, :orders, :groupings, :table_sql, :inserts, :limit, :offset, :name, :alias, :aggregation?, :alias?, :prefix_for, :column_for, - :hash, :engine, + :engine, :to => :relation def attributes @@ -16,4 +17,4 @@ module ActiveRelation descend(&:qualify) end end -end +end
\ No newline at end of file diff --git a/lib/active_relation/relations/insertion.rb b/lib/active_relation/relations/insertion.rb index ce065b64c1..7a13bc06bb 100644 --- a/lib/active_relation/relations/insertion.rb +++ b/lib/active_relation/relations/insertion.rb @@ -10,8 +10,8 @@ module ActiveRelation [ "INSERT", "INTO #{table_sql}", - "(#{record.keys.collect(&:to_sql).join(', ')})", - "VALUES #{record.values.to_sql}" + "(#{record.keys.collect(&:to_sql)})", + "VALUES #{record.to_sql}" ].join("\n") end diff --git a/lib/active_relation/relations/join.rb b/lib/active_relation/relations/join.rb index 4a57795280..835e965f72 100644 --- a/lib/active_relation/relations/join.rb +++ b/lib/active_relation/relations/join.rb @@ -1,8 +1,11 @@ module ActiveRelation class Join < Relation attr_reader :join_sql, :relation1, :relation2, :predicates + delegate :engine, :to => :relation1 + hash_on :relation1 + def initialize(join_sql, relation1, relation2, *predicates) @join_sql, @relation1, @relation2, @predicates = join_sql, relation1, relation2, predicates end @@ -14,7 +17,7 @@ module ActiveRelation (relation2 == other.relation1 and relation1 == other.relation2) ) end - + def qualify descend(&:qualify) end diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index e0fe43a0a0..039af61925 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -1,9 +1,5 @@ module ActiveRelation class Relation - abstract :attributes, :selects, :orders, :inserts, :groupings, :joins, :limit, :offset, :alias, :hash - - hash_on :hash - def session Session.new end @@ -108,10 +104,10 @@ module ActiveRelation def to_sql(strategy = Sql::SelectStatement.new(engine)) strategy.select [ - "SELECT #{attributes.collect{ |a| a.to_sql(Sql::SelectExpression.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::WhereClause.new(engine))}.join("\n\tAND ")}" unless selects.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? ), diff --git a/lib/active_relation/relations/writing.rb b/lib/active_relation/relations/writing.rb index 3054e9b72f..2714ce4d0c 100644 --- a/lib/active_relation/relations/writing.rb +++ b/lib/active_relation/relations/writing.rb @@ -1,5 +1,4 @@ module ActiveRelation class Writing < Compound - abstract :call, :to_sql end end
\ No newline at end of file diff --git a/lib/active_relation/sql.rb b/lib/active_relation/sql.rb index 0d233b307e..85dd591860 100644 --- a/lib/active_relation/sql.rb +++ b/lib/active_relation/sql.rb @@ -5,8 +5,6 @@ module ActiveRelation end class Formatter - abstract :attribute, :select, :value - attr_reader :engine include Quoting @@ -64,7 +62,15 @@ module ActiveRelation end def value(value) - quote(value, @attribute.column) + value.to_sql(self) + end + + def scalar(scalar) + quote(scalar, @attribute.column) + end + + def array(array) + "(" + array.collect { |e| e.to_sql(self) }.join(', ') + ")" end end |