pg_conn 0.12.0 → 0.13.0

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: '006583d902e2254d449d5275be8e91412f2d272ae6ce8de927a1f0bec87e1f05'
4
- data.tar.gz: 4a983af740d9032ff6856fffea989510d3c00452dea075312b1c2c9c9857eaef
3
+ metadata.gz: 7c975d085fc3e3f58a72fbb13d0e5ce9428254969e8723c8af0d6577381f0f70
4
+ data.tar.gz: 6a786d3466d2c0661b22ee738212a3b8a37afca86a99dbde59773e5d47b7a5f5
5
5
  SHA512:
6
- metadata.gz: c1ba88b850bb8abd1d0073b4be090033d1fbaabd51177f62476c511ab8dbf73867e657bd1daf8952d784253adb2d168109dba4dcc47105c4038bba03025cb533
7
- data.tar.gz: 8402eb677b0c1b24dbea124c48862e25dd39430585a0b44969383c78548c256267ca2c06d6755ea77e6e591b4209e74c5eb5e99f97af864c474aa17afcd1f37c
6
+ metadata.gz: 6710fbd23379b8e1b13bd41dda8ce206fcefca30c93f43f2be0f4b7e437d854d50c89aa429cb70f52901eb7d4b515a810012801adc334b2082307dcc4ca2aaa9
7
+ data.tar.gz: 838a84b8f67710abcee3381f3691e93fab21c9d07ec48efa1480faec5c4b7621a5904d1000f2a62666c998f30b499de06c74eada70fac6609ea601beabf7b976
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.12.0"
2
+ VERSION = "0.13.0"
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
@@ -226,8 +226,11 @@ module PgConn
226
226
  end
227
227
  end
228
228
 
229
- # Quote value as an identifier. Value should be a non-nil string
230
- def quote_identifier(s) = @pg_connection.escape_identifier(s)
229
+ # Quote value as an identifier. Value should be a non-nil string or a symbol
230
+ def quote_identifier(s)
231
+ s = s.to_s if s.is_a?(Symbol)
232
+ String@pg_connection.escape_identifier(s)
233
+ end
231
234
 
232
235
  # Quote the value as a string. Emit 'null' if value is nil
233
236
  #
@@ -250,7 +253,7 @@ module PgConn
250
253
  end
251
254
 
252
255
  # Quote array as a parenthesis-enclosed list of identifiers
253
- def quote_identifier_list(values) = "(#{values.map { quote_identifier(_1) }.join(', ')})"
256
+ def quote_identifier_list(identifiers) = "(#{identifiers.map { quote_identifier(_1) }.join(', ')})"
254
257
 
255
258
  # Quote array as a parenthesis-enclosed list of literals
256
259
  def quote_literal_list(values) = "(#{values.map { quote_literal(_1) }.join(', ')})"
@@ -544,6 +547,30 @@ module PgConn
544
547
  end
545
548
  end
546
549
 
550
+ # Insert record(s) in a table
551
+ def insert(schema = nil, table, array_or_hash)
552
+ table = [schema, table].compact.join(".")
553
+ array = array_or_hash.is_a?(Array) ? array_or_hash : [array_or_hash]
554
+ identifiers = quote_identifier_list(array.first.keys)
555
+ literals = array.map { |hash| quote_literal_list(hash.values) }.join(", ")
556
+ exec %(insert into #{table} #{identifiers} values #{literals})
557
+ end
558
+
559
+ # Update record(s)
560
+ def update(schema = nil, table, expr, hash)
561
+ table = [schema, table].compact.join(".")
562
+ assignments = hash.map { |k,v| "#{k} = #{quote_literal(v)}" }.join(", ")
563
+ constraint = (expr.is_a?(String) ? expr : "id = #{quote_literal(expr)}")
564
+ exec %(update #{table} set #{assignments} where #{constraint})
565
+ end
566
+
567
+ # Delete record(s)
568
+ def delete(schema = nil, table, expr)
569
+ table = [schema, table].compact.join(".")
570
+ constraint = (expr.is_a?(String) ? expr : "id = #{quote_literal(expr)}")
571
+ exec %(delete from #{table} where #{constraint})
572
+ end
573
+
547
574
  # Execute SQL statement(s) in a transaction and return the number of
548
575
  # affected records (if any). Also sets #timestamp unless a transaction is
549
576
  # 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.0
4
+ version: 0.13.0
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