Skip to content

Commit

Permalink
Add default decoder for anonymous record types to BasicTypeRegistry
Browse files Browse the repository at this point in the history
The PostgreSQL server just sends the generic OID 2249 for generic record/composite types in query results like:
  SELECT row(123, 'str', true, null)

Since we don't get the OIDs of the record items, we can not properly decode them.
Nevertheless it's helpful to decode at least with the default type map and decode all items to an array of strings like so:

c.exec("SELECT 6, row(123, 'str', true, null), 7").values
 => [[6, ["123", "str", "t", nil], 7]]

instead of:
 => [[6, "(123,str,t,)", 7]]

Related to ged#578
  • Loading branch information
larskanis committed Aug 10, 2024
1 parent 764f6d5 commit 6ac27c6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/pg/basic_type_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ def register_default_types
register_type 0, 'inet', PG::TextEncoder::Inet, PG::TextDecoder::Inet
alias_type 0, 'cidr', 'inet'

register_type 0, 'record', PG::TextEncoder::Record, PG::TextDecoder::Record


register_type 1, 'int2', PG::BinaryEncoder::Int2, PG::BinaryDecoder::Integer
Expand Down
10 changes: 10 additions & 0 deletions spec/pg/basic_type_map_for_results_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,16 @@
end
end

it "should do anonymous record type conversions" do
[0].each do |format|
res = @conn.exec_params( "SELECT 6, ROW(123, 'str', true, null), 7
", [], format )
expect( res.getvalue(0,0) ).to eq( 6 )
expect( res.getvalue(0,1) ).to eq( ["123", "str", "t", nil] )
expect( res.getvalue(0,2) ).to eq( 7 )
end
end

it "should do inet type conversions" do
[0].each do |format|
vals = [
Expand Down

0 comments on commit 6ac27c6

Please sign in to comment.