Commits

Lars Kanis committed 4280826 Draft

Fix wrong encoding for Result object passed to a notice-receiver block. Add proper specs for #set_notice_receiver.

Comments (0)

Files changed (2)

ext/pg_connection.c

 
 	if ((proc = rb_iv_get(self, "@notice_receiver")) != Qnil) {
 		rb_funcall(proc, rb_intern("call"), 1, 
-			Data_Wrap_Struct(rb_cPGresult, NULL, NULL, (PGresult*)result));
+			pg_new_result_for_callback(result, self));
 	}
 	return;
 }

spec/pg/connection_spec.rb

 	end
 
 	after( :each ) do
-		# Disable row processor
+		# Disable notice and row processor
+		@conn.set_notice_receiver
 		@conn.set_row_processor if @conn.respond_to?(:set_row_processor)
 		@conn.exec( 'ROLLBACK' ) unless example.metadata[:without_transaction]
 	end
 		expect { conn.finish }.to raise_error( PG::Error, /connection is closed/i )
 	end
 
+	it "can pipe notices to a block" do
+		called = false
+		@conn.set_notice_receiver do |result|
+			result.error_message.should =~ /foobar/
+			called = true
+		end
 
+		@conn.exec "do $$ BEGIN RAISE NOTICE 'foobar'; END; $$ LANGUAGE plpgsql;"
+		called.should == true
+	end
+	
 	context "under PostgreSQL 9", :postgresql_90 do
 
 		before( :each ) do
 			conn.finish if conn
 		end
 
+		it "encodes notices with the connection's encoding" do
+			@conn.set_notice_receiver do |result|
+				result.error_message.encoding.should == Encoding::UTF_8
+			end
+			@conn.exec "do $$ BEGIN RAISE NOTICE 'foobar'; END; $$ LANGUAGE plpgsql;"
+		end
+		
 	end
 end