pg_conn 0.26.1 → 0.27.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6dc75936f3c9bd5db772a2f8a80aa8e8bb25cbd1ae2181492766a7ed6ab9104a
4
- data.tar.gz: 8ac094777a20c074f4a018b4bb0ea53efff219eb2401e18bf9c5887721fbb1fe
3
+ metadata.gz: 735772bb654f60ebbd736fb3cd13cb9af8dfe00a3b796d5447457adea435dddd
4
+ data.tar.gz: 6ee452ddb1a6e49d46b735b27d8a2f106959637d4e4373bf33d5ec72db757b9f
5
5
  SHA512:
6
- metadata.gz: 8651be5de05b08d991cc257dab9cc060c4dfe22f8c22b2489f7e9ecc3654d51c55ad8494287b1cb829dc92a50cf60eb777b8814bcc23b348143caf9339729a8c
7
- data.tar.gz: 8c11d1e8cb3ceb859438e9ccb98fe38de9ed9126c449c58972cef97d9fb3108c87ea2a18899aea9d500eaa249625d0a1a783176e3fd4e8bc7ebe035cec31f67b
6
+ metadata.gz: 4bf460966a99794fc05a134638acca681da79700ec1be145fcde276bcad2070c1a743fcf8ee8fde6c7d65a07d21026ef96b2cedb24e54b8fa61aa80ce59694ca
7
+ data.tar.gz: fa3e00167e8323f410def0d42c0e568f0185c90939e3891033a6a7e55dcdd5a0b0f50a5343f95c5f3ca2b0bba22312fd65fef25bc323f26956dfca3b9f45844a
@@ -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.1"
2
+ VERSION = "0.27.0"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -250,7 +250,7 @@ module PgConn
250
250
  # or a symbol
251
251
  def quote_identifier(s)
252
252
  s = s.to_s if s.is_a?(Symbol)
253
- @pg_connection.escape_identifier(s)
253
+ @pg_connection.escape_identifier(s).gsub(/\./, '"."').sub(/"\*"/, "*")
254
254
  end
255
255
 
256
256
  # Quote identifiers and concatenate them using ',' as separator
@@ -339,15 +339,8 @@ module PgConn
339
339
  #
340
340
  # Return true iff the query returns exactly one record. Use '!empty?' to
341
341
  # check if the query returns one or more records
342
- def exist?(*args)
343
- arg1, arg2 = *args
344
- query =
345
- case arg2
346
- when Integer; "select from #{arg1} where id = #{arg2}"
347
- when String; "select from #{arg1} where #{arg2}"
348
- when NilClass; arg1
349
- end
350
- count(query) == 1
342
+ def exist?(*query)
343
+ !empty?(*query)
351
344
  end
352
345
 
353
346
  # :call-seq:
@@ -355,14 +348,9 @@ module PgConn
355
348
  # count(table, where_clause = nil)
356
349
  #
357
350
  # Return true if the table or the result of the query is empty
358
- def empty?(arg, where_clause = nil)
359
- if arg =~ /\s/
360
- value "select count(*) from (#{arg} limit 1) as inner_query"
361
- elsif where_clause
362
- value "select count(*) from (select 1 from #{arg} where #{where_clause} limit 1) as inner_query"
363
- else
364
- value "select count(*) from (select 1 from #{arg} limit 1) as inner_query"
365
- end == 0
351
+ def empty?(*query)
352
+ inner_query = parse_query *query
353
+ self.value("select count(*) from (#{inner_query} limit 1) as \"inner_query\"") == 0
366
354
  end
367
355
 
368
356
  # :call-seq:
@@ -370,27 +358,20 @@ module PgConn
370
358
  # count(table_name, where_clause = nil)
371
359
  #
372
360
  # The number of records in the table or in the query
373
- def count(arg, where_clause = nil)
374
- if arg =~ /\s/
375
- value("select count(*) from (#{arg}) as inner_query")
376
- else
377
- value("select count(*) from #{arg}" + (where_clause ? " where #{where_clause}" : ""))
378
- end
361
+ def count(*query)
362
+ inner_query = parse_query *query
363
+ value("select count(*) from (#{inner_query}) as inner_query")
379
364
  end
380
365
 
381
- # TODO
382
- # Query variants
383
- # (sql) - simple SQL statement
384
- # (schema = nil, table, id-or-where-clause = nil, field-or-fields)
385
- #
386
-
387
366
  # Return a single value. It is an error if the query doesn't return a
388
- # single record with a single column. If :transaction is true, the query
389
- # will be executed in a transaction and also be committed if :commit is
390
- # true (this is the default). It can also be used to execute 'insert'
391
- # statements with a 'returning' clause
392
- def value(query) #, transaction: false, commit: true)
393
- r = pg_exec(query)
367
+ # single record with a single column.
368
+ #
369
+ # TODO If :transaction is true, the query will be executed in a
370
+ # transaction and also be committed if :commit is true (this is the
371
+ # default). It can also be used to execute 'insert' statements with a
372
+ # 'returning' clause
373
+ def value(*query) #, transaction: false, commit: true)
374
+ r = pg_exec(parse_query *query)
394
375
  check_1c(r)
395
376
  check_1r(r)
396
377
  r.values[0][0]
@@ -398,8 +379,8 @@ module PgConn
398
379
 
399
380
  # Like #value but returns nil if no record was found. It is still an error
400
381
  # if the query returns more than one column
401
- def value?(query) #, transaction: false, commit: true)
402
- r = pg_exec(query)
382
+ def value?(*query) #, transaction: false, commit: true)
383
+ r = pg_exec(parse_query *query)
403
384
  check_1c(r)
404
385
  return nil if r.ntuples == 0
405
386
  check_1r(r)
@@ -407,28 +388,32 @@ module PgConn
407
388
  end
408
389
 
409
390
  # Return an array of values. It is an error if the query returns records
410
- # with more than one column. If :transaction is true, the query will be
411
- # executed in a transaction and be committed it :commit is true (the
412
- # default). This can be used in 'insert ... returning ...' statements
413
- def values(query)
414
- r = pg_exec(query)
391
+ # with more than one column.
392
+ #
393
+ # TODO If :transaction is true, the query will be executed in a
394
+ # transaction and be committed it :commit is true (the default). This can
395
+ # be used in 'insert ... returning ...' statements
396
+ def values(*query)
397
+ r = pg_exec(parse_query *query)
415
398
  check_1c(r)
416
399
  r.column_values(0)
417
400
  end
418
401
 
419
402
  # Return an array of column values. It is an error if the query returns
420
- # more than one record. If :transaction is true, the query will be executed
403
+ # more than one record.
404
+ #
405
+ # TODO If :transaction is true, the query will be executed
421
406
  # in a transaction and be committed it :commit is true (the default). This
422
407
  # can be used in 'insert ... returning ...' statements
423
- def tuple(query)
424
- r = pg_exec(query)
408
+ def tuple(*query)
409
+ r = pg_exec(parse_query *query)
425
410
  check_1r(r)
426
411
  r.values[0]
427
412
  end
428
413
 
429
414
  # Like #tuple but returns nil if no record was found
430
- def tuple?(query)
431
- r = pg_exec(query)
415
+ def tuple?(*query)
416
+ r = pg_exec(parse_query *query)
432
417
  return nil if r.ntuples == 0
433
418
  check_1r(r)
434
419
  r.values[0]
@@ -437,24 +422,24 @@ module PgConn
437
422
  # Return an array of tuples. If :transaction is true, the query will be
438
423
  # executed in a transaction and be committed it :commit is true (the
439
424
  # default). This can be used in 'insert ... returning ...' statements
440
- def tuples(query)
441
- pg_exec(query).values
425
+ def tuples(*query)
426
+ pg_exec(parse_query *query).values
442
427
  end
443
428
 
444
429
  # Return a single-element hash from column name to value. It is an error
445
430
  # if the query returns more than one record or more than one column. Note
446
- # that you will probably prefer to use #value instead when you expect only
447
- # a single field
448
- def field(query)
449
- r = pg_exec(query)
431
+ # that you will probably prefer to use #value instead when you query a
432
+ # single field
433
+ def field(*query)
434
+ r = pg_exec(parse_query *query)
450
435
  check_1c(r)
451
436
  check_1r(r)
452
437
  r.tuple(0).to_h
453
438
  end
454
439
 
455
440
  # Like #field but returns nil if no record was found
456
- def field?(query)
457
- r = pg_exec(query)
441
+ def field?(*query)
442
+ r = pg_exec(parse_query *query)
458
443
  check_1c(r)
459
444
  return nil if r.ntuples == 0
460
445
  check_1r(r)
@@ -465,8 +450,8 @@ module PgConn
465
450
  # is an error if the query returns records with more than one column. Note
466
451
  # that you will probably prefer to use #values instead when you expect only
467
452
  # single-column records
468
- def fields(query)
469
- r = pg_exec(query)
453
+ def fields(*query)
454
+ r = pg_exec(parse_query *query)
470
455
  check_1c(r)
471
456
  r.each.to_a.map(&:to_h)
472
457
  end
@@ -474,43 +459,43 @@ module PgConn
474
459
  # Return a hash from column name (a Symbol) to field value. It is an error if
475
460
  # the query returns more than one record. It blows up if a column name is
476
461
  # not a valid ruby symbol
477
- def record(query)
478
- r = pg_exec(query)
462
+ def record(*query)
463
+ r = pg_exec(parse_query *query)
479
464
  check_1r(r)
480
465
  r.tuple(0).to_h
481
466
  end
482
467
 
483
468
  # Like #record but returns nil if no record was found
484
- def record?(query)
485
- r = pg_exec(query)
469
+ def record?(*query)
470
+ r = pg_exec(parse_query *query)
486
471
  return nil if r.ntuples == 0
487
472
  check_1r(r)
488
473
  r.tuple(0).to_h
489
474
  end
490
475
 
491
476
  # Return an array of hashes from column name to field value
492
- def records(query)
493
- r = pg_exec(query)
477
+ def records(*query)
478
+ r = pg_exec(parse_query *query)
494
479
  r.each.to_a.map(&:to_h)
495
480
  end
496
481
 
497
482
  # Return a record as a OpenStruct object. It is an error if the query
498
483
  # returns more than one record. It blows up if a column name is not a valid
499
484
  # ruby symbol
500
- def struct(query)
501
- OpenStruct.new(**record(query))
485
+ def struct(*query)
486
+ OpenStruct.new(**record(parse_query *query))
502
487
  end
503
488
 
504
489
  # Like #struct but returns nil if no record was found
505
- def struct?(query)
506
- args = record?(query)
490
+ def struct?(*query)
491
+ args = record?(parse_query *query)
507
492
  return nil if args.nil?
508
493
  OpenStruct.new(**args)
509
494
  end
510
495
 
511
496
  # Return an array of OpenStruct objects
512
- def structs(query)
513
- records(query).map { |record| OpenStruct.new(**record) }
497
+ def structs(*query)
498
+ records(parse_query *query).map { |record| OpenStruct.new(**record) }
514
499
  end
515
500
 
516
501
  # Return a hash from the record id column to record (hash from column name
@@ -996,6 +981,35 @@ module PgConn
996
981
  end
997
982
  end
998
983
 
984
+ # :call-seq
985
+ # parse_query(query)
986
+ # parse_query(table, id_or_where_clause = "true", fields = [])
987
+ #
988
+ # Parse a query. Used in query-functions (#value etc.). The fields argument
989
+ # can be a list of fields or arrays of fields
990
+ #
991
+ def parse_query(*args)
992
+ args.size > 0 or raise ArgumentError
993
+ return args.first if args.size == 1 && args.first =~ / /
994
+
995
+ table = args.shift
996
+ where_clause = "true"
997
+ fields = []
998
+ case args.first
999
+ when Integer; where_clause = "id = #{args.shift}"
1000
+ when String; where_clause = args.shift
1001
+ when Symbol; fields << args.shift
1002
+ when Array; fields = args.shift
1003
+ when nil; where_clause = "true"
1004
+ else
1005
+ raise ArgumentError
1006
+ end
1007
+ fields.concat args.flatten
1008
+ field_list = fields.empty? ? "*" : self.quote_identifiers(fields)
1009
+
1010
+ "select #{field_list} from #{table} where #{where_clause}"
1011
+ end
1012
+
999
1013
  # :call-seq:
1000
1014
  # pg_exec(string)
1001
1015
  # 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.1
4
+ version: 0.27.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-11-17 00:00:00.000000000 Z
11
+ date: 2024-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg