Commits

Lars Kanis committed bb9c662 Draft

Fix encoding of values delivered to the row processor and add specs for binary and text data.

  • Participants
  • Parent commits b7633be
  • Branches row_processor

Comments (0)

Files changed (2)

ext/pg_connection.c

 	if ((proc = rb_iv_get(self, "@row_processor")) != Qnil) {
 		VALUE col_ary;
 		VALUE proc_result;
+		VALUE result = pg_new_result_for_callback(res, self);
 		
 		if ( columns ) {
 			int num_fields = PQnfields(res);
 #ifdef M17N_SUPPORTED
 					/* associate client encoding for text format only */
 					if ( 0 == PQfformat(res, field) ) {
-						ASSOCIATE_INDEX( val, self );
+						ASSOCIATE_INDEX( val, result );
 					} else {
 						rb_enc_associate( val, rb_ascii8bit_encoding() );
 					}
 		}
 
 		proc_result = rb_funcall(proc, rb_intern("call"), 2,
-			pg_new_result_for_callback(res, self),
+			result,
 			col_ary
 		);
 

spec/pg/connection_spec.rb

 			res.ntuples.should == 0
 		end
 
+		it "can use set_row_processor with binary data" do
+			binary_file = File.join(Dir.pwd, 'spec/data', 'random_binary_data')
+			bytes = File.open(binary_file, 'rb').read
+			@conn.set_row_processor do |result, row|
+				row.first.should == bytes if row
+				nil
+			end
+			@conn.exec( 'VALUES($1::bytea)', [ { :value => bytes, :format => 1 } ], 1 )
+		end
+
 		it "can use set_row_processor with skip_results" do
 			expected_rows = [nil, ['1','2'], ['2','3'], nil, ['4','5']]
 			@conn.set_row_processor do |result, row|
 		end
 
 		context "multinationalization support", :ruby_19 do
-			it "row processor gets proper result encoding" do
+			it "row processor gets proper text encoding" do
 				@conn.set_row_processor do |result, row|
 					result.fname(0).encoding.should == Encoding::UTF_8
+					row.first.encoding.should == Encoding::UTF_8 if row
 					nil
 				end
 				res = @conn.exec( 'VALUES(1)' )
 			end
+
+			it "row processor gets proper binary encoding" do
+				@conn.set_row_processor do |result, row|
+					result.fname(0).encoding.should == Encoding::UTF_8
+					row.first.encoding.should == Encoding::ASCII_8BIT if row
+					nil
+				end
+				res = @conn.exec( "VALUES(''::bytea)", [], 1 )
+			end
 		end
 	end