postgres/stmt
src/postgres/stmt.tur
defn
stmt-prepare
(stmt-prepare [conn :int name :cstr sql :cstr nparams :int] :ptr<void>)
compile a named prepared statement on the server.
Parameters
| conn | connection handle | |
| name | unique name for this prepared statement | |
| sql | SQL string with $1,$2,... placeholders | |
| nparams | number of parameters in sql |
Returns
(ok 0) on success; (err 0) on failure.
Example
(stmt-prepare conn "insert-user" "INSERT INTO users (name) VALUES ($1)" 1)
Since: PG2
defn
stmt-exec-prepared
(stmt-exec-prepared [conn :int name :cstr params :int] :ptr<void>)
execute a previously prepared statement with parameters.
Parameters
| conn | connection handle | |
| name | name used in stmt-prepare | |
| params | cons list of :cstr parameter values (positional, 1-indexed) |
Returns
(ok rows-handle) on success; (err 0) on failure. Free the rows-handle with rows-free from postgres/row when done.
Example
(let [r (stmt-exec-prepared conn "insert-user" (cons "Alice" 0))]
(if (ok? r) (rows-free (ok-val r)) (println "exec failed")))
Since: PG2
defn
stmt-deallocate
(stmt-deallocate [conn :int name :cstr] :ptr<void>)
drop a named prepared statement from the server.
Parameters
| conn | connection handle | |
| name | name used in stmt-prepare |
Returns
(ok 0) on success; (err 0) on failure.
Example
(stmt-deallocate conn "insert-user")
Since: PG2
Internal definitions
__ok-- create an ok result wrapping integer value v.__err-- create an err result wrapping integer error value e.__params-count-- count the number of cells in a cons list.__params-values-- build a heap-allocated char** from a cons list of :cstr.__dealloc-sql-- build "DEALLOCATE name" as a heap-allocated cstr.__pq-prepare-raw-- PQprepare; returns 1 on success, 0 on failure.__pq-exec-prepared-raw-- PQexecPrepared; returns PGresult* as :int (0 on failure).__pq-exec-raw-- execute SQL with no params; returns 1 on success, 0 on failure.