From 6865464845f24d6aa3dc29809e04901b648670d5 Mon Sep 17 00:00:00 2001 From: Erik Bryn Date: Thu, 23 Apr 2009 18:56:19 -0700 Subject: [PATCH] Pass column to #quote when possible --- activerecord/lib/active_record/base.rb | 26 +++++++++++++++----------- 1 files changed, 15 insertions(+), 11 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 97c36a6..fc08010 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1508,6 +1508,11 @@ module ActiveRecord #:nodoc: "(#{segments.join(') AND (')})" unless segments.empty? end + # Returns the column object for the named attribute. + def column_for_attribute(name) + columns_hash[name.to_s] + end + private def find_initial(options) options.update(:limit => 1) @@ -2316,13 +2321,12 @@ module ActiveRecord #:nodoc: table_name = connection.quote_table_name(table_name) end - attribute_condition("#{table_name}.#{connection.quote_column_name(attr)}", value) + condition = attribute_condition("#{table_name}.#{connection.quote_column_name(attr)}", value) + replace_bind_variables(condition, expand_range_bind_variables([value]), column_for_attribute(attr)) else sanitize_sql_hash_for_conditions(value, connection.quote_table_name(attr.to_s)) end end.join(' AND ') - - replace_bind_variables(conditions, expand_range_bind_variables(attrs.values)) end alias_method :sanitize_sql_hash, :sanitize_sql_hash_for_conditions @@ -2331,7 +2335,7 @@ module ActiveRecord #:nodoc: # # => "status = NULL , group_id = 1" def sanitize_sql_hash_for_assignment(attrs) attrs.map do |attr, value| - "#{connection.quote_column_name(attr)} = #{quote_bound_value(value)}" + "#{connection.quote_column_name(attr)} = #{quote_bound_value(value, column_for_attribute(attr))}" end.join(', ') end @@ -2351,10 +2355,10 @@ module ActiveRecord #:nodoc: alias_method :sanitize_conditions, :sanitize_sql - def replace_bind_variables(statement, values) #:nodoc: + def replace_bind_variables(statement, values, column = nil) #:nodoc: raise_if_bind_arity_mismatch(statement, statement.count('?'), values.size) bound = values.dup - statement.gsub('?') { quote_bound_value(bound.shift) } + statement.gsub('?') { quote_bound_value(bound.shift, column) } end def replace_named_bind_variables(statement, bind_vars) #:nodoc: @@ -2386,15 +2390,15 @@ module ActiveRecord #:nodoc: expanded end - def quote_bound_value(value) #:nodoc: + def quote_bound_value(value, column = nil) #:nodoc: if value.respond_to?(:map) && !value.acts_like?(:string) if value.respond_to?(:empty?) && value.empty? - connection.quote(nil) + connection.quote(nil, column) else - value.map { |v| connection.quote(v) }.join(',') + value.map { |v| connection.quote(v, column) }.join(',') end else - connection.quote(value) + connection.quote(value, column) end end @@ -2813,7 +2817,7 @@ module ActiveRecord #:nodoc: # Returns the column object for the named attribute. def column_for_attribute(name) - self.class.columns_hash[name.to_s] + self.class.column_for_attribute(name) end # Returns true if the +comparison_object+ is the same object, or is of the same type and has the same id. -- 1.6.0.2