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

Re-exported through pg_connection.nim; depends only on types.nim (in particular, does not touch PgConnection).

Procs

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

Apply a single connection parameter to a ConnConfig.

Note: host/hostaddr/port are applied as scalars here; the DSN parsers intercept these keys and expand their comma-separated multi-host forms via buildHosts instead of routing them through this proc.

proc initConnConfig(host = "127.0.0.1"; port = 5432; hostaddr = ""; user = "";
                    password = ""; database = ""; sslMode = sslPrefer;
                    sslRootCert = ""; 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): ConnConfig {.
    ...raises: [], 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],
    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' Within quoted values, \' and \\ are escape sequences.

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], tags: [], forbids: [].}
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 parseTargetSessionAttrs(s: string): TargetSessionAttrs {....raises: [PgError],
    tags: [], forbids: [].}
proc parseUriDsn(dsn: string): ConnConfig {....raises: [PgError],
    tags: [ReadIOEffect], forbids: [].}
Parse a PostgreSQL URI connection string into a ConnConfig.