From 43bbb25ddd413acc27998fe25be8f086585a7a2e Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 15 Oct 2010 16:41:44 -0700 Subject: [PATCH] bind substitution is working properly --- .../connection_adapters/postgresql_adapter.rb | 18 +++++++++++------- .../postgresql/postgresql_adapter_test.rb | 8 ++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 46c3486be6..71adfe8d8c 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -228,7 +228,7 @@ module ActiveRecord def clear_cache! @statements.each_value do |value| - exec "DEALLOCATE #{value}" + @connection.query "DEALLOCATE #{value}" end @statements.clear end @@ -251,6 +251,7 @@ module ActiveRecord def reconnect! if @connection.respond_to?(:reset) @connection.reset + clear_cache! configure_connection else disconnect! @@ -515,6 +516,10 @@ module ActiveRecord end end + def substitute_for(column, current_values) + Arel.sql("$#{current_values.length + 1}") + end + def exec(sql, name = 'SQL', binds = []) return async_exec(sql, name, binds) if @async @@ -537,7 +542,9 @@ module ActiveRecord }) @connection.block result = @connection.get_last_result - ActiveRecord::Result.new(result.fields, result_as_array(result)) + ret = ActiveRecord::Result.new(result.fields, result_as_array(result)) + result.clear + return ret end end @@ -1014,11 +1021,8 @@ module ActiveRecord # Executes a SELECT query and returns the results, performing any data type # conversions that are required to be performed here instead of in PostgreSQLColumn. - def select(sql, name = nil) - fields, rows = select_raw(sql, name) - rows.map do |row| - Hash[fields.zip(row)] - end + def select(sql, name = nil, binds = []) + exec(sql, name, binds).to_a end def select_raw(sql, name = nil) diff --git a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb index 70de8c5e6c..b0fd2273df 100644 --- a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb +++ b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb @@ -55,6 +55,14 @@ module ActiveRecord assert_equal [['1', 'foo']], result.rows end + + def test_substitute_for + bind = @connection.substitute_for(nil, []) + assert_equal Arel.sql('$1'), bind + + bind = @connection.substitute_for(nil, [nil]) + assert_equal Arel.sql('$2'), bind + end end end end