Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Verify array input to PG::TypeMapByColumn #621

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/pg_type_map_by_column.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pg_tmbc_fit_to_query( VALUE self, VALUE params )
t_tmbc *this = RTYPEDDATA_DATA( self );
t_typemap *default_tm;

Check_Type(params, T_ARRAY);
nfields = (int)RARRAY_LEN( params );
if ( this->nfields != nfields ) {
rb_raise( rb_eArgError, "number of result fields (%d) does not match number of mapped columns (%d)",
Expand Down
6 changes: 6 additions & 0 deletions spec/pg/type_map_by_column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ def decode(res, tuple, field)
expect{ PG::TypeMapByColumn.new( [123] ) }.to raise_error(TypeError, /wrong argument type (Integer|Fixnum)/)
end

it "should raise an error for invalid input when used as type_map" do
map = PG::TypeMapByColumn.new([PG::TextEncoder::Integer.new])
record_encoder = PG::TextEncoder::Record.new(type_map: map)
expect{ record_encoder.encode(123) }.to raise_error(TypeError)
end

it "shouldn't allow result mappings with different number of fields" do
res = @conn.exec( "SELECT 1" )
expect{ res.type_map = PG::TypeMapByColumn.new([]) }.to raise_error(ArgumentError, /mapped columns/)
Expand Down
Loading