Sha256: 291a4211baf07db7fc11e276ab2eb955237701ad3d78e5f521c7e3cedd1352f1

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 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
          # CPK
          # select_value("#{sql} RETURNING #{quote_column_name(pk)}")
          select_value("#{sql} RETURNING #{quote_column_names(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

        # CPK
        # sql = "#{sql} RETURNING #{quote_column_name(pk)}" if pk
        sql = "#{sql} RETURNING #{quote_column_names(pk)}" if pk

        [sql, binds]
      end

      # Returns a single value if query returns a single element
      # otherwise returns an array coresponding 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

      module Quoting
        def quote_column_name(name) #:nodoc:
          # CPK
          # PGconn.quote_ident(name.to_s)
          if name.is_a?(Array)
            name.map do |column|
              PGconn.quote_ident(column.to_s)
            end.join(', ')
          else
            PGconn.quote_ident(name.to_s)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
composite_primary_keys-7.0.16 lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb