diff --git a/lib/pg/connection.rb b/lib/pg/connection.rb index 7ff35a4..1a0fb0a 100644 --- a/lib/pg/connection.rb +++ b/lib/pg/connection.rb @@ -93,6 +93,27 @@ class PG::Connection return connect_hash_to_string(iopts) end + # Return a String representation of the object suitable for debugging. + def inspect + str = self.to_s + str[-1,0] = if finished? + " finished" + else + stats = [] + stats << " status=#{ PG.constants.grep(/CONNECTION_/).find{|c| PG.const_get(c) == status} }" if status != CONNECTION_OK + stats << " transaction_status=#{ PG.constants.grep(/PQTRANS_/).find{|c| PG.const_get(c) == transaction_status} }" if transaction_status != PG::PQTRANS_IDLE + stats << " nonblocking=#{ isnonblocking }" if isnonblocking + stats << " pipeline_status=#{ PG.constants.grep(/PQ_PIPELINE_/).find{|c| PG.const_get(c) == pipeline_status} }" if respond_to?(:pipeline_status) && pipeline_status != PG::PQ_PIPELINE_OFF + stats << " client_encoding=#{ get_client_encoding }" if get_client_encoding != "UTF8" + stats << " type_map_for_results=#{ type_map_for_results.to_s }" unless type_map_for_results.is_a?(PG::TypeMapAllStrings) + stats << " type_map_for_queries=#{ type_map_for_queries.to_s }" unless type_map_for_queries.is_a?(PG::TypeMapAllStrings) + stats << " encoder_for_put_copy_data=#{ encoder_for_put_copy_data.to_s }" if encoder_for_put_copy_data + stats << " decoder_for_get_copy_data=#{ decoder_for_get_copy_data.to_s }" if decoder_for_get_copy_data + " host=#{host} port=#{port} user=#{user}#{stats.join}" + end + return str + end + # call-seq: # conn.copy_data( sql [, coder] ) {|sql_result| ... } -> PG::Result # diff --git a/spec/helpers.rb b/spec/helpers.rb index aea76e5..927a210 100644 --- a/spec/helpers.rb +++ b/spec/helpers.rb @@ -46,6 +46,7 @@ module PG::TestingHelpers mod.around( :each ) do |example| begin + @conn.set_client_encoding "UTF8" @conn.set_default_encoding @conn.exec( 'BEGIN' ) unless example.metadata[:without_transaction] desc = example.source_location.join(':') @@ -64,6 +65,11 @@ module PG::TestingHelpers end @conn.exit_pipeline_mode end + @conn.setnonblocking false + @conn.type_map_for_results = PG::TypeMapAllStrings.new + @conn.type_map_for_queries = PG::TypeMapAllStrings.new + @conn.encoder_for_put_copy_data = nil + @conn.decoder_for_get_copy_data = nil @conn.exec( 'ROLLBACK' ) unless example.metadata[:without_transaction] end end diff --git a/spec/pg/connection_spec.rb b/spec/pg/connection_spec.rb index 6d54d43..ea865be 100644 --- a/spec/pg/connection_spec.rb +++ b/spec/pg/connection_spec.rb @@ -14,6 +14,63 @@ describe PG::Connection do expect( ObjectSpace.memsize_of(@conn) ).to be > DATA_OBJ_MEMSIZE end + describe "#inspect", :without_transaction do + it "should print host, port and user of a fresh connection, but not more" do + expect( @conn.inspect ).to match(//) + end + + it "should tell about finished connection" do + conn = PG.connect(@conninfo) + conn.finish + expect( conn.inspect ).to match(//) + end + + it "should tell about connection status" do + conn = PG::Connection.connect_start(@conninfo) + expect( conn.inspect ).to match(/ status=CONNECTION_STARTED/) + end + + it "should tell about pipeline mode", :postgresql_14 do + @conn.enter_pipeline_mode + expect( @conn.inspect ).to match(/ pipeline_status=PQ_PIPELINE_ON/) + end + + it "should tell about transaction_status" do + @conn.send_query "select 8" + expect( @conn.inspect ).to match(/ transaction_status=PQTRANS_ACTIVE/) + end + + it "should tell about nonblocking mode" do + @conn.setnonblocking true + expect( @conn.inspect ).to match(/ nonblocking=true/) + end + + it "should tell about non UTF8 client encoding" do + @conn.set_client_encoding "ISO-8859-1" + expect( @conn.inspect ).to match(/ client_encoding=LATIN1/) + end + + it "should tell about non default type_map_for_results" do + @conn.type_map_for_results = PG::TypeMapByColumn.new([]) + expect( @conn.inspect ).to match(/ type_map_for_results=#/) + end + + it "should tell about non default type_map_for_queries" do + @conn.type_map_for_queries = PG::TypeMapByColumn.new([]) + expect( @conn.inspect ).to match(/ type_map_for_queries=#/) + end + + it "should tell about encoder_for_put_copy_data" do + @conn.encoder_for_put_copy_data = PG::TextEncoder::CopyRow.new + expect( @conn.inspect ).to match(/ encoder_for_put_copy_data=#/) + end + + it "should tell about decoder_for_get_copy_data" do + @conn.decoder_for_get_copy_data = PG::TextDecoder::CopyRow.new + expect( @conn.inspect ).to match(/ decoder_for_get_copy_data=#/) + end + end + describe "PG::Connection#conninfo_parse" do it "encode and decode Hash to connection string to Hash" do hash = {