Simplistic Comptime Column Safety in SQLite Queries

10 points by Shorden


joelg

I also maintain a zig SQLite wrapper nDimensional/zig-sqlite with comptime types and I'm very pleased with the ergonomics -- every statement has generic Params and Result types.

const Params = struct { min: f32 };
const Result = struct {
    id: sqlite.Text,
    reputation: ?f32,
};

// checks the param names and types
const select = try db.prepare(Params, Result,
    \\SELECT * FROM users WHERE reputation >= :min
);
defer select.finalize();

// `Params` struct expected
try select.bind(.{ .min = 0 });
defer select.reset();

while (try select.step()) |user| {
    // now we have a concrete `Result` struct!
    std.log.info("id: {s}, age: {d}", .{ user.id.data, user.reputation orelse 0 });
}

simple, easy-to-remember, works with default fields and a void param type, all just a couple hundred lines or so to wrap the C API.