pg_conn 0.17.3 → 0.19.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 +23 -1
  3. data/lib/pg_conn/version.rb +1 -1
  4. data/lib/pg_conn.rb +73 -13
  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: 91bfceabfdba86eba789108cea2718e382fe6849b580ef3be3a428a8f08ab45e
4
+ data.tar.gz: 0014ae1ccc9c6cf95f63fce78d5a094b7068e711ed18d0edcdaafe35d47ef04b
5
5
  SHA512:
6
- metadata.gz: 52d717e25fbbe755c2bceaa77c1da02bfbe82dabc30f0ce22673f1791b9fbfe50cc0f6539221c766db526d42f224baeb1da22f29cba861ada5ea8334413b2899
7
- data.tar.gz: 54fe0f036e61099b2fd5807da67e1528930ce79386a91f570eb11ba9d6eac5d5c63a89e4c2702b5304a9680cecf904577860ef9fa251a4485d344fea31b61cdd
6
+ metadata.gz: 6302b2051fd7a152b026ed7a412ad9025ae7fbdb6f260173c5731c123f367c7535a9522e2654ccc09950d14c9e3009c1daf6d52fa7fc3114c3b9f196e6db0570
7
+ data.tar.gz: c793534b923e08cc22121dc824c858003cb495e2453cb77cd7007672e242a62ff8819ab966f2f9d70a552f3a080da2627c1ec1211990eefcbe87bdcc1a5c06e8
data/TODO CHANGED
@@ -1,6 +1,28 @@
1
1
  TODO
2
- o Create aliases
2
+ o db.context(schema: app_portal, transaction: true) { ... }
3
+ db.context(set: app_portal, transaction: true) { ... }
4
+ db.context(add: app_portal, transaction: true) { ... }
5
+
6
+ o In the doc; compare a VisualBasic call to a function with PgConn's:
7
+
8
+ Set cmd = Server.CreateObject("ADODB.Command")
9
+ cmd.CommandText = "sp_nic_update_comtext"
10
+ cmd.CommandType = adCmdStoredProc
11
+ cmd.Parameters.Append cmd.CreateParameter("Id", adGUID, adParamInput, ,strId)
12
+ cmd.Parameters.Append cmd.CreateParameter("comtext", adVarWChar, adParamInput,2048 ,strComtext)
13
+ cmd.Parameters.Append cmd.CreateParameter("UserId", adGUID, adParamInput, , Site.CurrentUser.Id.ToString())
14
+ cnn.ExecuteCommandNonQuery cmd
15
+
16
+ vs
17
+
18
+ server.call :sp_nic_update_comtext, str_id, str_comtext, site.current_user.id
3
19
 
20
+
21
+ o Have a 'with' method that combines multiple brachet-methods:
22
+
23
+ conn.with(schema: public, transation: true) { ... }
24
+
25
+ o Create aliases
4
26
  tuple -> array
5
27
  tuples arrays
6
28
  field
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.17.3"
2
+ VERSION = "0.19.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)
@@ -507,7 +507,7 @@ module PgConn
507
507
  # Returns a hash from the first field to a tuple of the remaining fields.
508
508
  # If there is only one remaining field then that value is used instead of a
509
509
  # tuple of that value. The optional +key+ argument sets the mapping field
510
- def map(query, key = nil)
510
+ def map(query, key = nil) # TODO Swap arguments
511
511
  r = pg_exec(query)
512
512
  begin
513
513
  key = (key || r.fname(0)).to_s
@@ -525,6 +525,23 @@ module PgConn
525
525
  h
526
526
  end
527
527
 
528
+ def multimap(query, key = nil)
529
+ r = pg_exec(query)
530
+ begin
531
+ key = (key || r.fname(0)).to_s
532
+ key_index = r.fnumber(key.to_s)
533
+ one = (r.nfields == 2)
534
+ rescue ArgumentError
535
+ raise Error, "Can't find column #{key}"
536
+ end
537
+ h = {}
538
+ r.each_row { |row|
539
+ key_value = row.delete_at(key_index)
540
+ (h[key_value] ||= []) << (one ? row.first : row)
541
+ }
542
+ h
543
+ end
544
+
528
545
  # Return the value of calling the given function (which can be a String or
529
546
  # a Symbol and can contain the schema of the function). It dynamically
530
547
  # detects the structure of the result and return a value or an array of
@@ -567,16 +584,59 @@ module PgConn
567
584
  end
568
585
  end
569
586
 
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)
587
+ # :call-seq*
588
+ # insert(table, record|records)
589
+ # insert(table, fields, record|records|tuples)
590
+ # insert(schema, table, record|records)
591
+ # insert(schema, table, fields, record|records|tuples)
592
+ #
593
+ # Insert record(s) in table and return id(s)
594
+ #
595
+ # There is no variant that takes a single tuple because it would then be
596
+ # impossible to have array or hash field values
597
+ def insert(*args)
598
+ # Add schema (=nil) if absent
599
+ args.unshift nil if args.size == 2 || (args.size == 3 && args[1].is_a?(Array))
600
+
601
+ # Add fields (=nil) if absent
602
+ args.insert(-2, nil) if !args[-2].is_a?(Array)
603
+
604
+ # Check number of arguments
605
+ args.size == 4 or raise ArgumentError, "Illegal number of arguments"
606
+
607
+ # Extract parameters
608
+ schema, table, fields, data = args
609
+
610
+ # Normalize table
611
+ table = schema ? "#{schema}.#{table}" : table
612
+
613
+ # Find method and normalize data
614
+ if data.is_a?(Array)
615
+ if data.empty?
616
+ return []
617
+ elsif data.first.is_a?(Array)
618
+ method = :values
619
+ fields or raise ArgumentError
620
+ tuples = data
621
+ elsif data.first.is_a?(Hash)
622
+ method = :values
623
+ fields ||= data.first.keys
624
+ tuples = data.map { |record| fields.map { |field| record[field] } }
625
+ else
626
+ raise ArgumentError
627
+ end
628
+ elsif data.is_a?(Hash)
629
+ method = :value
630
+ fields ||= data.keys
631
+ tuples = [fields.map { |field| data[field] }]
632
+ else
633
+ raise ArgumentError
634
+ end
635
+
636
+ # Build and execute SQL statement
637
+ columns = quote_identifier_list(fields)
638
+ values = tuples.map { |tuple| quote_literal_list(tuple) }.join(', ')
639
+ self.send method, %(insert into #{table} #{columns} values #{values} returning id)
580
640
  end
581
641
 
582
642
  # 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.19.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-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg