diff options
author | Dylan Smith <Dylan.Smith@shopify.com> | 2013-02-13 14:37:51 -0500 |
---|---|---|
committer | Dylan Thacker-Smith <Dylan.Smith@shopify.com> | 2014-04-28 03:10:47 -0400 |
commit | 1a2bf3df0e3350142d36a51d351ed38523b9f63c (patch) | |
tree | 435f45b71a5f9ca16710e8efae924ff164b363f8 | |
parent | de916331c3536db839f844034176bf321d94efa7 (diff) | |
download | rails-1a2bf3df0e3350142d36a51d351ed38523b9f63c.tar.gz rails-1a2bf3df0e3350142d36a51d351ed38523b9f63c.tar.bz2 rails-1a2bf3df0e3350142d36a51d351ed38523b9f63c.zip |
Test quoting integers when comparing a string column with integers.
An equality with a string column and integer like
SELECT * FROM `users` WHERE `login_token` = 0 LIMIT 1;
will match match any string that doesn't start with a digit in certain
databases, like mysql. Make sure we quote the integer to avoid this
problem in a database independant way.
-rw-r--r-- | test/support/fake_record.rb | 14 | ||||
-rw-r--r-- | test/visitors/test_to_sql.rb | 16 |
2 files changed, 25 insertions, 5 deletions
diff --git a/test/support/fake_record.rb b/test/support/fake_record.rb index ed4420a2cd..035a7a46cf 100644 --- a/test/support/fake_record.rb +++ b/test/support/fake_record.rb @@ -60,9 +60,13 @@ module FakeRecord end def quote thing, column = nil - if column && column.type == :integer - return 'NULL' if thing.nil? - return thing.to_i + if column && !thing.nil? + case column.type + when :integer + thing = thing.to_i + when :string + thing = thing.to_s + end end case thing @@ -111,6 +115,10 @@ module FakeRecord def schema_cache connection end + + def quote thing, column = nil + connection.quote thing, column + end end class Base diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb index eb102c1905..05e75b5e0d 100644 --- a/test/visitors/test_to_sql.rb +++ b/test/visitors/test_to_sql.rb @@ -119,6 +119,12 @@ module Arel sql.must_be_like %{ "users"."id" = 1 } end + it 'should use the column to quote integers' do + table = Table.new(:users) + sql = compile table[:name].eq(0) + sql.must_be_like %{ "users"."name" = '0' } + end + it 'should handle nil' do sql = compile Nodes::Equality.new(@table[:name], nil) sql.must_be_like %{ "users"."name" IS NULL } @@ -160,6 +166,12 @@ module Arel assert_match(/LIMIT 'omg'/, compile(sc)) end + it "should quote LIMIT without column type coercion" do + table = Table.new(:users) + sc = table.where(table[:name].eq(0)).take(1).ast + assert_match(/WHERE "users"."name" = '0' LIMIT 1/, compile(sc)) + end + it "should visit_DateTime" do called_with = nil @conn.connection.extend(Module.new { @@ -179,9 +191,9 @@ module Arel end it "should visit_Float" do - test = Table.new(:users)[:name].eq 2.14 + test = Table.new(:products)[:price].eq 2.14 sql = compile test - sql.must_be_like %{"users"."name" = 2.14} + sql.must_be_like %{"products"."price" = 2.14} end it "should visit_Not" do |