From 4c41be9b83792e6b5a4d06c260add1d773ad2f33 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 29 Apr 2011 09:06:37 -0700 Subject: statement cache for deletes working on mysql --- .../connection_adapters/mysql_adapter.rb | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 1136dbc45e..f173bab51d 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -490,6 +490,45 @@ module ActiveRecord @connection.affected_rows end + def exec_delete(sql, name, binds) + log(sql, name, binds) do + result = nil + + cache = {} + if binds.empty? + stmt = @connection.prepare(sql) + else + cache = @statements[sql] ||= { + :stmt => @connection.prepare(sql) + } + stmt = cache[:stmt] + end + + + begin + stmt.execute(*binds.map { |col, val| type_cast(val, col) }) + rescue Mysql::Error => e + # Older versions of MySQL leave the prepared statement in a bad + # place when an error occurs. To support older mysql versions, we + # need to close the statement and delete the statement from the + # cache. + stmt.close + @statements.delete sql + raise e + end + + if metadata = stmt.result_metadata + metadata.free + end + + result = stmt.affected_rows + stmt.free_result + stmt.close if binds.empty? + + result + end + end + def begin_db_transaction #:nodoc: exec_without_stmt "BEGIN" rescue Mysql::Error -- cgit v1.2.3