Sha256: 4fabf72d69fdecdf6b8079dc04c114da144e6a26d998d00f35300ab92dd6792e

Contents?: true

Size: 1.19 KB

Versions: 16

Compression:

Stored size: 1.19 KB

Contents

module ActiveRecord
  module ConnectionAdapters
    module DatabaseStatements
      def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
        sql, binds = to_sql_and_binds(arel, binds)
        value = exec_insert(sql, name, binds, pk, sequence_name)

        return id_value if id_value

        if pk.is_a?(Array) && !value.empty?
          # This is a CPK model and the query result is not empty. Thus we can figure out the new ids for each
          # auto incremented field
          pk.map {|key| value.first[key]}
        elsif pk.is_a?(Array)
          # This is CPK, but we don't know what autoincremented fields were updated.
          result = Array.new(pk.size)

          # Is there an autoincrementing field?
          auto_key = pk.find do |key|
            attribute = arel.ast.relation[key]
            column = column_for_attribute(attribute)
            if column.respond_to?(:auto_increment?)
              column.auto_increment?
            end
          end

          if auto_key
            result[pk.index(auto_key)] = last_inserted_id(value)
          end
          result
        else
          last_inserted_id(value)
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
composite_primary_keys-13.0.8 lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb
composite_primary_keys-14.0.6 lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb
composite_primary_keys-13.0.7 lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb
composite_primary_keys-13.0.6 lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb
composite_primary_keys-14.0.5 lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb
composite_primary_keys-13.0.5 lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb
composite_primary_keys-13.0.4 lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb
composite_primary_keys-14.0.4 lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb
composite_primary_keys-14.0.3 lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb
composite_primary_keys-13.0.3 lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb
composite_primary_keys-14.0.2 lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb
composite_primary_keys-13.0.2 lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb
composite_primary_keys-14.0.1 lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb
composite_primary_keys-13.0.1 lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb
composite_primary_keys-13.0.0 lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb
composite_primary_keys-12.0.10 lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb