pg_conn 0.26.0 → 0.26.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4148bbd8c21613249beed07d5248aa71780a8805881afc94899f9f29cd2d452d
4
- data.tar.gz: 21bea1c8c108a4a135eb1d6936a56995b457db0e8192a17b745d0b3dd1676846
3
+ metadata.gz: 419e3bd17f4343be6860d172864ad0eeaaed032ab72cc659a02afa14914cdf65
4
+ data.tar.gz: 00d673f75424491bd322cf1321cc8afbb67eba1d955a9545b4f7a0df334bc0b8
5
5
  SHA512:
6
- metadata.gz: 7cca5aea976d949607b1650e8f00347a22be14907c8296a26379cd804a51f5610bb9786bc21b3b9317496a00ef135564033f276d5e3300dbd4d133b056835a79
7
- data.tar.gz: 8bcf303b9712c1e6de69c301115c88a53d908aad90c8bce6b8e73e912d6b710772fb88a6bb191bc34142ca29f016845bfbc035cda4670c91d5e3d407ce82907e
6
+ metadata.gz: 9e732a7523af59a16052603766aeea731d3d54a4f36fbd250f2dea33b85d1bc890af2fb7de4f8efb96d816eed2ecce390fb4510224715f78bd75a693c4243062
7
+ data.tar.gz: f1b0b073a7231a68a5e0e3b255523c50000db9d50e24596a7545d9228b84bc69056a70b4c8111f899ad6852990ac6b84dc646f2e369c7cb4fbe77f77615d65e7
@@ -16,14 +16,14 @@ module PgConn
16
16
  else
17
17
  conn.tuples %(
18
18
  select datname, usename
19
- from pg_stat_activity
19
+ from pg_stat_activity
20
20
  where datname is not null and usename is not null
21
21
  )
22
22
  end
23
23
  end
24
24
 
25
25
  # Return true if the given database accepts connections
26
- def enabled?(database)
26
+ def enabled?(database)
27
27
  !database.nil? or raise ArgumentError
28
28
  conn.value "select datallowconn from pg_catalog.pg_database where datname = '#{database}'"
29
29
  end
@@ -50,19 +50,21 @@ module PgConn
50
50
  # multiple sessions (is this ever relevant?)
51
51
  #
52
52
  def terminate(database, *users)
53
+ users = Array(users).flatten
53
54
  !database.nil? or raise ArgumentError
54
55
  enabled = self.enabled?(database)
55
56
 
56
57
  case users
57
- when [];
58
+ when [];
58
59
  return
59
60
  when [nil]
60
61
  self.disable(database) if enabled
61
62
  users = self.list(database)
62
- else
63
+ else
63
64
  users = Array(users).flatten
64
65
  end
65
66
  pids = self.pids(database, users)
67
+
66
68
  if !pids.empty?
67
69
  pids_sql = pids.map { |pid| "(#{pid})" }.join(", ")
68
70
  conn.execute "select pg_terminate_backend(pid) from ( values #{pids_sql} ) as x(pid)"
@@ -71,7 +73,7 @@ module PgConn
71
73
  end
72
74
 
73
75
  # Run block without any connected users. Existing sessions are terminated
74
- def exclusive(database, &block)
76
+ def exclusive(database, &block)
75
77
  !database.nil? or raise ArgumentError
76
78
  begin
77
79
  disable(database)
@@ -85,7 +87,7 @@ module PgConn
85
87
  # Return true if session triggers are enabled. Triggers are enabled by
86
88
  # default by Postgres
87
89
  def triggers?() conn.value "select current_setting('session_replication_role') <> 'replica'" end
88
-
90
+
89
91
  # Enable session triggers
90
92
  def enable_triggers()
91
93
  conn.execute "set session session_replication_role = DEFAULT"
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.26.0"
2
+ VERSION = "0.26.2"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -315,41 +315,21 @@ module PgConn
315
315
  end
316
316
 
317
317
  # Quote a record and cast it into the given type, the type can also be a
318
- # table or view. 'data' is a hash or struct representation of the record
318
+ # table or view. 'data' is an array, hash, or struct representation of the
319
+ # record
319
320
  #
320
321
  # Note that the fields are retrived from the database so this method is not
321
322
  # as fast as the other quote-methods. It is however very convenient when
322
323
  # you're testing and need a composite type because record-quoting can
323
324
  # easily become unwieldly
324
- def quote_record(schema_name = nil, type, data, elem_types: nil)
325
- qual_name = [schema_name, type].compact .join('.')
326
-
327
- fields = self.values(%(
328
- select attname
329
- from pg_attribute
330
- where
331
- attrelid = '#{qual_name}'::regclass
332
- and attnum > 0
333
- and not attisdropped
334
- order by attnum
335
- )).map(&:to_sym)
336
-
337
- values =
338
- case data
339
- when Hash; fields.map { |f| data[f] }
340
- when OpenStruct; fields.map { |f| data.send(f) }
341
- when Array; data
342
- else
343
- raise Error, "Illegal value #{data.inspect}"
344
- end
345
-
346
- "(#{quote_tuple(values, elem_types: elem_types)})::#{qual_name}"
325
+ def quote_record(data, schema_name = nil, type, elem_types: nil)
326
+ quote_record_impl(data, schema_name, type, elem_types: elem_types, array: false)
347
327
  end
348
328
 
349
- def quote_records(schema_name = nil, type, datas, elem_types: nil)
350
- qual_name = [schema_name, type].compact .join('.') + "[]"
351
- records = datas.map { |data| quote_record(schema_name, type, data, elem_types: elem_types) }
352
- "array[#{records.join(", ")}]::#{qual_name}"
329
+ # Quote an array of records. The type is the record type, not the type of
330
+ # the enclosing array
331
+ def quote_records(data, schema_name = nil, type, elem_types: nil)
332
+ quote_record_impl(data, schema_name, type, elem_types: elem_types, array: true)
353
333
  end
354
334
 
355
335
  # :call-seq:
@@ -980,6 +960,42 @@ module PgConn
980
960
  end
981
961
  end
982
962
 
963
+ # Common implementation for #quote_record and #quote_records that avoids
964
+ # query the database multiple times while not duplication the code. the
965
+ # :array flag controls the mode
966
+ def quote_record_impl(datas, schema_name = nil, type, elem_types: nil, array: nil)
967
+ pg_type = [schema_name, type].compact.join('.')
968
+ fields = self.values(%(
969
+ select attname
970
+ from pg_attribute
971
+ where
972
+ attrelid = '#{pg_type}'::regclass
973
+ and attnum > 0
974
+ and not attisdropped
975
+ order by attnum
976
+ )).map(&:to_sym)
977
+
978
+ datas = [datas] if !array
979
+
980
+ literals = datas.map { |data|
981
+ values =
982
+ case data
983
+ when Hash; fields.map { |f| data[f] }
984
+ when OpenStruct; fields.map { |f| data.send(f) }
985
+ when Array; data
986
+ else
987
+ raise Error, "Illegal value #{data.inspect}"
988
+ end
989
+ "(#{quote_tuple(values, elem_types: elem_types)})::#{pg_type}"
990
+ }
991
+
992
+ if array
993
+ "array[#{literals.join(', ')}]::#{pg_type}[]"
994
+ else
995
+ literals.first
996
+ end
997
+ end
998
+
983
999
  # :call-seq:
984
1000
  # pg_exec(string)
985
1001
  # pg_exec(array)
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.26.0
4
+ version: 0.26.2
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-11-16 00:00:00.000000000 Z
11
+ date: 2024-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg