PostgreSQL client bindings for the Zero programming language, wrapping libpq via Zero's C interop layer.
This is the first PostgreSQL package for Zero. It is at skeleton stage — the FFI call wiring is stubbed pending verification of Zero v0.1.1's C interop call syntax for pointer-returning functions and out-pointer patterns.
zero check --jsonpasses (Zero v0.1.1, darwin-x64)- C function calls are marked
// SKELETONin each module - All wrapper shapes, enums, choice types, and control flow are complete
- Replace SKELETON sections once out-pointer FFI syntax is confirmed
Run from the repo root:
zero test zero.json # run the 68 guard/type tests
zero check --json . # type-check without building
zero build --out .zero/out/zero-postgres .The package links against libpq dynamically. On macOS with MacPorts:
sudo port install postgresql16On Linux:
apt install libpq-devThe zero.json declares "pkg_config": "libpq" so the compiler can discover
include and library paths via pkg-config libpq.
All symbols are in flat scope after use. Functions use the pg prefix to
avoid collisions when a consuming package uses multiple modules.
use connection
// Open a connection
let result = pgConnect("host=localhost dbname=mydb user=me password=secret")
match result {
.connected => conn { ... }
.failed => msg { ... }
}
// Check status
pgStatus(&conn) -> Bool
// Get last error
pgErrorMessage(&conn) -> String
// Close
pgClose(&mut conn) -> Void
Shapes and choices:
shape PgConnection { handle: usize }
choice PgConnectResult {
connected: PgConnection,
failed: String
}
use query
let exec_result = pgExec(&conn, "SELECT id, name FROM users")
match exec_result {
.rows => qr {
let nr = pgRowCount(&qr)
let nc = pgFieldCount(&qr)
let col_name = pgFieldName(&qr, 0)
let val = pgGetValue(&qr, 0, 0)
pgClear(&mut qr)
}
.failed => msg { ... }
}
Shapes and choices:
shape PgQueryResult { handle: usize, nrows: i32, ncols: i32 }
choice PgExecResult {
rows: PgQueryResult,
failed: String
}
Low-level enum helpers for mapping C integer return codes:
enum ConnStatus { ok, bad }
enum ExecStatus { commandOk, tuplesOk, fatalError, other }
pgConnStatusFromInt(code: i32) -> ConnStatus
pgExecStatusFromInt(code: i32) -> ExecStatus
From libpq-fe.h:
| C function | Zero wrapper |
|---|---|
PQconnectdb |
pgConnect |
PQstatus |
pgStatus |
PQerrorMessage |
pgErrorMessage |
PQfinish |
pgClose |
PQexec |
pgExec |
PQresultStatus |
internal in pgExec |
PQntuples |
internal in pgExec |
PQnfields |
internal in pgExec |
PQgetvalue |
pgGetValue |
PQfname |
pgFieldName |
PQclear |
pgClear |
vendor/include/libpq-fe-stub.h is a minimal stub declaring only the
functions above. It is used for Zero compiler type-model extraction.
The real libpq is linked at build time.
- Out-pointer FFI pattern (for functions that return handles via
**params) is not yet verified in Zero v0.1.1 Stringtoconst char*mapping through thelibpqalias is unverifiedchar*return values from C may require explicit span conversion
Apache-2.0. The libpq library itself is released under the PostgreSQL License.