pg_conn 0.2.1 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df5aa73c1d8e34263b51b29acb63f2d675e7945141e44d9f5adbd00987d1bb46
4
- data.tar.gz: 56ad22f1c45fb8e78d43f59ff4b982da09d86d4e1fdfd802cf7e396d66a16ece
3
+ metadata.gz: edc8c2d648a4ea373981633433cfc52ca94cdec159292f052a6ee5a077b56172
4
+ data.tar.gz: 7823404356129c95cfcb37869641a32a8982bd11da0d1d7f730893aa0f746701
5
5
  SHA512:
6
- metadata.gz: b94e9e6b1ed7605aceab03fb0393e79f5f57ef1e7bb26794cd8b25a8de93c7f4358686dc910a2d76132941daa7c2ce113d20c45144ff9e2ff6fb3e5a744f96b6
7
- data.tar.gz: 386d086a0af25b2f049046a083e1af1f947a7fbed63b0a5787659fe95414678761d93fcddb4ddce04b7a97d1dcb82160c7ff6bf79ec4aabe1d18ec49f55d62b9
6
+ metadata.gz: 6a539ed00e4a16a5ef9d3e400782d42000f19f578382e9721941b82c61cffb3f427856409b978224c42c2b533bb585df5e1c27765c73c1a9071809cc3c734358
7
+ data.tar.gz: c93dab2fa0404ffe61b02c4c1fbc0021d290dade58471cd6cabce8e74cbe2372a3b6c1f4a2fefd5eff82ffa4b545b71fb54851fd6f0fcf8abb9b53b16892324a
data/Gemfile CHANGED
@@ -5,6 +5,3 @@ source "https://rubygems.org"
5
5
  # Specify your gem's dependencies in pg_conn.gemspec
6
6
  gemspec
7
7
 
8
- gem "rake", "~> 13.0"
9
-
10
- gem "rspec", "~> 3.0"
data/TODO CHANGED
@@ -1,4 +1,6 @@
1
1
  TODO
2
+ o Create an abstract PgConnBase and have PgStmts (writes statements to array)
3
+ and PgConn (sends statements to server) classes derived from it
2
4
  o fix silent
3
5
  o fix fail
4
6
  o A PgConn.new with a block. Useful for temporary database connections (a
@@ -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
-
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.3"
3
3
  end
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 of a set of
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.values
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
- pg_exec(sql, fail: fail, silent: silent)&.cmd_tuples
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
- begin
630
- last_stmt = nil # To make the current SQL statement visible to the rescue clause
631
- if arg.is_a?(String)
632
- return nil if arg == ""
633
- last_stmt = arg
634
- @pg_connection.exec(last_stmt)
635
- else
636
- stmts = arg.flatten.compact
637
- return nil if stmts.empty?
638
- last_stmt = stmts.last
639
- @pg_connection.exec(stmts.join(";\n"))
640
- end
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
- raise
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
- return nil
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
- # Add your development dependencies here
41
- # spec.add_development_dependency GEM [, VERSION]
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.2.1
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-02-28 00:00:00.000000000 Z
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.2.26
109
+ rubygems_version: 3.1.4
68
110
  signing_key:
69
111
  specification_version: 4
70
112
  summary: Gem pg_conn