Skip to content

Commit

Permalink
Make sure PG::Result#stream_each and stream_each_tuple delivers typem…
Browse files Browse the repository at this point in the history
…apped values
  • Loading branch information
larskanis committed Nov 16, 2019
1 parent 83cc094 commit cb1047d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ext/pg.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ typedef struct {
/* Hash with fnames[] to field number mapping. */
VALUE field_map;

/* List of field names as frozen String objects.
/* List of field names as frozen String or Symbol objects.
* Only valid if nfields != -1
*/
VALUE fnames[0];
Expand Down
17 changes: 11 additions & 6 deletions spec/pg/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
end

context "result streaming in single row mode" do
let!(:textdec_int){ PG::TextDecoder::Integer.new name: 'INT4', oid: 23 }

it "can iterate over all rows as Hash" do
@conn.send_query( "SELECT generate_series(2,4) AS a; SELECT 1 AS b, generate_series(5,6) AS c" )
@conn.set_single_row_mode
Expand All @@ -102,13 +104,15 @@
expect( @conn.get_result ).to be_nil
end

it "can iterate over all rows as Hash with symbols" do
it "can iterate over all rows as Hash with symbols and typemap" do
@conn.send_query( "SELECT generate_series(2,4) AS a" )
@conn.set_single_row_mode
res = @conn.get_result.field_names_as(:symbol)
res.type_map = PG::TypeMapByColumn.new [textdec_int]
expect(
@conn.get_result.field_names_as(:symbol).stream_each.to_a
res.stream_each.to_a
).to eq(
[{:a=>"2"}, {:a=>"3"}, {:a=>"4"}]
[{:a=>2}, {:a=>3}, {:a=>4}]
)
expect( @conn.get_result ).to be_nil
end
Expand Down Expand Up @@ -188,13 +192,14 @@
expect( tuple1.keys[0].object_id ).to eq(tuple2.keys[0].object_id)
end

it "can iterate over all rows as PG::Tuple with symbols" do
it "can iterate over all rows as PG::Tuple with symbols and typemap" do
@conn.send_query( "SELECT generate_series(2,4) AS a" )
@conn.set_single_row_mode
res = @conn.get_result.field_names_as(:symbol)
res.type_map = PG::TypeMapByColumn.new [textdec_int]
tuples = res.stream_each_tuple.to_a
expect( tuples[0][0] ).to eq( "2" )
expect( tuples[1][:a] ).to eq( "3" )
expect( tuples[0][0] ).to eq( 2 )
expect( tuples[1][:a] ).to eq( 3 )
expect( @conn.get_result ).to be_nil
end

Expand Down

0 comments on commit cb1047d

Please sign in to comment.