vrischmann / zig-sqlite

zig-sqlite is a small wrapper around sqlite's C API, making it easier to use with Zig.
MIT License
367 stars 49 forks source link

Enable features without vendoring #161

Closed malcolmstill closed 3 months ago

malcolmstill commented 3 months ago

Description

This PR adds the ability to enable SQLite features (at this point only fts5) via build system options and in doing so allows customising SQLite features without resorting to vendoring (as per https://github.com/vrischmann/zig-sqlite?tab=readme-ov-file#using-the-bundled-sqlite-source-code-file).

This means for example you can take the example of installation via the official package manager (https://github.com/vrischmann/zig-sqlite?tab=readme-ov-file#official-package-manager) and simply add, e.g., .fts5 = true to the options to enable full text search:

const sqlite = b.dependency("sqlite", .{
    .target = target,
    .optimize = optimize,
    .fts5 = true,
});

exe.root_module.addImport("sqlite", sqlite.module("sqlite"));

// links the bundled sqlite3, so leave this out if you link the system one
exe.linkLibrary(sqlite.artifact("sqlite"));

A EnableOptions struct is added enumerating boolean options and instead of a static array with only -std=c99 as an option, we make the flags an ArrayList with the same initial content but use a comptime inline for to iterate over each field of EnableOptions to create a b.option and add to the flags ArrayList when the given flag has been enabled. As reference this is the approach taken in https://github.com/mitchellh/zig-build-libxml2/blob/main/build.zig.

As it stands this PR makes it easy to any SQLITE_ENABLE_* options by adding to the EnableOptions struct. This could be expanded to handle other options and indeed non-boolean options.

Also updates std.rand -> std.Random as introduced in recent zig master.

vrischmann commented 3 months ago

Thanks ! this looks great.

malcolmstill commented 3 months ago

Looks like a recent zig master has moved std.rand to std.Random...I've updated the PR.

vrischmann commented 3 months ago

Thank you. I'll merge as is because the windows tests failing have nothing to do with your PR.

malcolmstill commented 3 months ago

Amazing, thanks @vrischmann!