lib/pg_conn.rb in pg_conn-0.13.0 vs lib/pg_conn.rb in pg_conn-0.13.1
- old
+ new
@@ -545,16 +545,18 @@
r&.values
end
end
end
- # Insert record(s) in a table
+ # Insert record(s) in a table. Returns the id of the inserted record(s)
def insert(schema = nil, table, array_or_hash)
table = [schema, table].compact.join(".")
- array = array_or_hash.is_a?(Array) ? array_or_hash : [array_or_hash]
+ is_array = array_or_hash.is_a?(Array)
+ array = is_array ? array_or_hash : [array_or_hash]
identifiers = quote_identifier_list(array.first.keys)
literals = array.map { |hash| quote_literal_list(hash.values) }.join(", ")
- exec %(insert into #{table} #{identifiers} values #{literals})
+ method = (is_array ? :values : :value)
+ self.send method, %(insert into #{table} #{identifiers} values #{literals} returning id)
end
# Update record(s)
def update(schema = nil, table, expr, hash)
table = [schema, table].compact.join(".")