pg_conn 0.2.1 → 0.3.1

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: 8b90e77eda48e35d4577bfb9bc2f6dd553054342d22fabaec9931639a765c1eb
4
+ data.tar.gz: 1fca73a204f709583e67d6b035f7d48d896b63164a8e6b06a7133fbb1c038eb2
5
5
  SHA512:
6
- metadata.gz: b94e9e6b1ed7605aceab03fb0393e79f5f57ef1e7bb26794cd8b25a8de93c7f4358686dc910a2d76132941daa7c2ce113d20c45144ff9e2ff6fb3e5a744f96b6
7
- data.tar.gz: 386d086a0af25b2f049046a083e1af1f947a7fbed63b0a5787659fe95414678761d93fcddb4ddce04b7a97d1dcb82160c7ff6bf79ec4aabe1d18ec49f55d62b9
6
+ metadata.gz: 22b8ba44cd9b8aea11291977ea85d048f57b92627c9c67e2e306b9a398842f0af7b07b7d04c7cae86c5e0d8496904ef7a471730342ee5cc7aa476099d7f2bead
7
+ data.tar.gz: 215a4d41c900604a0ee10cbf87a2d9cd6eeed676a953372073084b1c43c493fa7246084dc93c4f6e321693f84cb83ec35944a14e15579347341ad90c147eae42
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
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.1"
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)
@@ -66,15 +66,16 @@ module PgConn
66
66
 
67
67
 
68
68
 
69
- # Initialize a connection object and connect to the database. #initialize has five
70
- # variations:
71
- #
69
+ # :call-seq:
72
70
  # initialize(dbname = nil, user = nil, field_name_class: Symbol)
73
71
  # initialize(connection_hash, field_name_class: Symbol)
74
72
  # initialize(connection_string, field_name_class: Symbol)
75
73
  # initialize(host, port, dbname, user, password, field_name_class: Symbol)
74
+ # initialize(array, field_name_class: Symbol)
76
75
  # initialize(pg_connection_object)
77
76
  #
77
+ # Initialize a connection object and connect to the database
78
+ #
78
79
  # The possible keys of the connection hash are :host, :port, :dbname, :user,
79
80
  # and :password. The connection string can either be a space-separated list
80
81
  # of <key>=<value> pairs with the same keys as the hash, or a URI with the
@@ -84,6 +85,11 @@ module PgConn
84
85
  # Symbol (the default) or String. The :timestamp option is used
85
86
  # internally to set the timestamp for transactions
86
87
  #
88
+ # If given an array argument, PgConn will not connect to the database and
89
+ # instead write its commands to the array. In this case, methods extracting
90
+ # values from the database (eg. #value) will return nil or raise an
91
+ # exception
92
+ #
87
93
  # The last variant is used to establish a PgConn from an existing
88
94
  # connection. It doesn't change the connection settings and is not
89
95
  # recommended except in cases where you want to piggyback on an existing
@@ -125,6 +131,9 @@ module PgConn
125
131
  end
126
132
  when Hash
127
133
  make_connection **arg
134
+ when Array
135
+ @pg_commands = arg
136
+ nil
128
137
  else
129
138
  raise Error, "Illegal argument type: #{arg.class}"
130
139
  end
@@ -136,7 +145,7 @@ module PgConn
136
145
  raise Error, "Illegal number of parameters: #{args.size}"
137
146
  end
138
147
 
139
- if !using_existing_connection
148
+ if @pg_connection && !using_existing_connection
140
149
  # Auto-convert to ruby types
141
150
  type_map = PG::BasicTypeMapForResults.new(@pg_connection)
142
151
 
@@ -170,7 +179,7 @@ module PgConn
170
179
 
171
180
  # Close the database connection
172
181
  def terminate()
173
- @pg_connection.close if !@pg_connection.finished?
182
+ @pg_connection.close if @pg_connection && !@pg_connection.finished?
174
183
  end
175
184
 
176
185
  def self.new(*args, &block)
@@ -178,7 +187,7 @@ module PgConn
178
187
  begin
179
188
  object = Connection.allocate
180
189
  object.send(:initialize, *args)
181
- yield(object) if object.pg_connection
190
+ yield(object) # if object.pg_connection
182
191
  ensure
183
192
  object.terminate if object.pg_connection
184
193
  end
@@ -187,9 +196,6 @@ module PgConn
187
196
  end
188
197
  end
189
198
 
190
- # # Return true iff the query returns exactly one value
191
- # def exist?(query) count(query) == 1 end
192
-
193
199
  # :call-seq:
194
200
  # exist?(query)
195
201
  # exist?(table, id)
@@ -459,7 +465,7 @@ module PgConn
459
465
  elsif r.nfields == 1
460
466
  r.column_values(0)
461
467
  else
462
- r.values
468
+ r&.values
463
469
  end
464
470
  end
465
471
 
@@ -489,7 +495,11 @@ module PgConn
489
495
  #
490
496
  # TODO: Handle postgres exceptions wrt transaction state and stack
491
497
  def execute(sql, fail: true, silent: false)
492
- pg_exec(sql, fail: fail, silent: silent)&.cmd_tuples
498
+ if @pg_connection
499
+ pg_exec(sql, fail: fail, silent: silent)&.cmd_tuples
500
+ else
501
+ pg_exec(sql, fail: fail, silent: silent)
502
+ end
493
503
  end
494
504
 
495
505
  # Switch user to the given user and execute the statement before swithcing
@@ -534,7 +544,7 @@ module PgConn
534
544
  else
535
545
  @savepoints = []
536
546
  pg_exec("begin")
537
- @timestamp = pg_exec("select current_timestamp").values[0][0]
547
+ @timestamp = pg_exec("select current_timestamp").values[0][0] if @pg_connection
538
548
  end
539
549
  end
540
550
 
@@ -626,30 +636,39 @@ module PgConn
626
636
  #
627
637
  # TODO: Fix silent by not handling exceptions
628
638
  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
639
+ if @pg_connection
640
+ begin
641
+ last_stmt = nil # To make the current SQL statement visible to the rescue clause
642
+ if arg.is_a?(String)
643
+ return nil if arg == ""
644
+ last_stmt = arg
645
+ @pg_connection.exec(last_stmt)
646
+ else
647
+ stmts = arg.flatten.compact
648
+ return nil if stmts.empty?
649
+ last_stmt = stmts.last
650
+ @pg_connection.exec(stmts.join(";\n"))
651
+ end
652
+
653
+ rescue PG::Error => ex
654
+ if fail
655
+ if !silent # FIXME Why do we handle this?
656
+ $stderr.puts arg
657
+ $stderr.puts ex.message
658
+ $stderr.flush
659
+ end
660
+ raise
661
+ else
662
+ return nil
648
663
  end
649
- raise
664
+ end
665
+ else # @pg_commands is defined
666
+ if arg.is_a?(String)
667
+ @pg_commands << arg if arg != ""
650
668
  else
651
- return nil
669
+ @pg_commands.concat(arg)
652
670
  end
671
+ nil
653
672
  end
654
673
  end
655
674
 
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,7 @@ 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 "simplecov"
54
32
  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.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: 2022-02-28 00:00:00.000000000 Z
11
+ date: 2022-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: simplecov
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'
27
41
  description: Gem pg_conn
28
42
  email:
29
43
  - claus.l.rasmussen@gmail.com
@@ -64,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
78
  - !ruby/object:Gem::Version
65
79
  version: '0'
66
80
  requirements: []
67
- rubygems_version: 3.2.26
81
+ rubygems_version: 3.1.4
68
82
  signing_key:
69
83
  specification_version: 4
70
84
  summary: Gem pg_conn