pg_conn 0.40.0 → 0.41.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 +4 -4
- data/lib/pg_conn/version.rb +1 -1
- data/lib/pg_conn.rb +60 -15
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3c3ec749b3c1c7937e8c4fb0869c9ae3ef20549585f5385b44b1bf1f59275f02
|
|
4
|
+
data.tar.gz: cc620bc609a52ccf03d1976c8c13d5c37cb5d6de1d5edacb44b1a5fb3e334f98
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f58e940330fe87d38ece2ed98516b2e0ba21b5c14c6f3f561bd3339278176182bd5201e5529d3071a6318c02b8e6f5031a9be7b8b491af0e95ac299c09b8769f
|
|
7
|
+
data.tar.gz: fa5b3de455427cf6885c2d2757121465e312280f17a22da665fef5d232baa2406b0a14b2babd8ae6ebe22f04bb1d8f4d029d3f93c79be9c58b6ea16d6f9029be
|
data/lib/pg_conn/version.rb
CHANGED
data/lib/pg_conn.rb
CHANGED
|
@@ -209,7 +209,31 @@ module PgConn
|
|
|
209
209
|
def debug?() !debug.nil? end
|
|
210
210
|
def debug=(value) set_option(:debug, value) end
|
|
211
211
|
|
|
212
|
-
|
|
212
|
+
# Log SQL or return current logger if sql is nil
|
|
213
|
+
def log(sql = nil)
|
|
214
|
+
case sql and dst = @options[:log]
|
|
215
|
+
when false; # do nothing
|
|
216
|
+
when true; $stderr.puts sql
|
|
217
|
+
when Proc; dst.call sql
|
|
218
|
+
when IO; dst.puts sql
|
|
219
|
+
when StringIO; dst.puts sql
|
|
220
|
+
when nil; return @options[:log]
|
|
221
|
+
else
|
|
222
|
+
raise "Oops"
|
|
223
|
+
end
|
|
224
|
+
sql # for convenience in #pg_exec
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# Return true if logging is enabled
|
|
228
|
+
def log?() @options[:log] != false end
|
|
229
|
+
|
|
230
|
+
# Control logging of SQL commands. It can be assigned true, false, nil, an
|
|
231
|
+
# unary Proc object, or a IO object. True causes the message to be printed
|
|
232
|
+
# to standard error, false ignores it, and nil resets the state to the
|
|
233
|
+
# default given when the connection was initialized. Default false
|
|
234
|
+
def log=(value) set_option(:log, value) end
|
|
235
|
+
|
|
236
|
+
DEFAULT_OPTIONS = { silent: false, warning: true, notice: false, info: false, debug: false, log: false }
|
|
213
237
|
|
|
214
238
|
# TODO: Move error message handling into the same framework as notice and
|
|
215
239
|
# warning but we have a name collision just below that would need to be
|
|
@@ -863,12 +887,12 @@ module PgConn
|
|
|
863
887
|
end
|
|
864
888
|
|
|
865
889
|
# Execute SQL statement using either :value or :values depending on data arity
|
|
866
|
-
self.send method,
|
|
890
|
+
self.send method, <<~SQL
|
|
867
891
|
insert into #{table} (#{quote_identifiers(fields)})
|
|
868
892
|
values #{quote_tuples(tuples)}
|
|
869
893
|
#{upsert_sql}
|
|
870
894
|
returning id
|
|
871
|
-
|
|
895
|
+
SQL
|
|
872
896
|
end
|
|
873
897
|
|
|
874
898
|
# Use upsert. Currently only 'on conflict do nothing' is supported
|
|
@@ -912,22 +936,25 @@ module PgConn
|
|
|
912
936
|
# very useful in RSpec tests
|
|
913
937
|
#
|
|
914
938
|
# Local options are :search_path that runs the block with the given
|
|
915
|
-
# schemas, :username that runs the block as the given user,
|
|
916
|
-
# that runs the block in a transaction if true or false; true
|
|
917
|
-
# transaction and false rolls it back. It is not run in a
|
|
918
|
-
# :commit is nil
|
|
939
|
+
# schemas, :username that runs the block as the given user, :log that controls logging,
|
|
940
|
+
# SQL, :commit that runs the block in a transaction if true or false; true
|
|
941
|
+
# commits the transaction and false rolls it back. It is not run in a
|
|
942
|
+
# transaction if :commit is nil
|
|
919
943
|
#
|
|
920
944
|
def with(**options, &block)
|
|
921
945
|
search_path = options.delete(:search_path)
|
|
922
946
|
username = options.delete(:username)
|
|
947
|
+
logging = options.delete(:log) || false
|
|
923
948
|
commit = options.delete(:commit)
|
|
924
949
|
|
|
925
950
|
saved_options = @options.dup
|
|
926
951
|
saved_search_path = self.search_path if search_path
|
|
952
|
+
saved_logger = self.log if logging
|
|
927
953
|
|
|
928
954
|
begin
|
|
929
955
|
set_options(options)
|
|
930
956
|
self.search_path = search_path if search_path
|
|
957
|
+
self.log = logging
|
|
931
958
|
|
|
932
959
|
inner = lambda {
|
|
933
960
|
if !commit.nil?
|
|
@@ -945,6 +972,7 @@ module PgConn
|
|
|
945
972
|
inner.call
|
|
946
973
|
end
|
|
947
974
|
ensure
|
|
975
|
+
self.log = saved_logger if logging
|
|
948
976
|
self.search_path = saved_search_path if search_path
|
|
949
977
|
set_options(saved_options)
|
|
950
978
|
end
|
|
@@ -1236,7 +1264,18 @@ module PgConn
|
|
|
1236
1264
|
datas = [datas] if !array
|
|
1237
1265
|
|
|
1238
1266
|
pg_type = [schema_name, type].compact.join('.')
|
|
1239
|
-
|
|
1267
|
+
# fields = self.values(%(
|
|
1268
|
+
# select attname
|
|
1269
|
+
# from pg_attribute
|
|
1270
|
+
# where
|
|
1271
|
+
# attrelid = '#{pg_type}'::regclass
|
|
1272
|
+
# and attnum > 0
|
|
1273
|
+
# and not attisdropped
|
|
1274
|
+
# order by attnum
|
|
1275
|
+
# )).map(&:to_sym)
|
|
1276
|
+
|
|
1277
|
+
|
|
1278
|
+
sql = <<~SQL
|
|
1240
1279
|
select attname
|
|
1241
1280
|
from pg_attribute
|
|
1242
1281
|
where
|
|
@@ -1244,7 +1283,9 @@ module PgConn
|
|
|
1244
1283
|
and attnum > 0
|
|
1245
1284
|
and not attisdropped
|
|
1246
1285
|
order by attnum
|
|
1247
|
-
|
|
1286
|
+
SQL
|
|
1287
|
+
|
|
1288
|
+
fields = self.values(sql).map(&:to_sym)
|
|
1248
1289
|
|
|
1249
1290
|
literals = datas.map { |data|
|
|
1250
1291
|
values =
|
|
@@ -1320,12 +1361,16 @@ module PgConn
|
|
|
1320
1361
|
# Assign default
|
|
1321
1362
|
value = @default_options[option] if value.nil?
|
|
1322
1363
|
|
|
1364
|
+
# Set new value. Can be true, false, or a Proc object. The :log option
|
|
1365
|
+
# also allows an IO og StringIO object
|
|
1366
|
+
@options[option] = value
|
|
1367
|
+
|
|
1368
|
+
# Exit early if log
|
|
1369
|
+
return if option == :log
|
|
1370
|
+
|
|
1323
1371
|
# Find current message level
|
|
1324
1372
|
old_level = DEFAULT_PRODUCER.keys.find { |level| @producers[level] } || :error
|
|
1325
1373
|
|
|
1326
|
-
# Set new value. Can be true, false, or a Proc object
|
|
1327
|
-
@options[option] = value
|
|
1328
|
-
|
|
1329
1374
|
# Set producer
|
|
1330
1375
|
case option
|
|
1331
1376
|
when :silent
|
|
@@ -1404,17 +1449,17 @@ module PgConn
|
|
|
1404
1449
|
saved_silent = self.silent
|
|
1405
1450
|
self.silent = silent
|
|
1406
1451
|
|
|
1407
|
-
last_stmt = nil # To make the
|
|
1452
|
+
last_stmt = nil # To make the last (failed) SQL statement visible to the rescue clause. FIXME Not used?
|
|
1408
1453
|
if arg.is_a?(String)
|
|
1409
1454
|
return nil if arg == ""
|
|
1410
1455
|
last_stmt = arg
|
|
1411
|
-
@pg_connection.exec(
|
|
1456
|
+
@pg_connection.exec(log arg)
|
|
1412
1457
|
else
|
|
1413
1458
|
stmts = arg.flatten.compact
|
|
1414
1459
|
return nil if stmts.empty?
|
|
1415
1460
|
# stmts.unshift("set on_error_exit stop")
|
|
1416
1461
|
last_stmt = stmts.last
|
|
1417
|
-
@pg_connection.exec(stmts.join(";\n"))
|
|
1462
|
+
@pg_connection.exec(log stmts.join(";\n"))
|
|
1418
1463
|
end
|
|
1419
1464
|
rescue PG::Error => ex
|
|
1420
1465
|
if @error.nil?
|