diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-04-30 17:26:42 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-04-30 17:27:46 -0700 |
commit | 1d7c0336ef18cfde7b55911f60d912b5feba2b68 (patch) | |
tree | fe63e8b3db669ff8d07b9da786a28c2f394414ff | |
parent | e14e696e9627b0e7fb903bc31b2b72070773c0a9 (diff) | |
download | rails-1d7c0336ef18cfde7b55911f60d912b5feba2b68.tar.gz rails-1d7c0336ef18cfde7b55911f60d912b5feba2b68.tar.bz2 rails-1d7c0336ef18cfde7b55911f60d912b5feba2b68.zip |
using bind parameters for updates
6 files changed, 16 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb index 6d52cc344d..3045e30407 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -55,20 +55,27 @@ module ActiveRecord def exec_query(sql, name = 'SQL', binds = []) end - # Executes insert +sql+ statement in the context of this connection using + # Executes insert +sql+ statement in the context of this connection using # +binds+ as the bind substitutes. +name+ is the logged along with # the executed +sql+ statement. def exec_insert(sql, name, binds) exec_query(sql, name, binds) end - # Executes delete +sql+ statement in the context of this connection using + # Executes delete +sql+ statement in the context of this connection using # +binds+ as the bind substitutes. +name+ is the logged along with # the executed +sql+ statement. def exec_delete(sql, name, binds) exec_query(sql, name, binds) end + # Executes update +sql+ statement in the context of this connection using + # +binds+ as the bind substitutes. +name+ is the logged along with + # the executed +sql+ statement. + def exec_update(sql, name, binds) + exec_query(sql, name, binds) + end + # Returns the last auto-generated ID from the affected table. # # +id_value+ will be returned unless the value is nil, in @@ -84,8 +91,8 @@ module ActiveRecord end # Executes the update statement and returns the number of rows affected. - def update(sql, name = nil) - update_sql(sql, name) + def update(sql, name = nil, binds = []) + exec_update(sql, name, binds) end # Executes the delete statement and returns the number of rows affected. diff --git a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb index 199bf7d494..8973544028 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb @@ -297,6 +297,7 @@ module ActiveRecord execute sql.gsub('?') { quote(*binds.shift.reverse) }, name @connection.affected_rows end + alias :exec_update :exec_delete def last_inserted_id(result) @connection.last_id diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 07608ea6a7..356aaf0ebe 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -455,6 +455,7 @@ module ActiveRecord end end end + alias :exec_update :exec_delete def begin_db_transaction #:nodoc: exec_without_stmt "BEGIN" diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index f3e0248e39..3b0d960acc 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -548,6 +548,7 @@ module ActiveRecord affected end end + alias :exec_update :exec_delete def sql_for_insert(sql, pk, id_value, sequence_name, binds) unless pk diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index ad511a0731..04367657ec 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -182,6 +182,7 @@ module ActiveRecord exec_query(sql, name, binds) @connection.changes end + alias :exec_update :exec_delete def last_inserted_id(result) @connection.last_insert_row_id diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 658a949331..ae9afad48a 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -220,7 +220,7 @@ module ActiveRecord stmt.take limit if limit stmt.order(*order) stmt.key = table[primary_key] - @klass.connection.update stmt.to_sql + @klass.connection.update stmt.to_sql, 'SQL', bind_values end end |