2 module ConnectionAdapters
3 class PostgreSQLAdapter < AbstractAdapter
5 # This mightn't be in Core, but count(distinct x,y) doesn't work for me
6 def supports_count_distinct? #:nodoc:
11 columns = columns.map { |c| "CAST(#{c} AS varchar)" }
12 "(#{columns.join('||')})"
15 # Executes an INSERT query and returns the new record's ID
16 def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
17 # Extract the table from the insert sql. Yuck.
18 table = sql.split(" ", 4)[2].gsub('"', '')
20 # Try an insert with 'returning id' if available (PG >= 8.2)
21 if supports_insert_with_returning?
22 pk, sequence_name = *pk_and_sequence_for(table) unless pk
24 quoted_pk = if pk.is_a?(Array)
25 pk.map { |col| quote_column_name(col) }.join(ID_SEP)
29 id = select_value("#{sql} RETURNING #{quoted_pk}")
35 # Otherwise, insert then grab last_insert_id.
39 # If neither pk nor sequence name is given, look them up.
40 unless pk || sequence_name
41 pk, sequence_name = *pk_and_sequence_for(table)
44 # If a pk is given, fallback to default sequence name.
45 # Don't fetch last insert id for a table without a pk.
46 if pk && sequence_name ||= default_sequence_name(table, pk)
47 last_insert_id(table, sequence_name)