pg_conn 0.12.1 → 0.13.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d38593bbd3985871d0047ac1c40a093136f34555b051b8c2bb454549cc6634a
4
- data.tar.gz: f72a7ab003ee1450eb7aa654bb7b2ba6234d10f25aeefa90c37df0d9d7b189cd
3
+ metadata.gz: 30f37deaa02a7ce6c10c42be3b5432ac3e80c21fd82ec8a0d33d10cfc29ad904
4
+ data.tar.gz: b78696618951f214e39f5e075ad1d657e86d0d5dda691b5f9b081e51b45b40a1
5
5
  SHA512:
6
- metadata.gz: 2ec6bbd626ab5bfff53639ad774102c5d1ad273b24cb7ad9a3d6c3c2f8852f53c095d3d83451eb6c343ac71b43da7162153bcbd8e9da5a681cb689969786ae6a
7
- data.tar.gz: bfa053c3720fc5ffbd52d2d6c0cc763675a21322810a17f3cf95abafef868671eefa6dbb15347b847d2892e1884ee210b19c1827310f17174c402e3a2fa59d81
6
+ metadata.gz: 68bab843ada37c8214bd7281bda915a6ec509dd337b4cae40384308d60408bfdc7de4abd10eefc94123248d438db4f01942ba7bdc2fd4e17f3dd28096e07aaac
7
+ data.tar.gz: 1a64027e97471f53ab114f617de495be2f4adab7e64cd30cf63b91a9a698462cae13531b990c3f96241d008e7a343b7163bcde8bd1d0371efb1935a741215384
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.12.1"
2
+ VERSION = "0.13.1"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -44,11 +44,11 @@ module PgConn
44
44
 
45
45
  # Name of user
46
46
  def user() @pg_connection.user end
47
- alias_method :username, :user # Obsolete
47
+ alias_method :username, :user # Obsolete FIXME Is it?
48
48
 
49
49
  # Name of database
50
50
  def name() @pg_connection.db end
51
- alias_method :database, :name # Obsolete
51
+ alias_method :database, :name # Obsolete FIXME Is it?
52
52
 
53
53
  # Database manipulation methods: #exist?, #create, #drop, #list
54
54
  attr_reader :rdbms
@@ -253,7 +253,7 @@ module PgConn
253
253
  end
254
254
 
255
255
  # Quote array as a parenthesis-enclosed list of identifiers
256
- def quote_identifier_list(values) = "(#{values.map { quote_identifier(_1) }.join(', ')})"
256
+ def quote_identifier_list(identifiers) = "(#{identifiers.map { quote_identifier(_1) }.join(', ')})"
257
257
 
258
258
  # Quote array as a parenthesis-enclosed list of literals
259
259
  def quote_literal_list(values) = "(#{values.map { quote_literal(_1) }.join(', ')})"
@@ -547,6 +547,32 @@ module PgConn
547
547
  end
548
548
  end
549
549
 
550
+ # Insert record(s) in a table. Returns the id of the inserted record(s)
551
+ def insert(schema = nil, table, array_or_hash)
552
+ table = [schema, table].compact.join(".")
553
+ is_array = array_or_hash.is_a?(Array)
554
+ array = is_array ? array_or_hash : [array_or_hash]
555
+ identifiers = quote_identifier_list(array.first.keys)
556
+ literals = array.map { |hash| quote_literal_list(hash.values) }.join(", ")
557
+ method = (is_array ? :values : :value)
558
+ self.send method, %(insert into #{table} #{identifiers} values #{literals} returning id)
559
+ end
560
+
561
+ # Update record(s)
562
+ def update(schema = nil, table, expr, hash)
563
+ table = [schema, table].compact.join(".")
564
+ assignments = hash.map { |k,v| "#{k} = #{quote_literal(v)}" }.join(", ")
565
+ constraint = (expr.is_a?(String) ? expr : "id = #{quote_literal(expr)}")
566
+ exec %(update #{table} set #{assignments} where #{constraint})
567
+ end
568
+
569
+ # Delete record(s)
570
+ def delete(schema = nil, table, expr)
571
+ table = [schema, table].compact.join(".")
572
+ constraint = (expr.is_a?(String) ? expr : "id = #{quote_literal(expr)}")
573
+ exec %(delete from #{table} where #{constraint})
574
+ end
575
+
550
576
  # Execute SQL statement(s) in a transaction and return the number of
551
577
  # affected records (if any). Also sets #timestamp unless a transaction is
552
578
  # already in progress. The +sql+ argument can be a command (String) or an
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_conn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.1
4
+ version: 0.13.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-25 00:00:00.000000000 Z
11
+ date: 2024-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg