From 9d46e0d012e5c4687af4d14584f1230e71d7d654 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 15 Oct 2010 16:24:04 -0700 Subject: [PATCH] bind parameters are now typecast --- .../connection_adapters/postgresql_adapter.rb | 4 +++- .../adapters/postgresql/postgresql_adapter_test.rb | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 80b987fb89..46c3486be6 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -532,7 +532,9 @@ module ActiveRecord # Clear the queue @connection.get_last_result - @connection.send_query_prepared(key, binds.map { |col, val| val }) + @connection.send_query_prepared(key, binds.map { |col, val| + col ? col.type_cast(val) : val + }) @connection.block result = @connection.get_last_result ActiveRecord::Result.new(result.fields, result_as_array(result)) diff --git a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb index 0b295c9613..70de8c5e6c 100644 --- a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb +++ b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb @@ -41,6 +41,20 @@ module ActiveRecord assert_equal [['1', 'foo']], result.rows end + + def test_exec_typecasts_bind_vals + string = @connection.quote('foo') + @connection.exec("INSERT INTO ex (id, data) VALUES (1, #{string})") + + column = @connection.columns('ex').find { |col| col.name == 'id' } + result = @connection.exec( + 'SELECT id, data FROM ex WHERE id = $1', nil, [[column, '1-fuu']]) + + assert_equal 1, result.rows.length + assert_equal 2, result.columns.length + + assert_equal [['1', 'foo']], result.rows + end end end end