Skip to content

Commit 83e91d0

Browse files
committed
Add some useful information to PG::Connection#inspect
.. instead of the useless instance variable.
1 parent 2ccb200 commit 83e91d0

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

lib/pg/connection.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,27 @@ def self.parse_connect_args( *args )
9393
return connect_hash_to_string(iopts)
9494
end
9595

96+
# Return a String representation of the object suitable for debugging.
97+
def inspect
98+
str = self.to_s
99+
str[-1,0] = if finished?
100+
" finished"
101+
else
102+
stats = []
103+
stats << " status=#{ PG.constants.grep(/CONNECTION_/).find{|c| PG.const_get(c) == status} }" if status != CONNECTION_OK
104+
stats << " transaction_status=#{ PG.constants.grep(/PQTRANS_/).find{|c| PG.const_get(c) == transaction_status} }" if transaction_status != PG::PQTRANS_IDLE
105+
stats << " nonblocking=#{ isnonblocking }" if isnonblocking
106+
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
107+
stats << " client_encoding=#{ get_client_encoding }" if get_client_encoding != "UTF8"
108+
stats << " type_map_for_results=#{ type_map_for_results.to_s }" unless type_map_for_results.is_a?(PG::TypeMapAllStrings)
109+
stats << " type_map_for_queries=#{ type_map_for_queries.to_s }" unless type_map_for_queries.is_a?(PG::TypeMapAllStrings)
110+
stats << " encoder_for_put_copy_data=#{ encoder_for_put_copy_data.to_s }" if encoder_for_put_copy_data
111+
stats << " decoder_for_get_copy_data=#{ decoder_for_get_copy_data.to_s }" if decoder_for_get_copy_data
112+
" host=#{host} port=#{port} user=#{user}#{stats.join}"
113+
end
114+
return str
115+
end
116+
96117
# call-seq:
97118
# conn.copy_data( sql [, coder] ) {|sql_result| ... } -> PG::Result
98119
#

spec/helpers.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def self::included( mod )
4646

4747
mod.around( :each ) do |example|
4848
begin
49+
@conn.set_client_encoding "UTF8"
4950
@conn.set_default_encoding
5051
@conn.exec( 'BEGIN' ) unless example.metadata[:without_transaction]
5152
desc = example.source_location.join(':')
@@ -64,6 +65,11 @@ def self::included( mod )
6465
end
6566
@conn.exit_pipeline_mode
6667
end
68+
@conn.setnonblocking false
69+
@conn.type_map_for_results = PG::TypeMapAllStrings.new
70+
@conn.type_map_for_queries = PG::TypeMapAllStrings.new
71+
@conn.encoder_for_put_copy_data = nil
72+
@conn.decoder_for_get_copy_data = nil
6773
@conn.exec( 'ROLLBACK' ) unless example.metadata[:without_transaction]
6874
end
6975
end

spec/pg/connection_spec.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,63 @@
1414
expect( ObjectSpace.memsize_of(@conn) ).to be > DATA_OBJ_MEMSIZE
1515
end
1616

17+
describe "#inspect", :without_transaction do
18+
it "should print host, port and user of a fresh connection, but not more" do
19+
expect( @conn.inspect ).to match(/<PG::Connection:[0-9a-fx]+ host=localhost port=#{@port} user=\w*>/)
20+
end
21+
22+
it "should tell about finished connection" do
23+
conn = PG.connect(@conninfo)
24+
conn.finish
25+
expect( conn.inspect ).to match(/<PG::Connection:[0-9a-fx]+ finished>/)
26+
end
27+
28+
it "should tell about connection status" do
29+
conn = PG::Connection.connect_start(@conninfo)
30+
expect( conn.inspect ).to match(/ status=CONNECTION_STARTED/)
31+
end
32+
33+
it "should tell about pipeline mode", :postgresql_14 do
34+
@conn.enter_pipeline_mode
35+
expect( @conn.inspect ).to match(/ pipeline_status=PQ_PIPELINE_ON/)
36+
end
37+
38+
it "should tell about transaction_status" do
39+
@conn.send_query "select 8"
40+
expect( @conn.inspect ).to match(/ transaction_status=PQTRANS_ACTIVE/)
41+
end
42+
43+
it "should tell about nonblocking mode" do
44+
@conn.setnonblocking true
45+
expect( @conn.inspect ).to match(/ nonblocking=true/)
46+
end
47+
48+
it "should tell about non UTF8 client encoding" do
49+
@conn.set_client_encoding "ISO-8859-1"
50+
expect( @conn.inspect ).to match(/ client_encoding=LATIN1/)
51+
end
52+
53+
it "should tell about non default type_map_for_results" do
54+
@conn.type_map_for_results = PG::TypeMapByColumn.new([])
55+
expect( @conn.inspect ).to match(/ type_map_for_results=#<PG::TypeMapByColumn:[0-9a-fx]+>/)
56+
end
57+
58+
it "should tell about non default type_map_for_queries" do
59+
@conn.type_map_for_queries = PG::TypeMapByColumn.new([])
60+
expect( @conn.inspect ).to match(/ type_map_for_queries=#<PG::TypeMapByColumn:[0-9a-fx]+>/)
61+
end
62+
63+
it "should tell about encoder_for_put_copy_data" do
64+
@conn.encoder_for_put_copy_data = PG::TextEncoder::CopyRow.new
65+
expect( @conn.inspect ).to match(/ encoder_for_put_copy_data=#<PG::TextEncoder::CopyRow:[0-9a-fx]+>/)
66+
end
67+
68+
it "should tell about decoder_for_get_copy_data" do
69+
@conn.decoder_for_get_copy_data = PG::TextDecoder::CopyRow.new
70+
expect( @conn.inspect ).to match(/ decoder_for_get_copy_data=#<PG::TextDecoder::CopyRow:[0-9a-fx]+>/)
71+
end
72+
end
73+
1774
describe "PG::Connection#conninfo_parse" do
1875
it "encode and decode Hash to connection string to Hash" do
1976
hash = {

0 commit comments

Comments
 (0)