diff options
author | Guillermo Iguaran <guilleiguaran@gmail.com> | 2013-02-07 18:45:25 -0800 |
---|---|---|
committer | Guillermo Iguaran <guilleiguaran@gmail.com> | 2013-02-07 18:45:25 -0800 |
commit | 00e0b258eefab192a2012df9cf0680d214c89504 (patch) | |
tree | c57b23a3197d773dcce73a322806f49833208dbf /activerecord/lib | |
parent | b4f189a162f4280b360dbc2a6635bbff6c6f09bc (diff) | |
parent | 04c2d2e2e4197a1564c33f09d3e52253d845925f (diff) | |
download | rails-00e0b258eefab192a2012df9cf0680d214c89504.tar.gz rails-00e0b258eefab192a2012df9cf0680d214c89504.tar.bz2 rails-00e0b258eefab192a2012df9cf0680d214c89504.zip |
Merge pull request #9208 from dylanahsmith/3-2-mysql-quote-numeric
[3.2] active_record: Quote numeric values compared to string columns.
Diffstat (limited to 'activerecord/lib')
3 files changed, 12 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb index f93c7cd74a..fe0b1959f6 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb @@ -25,13 +25,19 @@ module ActiveRecord when true, false if column && column.type == :integer value ? '1' : '0' + elsif column && [:text, :string, :binary].include?(column.type) + value ? "'1'" : "'0'" else value ? quoted_true : quoted_false end # BigDecimals need to be put in a non-normalized form and quoted. when nil then "NULL" - when BigDecimal then value.to_s('F') - when Numeric then value.to_s + when Numeric, ActiveSupport::Duration + value = BigDecimal === value ? value.to_s('F') : value.to_s + if column && ![:integer, :float, :decimal].include?(column.type) + value = "'#{value}'" + end + value when Date, Time then "'#{quoted_date(value)}'" when Symbol then "'#{quote_string(value.to_s)}'" else diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index abccc3af24..61c5e8040e 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -199,8 +199,6 @@ module ActiveRecord if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary) s = column.class.string_to_binary(value).unpack("H*")[0] "x'#{s}'" - elsif value.kind_of?(BigDecimal) - value.to_s("F") else super end diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index b31fdfd981..5f311ed6d4 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -51,6 +51,10 @@ module ActiveRecord when Class # FIXME: I think we need to deprecate this behavior attribute.eq(value.name) + when Integer, ActiveSupport::Duration + # Arel treats integers as literals, but they should be quoted when compared with strings + column = engine.connection.schema_cache.columns_hash(table.name)[attribute.name.to_s] + attribute.eq(Arel::Nodes::SqlLiteral.new(engine.connection.quote(value, column))) else attribute.eq(value) end |