Sha256: 58918ef61d70962682c022264857ad8021783af8e347a522d2fc286bd2e4beb4

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

module ActiveRecord
  module ConnectionAdapters
    class PostgreSQLAdapter
      def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
        unless pk
          # Extract the table from the insert sql. Yuck.
          table_ref = extract_table_ref_from_insert_sql(sql)
          pk = primary_key(table_ref) if table_ref
        end

        if pk && use_insert_returning?
          # CPK
          # select_value("#{sql} RETURNING #{quote_column_names(pk)}")
          select_value("#{sql} RETURNING #{quote_column_names(pk)}")
        elsif pk
          super
          last_insert_id_value(sequence_name || default_sequence_name(table_ref, pk))
        else
          super
        end
      end
      alias :create :insert

      def sql_for_insert(sql, pk, id_value, sequence_name, binds)
        unless pk
          # Extract the table from the insert sql. Yuck.
          table_ref = extract_table_ref_from_insert_sql(sql)
          pk = primary_key(table_ref) if table_ref
        end

        if pk && use_insert_returning?
          # CPK
          # sql = "#{sql} RETURNING #{quote_column_names(pk)}"
          sql = "#{sql} RETURNING #{quote_column_names(pk)}"
        end

        [sql, binds]
      end

      # Returns a single value if query returns a single element
      # otherwise returns an array corresponding to the composite keys
      def last_inserted_id(result)
        row = result && result.rows.first
        if Array === row
          row.size == 1 ? row[0] : row
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
composite_primary_keys-9.0.4 lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb
composite_primary_keys-9.0.2 lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb
composite_primary_keys-9.0.1 lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb
composite_primary_keys-9.0.0 lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb
composite_primary_keys-9.0.0.beta1 lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb