pg_conn 0.2.1 → 0.3.3
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/Gemfile +0 -3
- data/TODO +2 -0
- data/lib/pg_conn/schema_methods.rb +21 -8
- data/lib/pg_conn/version.rb +1 -1
- data/lib/pg_conn.rb +53 -36
- data/pg_conn.gemspec +3 -23
- metadata +45 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: edc8c2d648a4ea373981633433cfc52ca94cdec159292f052a6ee5a077b56172
|
4
|
+
data.tar.gz: 7823404356129c95cfcb37869641a32a8982bd11da0d1d7f730893aa0f746701
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a539ed00e4a16a5ef9d3e400782d42000f19f578382e9721941b82c61cffb3f427856409b978224c42c2b533bb585df5e1c27765c73c1a9071809cc3c734358
|
7
|
+
data.tar.gz: c93dab2fa0404ffe61b02c4c1fbc0021d290dade58471cd6cabce8e74cbe2372a3b6c1f4a2fefd5eff82ffa4b545b71fb54851fd6f0fcf8abb9b53b16892324a
|
data/Gemfile
CHANGED
data/TODO
CHANGED
@@ -81,13 +81,18 @@ module PgConn
|
|
81
81
|
conn.values relation_list_query(schema, kind: %w(v m))
|
82
82
|
end
|
83
83
|
|
84
|
-
# Return a list of columns. If relation is defined, only columns from that
|
84
|
+
# Return a list of columns. If +relation+ is defined, only columns from that
|
85
85
|
# relation are listed. Columns are returned as fully qualified names (eg.
|
86
86
|
# "schema.relation.column")
|
87
87
|
def list_columns(schema, relation = nil)
|
88
88
|
conn.values column_list_query(schema, relation)
|
89
89
|
end
|
90
90
|
|
91
|
+
# Like #list_columns but returns a tuple of column UID and column type
|
92
|
+
def list_column_types(schema, relation = nil)
|
93
|
+
conn.tuples column_list_type_query(schema, relation)
|
94
|
+
end
|
95
|
+
|
91
96
|
def exist_function(schema, function, signature)
|
92
97
|
raise NotImplementedError
|
93
98
|
end
|
@@ -143,15 +148,23 @@ module PgConn
|
|
143
148
|
relation_clause
|
144
149
|
].compact.join(" and ")
|
145
150
|
end
|
151
|
+
|
152
|
+
def column_list_type_query(schema, relation)
|
153
|
+
relation_clause = relation ? "relname = '#{relation}'" : nil
|
154
|
+
[
|
155
|
+
%(
|
156
|
+
select '#{schema}' || '.' || c.relname || '.' || a.attname as "column",
|
157
|
+
a.atttypid::regtype::text as "type"
|
158
|
+
from pg_class c
|
159
|
+
join pg_attribute a on a.attrelid = c.oid
|
160
|
+
where relnamespace::regnamespace::text = 'public'
|
161
|
+
and a.attnum > 0
|
162
|
+
),
|
163
|
+
relation_clause
|
164
|
+
].compact.join(" and ")
|
165
|
+
end
|
146
166
|
end
|
147
167
|
end
|
148
168
|
|
149
169
|
|
150
170
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
data/lib/pg_conn/version.rb
CHANGED
data/lib/pg_conn.rb
CHANGED
@@ -19,7 +19,7 @@ module PgConn
|
|
19
19
|
def self.===(element) element.is_a?(PgConn::Connection) or super end
|
20
20
|
|
21
21
|
# Returns a PgConn::Connection object (aka. a PgConn object). It's arguments
|
22
|
-
# can be an existing connection that will just be returned
|
22
|
+
# can be an existing connection that will just be returned or a set of
|
23
23
|
# PgConn::Connection#initialize arguments that will be used to create a new
|
24
24
|
# PgConn::Connection object
|
25
25
|
def self.ensure(*args)
|
@@ -64,17 +64,16 @@ module PgConn
|
|
64
64
|
# #exec or #transaction block
|
65
65
|
attr_reader :timestamp
|
66
66
|
|
67
|
-
|
68
|
-
|
69
|
-
# Initialize a connection object and connect to the database. #initialize has five
|
70
|
-
# variations:
|
71
|
-
#
|
67
|
+
# :call-seq:
|
72
68
|
# initialize(dbname = nil, user = nil, field_name_class: Symbol)
|
73
69
|
# initialize(connection_hash, field_name_class: Symbol)
|
74
70
|
# initialize(connection_string, field_name_class: Symbol)
|
75
71
|
# initialize(host, port, dbname, user, password, field_name_class: Symbol)
|
72
|
+
# initialize(array, field_name_class: Symbol)
|
76
73
|
# initialize(pg_connection_object)
|
77
74
|
#
|
75
|
+
# Initialize a connection object and connect to the database
|
76
|
+
#
|
78
77
|
# The possible keys of the connection hash are :host, :port, :dbname, :user,
|
79
78
|
# and :password. The connection string can either be a space-separated list
|
80
79
|
# of <key>=<value> pairs with the same keys as the hash, or a URI with the
|
@@ -84,6 +83,11 @@ module PgConn
|
|
84
83
|
# Symbol (the default) or String. The :timestamp option is used
|
85
84
|
# internally to set the timestamp for transactions
|
86
85
|
#
|
86
|
+
# If given an array argument, PgConn will not connect to the database and
|
87
|
+
# instead write its commands to the array. In this case, methods extracting
|
88
|
+
# values from the database (eg. #value) will return nil or raise an
|
89
|
+
# exception
|
90
|
+
#
|
87
91
|
# The last variant is used to establish a PgConn from an existing
|
88
92
|
# connection. It doesn't change the connection settings and is not
|
89
93
|
# recommended except in cases where you want to piggyback on an existing
|
@@ -125,6 +129,9 @@ module PgConn
|
|
125
129
|
end
|
126
130
|
when Hash
|
127
131
|
make_connection **arg
|
132
|
+
when Array
|
133
|
+
@pg_commands = arg
|
134
|
+
nil
|
128
135
|
else
|
129
136
|
raise Error, "Illegal argument type: #{arg.class}"
|
130
137
|
end
|
@@ -136,7 +143,7 @@ module PgConn
|
|
136
143
|
raise Error, "Illegal number of parameters: #{args.size}"
|
137
144
|
end
|
138
145
|
|
139
|
-
if !using_existing_connection
|
146
|
+
if @pg_connection && !using_existing_connection
|
140
147
|
# Auto-convert to ruby types
|
141
148
|
type_map = PG::BasicTypeMapForResults.new(@pg_connection)
|
142
149
|
|
@@ -170,7 +177,7 @@ module PgConn
|
|
170
177
|
|
171
178
|
# Close the database connection
|
172
179
|
def terminate()
|
173
|
-
@pg_connection.close if !@pg_connection.finished?
|
180
|
+
@pg_connection.close if @pg_connection && !@pg_connection.finished?
|
174
181
|
end
|
175
182
|
|
176
183
|
def self.new(*args, &block)
|
@@ -178,7 +185,7 @@ module PgConn
|
|
178
185
|
begin
|
179
186
|
object = Connection.allocate
|
180
187
|
object.send(:initialize, *args)
|
181
|
-
yield(object) if object.pg_connection
|
188
|
+
yield(object) # if object.pg_connection
|
182
189
|
ensure
|
183
190
|
object.terminate if object.pg_connection
|
184
191
|
end
|
@@ -187,9 +194,6 @@ module PgConn
|
|
187
194
|
end
|
188
195
|
end
|
189
196
|
|
190
|
-
# # Return true iff the query returns exactly one value
|
191
|
-
# def exist?(query) count(query) == 1 end
|
192
|
-
|
193
197
|
# :call-seq:
|
194
198
|
# exist?(query)
|
195
199
|
# exist?(table, id)
|
@@ -459,7 +463,7 @@ module PgConn
|
|
459
463
|
elsif r.nfields == 1
|
460
464
|
r.column_values(0)
|
461
465
|
else
|
462
|
-
r
|
466
|
+
r&.values
|
463
467
|
end
|
464
468
|
end
|
465
469
|
|
@@ -489,7 +493,11 @@ module PgConn
|
|
489
493
|
#
|
490
494
|
# TODO: Handle postgres exceptions wrt transaction state and stack
|
491
495
|
def execute(sql, fail: true, silent: false)
|
492
|
-
|
496
|
+
if @pg_connection
|
497
|
+
pg_exec(sql, fail: fail, silent: silent)&.cmd_tuples
|
498
|
+
else
|
499
|
+
pg_exec(sql, fail: fail, silent: silent)
|
500
|
+
end
|
493
501
|
end
|
494
502
|
|
495
503
|
# Switch user to the given user and execute the statement before swithcing
|
@@ -534,7 +542,7 @@ module PgConn
|
|
534
542
|
else
|
535
543
|
@savepoints = []
|
536
544
|
pg_exec("begin")
|
537
|
-
@timestamp = pg_exec("select current_timestamp").values[0][0]
|
545
|
+
@timestamp = pg_exec("select current_timestamp").values[0][0] if @pg_connection
|
538
546
|
end
|
539
547
|
end
|
540
548
|
|
@@ -626,30 +634,39 @@ module PgConn
|
|
626
634
|
#
|
627
635
|
# TODO: Fix silent by not handling exceptions
|
628
636
|
def pg_exec(arg, fail: true, silent: false)
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
rescue PG::Error => ex
|
643
|
-
if fail
|
644
|
-
if !silent # FIXME Why do we handle this?
|
645
|
-
$stderr.puts arg
|
646
|
-
$stderr.puts ex.message
|
647
|
-
$stderr.flush
|
637
|
+
if @pg_connection
|
638
|
+
begin
|
639
|
+
last_stmt = nil # To make the current SQL statement visible to the rescue clause
|
640
|
+
if arg.is_a?(String)
|
641
|
+
return nil if arg == ""
|
642
|
+
last_stmt = arg
|
643
|
+
@pg_connection.exec(last_stmt)
|
644
|
+
else
|
645
|
+
stmts = arg.flatten.compact
|
646
|
+
return nil if stmts.empty?
|
647
|
+
last_stmt = stmts.last
|
648
|
+
@pg_connection.exec(stmts.join(";\n"))
|
648
649
|
end
|
649
|
-
|
650
|
+
|
651
|
+
rescue PG::Error => ex
|
652
|
+
if fail
|
653
|
+
if !silent # FIXME Why do we handle this?
|
654
|
+
$stderr.puts arg
|
655
|
+
$stderr.puts ex.message
|
656
|
+
$stderr.flush
|
657
|
+
end
|
658
|
+
raise
|
659
|
+
else
|
660
|
+
return nil
|
661
|
+
end
|
662
|
+
end
|
663
|
+
else # @pg_commands is defined
|
664
|
+
if arg.is_a?(String)
|
665
|
+
@pg_commands << arg if arg != ""
|
650
666
|
else
|
651
|
-
|
667
|
+
@pg_commands.concat(arg)
|
652
668
|
end
|
669
|
+
nil
|
653
670
|
end
|
654
671
|
end
|
655
672
|
|
data/pg_conn.gemspec
CHANGED
@@ -13,7 +13,6 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.homepage = "http://www.nowhere.com/"
|
14
14
|
spec.required_ruby_version = ">= 2.4.0"
|
15
15
|
|
16
|
-
|
17
16
|
spec.metadata["homepage_uri"] = spec.homepage
|
18
17
|
|
19
18
|
# Specify which files should be added to the gem when it is released.
|
@@ -27,28 +26,9 @@ Gem::Specification.new do |spec|
|
|
27
26
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
28
27
|
spec.require_paths = ["lib"]
|
29
28
|
|
30
|
-
# Uncomment to register a new dependency of your gem
|
31
|
-
# spec.add_dependency "example-gem", "~> 1.0"
|
32
|
-
|
33
|
-
# For more information and examples about making a new gem, checkout our
|
34
|
-
# guide at: https://bundler.io/guides/creating_gem.html
|
35
|
-
|
36
|
-
# Add your production dependencies here
|
37
|
-
# spec.add_dependency GEM [, VERSION]
|
38
29
|
spec.add_dependency "pg"
|
39
30
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
# Also un-comment in spec/spec_helper to use simplecov
|
44
|
-
# spec.add_development_dependency "simplecov"
|
45
|
-
|
46
|
-
# In development mode override load paths for gems whose source are located
|
47
|
-
# as siblings of this project directory
|
48
|
-
if File.directory?("#{__dir__}/.git")
|
49
|
-
local_projects = Dir["../*"].select { |path|
|
50
|
-
File.directory?(path) && File.exist?("#{path}/Gemfile")
|
51
|
-
}.map { |relpath| "#{File.absolute_path(relpath)}/lib" }
|
52
|
-
$LOAD_PATH.unshift *local_projects
|
53
|
-
end
|
31
|
+
spec.add_development_dependency "rake"
|
32
|
+
spec.add_development_dependency "rspec"
|
33
|
+
spec.add_development_dependency "simplecov"
|
54
34
|
end
|
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.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Claus Rasmussen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-04-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pg
|
@@ -24,6 +24,48 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
27
69
|
description: Gem pg_conn
|
28
70
|
email:
|
29
71
|
- claus.l.rasmussen@gmail.com
|
@@ -64,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
106
|
- !ruby/object:Gem::Version
|
65
107
|
version: '0'
|
66
108
|
requirements: []
|
67
|
-
rubygems_version: 3.
|
109
|
+
rubygems_version: 3.1.4
|
68
110
|
signing_key:
|
69
111
|
specification_version: 4
|
70
112
|
summary: Gem pg_conn
|