Skip to content

Commit

Permalink
Add block-call semantics to PG::Connection.new
Browse files Browse the repository at this point in the history
This was an oversight, when implementing the async interface of pg-1.3.0.

Fixes #454
  • Loading branch information
larskanis committed May 18, 2022
1 parent a40dd7b commit 3e4e457
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
8 changes: 4 additions & 4 deletions lib/pg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ class NotInBlockingMode < PG::Error
# Get the PG library version.
#
# +include_buildnum+ is no longer used and any value passed will be ignored.
def self::version_string( include_buildnum=nil )
return "%s %s" % [ self.name, VERSION ]
def self.version_string( include_buildnum=nil )
"%s %s" % [ self.name, VERSION ]
end


### Convenience alias for PG::Connection.new.
def self::connect( *args, **kwargs )
return PG::Connection.new( *args, **kwargs )
def self.connect( *args, **kwargs, &block )
Connection.new( *args, **kwargs, &block )
end


Expand Down
12 changes: 10 additions & 2 deletions lib/pg/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ def encrypt_password( password, username, algorithm=nil )
def reset
reset_start
async_connect_or_reset(:reset_poll)
self
end
alias async_reset reset

Expand Down Expand Up @@ -646,8 +647,6 @@ def cancel
sync_setnonblocking(true)
self.flush_data = true
set_default_encoding

self
end

class << self
Expand Down Expand Up @@ -709,6 +708,15 @@ def new(*args, **kwargs)
raise(PG::ConnectionBad, conn.error_message) if conn.status == PG::CONNECTION_BAD

conn.send(:async_connect_or_reset, :connect_poll)

if block_given?
begin
yield conn
ensure
conn.finish
end
end
conn
end
alias async_connect new
alias connect new
Expand Down
11 changes: 6 additions & 5 deletions spec/pg/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@
klass = Class.new(described_class) do
alias execute exec
end
conn = klass.send(meth, @conninfo)
expect( conn ).to be_a_kind_of( klass )
expect( conn.execute("SELECT 1") ).to be_a_kind_of( PG::Result )
conn.close
klass.send(meth, @conninfo) do |conn|
expect( conn ).to be_a_kind_of( klass )
expect( conn.execute("SELECT 1") ).to be_a_kind_of( PG::Result )
end
end
end

Expand Down Expand Up @@ -1436,7 +1436,8 @@ def interrupt_thread(exc=nil)
# Close the two pipe file descriptors, so that the file descriptor of
# newly established connection is probably distinct from the previous one.
ios.each(&:close)
conn.reset
res = conn.reset
expect( res ).to eq( conn )

# The new connection should work even when the file descriptor has changed.
expect( conn.exec("SELECT 1").values ).to eq([["1"]])
Expand Down
10 changes: 10 additions & 0 deletions spec/pg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,14 @@
expect( c ).to be_a_kind_of( PG::Connection )
c.close
end

it "can #connect with block" do
res = PG.connect(@conninfo) do |c|
res = c.exec "SELECT 5"
expect( res.values ).to eq( [["5"]] )
end

expect( res ).to be_a_kind_of(PG::Connection)
expect( res ).to be_finished
end
end

0 comments on commit 3e4e457

Please sign in to comment.