Skip to content

Commit

Permalink
Merge pull request #596 from larskanis/fix-bin-copy
Browse files Browse the repository at this point in the history
Fix binary copy_data in Ractor context
  • Loading branch information
larskanis authored Oct 23, 2024
2 parents 251fc4a + 8d22de7 commit 626102e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/pg/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def inspect
return str
end

BinarySignature = "PGCOPY\n\377\r\n\0".b
BinarySignature = "PGCOPY\n\377\r\n\0"
private_constant :BinarySignature

# call-seq:
Expand Down
25 changes: 24 additions & 1 deletion spec/pg/basic_type_map_based_on_result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
Ractor.make_shareable(basic_type_mapping)
end

it "should be usable with Ractor", :ractor do
it "should be usable with Ractor in text format", :ractor do
vals = Ractor.new(@conninfo) do |conninfo|
conn = PG.connect(conninfo)
basic_type_mapping = PG::BasicTypeMapBasedOnResult.new(conn)
Expand All @@ -52,6 +52,29 @@
expect( vals ).to eq( [['b', '234', '{2,3}']] )
end

it "should be usable with Ractor in binary format", :ractor do
vals = Ractor.new(@conninfo) do |conninfo|
conn = PG.connect(conninfo)
basic_type_mapping = PG::BasicTypeMapBasedOnResult.new(conn)
conn.exec( "CREATE TEMP TABLE copytable (t TEXT, i INT)" )

# Retrieve table OIDs per empty result set.
res = conn.exec( "SELECT * FROM copytable LIMIT 0", [], 1)
tm = basic_type_mapping.build_column_map( res )
row_encoder = PG::BinaryEncoder::CopyRow.new type_map: tm

conn.copy_data( "COPY copytable FROM STDIN WITH (FORMAT binary)", row_encoder ) do |res|
conn.put_copy_data ['b', 234]
end
res = conn.exec( "SELECT * FROM copytable" )
res.values
ensure
conn&.finish
end.take

expect( vals ).to eq( [['b', '234']] )
end

context "with usage of result oids for bind params encoder selection" do
it "can type cast query params" do
@conn.exec( "CREATE TEMP TABLE copytable (t TEXT, i INT, ai INT[], by BYTEA)" )
Expand Down
10 changes: 10 additions & 0 deletions spec/pg/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@
expect( vals ).to eq( [["123"]] )
end

it "connects using 7 arguments in a Ractor", :ractor do
vals = Ractor.new do
PG.connect( 'localhost', @port, nil, nil, :test, nil, nil ) do |conn|
conn.exec("SELECT 234").values
end
end.take

expect( vals ).to eq( [["234"]] )
end

describe "#inspect", :without_transaction do
it "should print host, port and user of a fresh connection, but not more" do
expect( @conn.inspect ).to match(/<PG::Connection:[0-9a-fx]+ host=localhost port=#{@port} user=\w*>/)
Expand Down

0 comments on commit 626102e

Please sign in to comment.