async_postgres/pg_connection/dsn

DSN parsing for PostgreSQL connection strings.

Supports both libpq formats:

  • keyword=value: host=localhost port=5432 dbname=test
  • URI: postgresql://user:pass@host:port/db?param=value

Only initConnConfig / parseDsn are re-exported through pg_connection.nim; the intermediate parsers stay here. Depends only on types.nim (does not touch PgConnection).

Procs

proc applyParam(result: var ConnConfig; key, val: string) {....raises: [PgError,
    PgError, PgError, PgError, PgError, PgError, PgError, PgError, PgError],
    tags: [ReadIOEffect], forbids: [].}

Apply a single connection parameter to a ConnConfig.

host/hostaddr/port accept comma-separated multi-host lists and rebuild hosts on each call. The DSN parsers still intercept these keys: there all values are collected first and expanded once, so their correlation is order-independent, while repeated applyParam calls correlate each list against the already-expanded state.

proc initConnConfig(host = "127.0.0.1"; port = 5432; hostaddr = ""; user = "";
                    password = ""; database = ""; sslMode = sslPrefer;
                    sslRootCert = ""; sslCert = ""; sslKey = ""; sslSni = true;
                    channelBinding = cbPrefer; applicationName = "";
                    connectTimeout = ZeroDuration; keepAlive = true;
                    keepAliveIdle = 0; keepAliveInterval = 0;
                    keepAliveCount = 0; hosts: seq[HostEntry] = @[];
                    targetSessionAttrs = tsaAny; loadBalanceHosts = lbhDisable;
                    requireAuth: set[AuthMethod] = {};
                    extraParams: seq[(string, string)] = @[];
                    maxMessageSize = 0; maxScramIterations = 0;
                    sslNegotiation = sslnPostgres): ConnConfig {.
    ...raises: [PgError], tags: [], forbids: [].}
Create a connection configuration with sensible defaults. For DSN-based configuration, use parseDsn instead.
proc parseAuthMethod(s: string): AuthMethod {....raises: [PgError], tags: [],
    forbids: [].}
proc parseChannelBindingMode(s: string): ChannelBindingMode {....raises: [PgError],
    tags: [], forbids: [].}
proc parseDsn(dsn: string): ConnConfig {....raises: [PgError, ValueError],
    tags: [ReadIOEffect], forbids: [].}

Parse a PostgreSQL connection string into a ConnConfig.

Supports two formats:

  • URI: postgresql://[user[:password]@][host[:port]][/database][?param=value&...]
  • keyword=value: host=localhost port=5432 dbname=test (libpq compatible)

Both postgresql:// and postgres:// schemes are accepted for URI format.

proc parseKeyValueDsn(dsn: string): ConnConfig {....raises: [PgError],
    tags: [ReadIOEffect], forbids: [].}

Parse a libpq keyword=value connection string into a ConnConfig.

Format: host=localhost port=5432 dbname=test user=myuser

Values may be single-quoted: password='has spaces' As in libpq, a backslash escapes the following character both inside and outside quotes (\', \\, host=foo\ bar).

host, hostaddr, and port accept comma-separated lists for multi-host failover (libpq compatible): host=h1,h2 port=5433,5434.

proc parseLoadBalanceHosts(s: string): LoadBalanceHosts {....raises: [PgError],
    tags: [], forbids: [].}
proc parsePort(s: string): int {....raises: [PgError, PgError], tags: [],
                                 forbids: [].}
Follows libpq's strtol-based rules: surrounding whitespace and a leading sign are accepted (libpq takes +5432 too), digit-group underscores are not, and the final value must be in 1–65535.
proc parseRequireAuth(s: string): set[AuthMethod] {....raises: [PgError], tags: [],
    forbids: [].}
Parse a comma-separated list of auth method names into a set (libpq require_auth syntax; negation prefix ! is not yet supported). Empty input returns the empty set (allow any).
proc parseSslMode(s: string): SslMode {....raises: [PgError], tags: [], forbids: [].}
proc parseSslNegotiation(s: string): SslNegotiation {....raises: [PgError],
    tags: [], forbids: [].}
proc parseTargetSessionAttrs(s: string): TargetSessionAttrs {....raises: [PgError],
    tags: [], forbids: [].}
proc parseUriDsn(dsn: string): ConnConfig {....raises: [PgError, ValueError],
    tags: [ReadIOEffect], forbids: [].}
Parse a PostgreSQL URI connection string into a ConnConfig.