Sha256: 5baf6dbde510047cf7c0e13c600687af98a25cc7cbf4d91103da46f9ab29976b

Contents?: true

Size: 1.99 KB

Versions: 6

Compression:

Stored size: 1.99 KB

Contents

#!/usr/bin/env ruby

require 'pg' unless defined?( PG )

# The PG connection class.
class PG::Connection

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


	### Quote the given +value+ for use in a connection-parameter string.
	def self::quote_connstr( value )
		return "'" + value.to_s.gsub( /[\\']/ ) {|m| '\\' + m } + "'"
	end


	### Parse the connection +args+ into a connection-parameter string. See PG::Connection.new
	### for valid arguments.
	def self::parse_connect_args( *args )
		return '' if args.empty?

		# This will be swapped soon for code that makes options like those required for
		# PQconnectdbParams()/PQconnectStartParams(). For now, stick to an options string for
		# PQconnectdb()/PQconnectStart().

		# Parameter 'fallback_application_name' was introduced in PostgreSQL 9.0
		# together with PQescapeLiteral().
		if PG::Connection.instance_methods.find{|m| m.to_sym == :escape_literal }
			appname = PG::Connection.quote_connstr( $0 )
			connopts = ["fallback_application_name=#{appname}"]
		else
			connopts = []
		end

		# Handle an options hash first
		if args.last.is_a?( Hash )
			opthash = args.pop
			opthash.each do |key, val|
				connopts.push( "%s=%s" % [key, PG::Connection.quote_connstr(val)] )
			end
		end

		# Option string style
		if args.length == 1 && args.first.to_s.index( '=' )
			connopts.unshift( args.first )

		# Append positional parameters
		else
			args.each_with_index do |val, i|
				next unless val # Skip nil placeholders

				key = CONNECT_ARGUMENT_ORDER[ i ] or
					raise ArgumentError, "Extra positional parameter %d: %p" % [ i+1, val ]
				connopts.push( "%s=%s" % [key, PG::Connection.quote_connstr(val.to_s)] )
			end
		end

		return connopts.join(' ')
	end


	# Backward-compatibility aliases for stuff that's moved into PG.
	class << self
		define_method( :isthreadsafe, &PG.method(:isthreadsafe) )
	end
end # class PG::Connection

# Backward-compatible alias
PGconn = PG::Connection

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pg-0.15.0-x86-mingw32 lib/pg/connection.rb
pg-0.15.0-x64-mingw32 lib/pg/connection.rb
pg-0.15.0 lib/pg/connection.rb
pg-0.15.0.pre.454-x64-mingw32 lib/pg/connection.rb
pg-0.15.0.pre.454-x86-mingw32 lib/pg/connection.rb
pg-0.15.0.pre.454 lib/pg/connection.rb