Module rdbms

Public RDBMS API — thin facade over the poolboy / rdbms_worker / rdbms_pgsql stack.

Description

Public RDBMS API — thin facade over the poolboy / rdbms_worker / rdbms_pgsql stack.

All functions check out a connection from the rdbms_pool poolboy pool and execute synchronously in the caller's process via gen_server:call/2.

Result types:

Data Types

rdbms_result()

rdbms_result() = {selected, [tuple()]} | {updated, non_neg_integer()} | {updated, non_neg_integer(), [tuple()]} | {error, duplicate_key | term()}

Function Index

sql_query/1Execute a parameterised SQL statement with no bind parameters.
sql_query/2Execute a parameterised SQL statement with bind parameters.
squery/1Execute a simple (non-parameterised) SQL statement.
transaction/1Run Fun(Conn) inside a single PostgreSQL transaction.

Function Details

sql_query/1

sql_query(SQL::iodata()) -> rdbms_result()

SQL: The SQL statement (iodata).

returns: A normalised rdbms_result().

Execute a parameterised SQL statement with no bind parameters.

Equivalent to sql_query(SQL, []).

sql_query/2

sql_query(SQL::iodata(), Params::[term()]) -> rdbms_result()

SQL: The SQL statement, with $1, $2... placeholders.
Params: A list of values matching the placeholders.

returns: A normalised rdbms_result().

Execute a parameterised SQL statement with bind parameters.

The statement is sent through epgsql:equery/3 and the result is normalised into a uniform shape (see module doc).

squery/1

squery(SQL::iodata()) -> term()

SQL: The SQL statement (iodata).

returns: The raw epgsql result.

Execute a simple (non-parameterised) SQL statement.

Used for DDL (CREATE TABLE, CREATE INDEX, ...) where parameter binding is not supported by the protocol. The raw epgsql return value is passed through unchanged so callers can pattern-match on [{ok, _}, {ok, _}, ...] for multi-statement scripts.

transaction/1

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

Fun: Function of arity 1 taking the epgsql connection.

returns: {ok, T} on COMMIT (where T is Funs return value), or {error, Reason} on ROLLBACK.

Run Fun(Conn) inside a single PostgreSQL transaction.

Fun receives the raw epgsql connection. Use rdbms_pgsql:query/3 inside the fun for normalised results. Throwing any term inside Fun triggers a ROLLBACK; the thrown value is returned as {error, Reason}. Returning {rollback, Reason} is preferred over throw — it avoids stacktrace cost for the common conflict path.


Generated by EDoc