Method: PGconn.parse_connect_args

Defined in:
lib/pg.rb

.parse_connect_args(*args) ⇒ String

Parse the connection args into a connection-parameter string

Parameters:

  • the connection parameters

Returns:

  • a connection parameters string



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pg.rb', line 35

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().
  connopts = []

  # Handle an options hash first
  if args.last.is_a?( Hash )
    opthash = args.pop 
    opthash.each do |key, val|
      connopts.push( "%s=%s" % [key, PGconn.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, PGconn.quote_connstr(val.to_s)] )
    end
  end

  return connopts.join(' ')
end