From 638505b90f593c8ee48a060f378ed6a9c93442e1 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Sat, 6 Oct 2007 00:49:58 +0000 Subject: Send the correct INSERT statement when dealing with objects with only primary keys. Closes #9523 [tarmo] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7753 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/base.rb | 19 +++++++++++++------ .../abstract/database_statements.rb | 4 ++++ .../connection_adapters/sqlite_adapter.rb | 3 +++ 3 files changed, 20 insertions(+), 6 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 5e1185065e..068d9a5c45 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1968,9 +1968,11 @@ module ActiveRecord #:nodoc: # Updates the associated record with values matching those of the instance attributes. # Returns the number of affected rows. def update + quoted_attributes = attributes_with_quotes(false, false) + return 0 if quoted_attributes.empty? connection.update( "UPDATE #{self.class.table_name} " + - "SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false, false))} " + + "SET #{quoted_comma_pair_list(connection, quoted_attributes)} " + "WHERE #{connection.quote_column_name(self.class.primary_key)} = #{quote_value(id)}", "#{self.class.name} Update" ) @@ -1983,13 +1985,18 @@ module ActiveRecord #:nodoc: self.id = connection.next_sequence_value(self.class.sequence_name) end - self.id = connection.insert( + quoted_attributes = attributes_with_quotes + + statement = if quoted_attributes.empty? + connection.empty_insert_statement(self.class.table_name) + else "INSERT INTO #{self.class.table_name} " + "(#{quoted_column_names.join(', ')}) " + - "VALUES(#{attributes_with_quotes.values.join(', ')})", - "#{self.class.name} Create", - self.class.primary_key, self.id, self.class.sequence_name - ) + "VALUES(#{quoted_attributes.values.join(', ')})" + end + + self.id = connection.insert(statement, "#{self.class.name} Create", + self.class.primary_key, self.id, self.class.sequence_name) @new_record = false id 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 a80025e7a7..341b104f06 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -139,6 +139,10 @@ module ActiveRecord execute "INSERT INTO #{table_name} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert' end + def empty_insert_statement(table_name) + "INSERT INTO #{table_name} VALUES(DEFAULT)" + end + protected # Returns an array of record hashes with the column names as keys and # column values as values. diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index 2b78c300f5..c171b0239e 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -247,6 +247,9 @@ module ActiveRecord alter_table(table_name, :rename => {column_name.to_s => new_column_name.to_s}) end + def empty_insert_statement(table_name) + "INSERT INTO #{table_name} VALUES(NULL)" + end protected def select(sql, name = nil) #:nodoc: -- cgit v1.2.3