]> git.openstreetmap.org Git - rails.git/blob
65fce48f6aa31d31c7ea72822b6fe7acbbdbb9d6
[rails.git] /
1 module ActiveRecord
2   module ConnectionAdapters
3     class PostgreSQLAdapter < AbstractAdapter
4       
5       # This mightn't be in Core, but count(distinct x,y) doesn't work for me
6       def supports_count_distinct? #:nodoc:
7         false
8       end
9
10       def concat(*columns)
11         columns = columns.map { |c| "CAST(#{c} AS varchar)" }
12         "(#{columns.join('||')})"
13       end
14       
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('"', '')
19
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
23           if pk
24             quoted_pk = if pk.is_a?(Array)
25                           pk.map { |col| quote_column_name(col) }.join(ID_SEP)
26                         else
27                           quote_column_name(pk)
28                         end
29             id = select_value("#{sql} RETURNING #{quoted_pk}")
30             clear_query_cache
31             return id
32           end
33         end
34
35         # Otherwise, insert then grab last_insert_id.
36         if insert_id = super
37           insert_id
38         else
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)
42           end
43
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)
48           end
49         end
50       end
51     end
52   end
53 end