From b445ab986a2c177621a2154b0559be4c700abeeb Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Mon, 4 Sep 2006 23:41:13 +0000 Subject: Rename quote to quote_value so the name can be used in AR models. #3628 [Koz] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5007 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/associations.rb | 10 +++---- .../has_and_belongs_to_many_association.rb | 2 +- .../associations/has_many_association.rb | 2 +- .../associations/has_many_through_association.rb | 4 +-- .../associations/has_one_association.rb | 2 +- activerecord/lib/active_record/base.rb | 33 +++++++++++++++------- .../lib/active_record/locking/optimistic.rb | 4 +-- 7 files changed, 35 insertions(+), 22 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 33fd8afee2..e2dca89d49 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1457,7 +1457,7 @@ module ActiveRecord table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name, polymorphic_foreign_key, parent.aliased_table_name, parent.primary_key, - aliased_join_table_name, polymorphic_foreign_type, klass.quote(parent.active_record.base_class.name)] + + aliased_join_table_name, polymorphic_foreign_type, klass.quote_value(parent.active_record.base_class.name)] + " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [table_name_and_alias, aliased_table_name, primary_key, aliased_join_table_name, options[:foreign_key] || reflection.klass.to_s.classify.foreign_key ] @@ -1472,7 +1472,7 @@ module ActiveRecord aliased_table_name, "#{source_reflection.options[:as]}_id", aliased_join_table_name, options[:foreign_key] || primary_key, aliased_table_name, "#{source_reflection.options[:as]}_type", - klass.quote(source_reflection.active_record.base_class.name) + klass.quote_value(source_reflection.active_record.base_class.name) ] else case source_reflection.macro @@ -1501,7 +1501,7 @@ module ActiveRecord aliased_table_name, "#{reflection.options[:as]}_id", parent.aliased_table_name, parent.primary_key, aliased_table_name, "#{reflection.options[:as]}_type", - klass.quote(parent.active_record.base_class.name) + klass.quote_value(parent.active_record.base_class.name) ] when reflection.macro == :has_one && reflection.options[:as] " LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s " % [ @@ -1509,7 +1509,7 @@ module ActiveRecord aliased_table_name, "#{reflection.options[:as]}_id", parent.aliased_table_name, parent.primary_key, aliased_table_name, "#{reflection.options[:as]}_type", - klass.quote(reflection.active_record.base_class.name) + klass.quote_value(reflection.active_record.base_class.name) ] else foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key @@ -1530,7 +1530,7 @@ module ActiveRecord join << %(AND %s.%s = %s ) % [ aliased_table_name, reflection.active_record.connection.quote_column_name(reflection.active_record.inheritance_column), - klass.quote(klass.name.demodulize)] unless klass.descends_from_active_record? + klass.quote_value(klass.name.demodulize)] unless klass.descends_from_active_record? join << "AND #{interpolate_sql(sanitize_sql(reflection.options[:conditions]))} " if reflection.options[:conditions] join end diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index fc1de92259..8c0e50e38e 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -118,7 +118,7 @@ module ActiveRecord attributes[column.name] = record.quoted_id else if record.attributes.has_key?(column.name) - value = @owner.send(:quote, record[column.name], column) + value = @owner.send(:quote_value, record[column.name], column) attributes[column.name] = value unless value.nil? end end diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index e79fa858c4..d3fdcbe02f 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -184,7 +184,7 @@ module ActiveRecord when @reflection.options[:as] @finder_sql = "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " + - "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote @owner.class.base_class.name.to_s}" + "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}" @finder_sql << " AND (#{conditions})" if conditions else diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 3888bc62ba..d7ca7b5834 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -127,7 +127,7 @@ module ActiveRecord def construct_quoted_owner_attributes(reflection) if as = reflection.options[:as] { "#{as}_id" => @owner.quoted_id, - "#{as}_type" => reflection.klass.quote( + "#{as}_type" => reflection.klass.quote_value( @owner.class.base_class.name.to_s, reflection.klass.columns_hash["#{as}_type"]) } else @@ -164,7 +164,7 @@ module ActiveRecord if @reflection.source_reflection.options[:as] polymorphic_join = "AND %s.%s = %s" % [ @reflection.table_name, "#{@reflection.source_reflection.options[:as]}_type", - @owner.class.quote(@reflection.through_reflection.klass.name) + @owner.class.quote_value(@reflection.through_reflection.klass.name) ] end end diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index e881d49420..4ccd725ebd 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -69,7 +69,7 @@ module ActiveRecord when @reflection.options[:as] @finder_sql = "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " + - "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote @owner.class.base_class.name.to_s}" + "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}" else @finder_sql = "#{@reflection.table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}" end diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 346f6549f1..e9c9212560 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -523,12 +523,12 @@ module ActiveRecord #:nodoc: # for looping over a collection where each element require a number of aggregate values. Like the DiscussionBoard # that needs to list both the number of posts and comments. def increment_counter(counter_name, id) - update_all "#{counter_name} = #{counter_name} + 1", "#{primary_key} = #{quote(id)}" + update_all "#{counter_name} = #{counter_name} + 1", "#{primary_key} = #{quote_value(id)}" end # Works like increment_counter, but decrements instead. def decrement_counter(counter_name, id) - update_all "#{counter_name} = #{counter_name} - 1", "#{primary_key} = #{quote(id)}" + update_all "#{counter_name} = #{counter_name} - 1", "#{primary_key} = #{quote_value(id)}" end @@ -818,10 +818,16 @@ module ActiveRecord #:nodoc: superclass == Base || !columns_hash.include?(inheritance_column) end - def quote(value, column = nil) #:nodoc: + + def quote_value(value, column = nil) #:nodoc: connection.quote(value,column) end + def quote(value, column = nil) #:nodoc: + connection.quote(value, column) + end + deprecate :quote + # Used to sanitize objects before they're used in an SELECT SQL-statement. Delegates to connection.quote. def sanitize(object) #:nodoc: connection.quote(object) @@ -1010,7 +1016,7 @@ module ActiveRecord #:nodoc: def find_one(id, options) conditions = " AND (#{sanitize_sql(options[:conditions])})" if options[:conditions] - options.update :conditions => "#{table_name}.#{primary_key} = #{quote(id,columns_hash[primary_key])}#{conditions}" + options.update :conditions => "#{table_name}.#{primary_key} = #{quote_value(id,columns_hash[primary_key])}#{conditions}" # Use find_every(options).first since the primary key condition # already ensures we have a single record. Using find_initial adds @@ -1024,7 +1030,7 @@ module ActiveRecord #:nodoc: def find_some(ids, options) conditions = " AND (#{sanitize_sql(options[:conditions])})" if options[:conditions] - ids_list = ids.map { |id| quote(id,columns_hash[primary_key]) }.join(',') + ids_list = ids.map { |id| quote_value(id,columns_hash[primary_key]) }.join(',') options.update :conditions => "#{table_name}.#{primary_key} IN (#{ids_list})#{conditions}" result = find_every(options) @@ -1385,7 +1391,7 @@ module ActiveRecord #:nodoc: # { :name => "foo'bar", :group_id => 4 } returns "name='foo''bar' and group_id= 4" def sanitize_sql_hash(hash) hash.collect { |attrib, value| - "#{table_name}.#{connection.quote_column_name(attrib)} = #{quote(value)}" + "#{table_name}.#{connection.quote_column_name(attrib)} = #{quote_value(value)}" }.join(" AND ") end @@ -1503,7 +1509,7 @@ module ActiveRecord #:nodoc: end def quoted_id #:nodoc: - quote(id, column_for_attribute(self.class.primary_key)) + quote_value(id, column_for_attribute(self.class.primary_key)) end # Sets the primary ID. @@ -1767,7 +1773,7 @@ module ActiveRecord #:nodoc: connection.update( "UPDATE #{self.class.table_name} " + "SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} " + - "WHERE #{self.class.primary_key} = #{quote(id)}", + "WHERE #{self.class.primary_key} = #{quote_value(id)}", "#{self.class.name} Update" ) end @@ -1983,17 +1989,24 @@ module ActiveRecord #:nodoc: def attributes_with_quotes(include_primary_key = true) attributes.inject({}) do |quoted, (name, value)| if column = column_for_attribute(name) - quoted[name] = quote(value, column) unless !include_primary_key && column.primary + quoted[name] = quote_value(value, column) unless !include_primary_key && column.primary end quoted end end # Quote strings appropriately for SQL statements. - def quote(value, column = nil) + def quote_value(value, column = nil) self.class.connection.quote(value, column) end + # Deprecated, use quote_value + def quote(value, column = nil) + self.class.connection.quote(value, column) + end + deprecate :quote + + # Interpolate custom sql string in instance context. # Optional record argument is meant for custom insert_sql. def interpolate_sql(sql, record = nil) diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index 823f4e19fc..e9e8a44273 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -66,8 +66,8 @@ module ActiveRecord affected_rows = connection.update(<<-end_sql, "#{self.class.name} Update with optimistic locking") UPDATE #{self.class.table_name} SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} - WHERE #{self.class.primary_key} = #{quote(id)} - AND #{self.class.quoted_locking_column} = #{quote(previous_value)} + WHERE #{self.class.primary_key} = #{quote_value(id)} + AND #{self.class.quoted_locking_column} = #{quote_value(previous_value)} end_sql unless affected_rows == 1 -- cgit v1.2.3