From 260c847817bbc3ad990609b7e41f787e1d2940ea Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Wed, 1 Jul 2009 20:34:26 -0300 Subject: [PATCH] Create is now powered by Arel. Removed methods that are no longer used. --- activerecord/lib/active_record/base.rb | 29 ++++++++++--------- .../abstract/database_statements.rb | 4 +-- .../connection_adapters/sqlite_adapter.rb | 4 +-- arel | 2 +- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index e8793c3d2de2b..1f96e6dcc5292 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2907,18 +2907,13 @@ def create self.id = connection.next_sequence_value(self.class.sequence_name) end - quoted_attributes = attributes_with_quotes - - statement = if quoted_attributes.empty? - connection.empty_insert_statement(self.class.table_name) + new_id = if arel_attributes_values.empty? + arel_table.insert connection.empty_insert_statement_value else - "INSERT INTO #{self.class.quoted_table_name} " + - "(#{quoted_column_names.join(', ')}) " + - "VALUES(#{quoted_attributes.values.join(', ')})" + arel_table.insert arel_attributes_values end - self.id = connection.insert(statement, "#{self.class.name} Create", - self.class.primary_key, self.id, self.class.sequence_name) + self.id ||= new_id @new_record = false id @@ -2987,6 +2982,10 @@ def attributes_protected_by_default default end + def arel_table + @arel_table = Arel::Table.new(self.class.table_name) + end + # Returns a copy of the attributes hash where all the values have been safely quoted for use in # an SQL statement. def attributes_with_quotes(include_primary_key = true, include_readonly_attributes = true, attribute_names = @attributes.keys) @@ -3007,10 +3006,8 @@ def attributes_with_quotes(include_primary_key = true, include_readonly_attribut include_readonly_attributes ? quoted : remove_readonly_attributes(quoted) end - def arel_table - @arel_table = Arel::Table.new(self.class.table_name) - end - + # Returns a copy of the attributes hash where all the values have been safely quoted for use in + # an Arel insert/update method. def arel_attributes_values(include_primary_key = true, include_readonly_attributes = true, attribute_names = @attributes.keys) attrs = {} connection = self.class.connection @@ -3019,7 +3016,11 @@ def arel_attributes_values(include_primary_key = true, include_readonly_attribut value = read_attribute(name) if include_readonly_attributes || (!include_readonly_attributes && !self.class.readonly_attributes.include?(name)) - attrs[arel_table[name]] = value.is_a?(Hash) ? value.to_yaml : value + # We need explicit to_yaml because quote() does not properly convert Time/Date fields to YAML. + if value && self.class.serialized_attributes.has_key?(name) && (value.acts_like?(:date) || value.acts_like?(:time)) + value = value.to_yaml + end + attrs[arel_table[name]] = (value.is_a?(Hash) || value.is_a?(Array)) ? value.to_yaml : value end end end 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 26bf04f44917d..be89873632e95 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -208,8 +208,8 @@ def insert_fixture(fixture, table_name) execute "INSERT INTO #{quote_table_name(table_name)} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert' end - def empty_insert_statement(table_name) - "INSERT INTO #{quote_table_name(table_name)} VALUES(DEFAULT)" + def empty_insert_statement_value + "VALUES(DEFAULT)" end def case_sensitive_equality_operator diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index 8cbe08cd77267..16a976a8c766e 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -308,8 +308,8 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc: 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)" + def empty_insert_statement_value + "VALUES(NULL)" end protected diff --git a/arel b/arel index 97811698ab0e6..808b9e90a38c6 160000 --- a/arel +++ b/arel @@ -1 +1 @@ -Subproject commit 97811698ab0e68b33fbf3067c3764e385dd75d53 +Subproject commit 808b9e90a38c6c19e109da8eb5f2a264fd780d9a