Skip to content

Commit

Permalink
Add some useful information to PG::Connection#inspect
Browse files Browse the repository at this point in the history
.. instead of the useless instance variable.
  • Loading branch information
larskanis committed Oct 11, 2022
1 parent 2ccb200 commit 83e91d0
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/pg/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ def self.parse_connect_args( *args )
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
#
Expand Down
6 changes: 6 additions & 0 deletions spec/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def self::included( mod )

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(':')
Expand All @@ -64,6 +65,11 @@ def self::included( mod )
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
Expand Down
57 changes: 57 additions & 0 deletions spec/pg/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,63 @@
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 = {
Expand Down

0 comments on commit 83e91d0

Please sign in to comment.