Skip to content

Commit

Permalink
More privacy for our constants!
Browse files Browse the repository at this point in the history
  • Loading branch information
larskanis committed Apr 20, 2023
1 parent 2e663b3 commit d9e08a0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/pg/basic_type_map_for_queries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def get_array_type(value)
Array => :get_array_type,
BinaryData => [1, 'bytea'],
})
private_constant :DEFAULT_TYPE_MAP

DEFAULT_ARRAY_TYPE_MAP = PG.make_shareable({
TrueClass => [0, '_bool'],
Expand All @@ -193,5 +194,5 @@ def get_array_type(value)
Time => [0, '_timestamptz'],
IPAddr => [0, '_inet'],
})

private_constant :DEFAULT_ARRAY_TYPE_MAP
end
4 changes: 3 additions & 1 deletion lib/pg/basic_type_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class CoderMap
bool
date timestamp timestamptz
].inject({}){|h,e| h[e] = true; h }.freeze
private_constant :DONT_QUOTE_TYPES

def initialize(result, coders_by_name, format, arraycoder)
coder_map = {}
Expand Down Expand Up @@ -150,6 +151,7 @@ def map_for(format, direction)
module Checker
ValidFormats = { 0 => true, 1 => true }.freeze
ValidDirections = { :encoder => true, :decoder => true }.freeze
private_constant :ValidFormats, :ValidDirections

protected def check_format_and_direction(format, direction)
raise(ArgumentError, "Invalid format value %p" % format) unless ValidFormats[format]
Expand Down Expand Up @@ -292,6 +294,6 @@ def register_default_types

alias define_default_types register_default_types

# @private
DEFAULT_TYPE_REGISTRY = PG.make_shareable(PG::BasicTypeRegistry.new.register_default_types)
private_constant :DEFAULT_TYPE_REGISTRY
end
7 changes: 6 additions & 1 deletion lib/pg/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PG::Connection

# The order the options are passed to the ::connect method.
CONNECT_ARGUMENT_ORDER = %w[host port options tty dbname user password].freeze

private_constant :CONNECT_ARGUMENT_ORDER

### Quote a single +value+ for use in a connection-parameter string.
def self.quote_connstr( value )
Expand All @@ -47,6 +47,7 @@ def self.connect_hash_to_string( hash )

# Shareable program name for Ractor
PROGRAM_NAME = $PROGRAM_NAME.dup.freeze
private_constant :PROGRAM_NAME

# Parse the connection +args+ into a connection-parameter string.
# See PG::Connection.new for valid arguments.
Expand Down Expand Up @@ -117,6 +118,7 @@ def inspect
end

BinarySignature = "PGCOPY\n\377\r\n\0".b
private_constant :BinarySignature

# call-seq:
# conn.copy_data( sql [, coder] ) {|sql_result| ... } -> PG::Result
Expand Down Expand Up @@ -851,6 +853,7 @@ def ping(*args)
:setdblogin => [:async_connect, :sync_connect],
:ping => [:async_ping, :sync_ping],
})
private_constant :REDIRECT_CLASS_METHODS

# These methods are affected by PQsetnonblocking
REDIRECT_SEND_METHODS = PG.make_shareable({
Expand All @@ -860,6 +863,7 @@ def ping(*args)
:put_copy_end => [:async_put_copy_end, :sync_put_copy_end],
:flush => [:async_flush, :sync_flush],
})
private_constant :REDIRECT_SEND_METHODS
REDIRECT_METHODS = {
:exec => [:async_exec, :sync_exec],
:query => [:async_exec, :sync_exec],
Expand All @@ -877,6 +881,7 @@ def ping(*args)
:client_encoding= => [:async_set_client_encoding, :sync_set_client_encoding],
:cancel => [:async_cancel, :sync_cancel],
}
private_constant :REDIRECT_METHODS

if PG::Connection.instance_methods.include? :async_encrypt_password
REDIRECT_METHODS.merge!({
Expand Down
11 changes: 6 additions & 5 deletions spec/pg/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,17 @@
end

it "sets a shortened fallback_application_name on new connections" do
old_script_name = PG::Connection::PROGRAM_NAME
old_script_name = PG::Connection.class_eval("PROGRAM_NAME")
begin
PG::Connection.const_set(:PROGRAM_NAME, "/this/is/a/very/long/path/with/many/directories/to/our/beloved/ruby")
prg = '/this/is/a/very/long/path/with/many/directories/to/our/beloved/ruby'
PG::Connection.class_eval("PROGRAM_NAME=#{prg.inspect}")
conn_string = PG::Connection.parse_connect_args( 'dbname=test' )
conn_name = conn_string[ /application_name='(.*?)'/, 1 ]
expect( conn_name ).to include( PG::Connection::PROGRAM_NAME[0..10] )
expect( conn_name ).to include( PG::Connection::PROGRAM_NAME[-10..-1] )
expect( conn_name ).to include( prg[0..10] )
expect( conn_name ).to include( prg[-10..-1] )
expect( conn_name.length ).to be <= 64
ensure
PG::Connection.const_set(:PROGRAM_NAME, old_script_name)
PG::Connection.class_eval("PROGRAM_NAME=PG.make_shareable(#{old_script_name.inspect})")
end
end
end
Expand Down

0 comments on commit d9e08a0

Please sign in to comment.