pg_conn 0.55.0 → 0.56.1

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: c7a478850b588c635e78fd50cac46e52665ece96fc538eb467eb2bf88fe6d6d7
4
- data.tar.gz: 8882d70f0289f8b3cdec8c9e3135513788eb09416a0ceb5ad763849f71324d95
3
+ metadata.gz: 26dbcd85db043fdf28c698606f7f35450d77bf0cbafb63324d62297724a4698f
4
+ data.tar.gz: 59444453e3ca2b4c6244d356696264f7caef6f20038549edc33ad085e9339f08
5
5
  SHA512:
6
- metadata.gz: 5c3709874689cbfadf194fde8e4dd1d956ab0456ab205fd70064b680aa5dcebc161e11e5f3441237f502bf2c4df7cafa5a9a9b6608df42cd21ddc75848062c64
7
- data.tar.gz: b791c4afaefddc8286abf836abdbb6e92657bb5139bc377be032f174a6d2888dc774c8f8fd3c8643c6ca9f98f920d3f747d3163380c2fa85569a373ac30e45f0
6
+ metadata.gz: 5fddf126010d3cd13cefbbb8bf1a94402cee13987c967e17e346b32db9c02044c1e035ddd0f69d0c2a1fd91cdfedf8e0cbbf8da556c1c1588c6bb8bb5e28092c
7
+ data.tar.gz: aa082a3881474398e3c17c19bd219801f65596470b69d7046217d43093a511c06ef436e5f13189a602d97ca3ab3657d68582c4a53338f38b5f05810f126921d7
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.55.0"
2
+ VERSION = "0.56.1"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "pg"
2
2
  require 'ostruct'
3
+ require 'open3'
3
4
  require 'json'
4
5
 
5
6
  require_relative "pg_conn/version"
@@ -206,8 +207,8 @@ module PgConn
206
207
  def logger() = @logger
207
208
 
208
209
  # Controls error messages. It can be assigned true, false, nil, or a Proc
209
- # object that recieves the message. True causes the message to be printed
210
- # to standard error, false ignores it, and nil resets the state to the
210
+ # object that recieves the message. True causes the message to be ignored,
211
+ # false prints it to standard error, and nil resets the state to the
211
212
  # default given when the connection was initialized. #silent? returns true
212
213
  # if #silent is false or a Proc object and should be used instead #silent
213
214
  # to check the state because #silent returns truish when output is
@@ -1066,6 +1067,34 @@ module PgConn
1066
1067
  end
1067
1068
  end
1068
1069
 
1070
+ # Execute SQL or pgsql file. The file must be pure SQL or pgsql statements,
1071
+ # not psql meta commands. Any output from the file is ignored
1072
+ def sqlexec(file, **opts)
1073
+ source = IO.read(file) or error "Can't read #{file}"
1074
+ exec(source, **opts)
1075
+ end
1076
+
1077
+ # Execute file using psql(1) so that special psql meta commands (eg. \gset)
1078
+ # can be used. Return the output from the SQL file. The file is executed
1079
+ # with ON_ERROR_STOP on
1080
+ def psqlexec(file, fail: true, silent: self.silent)
1081
+ begin
1082
+ output, errors, status = Open3.capture3 %(
1083
+ psql -U #{username} -d #{database} --no-psqlrc -c '\\set ON_ERROR_STOP on' -f #{file}
1084
+ )
1085
+ status.success? or raise PG::Error.new(errors)
1086
+ output
1087
+ rescue PG::Error => ex
1088
+ if @error.nil?
1089
+ @error = ex
1090
+ @err = nil
1091
+ end
1092
+ $stderr.puts errors if !silent
1093
+ raise if fail
1094
+ nil
1095
+ end
1096
+ end
1097
+
1069
1098
  # Execute block with the given set of global or local options and reset
1070
1099
  # them afterwards
1071
1100
  #
@@ -1542,11 +1571,12 @@ module PgConn
1542
1571
  @err = nil
1543
1572
  end
1544
1573
 
1545
- # Process message before resetting silent state
1574
+ # Process message
1546
1575
  message_processor(@error.message, arg)
1547
- self.silent = saved_silent
1548
1576
 
1549
1577
  raise
1578
+ ensure
1579
+ self.silent = saved_silent
1550
1580
  end
1551
1581
 
1552
1582
  # For dump of SQL statements
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_conn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.55.0
4
+ version: 0.56.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen