Class: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb

Instance Method Summary collapse

Instance Method Details

#concat(*columns) ⇒ Object



10
11
12
13
# File 'lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb', line 10

def concat(*columns)
  columns = columns.map { |c| "CAST(#{c} AS varchar)" }
  "(#{columns.join('||')})"
end

#insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object

Executes an INSERT query and returns the new record’s ID



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb', line 16

def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
  # Extract the table from the insert sql. Yuck.
  table = sql.split(" ", 4)[2].gsub('"', '')

  # Try an insert with 'returning id' if available (PG >= 8.2)
  if supports_insert_with_returning?
    pk, sequence_name = *pk_and_sequence_for(table) unless pk
    if pk
      quoted_pk = if pk.is_a?(Array)
                    pk.map { |col| quote_column_name(col) }.join(ID_SEP)
                  else
                    quote_column_name(pk)
                  end
      id = select_value("#{sql} RETURNING #{quoted_pk}")
      clear_query_cache
      return id
    end
  end

  # Otherwise, insert then grab last_insert_id.
  if insert_id = super
    insert_id
  else
    # If neither pk nor sequence name is given, look them up.
    unless pk || sequence_name
      pk, sequence_name = *pk_and_sequence_for(table)
    end

    # If a pk is given, fallback to default sequence name.
    # Don't fetch last insert id for a table without a pk.
    if pk && sequence_name ||= default_sequence_name(table, pk)
      last_insert_id(table, sequence_name)
    end
  end
end

#supports_count_distinct?Boolean

This mightn’t be in Core, but count(distinct x,y) doesn’t work for me

Returns:

  • (Boolean)


6
7
8
# File 'lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb', line 6

def supports_count_distinct? #:nodoc:
  false
end