postgres/db
db-connect
(db-connect [connstr :cstr] :ptr<void>)
open a connection to a PostgreSQL server.
| connstr | libpq connection string, e.g. "host=localhost dbname=mydb" |
(ok conn-handle) on success; (err 0) on failure. Inspect with ok? / ok-val / err-val from stdlib/result.
(let [r (db-connect "host=localhost dbname=test user=postgres")]
(if (ok? r) (let [conn (ok-val r)] ...) (println "connect failed")))
Since: PG0
db-close
(db-close [conn :int] :ptr<void>)
close a connection opened with db-connect.
| conn | connection handle (ok-val from db-connect) |
(ok 0) always.
(db-close conn)
Since: PG0
db-exec
(db-exec [conn :int sql :cstr] :ptr<void>)
execute a SQL statement that returns no rows.
| conn | connection handle | |
| sql | DDL or DML SQL string |
(ok 0) on success; (err 0) on failure.
(db-exec conn "CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY, name TEXT)")
Since: PG0
db-query
(db-query [conn :int sql :cstr] :ptr<void>)
execute a SELECT and return a rows handle.
| conn | connection handle | |
| sql | SELECT SQL string (no $N placeholders; use db-query-params for those) |
(ok rows-handle) on success; (err 0) on failure. Free the rows-handle with rows-free from postgres/row when done.
(let [r (db-query conn "SELECT id, name FROM users")]
(if (ok? r)
(let [rows (ok-val r)]
(println (rows-count rows))
(rows-free rows))
(println "query failed")))
Since: PG0
db-query-params
(db-query-params [conn :int sql :cstr params :int] :ptr<void>)
execute a parameterized SELECT with $1,$2,... placeholders.
| conn | connection handle | |
| sql | SELECT SQL string with $1,$2,... placeholders | |
| params | cons list of :cstr parameter values (positional, 1-indexed) |
(ok rows-handle) on success; (err 0) on failure. Free the rows-handle with rows-free from postgres/row when done.
(let [r (db-query-params conn "SELECT * FROM users WHERE name = $1"
(cons "Alice" 0))]
(if (ok? r) (let [rows (ok-val r)] ...) (println "query failed")))
Since: PG1
db-begin
(db-begin [conn :int] :ptr<void>)
begin a transaction.
| conn | connection handle |
(ok 0) on success; (err 0) on failure.
(db-begin conn)
Since: PG1
db-commit
(db-commit [conn :int] :ptr<void>)
commit the current transaction.
| conn | connection handle |
(ok 0) on success; (err 0) on failure.
(db-commit conn)
Since: PG1
db-rollback
(db-rollback [conn :int] :ptr<void>)
roll back the current transaction.
| conn | connection handle |
(ok 0) on success; (err 0) on failure.
(db-rollback conn)
Since: PG1
Internal definitions
__ok-- create an ok result wrapping integer value v.__err-- create an err result wrapping integer error value e.__ok?-- return true if a result is ok.__ok-val-- extract the ok value from a result.__nil-- return the empty list sentinel (0).__cons-- prepend a value to a cons list; returns new cell as :int.__params-count-- count the number of cells in a cons list.__params-values-- build a heap-allocated char** from a cons list of :cstr.__pq-connect-raw-- connect to postgres; returns PGconn* as :int (0 on failure).__pq-finish-raw-- close a PGconn*.__pq-exec-raw-- execute SQL; returns 1 on success, 0 on failure.__pq-query-raw-- execute SELECT; returns PGresult* as :int (0 on failure).__pq-query-params-raw-- PQexecParams; returns PGresult* as :int (0 on failure).