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

Readme out of Date Linking Instructions #123

Closed nathananderson98 closed 1 year ago

nathananderson98 commented 1 year ago

Took me a bit of time looking through the std lib and discord to get the right calls, just two changed when linking with the bundled sqlite source code. I have a branch with the two readme edits I'm happy to submit to PR.

Changed relevant Readme section to:

Using the bundled sqlite source code file

If you want to use the bundled sqlite source code file, first you need to add it as a static library in your build.zig file:

const sqlite = b.addStaticLibrary(.{ .name = "sqlite", .target = target, .optimize = .ReleaseSafe });
sqlite.addCSourceFile("third_party/zig-sqlite/c/sqlite3.c", &[_][]const u8{"-std=c99"});
sqlite.linkLibC();

If you need to define custom compile-time options for sqlite, modify the flags (second argument to addCSourceFile).

Now it's just a matter of linking your build.zig target(s) to this library instead of the system one:

exe.linkLibrary(sqlite);
exe.addModule("sqlite", b.createModule(.{ .source_file = .{ .path = "third_party/zig-sqlite/sqlite.zig", } }));
exe.addIncludePath("third_party/zig-sqlite/c");
vrischmann commented 1 year ago

Hi,

it would be great if you could submit a PR! Thanks.

nathananderson98 commented 1 year ago

I tried to push a branch up but it says I don't have access, do I need to like create my own Fork and then merge request it in?

vrischmann commented 1 year ago

I tried to push a branch up but it says I don't have access, do I need to like create my own Fork and then merge request it in?

Yes, that's the process. If you're not familiar with PR I can suggest the official GitHub documentation on this topic which is quite good.

nathananderson98 commented 1 year ago

Sorry its been a few days, happy to get working on that today!

As an aside, I just spent the last few days writing up some functions to create SQLite queries (SELECT, UPDATE, etc) as well as Table migration queries based on Types using comptime type reflection. Would this be something helpful to include in the repo somewhere? Or is this best left out? I only bring it up because I figure it could be helpful to others.

As an example, here is what the function looks like for creating an update query:

pub inline fn createUpdateQuery(comptime Type: type) []const u8 {
    comptime {
        var query: []const u8 = "UPDATE " ++ getTypeTableName(Type) ++ " SET ";
        inline for (@typeInfo(Type).Struct.fields, 0..) |field, i| {
            if (i != 0) {
                query = query ++ " = ?, ";
            }
            query = query ++ field.name;
        }
        query = query ++ " = ? WHERE id = ?;";
        return query;
    }
}

Then calling the function like this: query = createUpdateQuery(@TypeOf(.{.id = 1, .name = "Johnny", .email = "secret@mail.net"})); gives:

UPDATE users SET id = ?, name = ?, email = ? WHERE id = ?;
vrischmann commented 1 year ago

So it's a query builder essentially ? It could be put in a query_builder.zig file, exported and referenced with sqlite.query_builder.

So something like:

const query = sqlite.query_builder.createUpdateQuery(@TypeOf(.{.id = 1, .name = "Johnny", .email = "secret@mail.net"}));

What do you think ?

nathananderson98 commented 1 year ago

Working on a PR right now, hopefully can have it up later today.

vrischmann commented 1 year ago

Hi, it's been a while now.

I just fixed the linking instructions in the readme, addressing your original issue. Feel free to open another issue or submit a PR for the query builder stuff you mentioned.