pg_conn 0.13.0 → 0.13.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/pg_conn/version.rb +1 -1
- data/lib/pg_conn.rb +21 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a1eb7cce679245ec285129507e3d56948c2239b930ea18a757dd90c2e293404
|
4
|
+
data.tar.gz: c60efa88bb84530b91a58fd296781052b500968010808f9fa2c3dca4873db772
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de7cea3519b5dbd3763c24681ed917ae525fb87e359bc17461e0c950cc70ea47d3c79e230840d878a14533b7921a55384bf17235d32020787e22c37b8fb5e16c
|
7
|
+
data.tar.gz: 6dde3d8a9ea145db3e28b2467df50333042f3243da9d7b741e1195bb2d17856577541ff8df09744cc94517f9e02c7e6de44eba52fb3ba0ca363d0103edc89d13
|
data/lib/pg_conn/version.rb
CHANGED
data/lib/pg_conn.rb
CHANGED
@@ -547,27 +547,43 @@ module PgConn
|
|
547
547
|
end
|
548
548
|
end
|
549
549
|
|
550
|
-
# Insert record(s) in a table
|
550
|
+
# Insert record(s) in a table. Returns the id of the inserted record(s)
|
551
551
|
def insert(schema = nil, table, array_or_hash)
|
552
552
|
table = [schema, table].compact.join(".")
|
553
|
-
|
553
|
+
is_array = array_or_hash.is_a?(Array)
|
554
|
+
array = is_array ? array_or_hash : [array_or_hash]
|
554
555
|
identifiers = quote_identifier_list(array.first.keys)
|
555
556
|
literals = array.map { |hash| quote_literal_list(hash.values) }.join(", ")
|
556
|
-
|
557
|
+
method = (is_array ? :values : :value)
|
558
|
+
self.send method, %(insert into #{table} #{identifiers} values #{literals} returning id)
|
557
559
|
end
|
558
560
|
|
559
561
|
# Update record(s)
|
560
562
|
def update(schema = nil, table, expr, hash)
|
561
563
|
table = [schema, table].compact.join(".")
|
562
564
|
assignments = hash.map { |k,v| "#{k} = #{quote_literal(v)}" }.join(", ")
|
563
|
-
constraint =
|
565
|
+
constraint =
|
566
|
+
case expr
|
567
|
+
when String; expr
|
568
|
+
when Integer; "id = #{quote_literal(expr)}"
|
569
|
+
when Array; "id in #{quote_literal_list(expr)}"
|
570
|
+
else
|
571
|
+
raise ArgumentError
|
572
|
+
end
|
564
573
|
exec %(update #{table} set #{assignments} where #{constraint})
|
565
574
|
end
|
566
575
|
|
567
576
|
# Delete record(s)
|
568
577
|
def delete(schema = nil, table, expr)
|
569
578
|
table = [schema, table].compact.join(".")
|
570
|
-
constraint =
|
579
|
+
constraint =
|
580
|
+
case expr
|
581
|
+
when String; expr
|
582
|
+
when Integer; "id = #{quote_literal(expr)}"
|
583
|
+
when Array; "id in #{quote_literal_list(expr)}"
|
584
|
+
else
|
585
|
+
raise ArgumentError
|
586
|
+
end
|
571
587
|
exec %(delete from #{table} where #{constraint})
|
572
588
|
end
|
573
589
|
|