Class: PG::AzureWorkloadIdentity::ConnectionInfo::KeyValueString

Inherits:
Object
  • Object
show all
Defined in:
lib/pg/azure_workload_identity/connection_info/key_value_string.rb

Overview

Parses and manipulates a PostgreSQL connection string in libpq's key=value format (e.g. "host=localhost user=admin port=5432").

The parser preserves functional parity with libpq's conninfo_parse, supporting whitespace around the =, single-quoted values, and backslash escapes (\\X -> X) in both quoted and unquoted values.

The non-standard azure_workload_identity parameter is recognized and extracted on construction so it can drive Azure Workload Identity authentication without being forwarded to libpq.

Defined Under Namespace

Classes: Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_string) ⇒ KeyValueString

Parses the given connection string and extracts the azure_workload_identity parameter (if any) so it is not forwarded to libpq.

Parameters:

  • connection_string (String)

    a PostgreSQL key=value connection string.

Raises:

  • (Error)

    if the connection string is malformed.



36
37
38
39
# File 'lib/pg/azure_workload_identity/connection_info/key_value_string.rb', line 36

def initialize(connection_string)
  @params = Parser.new(connection_string).parse
  @azure_workload_identity = @params.delete(:azure_workload_identity)
end

Instance Attribute Details

#azure_workload_identityString? (readonly)

Returns the value of the azure_workload_identity parameter that was present in the original connection string, or nil if the parameter was not set.

Returns:

  • (String, nil)

    the value of the azure_workload_identity parameter that was present in the original connection string, or nil if the parameter was not set.



27
28
29
# File 'lib/pg/azure_workload_identity/connection_info/key_value_string.rb', line 27

def azure_workload_identity
  @azure_workload_identity
end

Instance Method Details

#hostString?

Returns the value of the host parameter.

Returns:

  • (String, nil)

    the value of the host parameter.



47
48
49
# File 'lib/pg/azure_workload_identity/connection_info/key_value_string.rb', line 47

def host
  @params[:host]
end

#password=(value) ⇒ String

Sets the password parameter.

Parameters:

  • value (String)

    the password to set.

Returns:

  • (String)

    the value that was set.



60
61
62
# File 'lib/pg/azure_workload_identity/connection_info/key_value_string.rb', line 60

def password=(value)
  @params[:password] = value
end

#portString?

Returns the value of the port parameter.

Returns:

  • (String, nil)

    the value of the port parameter.



52
53
54
# File 'lib/pg/azure_workload_identity/connection_info/key_value_string.rb', line 52

def port
  @params[:port]
end

#to_hHash{Symbol => String}

Returns a copy of the parsed parameters (excluding azure_workload_identity, which was extracted at construction).

Returns:

  • (Hash{Symbol => String})

    a copy of the parsed parameters (excluding azure_workload_identity, which was extracted at construction).



78
79
80
# File 'lib/pg/azure_workload_identity/connection_info/key_value_string.rb', line 78

def to_h
  @params.dup
end

#to_sString

Serializes the parameters back to a libpq key=value connection string. Values are quoted/escaped via PG::Connection.quote_connstr so they remain valid when whitespace or special characters are present. The azure_workload_identity parameter extracted at construction is not included.

Returns:

  • (String)

    the connection string.



71
72
73
# File 'lib/pg/azure_workload_identity/connection_info/key_value_string.rb', line 71

def to_s
  @params.map { |key, value| "#{key}=#{PG::Connection.quote_connstr(value)}" }.join(" ")
end

#userString?

Returns the value of the user parameter.

Returns:

  • (String, nil)

    the value of the user parameter.



42
43
44
# File 'lib/pg/azure_workload_identity/connection_info/key_value_string.rb', line 42

def user
  @params[:user]
end