Skip to content

Commit

Permalink
Revert "Merge pull request #9208 from dylanahsmith/3-2-mysql-quote-nu…
Browse files Browse the repository at this point in the history
…meric"

This reverts commit 921a296.
  • Loading branch information
steveklabnik committed Feb 27, 2013
1 parent 55a1765 commit ee291b9
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 49 deletions.
Expand Up @@ -25,19 +25,13 @@ def quote(value, column = nil)
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 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 BigDecimal then value.to_s('F')
when Numeric then value.to_s
when Date, Time then "'#{quoted_date(value)}'"
when Symbol then "'#{quote_string(value.to_s)}'"
else
Expand Down
Expand Up @@ -199,6 +199,8 @@ def quote(value, column = nil)
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
Expand Down
4 changes: 0 additions & 4 deletions activerecord/lib/active_record/relation/predicate_builder.rb
Expand Up @@ -51,10 +51,6 @@ def self.build_from_hash(engine, attributes, default_table, allow_table_name = t
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
Expand Down
14 changes: 7 additions & 7 deletions activerecord/test/cases/quoting_test.rb
Expand Up @@ -122,35 +122,35 @@ def test_quote_false
def test_quote_float
float = 1.2
assert_equal float.to_s, @quoter.quote(float, nil)
assert_equal float.to_s, @quoter.quote(float, FakeColumn.new(:float))
assert_equal float.to_s, @quoter.quote(float, Object.new)
end

def test_quote_fixnum
fixnum = 1
assert_equal fixnum.to_s, @quoter.quote(fixnum, nil)
assert_equal fixnum.to_s, @quoter.quote(fixnum, FakeColumn.new(:integer))
assert_equal fixnum.to_s, @quoter.quote(fixnum, Object.new)
end

def test_quote_bignum
bignum = 1 << 100
assert_equal bignum.to_s, @quoter.quote(bignum, nil)
assert_equal bignum.to_s, @quoter.quote(bignum, FakeColumn.new(:integer))
assert_equal bignum.to_s, @quoter.quote(bignum, Object.new)
end

def test_quote_bigdecimal
bigdec = BigDecimal.new((1 << 100).to_s)
assert_equal bigdec.to_s('F'), @quoter.quote(bigdec, nil)
assert_equal bigdec.to_s('F'), @quoter.quote(bigdec, FakeColumn.new(:decimal))
assert_equal bigdec.to_s('F'), @quoter.quote(bigdec, Object.new)
end

def test_dates_and_times
@quoter.extend(Module.new { def quoted_date(value) 'lol' end })
assert_equal "'lol'", @quoter.quote(Date.today, nil)
assert_equal "'lol'", @quoter.quote(Date.today, FakeColumn.new(:date))
assert_equal "'lol'", @quoter.quote(Date.today, Object.new)
assert_equal "'lol'", @quoter.quote(Time.now, nil)
assert_equal "'lol'", @quoter.quote(Time.now, FakeColumn.new(:time))
assert_equal "'lol'", @quoter.quote(Time.now, Object.new)
assert_equal "'lol'", @quoter.quote(DateTime.now, nil)
assert_equal "'lol'", @quoter.quote(DateTime.now, FakeColumn.new(:datetime))
assert_equal "'lol'", @quoter.quote(DateTime.now, Object.new)
end

def test_crazy_object
Expand Down
25 changes: 0 additions & 25 deletions activerecord/test/cases/relation/where_test.rb
Expand Up @@ -35,30 +35,5 @@ def test_where_with_table_name_and_empty_array
def test_where_with_empty_hash_and_no_foreign_key
assert_equal 0, Edge.where(:sink => {}).count
end

def test_where_with_integer_for_string_column
count = Post.where(:title => 0).count
assert_equal 0, count
end

def test_where_with_float_for_string_column
count = Post.where(:title => 0.0).count
assert_equal 0, count
end

def test_where_with_boolean_for_string_column
count = Post.where(:title => false).count
assert_equal 0, count
end

def test_where_with_decimal_for_string_column
count = Post.where(:title => BigDecimal.new('0')).count
assert_equal 0, count
end

def test_where_with_duration_for_string_column
count = Post.where(:title => 0.seconds).count
assert_equal 0, count
end
end
end
6 changes: 3 additions & 3 deletions activerecord/test/cases/relation_scoping_test.rb
Expand Up @@ -380,19 +380,19 @@ def test_default_scoping_with_threads
def test_default_scope_with_inheritance
wheres = InheritedPoorDeveloperCalledJamis.scoped.where_values_hash
assert_equal "Jamis", wheres[:name]
assert_equal Arel.sql("50000"), wheres[:salary]
assert_equal 50000, wheres[:salary]
end

def test_default_scope_with_module_includes
wheres = ModuleIncludedPoorDeveloperCalledJamis.scoped.where_values_hash
assert_equal "Jamis", wheres[:name]
assert_equal Arel.sql("50000"), wheres[:salary]
assert_equal 50000, wheres[:salary]
end

def test_default_scope_with_multiple_calls
wheres = MultiplePoorDeveloperCalledJamis.scoped.where_values_hash
assert_equal "Jamis", wheres[:name]
assert_equal Arel.sql("50000"), wheres[:salary]
assert_equal 50000, wheres[:salary]
end

def test_method_scope
Expand Down
2 changes: 0 additions & 2 deletions activerecord/test/schema/schema.rb
Expand Up @@ -530,8 +530,6 @@ def create_table(*args, &block)
create_table :price_estimates, :force => true do |t|
t.string :estimate_of_type
t.integer :estimate_of_id
t.string :thing_type
t.integer :thing_id
t.integer :price
end

Expand Down

0 comments on commit ee291b9

Please sign in to comment.