No matching definitions.

postgres/db

src/postgres/db.tur
defn

db-connect

(db-connect [connstr :cstr] :ptr<void>)

open a connection to a PostgreSQL server.

connstrlibpq 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

defn

db-close

(db-close [conn :int] :ptr<void>)

close a connection opened with db-connect.

connconnection handle (ok-val from db-connect)

(ok 0) always.

(db-close conn)

Since: PG0

defn

db-exec

(db-exec [conn :int sql :cstr] :ptr<void>)

execute a SQL statement that returns no rows.

connconnection handle
sqlDDL 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

defn

db-query

(db-query [conn :int sql :cstr] :ptr<void>)

execute a SELECT and return a rows handle.

connconnection handle
sqlSELECT 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

defn

db-query-params

(db-query-params [conn :int sql :cstr params :int] :ptr<void>)

execute a parameterized SELECT with $1,$2,... placeholders.

connconnection handle
sqlSELECT SQL string with $1,$2,... placeholders
paramscons 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

defn

db-begin

(db-begin [conn :int] :ptr<void>)

begin a transaction.

connconnection handle

(ok 0) on success; (err 0) on failure.

(db-begin conn)

Since: PG1

defn

db-commit

(db-commit [conn :int] :ptr<void>)

commit the current transaction.

connconnection handle

(ok 0) on success; (err 0) on failure.

(db-commit conn)

Since: PG1

defn

db-rollback

(db-rollback [conn :int] :ptr<void>)

roll back the current transaction.

connconnection 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).