- Zig 99.8%
- Nix 0.2%
| lib | ||
| tools | ||
| .envrc | ||
| .gitignore | ||
| build.zig | ||
| build.zig.zon | ||
| flake.lock | ||
| flake.nix | ||
| LICENSE | ||
| README.md | ||
| treefmt.nix | ||
quarry
Quarry is a database toolkit and object-relational mapper for Zig. It exposes one connection, statement, cursor, transaction, and model API across six database families.
Every driver has two backends:
- a self-contained Zig implementation, selected by default; and
- an implementation backed by the database's system C library, selected with
Zig's standard
-fsys=<name>integration switch.
An application therefore remains usable without a C database dependency, but can opt into the vendor library when compatibility with that library matters.
Drivers
| Driver | URL schemes | Default backend | -fsys backend |
|---|---|---|---|
| PostgreSQL | postgres, postgresql |
PostgreSQL wire protocol | libpq |
| SQLite | sqlite |
sqlite.zig | SQLite C API |
| MySQL/MariaDB | mysql, mariadb |
MySQL wire protocol through myzql | MariaDB Connector/C |
| SQL Server | sqlserver, mssql |
TDS | ODBC |
| libSQL/Turso | libsql, turso |
Hrana 3 over HTTP | libsql-c |
| DuckDB | duckdb |
Quarry's Zig columnar engine | DuckDB C API |
The pure and system variants implement the same Quarry interface and run the
same live CRUD, cursor, and transaction conformance suite. PostgreSQL also
supports streaming COPY IN and COPY OUT. The other drivers report
error.CopyModeMismatch for those PostgreSQL-specific operations.
Transactions, prepared statements, and savepoints are available on both
backends except that native DuckDB does not implement savepoints and returns
error.Unimplemented. The pure Zig DuckDB engine does implement them.
Backend differences
The pure DuckDB backend is a separate implementation, not a Zig binding to
DuckDB. Persistent duckdb: databases use Quarry's versioned columnar
snapshot format (QRYDUCK1) and are not byte-compatible with files created by
native DuckDB. Select -fsys=duckdb to read and write native DuckDB files.
The pure SQL Server backend defaults to an unencrypted TDS 7 connection. Use
?encrypt=strict for TDS 8 with TLS established before any TDS message.
Certificates and the server name are verified unless
trust-server-certificate=true is explicitly set.
The system SQL Server backend uses the ODBC driver manager. A suitable SQL Server ODBC driver must also be installed and configured at runtime.
The pure libSQL backend connects to remote Hrana endpoints. The libsql-c
backend can also open a local libsql:path/to/database.db database.
Adding Quarry to a build
Add Quarry to build.zig.zon, then import its module:
const quarry_dependency = b.dependency("quarry", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("quarry", quarry_dependency.module("quarry"));
Pure Zig drivers are the default. Select any system integrations when invoking the top-level build:
zig build -fsys=postgres -fsys=sqlite
The corresponding development libraries must be discoverable by the compiler and linker. Quarry uses libpq, SQLite, MariaDB Connector/C, unixODBC, libsql-c, and DuckDB respectively.
Opening connections
quarry.open chooses a driver from the URL scheme. It accepts an allocator,
the Zig I/O instance, the URL, and optional limits, diagnostics, environment,
or a custom registration set:
const std = @import("std");
const quarry = @import("quarry");
var connection = try quarry.open(gpa, io,
"postgresql://app:secret@localhost:5432/shop?sslmode=require", .{});
defer connection.close();
try connection.ping();
Common URL forms are:
postgresql://app:secret@db.example:5432/shop?sslmode=require
postgresql:///shop?host=/run/postgresql&user=app&sslmode=disable
sqlite:var/data/app.sqlite
sqlite::memory:
mysql://app:secret@db.example:3306/shop?ssl-mode=required
mysql://root@localhost/shop?socket=/run/mysqld/mysqld.sock&ssl=false
sqlserver://app:secret@db.example:1433/shop?encrypt=strict
libsql://database.turso.io?auth-token=secret
libsql:var/data/local.db
duckdb:var/data/analytics.duckdb
PostgreSQL also reads the standard libpq environment variables when an
environment map is supplied to OpenOptions. MySQL uses MYSQL_HOST,
MYSQL_TCP_PORT, MYSQL_USER, MYSQL_DATABASE, and MYSQL_PWD; SQL Server
uses the equivalent MSSQL_* variables. libSQL accepts TURSO_DATABASE_URL
or LIBSQL_URL and TURSO_AUTH_TOKEN or LIBSQL_AUTH_TOKEN.
Ephemeral databases for tests
Every driver exports a testing module. Its Server owns a database isolated
under std.testing.tmpDir(), exposes a ready-to-open url, and removes the
directory in deinit. This lets downstream packages test the same driver they
ship without Docker or a shared developer database:
test "repository works with PostgreSQL" {
var server = try quarry.postgres.testing.Server.start(
std.testing.allocator,
std.testing.io,
.{},
);
defer server.deinit();
var connection = try quarry.open(
std.testing.allocator,
std.testing.io,
server.url,
.{},
);
defer connection.close();
// Run the downstream package's normal test suite here.
}
The helpers are intended for Zig test builds because they use
std.testing.tmpDir().
| Module | What it provisions | Required executable |
|---|---|---|
quarry.postgres.testing |
PostgreSQL cluster and Unix socket | initdb, postgres, pg_isready, createdb |
quarry.mysql.testing |
MariaDB data directory and Unix socket | mariadb-install-db, mariadbd, mariadb-admin, mariadb |
quarry.sqlite.testing |
Embedded temporary database file | none |
quarry.duckdb.testing |
Embedded temporary database file | none |
quarry.libsql.testing.Server |
Local Hrana service | sqld |
quarry.libsql.testing.Database |
Local libsql-c database file | none beyond libsql-c |
quarry.sqlserver.testing |
Native SQL Server process | sqlservr |
PostgreSQL and MariaDB bind only Unix sockets; sqld and SQL Server bind a
random high loopback port. The SQL Server helper returns error.FileNotFound
when Microsoft does not publish sqlservr for the host platform. Quarry's own
test translates that case into a skip. It does not use CPU or container
emulation.
Each helper accepts an Options value for executable paths and service-specific
settings. Keep the value alive until all connections are closed, then call
deinit to stop the child and clean up its files.
Developing Quarry
Run the default pure-Zig matrix with:
zig build test
Run a system backend with its standard integration switch:
zig build test -fsys=mysql
The complete local matrix is:
zig build test
zig build test -fsys=postgres
zig build test -fsys=sqlite
zig build test -fsys=mysql
zig build test -fsys=sqlserver
zig build test -fsys=libsql
zig build test -fsys=duckdb
nix develop supplies Zig and all open-source service executables and C
libraries used by that matrix. nix flake check runs it in the Nix build.
Microsoft SQL Server itself is not in the Nix shell, so its native-service
test is skipped when sqlservr is unavailable; both SQL Server driver
backends still compile and run their unit tests.
Protocol parsers for PostgreSQL, MySQL, TDS, and Hrana include malformed-input fuzz targets. Zig runs their corpus paths during the normal suite and can run the same tests under its fuzzing mode for sustained mutation.
Requirements
Quarry requires Zig 0.17.0 or newer. The Nix development shell currently pins Zig 0.17.