pg_conn 0.26.2 → 0.27.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pg_conn/version.rb +1 -1
  3. data/lib/pg_conn.rb +91 -70
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 419e3bd17f4343be6860d172864ad0eeaaed032ab72cc659a02afa14914cdf65
4
- data.tar.gz: 00d673f75424491bd322cf1321cc8afbb67eba1d955a9545b4f7a0df334bc0b8
3
+ metadata.gz: 01735ada71fba436bf8387651722d380b68bfa9c028666946d343e22f94fb1ea
4
+ data.tar.gz: de757bfe82276b1641c693f93afcc630bcdea37d1bb33d65888652fdce6f3656
5
5
  SHA512:
6
- metadata.gz: 9e732a7523af59a16052603766aeea731d3d54a4f36fbd250f2dea33b85d1bc890af2fb7de4f8efb96d816eed2ecce390fb4510224715f78bd75a693c4243062
7
- data.tar.gz: f1b0b073a7231a68a5e0e3b255523c50000db9d50e24596a7545d9228b84bc69056a70b4c8111f899ad6852990ac6b84dc646f2e369c7cb4fbe77f77615d65e7
6
+ metadata.gz: d15f076992d6bf1eaa05b5ba8b7f3a3cc7ca4150543449717ecf1a94f16d08d71602e55384dedb13fba4ab06465a8dc1ae1e109ef698cf848d0264243d49e1c5
7
+ data.tar.gz: 69b9a3cbd175d20e4b806a18124a008eff5ef33bdcfd4d3b482d50cbba20ce95f9684a407b1122cd7dee2464b96496b2803d22c93b2c6190ba52332248a8de4d
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.26.2"
2
+ VERSION = "0.27.1"
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,42 @@ module PgConn
996
981
  end
997
982
  end
998
983
 
984
+ # :call-seq
985
+ # parse_query(query)
986
+ # parse_query(table, id, fields...)
987
+ # parse_query(table, where_clause, fields...)
988
+ # parse_query(table, hash, fields...)
989
+ # parse_query(table, fields...)
990
+ #
991
+ # Parse a query. Used in query-functions (#value etc.). The fields argument
992
+ # can be a list of fields or arrays of fields
993
+ #
994
+ def parse_query(*args)
995
+ args.size > 0 or raise ArgumentError
996
+ return args.first if args.size == 1 && args.first =~ / /
997
+
998
+ table = args.shift
999
+ where_clause = "true"
1000
+ fields = []
1001
+ case args.first
1002
+ when Integer; where_clause = "id = #{args.shift}"
1003
+ when String; where_clause = args.shift
1004
+ when Hash
1005
+ where_clause = args.shift.map { |k,v|
1006
+ "#{self.quote_identifier(k)} = #{self.quote_value(v) }"
1007
+ }.join(" and ")
1008
+ when Symbol; fields << args.shift
1009
+ when Array; fields = args.shift
1010
+ when nil; where_clause = "true"
1011
+ else
1012
+ raise ArgumentError
1013
+ end
1014
+ fields.concat args.flatten
1015
+ field_list = fields.empty? ? "*" : self.quote_identifiers(fields)
1016
+
1017
+ "select #{field_list} from #{table} where #{where_clause}"
1018
+ end
1019
+
999
1020
  # :call-seq:
1000
1021
  # pg_exec(string)
1001
1022
  # 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.2
4
+ version: 0.27.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-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