Add some useful information to PG::Connection#inspect

.. instead of the useless instance variable.
This commit is contained in:
Lars Kanis 2022-10-11 10:06:24 +02:00
parent 2ccb200fba
commit 83e91d005a
3 changed files with 84 additions and 0 deletions

View File

@ -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
#

View File

@ -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

View File

@ -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(/<PG::Connection:[0-9a-fx]+ host=localhost port=#{@port} user=\w*>/)
end
it "should tell about finished connection" do
conn = PG.connect(@conninfo)
conn.finish
expect( conn.inspect ).to match(/<PG::Connection:[0-9a-fx]+ finished>/)
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=#<PG::TypeMapByColumn:[0-9a-fx]+>/)
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=#<PG::TypeMapByColumn:[0-9a-fx]+>/)
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=#<PG::TextEncoder::CopyRow:[0-9a-fx]+>/)
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=#<PG::TextDecoder::CopyRow:[0-9a-fx]+>/)
end
end
describe "PG::Connection#conninfo_parse" do
it "encode and decode Hash to connection string to Hash" do
hash = {