diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-04-29 09:06:37 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-04-29 11:09:37 -0700 |
commit | 4c41be9b83792e6b5a4d06c260add1d773ad2f33 (patch) | |
tree | 09060168f69488dbcb71575a9c5661e657ff294a /activerecord/lib | |
parent | 453bd2f39dfc59641a2d9028088c99e0fd529242 (diff) | |
download | rails-4c41be9b83792e6b5a4d06c260add1d773ad2f33.tar.gz rails-4c41be9b83792e6b5a4d06c260add1d773ad2f33.tar.bz2 rails-4c41be9b83792e6b5a4d06c260add1d773ad2f33.zip |
statement cache for deletes working on mysql
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/mysql_adapter.rb | 39 |
1 files changed, 39 insertions, 0 deletions
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 |