Module rdbms_pgsql

PostgreSQL driver layer — wraps epgsql and normalises its return values into a unified rdbms_result() type.

Description

PostgreSQL driver layer — wraps epgsql and normalises its return values into a unified rdbms_result() type.

epgsql return shapes (input): Normalised shapes (output):

Data Types

rdbms_result()

rdbms_result() = rdbms:rdbms_result()

Function Index

connect/1Open a new PostgreSQL connection.
disconnect/1Close a PostgreSQL connection.
query/3Run a parameterised query and normalise the result.
squery/2Run a simple (non-parameterised) statement.
transaction/2Run Fun(Conn) inside a BEGIN/COMMIT block.

Function Details

connect/1

connect(Opts::proplists:proplist()) -> {ok, epgsql:connection()} | {error, term()}

Opts: Proplist of connection options (host, port, username, password, database, ssl).

returns: {ok, Connection} or {error, Reason}.

Open a new PostgreSQL connection.

Connection options are taken from a proplist with the same shape as the postgres application environment key. Missing keys fall back to safe defaults (localhost:5432, user postgres).

disconnect/1

disconnect(Conn::epgsql:connection()) -> ok

Conn: The epgsql connection to close.

returns: ok (always).

Close a PostgreSQL connection.

Wraps epgsql:close/1 in catch so callers don't have to worry about a connection that has already died.

query/3

query(Conn::epgsql:connection(), SQL::iodata(), Params::[term()]) -> rdbms_result()

Conn: The epgsql connection.
SQL: SQL with $1, $2... placeholders.
Params: Bind parameters matching the placeholders.

returns: A normalised rdbms_result().

Run a parameterised query and normalise the result.

squery/2

squery(Conn::epgsql:connection(), SQL::iodata()) -> term()

Conn: The epgsql connection.
SQL: The SQL statement (iodata).

returns: The raw epgsql result.

Run a simple (non-parameterised) statement.

Used for DDL and transaction control (BEGIN, COMMIT, ROLLBACK). The raw epgsql result is returned unchanged.

transaction/2

transaction(Conn::epgsql:connection(), Fun::fun((epgsql:connection()) -> T)) -> {ok, T} | {error, term()}

Conn: The epgsql connection.
Fun: Function of arity 1 taking the connection.

returns: {ok, T} on COMMIT, or {error, Reason} on ROLLBACK.

Run Fun(Conn) inside a BEGIN/COMMIT block.

Fun should return its result directly on success. To abort and trigger a ROLLBACK, Fun returns {rollback, Reason} — this is preferred over throw because it avoids the stacktrace cost on the common conflict path. Throws are still caught for compatibility; unexpected exceptions are re-raised after rolling back.


Generated by EDoc