pg_conn 0.17.3 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/TODO +4 -1
  3. data/lib/pg_conn/version.rb +1 -1
  4. data/lib/pg_conn.rb +55 -12
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ef63ee68c793ec597f74abe4f85815b489199f5f71db4e92f583e3a7789b58ef
4
- data.tar.gz: 073d54f4b5000882ce86b6b09ae92b4fa8d2230015198ad07f54d0b6ed465c03
3
+ metadata.gz: f7b067cc4d9e831d6c30c135a351a05362953d75414d888a9c012105117dfcd1
4
+ data.tar.gz: 17eeef29e2844094019bf16890a98cade8b788f2465eb63797a46cdac66d3b93
5
5
  SHA512:
6
- metadata.gz: 52d717e25fbbe755c2bceaa77c1da02bfbe82dabc30f0ce22673f1791b9fbfe50cc0f6539221c766db526d42f224baeb1da22f29cba861ada5ea8334413b2899
7
- data.tar.gz: 54fe0f036e61099b2fd5807da67e1528930ce79386a91f570eb11ba9d6eac5d5c63a89e4c2702b5304a9680cecf904577860ef9fa251a4485d344fea31b61cdd
6
+ metadata.gz: d4e77389acba0ebdd70d4e22440db2449129184bec8a775019d9e8e5a1e601990b276be89482286f4e24c7a35767f3aeba8293a6e2de9832600a16183dfe97db
7
+ data.tar.gz: 348441a6c040059ad68f648146c468d52e914841411f282efde924cee707d97ef7616092612b7d82844a0ededbb05e8bc33122cf408458f040cd9701214ddebc
data/TODO CHANGED
@@ -1,6 +1,9 @@
1
1
  TODO
2
- o Create aliases
2
+ o Have a 'with' method that combines multiple brachet-methods:
3
+
4
+ conn.with(schema: public, transation: true) { ... }
3
5
 
6
+ o Create aliases
4
7
  tuple -> array
5
8
  tuples arrays
6
9
  field
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.17.3"
2
+ VERSION = "0.18.0"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -327,8 +327,8 @@ module PgConn
327
327
  # Return a single value. It is an error if the query doesn't return a
328
328
  # single record with a single column. If :transaction is true, the query
329
329
  # will be executed in a transaction and also be committed if :commit is
330
- # true (this is the default). It can be used to execute 'insert' statements
331
- # with a 'returning' clause
330
+ # true (this is the default). It can also be used to execute 'insert'
331
+ # statements with a 'returning' clause
332
332
  def value(query) #, transaction: false, commit: true)
333
333
  r = pg_exec(query)
334
334
  check_1c(r)
@@ -567,16 +567,59 @@ module PgConn
567
567
  end
568
568
  end
569
569
 
570
- # Insert record(s) in a table. Returns the id of the inserted record(s)
571
- def insert(schema = nil, table, array_or_hash)
572
- return [] if array_or_hash.empty?
573
- table = [schema, table].compact.join(".")
574
- is_array = array_or_hash.is_a?(Array)
575
- array = is_array ? array_or_hash : [array_or_hash]
576
- identifiers = quote_identifier_list(array.first.keys)
577
- literals = array.map { |hash| quote_literal_list(hash.values) }.join(", ")
578
- method = (is_array ? :values : :value)
579
- self.send method, %(insert into #{table} #{identifiers} values #{literals} returning id)
570
+ # :call-seq*
571
+ # insert(table, record|records)
572
+ # insert(table, fields, record|records|tuples)
573
+ # insert(schema, table, record|records)
574
+ # insert(schema, table, fields, record|records|tuples)
575
+ #
576
+ # Insert record(s) in table and return id(s)
577
+ #
578
+ # There is no variant that takes a single tuple because it would then be
579
+ # impossible to have array or hash field values
580
+ def insert(*args)
581
+ # Add schema (=nil) if absent
582
+ args.unshift nil if args.size == 2 || (args.size == 3 && args[1].is_a?(Array))
583
+
584
+ # Add fields (=nil) if absent
585
+ args.insert(-2, nil) if !args[-2].is_a?(Array)
586
+
587
+ # Check number of arguments
588
+ args.size == 4 or raise ArgumentError, "Illegal number of arguments"
589
+
590
+ # Extract parameters
591
+ schema, table, fields, data = args
592
+
593
+ # Normalize table
594
+ table = schema ? "#{schema}.#{table}" : table
595
+
596
+ # Find method and normalize data
597
+ if data.is_a?(Array)
598
+ if data.empty?
599
+ return []
600
+ elsif data.first.is_a?(Array)
601
+ method = :values
602
+ fields or raise ArgumentError
603
+ tuples = data
604
+ elsif data.first.is_a?(Hash)
605
+ method = :values
606
+ fields ||= data.first.keys
607
+ tuples = data.map { |record| fields.map { |field| record[field] } }
608
+ else
609
+ raise ArgumentError
610
+ end
611
+ elsif data.is_a?(Hash)
612
+ method = :value
613
+ fields ||= data.keys
614
+ tuples = [fields.map { |field| data[field] }]
615
+ else
616
+ raise ArgumentError
617
+ end
618
+
619
+ # Build and execute SQL statement
620
+ columns = quote_identifier_list(fields)
621
+ values = tuples.map { |tuple| quote_literal_list(tuple) }.join(', ')
622
+ self.send method, %(insert into #{table} #{columns} values #{values} returning id)
580
623
  end
581
624
 
582
625
  # Update record(s)
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.17.3
4
+ version: 0.18.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-05-10 00:00:00.000000000 Z
11
+ date: 2024-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg