diff options
Diffstat (limited to 'activerecord/lib/active_record')
3 files changed, 35 insertions, 11 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 bc59f064aa..3b35a190f9 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -119,6 +119,12 @@ module ActiveRecord # Do nothing by default. Implement for PostgreSQL, Oracle, ... end + # Inserts the given fixture into the table. Overriden in adapters that require + # something beyond a simple insert (eg. Oracle). + def insert_fixture(fixture, table_name) + execute "INSERT INTO #{table_name} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert' + 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/oracle_adapter.rb b/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb index 74101cf09a..25eca87304 100644 --- a/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb @@ -44,17 +44,9 @@ begin # After setting large objects to empty, select the OCI8::LOB # and write back the data. after_save :write_lobs - def write_lobs() #:nodoc: + def write_lobs #:nodoc: if connection.is_a?(ConnectionAdapters::OracleAdapter) - self.class.columns.select { |c| c.sql_type =~ /LOB$/i }.each { |c| - value = self[c.name] - value = value.to_yaml if unserializable_attribute?(c.name, c) - next if value.nil? || (value == '') - lob = connection.select_one( - "SELECT #{c.name} FROM #{self.class.table_name} WHERE #{self.class.primary_key} = #{quote_value(id)}", - 'Writable Large Object')[c.name] - lob.write value - } + connection.write_lobs(self.class.table_name, self.class, attributes) end end @@ -273,6 +265,30 @@ begin end + # Inserts the given fixture into the table. Overriden to properly handle lobs. + def insert_fixture(fixture, table_name) + super + + klass = fixture.class_name.constantize rescue nil + if klass.respond_to?(:ancestors) && klass.ancestors.include?(ActiveRecord::Base) + write_lobs(table_name, klass, fixture) + end + end + + # Writes LOB values from attributes, as indicated by the LOB columns of klass. + def write_lobs(table_name, klass, attributes) + id = quote(attributes[klass.primary_key]) + klass.columns.select { |col| col.sql_type =~ /LOB$/i }.each do |col| + value = attributes[col.name] + value = value.to_yaml if col.text? && klass.serialized_attributes[col.name] + next if value.nil? || (value == '') + lob = select_one("SELECT #{col.name} FROM #{table_name} WHERE #{klass.primary_key} = #{id}", + 'Writable Large Object')[col.name] + lob.write value + end + end + + # SCHEMA STATEMENTS ======================================== # # see: abstract/schema_statements.rb diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index c837fd7b50..47a77e4249 100755 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -290,7 +290,7 @@ class Fixtures < YAML::Omap def insert_fixtures values.each do |fixture| - @connection.execute "INSERT INTO #{@table_name} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert' + @connection.insert_fixture fixture, @table_name end end @@ -381,6 +381,8 @@ class Fixture #:nodoc: class FormatError < FixtureError#:nodoc: end + attr_reader :class_name + def initialize(fixture, class_name) case fixture when Hash, YAML::Omap |