From cb1047d3054887c062765d2f0ac83281d9058c28 Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Tue, 12 Nov 2019 21:53:25 +0100 Subject: [PATCH] Make sure PG::Result#stream_each and stream_each_tuple delivers typemapped values --- ext/pg.h | 2 +- spec/pg/result_spec.rb | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/ext/pg.h b/ext/pg.h index 07b46844d..63b7d654f 100644 --- a/ext/pg.h +++ b/ext/pg.h @@ -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]; diff --git a/spec/pg/result_spec.rb b/spec/pg/result_spec.rb index 9fbbb5028..cde5816bb 100644 --- a/spec/pg/result_spec.rb +++ b/spec/pg/result_spec.rb @@ -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 @@ -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 @@ -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