pg_conn 0.6.1 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pg_conn/rdbms_methods.rb +10 -8
- data/lib/pg_conn/version.rb +1 -1
- data/lib/pg_conn.rb +42 -21
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 515c790d7c2850a3d679f3b56601a8cd7311eeeb7c8875f23e7e24a9b1d3389a
|
4
|
+
data.tar.gz: '08b7e60560b80649d613e7469c7d544b136c5cff789d9f107eeb83a56e4449ff'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aee7f2323e2bd7cb4893780210161c7839dd985e33dec5d1c93b6318c4f3c03494ef2108031fe0aff0e83352c94bb6c5a421830ac7e8972cd23d830159a61f08
|
7
|
+
data.tar.gz: c4ad3b31c34a19ce3c697ab9cdb318f9aee9752c7eb3bc598588da48923d488908b26c324fd29cf6381d60d08e249920a99671324b84434776fbdedeb85ad5f1
|
@@ -44,7 +44,7 @@ module PgConn
|
|
44
44
|
conn.values stmt
|
45
45
|
end
|
46
46
|
|
47
|
-
# Return the owner of
|
47
|
+
# Return the owner of a given database
|
48
48
|
def owner(database)
|
49
49
|
conn.value %(
|
50
50
|
select r.rolname
|
@@ -54,13 +54,13 @@ module PgConn
|
|
54
54
|
)
|
55
55
|
end
|
56
56
|
|
57
|
-
# Return list of users currently logged in to the database or to any
|
57
|
+
# Return list of users currently logged in to the given database or to any
|
58
58
|
# database if database is nil
|
59
59
|
#
|
60
|
-
# FIXME: There is a possible race-condition here where some process
|
61
|
-
#
|
62
|
-
# easy fix is to have usename not null but it would be nice to know
|
63
|
-
# exactly is triggering this problem
|
60
|
+
# FIXME: There is a possible race-condition here where some process (eg.
|
61
|
+
# auto-vacuum) is logged in to the database but has a nil username. The
|
62
|
+
# easy fix is to have 'usename is not null' but it would be nice to know
|
63
|
+
# what exactly is triggering this problem
|
64
64
|
#
|
65
65
|
def users(database)
|
66
66
|
database_clause = database ? "datname = '#{database}'" : nil
|
@@ -69,7 +69,7 @@ module PgConn
|
|
69
69
|
end
|
70
70
|
|
71
71
|
# Hollow-out a database by removing all schemas in the database. The public
|
72
|
-
# schema is recreated afterwards if :public is
|
72
|
+
# schema is recreated afterwards unless if :public is false. Uses the current
|
73
73
|
# database if @database is nil
|
74
74
|
#
|
75
75
|
# Note that the database can have active users logged in while the database
|
@@ -85,9 +85,11 @@ module PgConn
|
|
85
85
|
.select { |schema| !exclude.include?(schema) }
|
86
86
|
.join(", ")
|
87
87
|
conn.exec "drop schema #{schemas} cascade"
|
88
|
+
|
89
|
+
# FIXME SECURITY Why grant 'create' to public?
|
88
90
|
conn.exec %(
|
89
91
|
create schema public authorization postgres;
|
90
|
-
grant usage, create on schema public to public
|
92
|
+
grant usage, create on schema public to public
|
91
93
|
) if public
|
92
94
|
ensure
|
93
95
|
conn&.terminate if local
|
data/lib/pg_conn/version.rb
CHANGED
data/lib/pg_conn.rb
CHANGED
@@ -49,8 +49,7 @@ module PgConn
|
|
49
49
|
def name() @pg_connection.db end
|
50
50
|
alias_method :database, :name # Obsolete
|
51
51
|
|
52
|
-
# Database manipulation methods: #exist?, #create, #drop, #list
|
53
|
-
# named 'rdbms' because #database is already defined
|
52
|
+
# Database manipulation methods: #exist?, #create, #drop, #list
|
54
53
|
attr_reader :rdbms
|
55
54
|
|
56
55
|
# Role manipulation methods: #exist?, #create, #drop, #list
|
@@ -64,6 +63,22 @@ module PgConn
|
|
64
63
|
# #exec or #transaction block
|
65
64
|
attr_reader :timestamp
|
66
65
|
|
66
|
+
# PG::Error object if the last statement failed; otherwise nil
|
67
|
+
attr_reader :err
|
68
|
+
|
69
|
+
# Last error message. The error message is the first line of the PG error
|
70
|
+
# message that may contain additional info. It doesn't contain a
|
71
|
+
# terminating newline
|
72
|
+
def errmsg = err&.message =~ /^ERROR:\s*(.*?)\n/m && $1.capitalize
|
73
|
+
|
74
|
+
# The one-based line number of the last PG::Error or nil if absent in the
|
75
|
+
# Postgres error message
|
76
|
+
def errline = err&.message =~ /\n\s*LINE\s+(\d+):/m && $1.to_i
|
77
|
+
|
78
|
+
# The one-based character number of the error in the last PG::Error or nil
|
79
|
+
# if absent in the Postgres error message
|
80
|
+
def errchar = err&.message =~ /\n(\s*LINE\s+\d+: ).*?\n(\s+)\^\n/m && ($2.size - $1.size + 1)
|
81
|
+
|
67
82
|
# :call-seq:
|
68
83
|
# initialize(dbname = nil, user = nil, field_name_class: Symbol)
|
69
84
|
# initialize(connection_hash, field_name_class: Symbol)
|
@@ -79,10 +94,6 @@ module PgConn
|
|
79
94
|
# of <key>=<value> pairs with the same keys as the hash, or a URI with the
|
80
95
|
# format 'postgres[ql]://[user[:password]@][host][:port][/name]
|
81
96
|
#
|
82
|
-
# The :field_name_class option controls the Ruby type of column names. It can be
|
83
|
-
# Symbol (the default) or String. The :timestamp option is used
|
84
|
-
# internally to set the timestamp for transactions
|
85
|
-
#
|
86
97
|
# If given an array argument, PgConn will not connect to the database and
|
87
98
|
# instead write its commands to the array. In this case, methods extracting
|
88
99
|
# values from the database (eg. #value) will return nil or raise an
|
@@ -93,6 +104,10 @@ module PgConn
|
|
93
104
|
# recommended except in cases where you want to piggyback on an existing
|
94
105
|
# connection (eg. a Rails connection)
|
95
106
|
#
|
107
|
+
# The :field_name_class option controls the Ruby type of column names. It can be
|
108
|
+
# Symbol (the default) or String. The :timestamp option is used
|
109
|
+
# internally to set the timestamp for transactions
|
110
|
+
#
|
96
111
|
# Note that the connection hash and the connection string may support more
|
97
112
|
# parameters than documented here. Consult
|
98
113
|
# https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
|
@@ -478,9 +493,10 @@ module PgConn
|
|
478
493
|
|
479
494
|
# Execute SQL statement(s) in a transaction and return the number of
|
480
495
|
# affected records (if any). Also sets #timestamp unless a transaction is
|
481
|
-
# already in progress. The +sql+ argument can be a String or an
|
482
|
-
# nested array of
|
483
|
-
#
|
496
|
+
# already in progress. The +sql+ argument can be a command (String) or an
|
497
|
+
# arbitrarily nested array of commands. Note that you can't have commands
|
498
|
+
# that span multiple lines. The empty array is a NOP but the empty string
|
499
|
+
# is not.
|
484
500
|
#
|
485
501
|
# #exec pass Postgres exceptions to the caller unless :fail is false. If
|
486
502
|
# fail is false #exec instead return nil but note that postgres doesn't
|
@@ -495,10 +511,10 @@ module PgConn
|
|
495
511
|
|
496
512
|
# Execute SQL statement(s) without a transaction block and return the
|
497
513
|
# number of affected records (if any). This used to call procedures that
|
498
|
-
# may manipulate transactions. The +sql+ argument can be a
|
499
|
-
# an arbitrarily nested array of
|
500
|
-
# empty string is not. #
|
501
|
-
# :fail is false in which case it returns nil
|
514
|
+
# may manipulate transactions. The +sql+ argument can be a SQL command or
|
515
|
+
# an arbitrarily nested array of commands. The empty array is a NOP but the
|
516
|
+
# empty string is not. #execute pass Postgres exceptions to the caller
|
517
|
+
# unless :fail is false in which case it returns nil
|
502
518
|
#
|
503
519
|
# TODO: Handle postgres exceptions wrt transaction state and stack
|
504
520
|
def execute(sql, fail: true, silent: false)
|
@@ -589,7 +605,7 @@ module PgConn
|
|
589
605
|
push_transaction
|
590
606
|
result = yield
|
591
607
|
rescue PgConn::Rollback
|
592
|
-
|
608
|
+
pop_transaction(commit: false)
|
593
609
|
return nil
|
594
610
|
# FIXME: Rescue other postgres errors and wipe-out stack
|
595
611
|
end
|
@@ -626,25 +642,27 @@ module PgConn
|
|
626
642
|
PG::Connection.new *args, **opts
|
627
643
|
end
|
628
644
|
end
|
629
|
-
|
645
|
+
|
630
646
|
# :call-seq:
|
631
647
|
# pg_exec(string)
|
632
648
|
# pg_exec(array)
|
633
649
|
#
|
634
|
-
#
|
635
|
-
# concatenated before being sent to the
|
636
|
-
# object or nil if +arg+ was empty.
|
637
|
-
# caller unless :fail is false
|
650
|
+
# Execute statement(s) on the server. If the argument is an array of
|
651
|
+
# commands, the commands are concatenated with ';' before being sent to the
|
652
|
+
# server. #pg_exec returns a PG::Result object or nil if +arg+ was empty.
|
653
|
+
# #exec pass Postgres exceptions to the caller unless :fail is false
|
638
654
|
#
|
639
655
|
# FIXME: Error message prints the last statement but what if another
|
640
656
|
# statement failed?
|
641
657
|
#
|
642
|
-
# TODO
|
643
|
-
#
|
658
|
+
# TODO: Connsider executing statements one-by-one so we're able to
|
659
|
+
# pin-point Postgres errors without a line number. This may be expensive,
|
660
|
+
# though
|
644
661
|
#
|
645
662
|
# TODO: Fix silent by not handling exceptions
|
646
663
|
def pg_exec(arg, fail: true, silent: false)
|
647
664
|
if @pg_connection
|
665
|
+
@err = nil
|
648
666
|
begin
|
649
667
|
last_stmt = nil # To make the current SQL statement visible to the rescue clause. FIXME Not used?
|
650
668
|
if arg.is_a?(String)
|
@@ -659,9 +677,11 @@ module PgConn
|
|
659
677
|
end
|
660
678
|
|
661
679
|
rescue PG::Error => ex
|
680
|
+
@err = ex
|
662
681
|
if fail
|
663
682
|
if !silent # FIXME Why do we handle this?
|
664
683
|
$stderr.puts arg
|
684
|
+
$stderr.puts
|
665
685
|
$stderr.puts ex.message
|
666
686
|
$stderr.flush
|
667
687
|
end
|
@@ -670,6 +690,7 @@ module PgConn
|
|
670
690
|
return nil
|
671
691
|
end
|
672
692
|
end
|
693
|
+
|
673
694
|
else # @pg_commands is defined
|
674
695
|
if arg.is_a?(String)
|
675
696
|
@pg_commands << arg if arg != ""
|
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.
|
4
|
+
version: 0.7.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-01-
|
11
|
+
date: 2024-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pg
|