authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-09 01:38:46 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-09 01:38:46 -07:00
log193183b6c27ac4c0019e6257397de43a2fa86e54
treea52fa6fe486949e755f24be6a58cecd0a5f10797
parent5372a88d5acf3ae064b32c8f85e58288ac5c2dd0

add initial code implementation


7 files changed, 119 insertions(+), 0 deletions(-)

.gitignore created+4
......@@ -0,0 +1,4 @@
1zig-*
2.zigmod
3deps.zig
4*.db
build.zig created+23
......@@ -0,0 +1,23 @@
1const std = @import("std");
2const deps = @import("./deps.zig");
3
4pub fn build(b: *std.build.Builder) void {
5 const target = b.standardTargetOptions(.{});
6
7 const mode = b.standardReleaseOptions();
8
9 const exe = b.addExecutable("zig-zorm", "src/main.zig");
10 exe.setTarget(target);
11 exe.setBuildMode(mode);
12 deps.addAllTo(exe);
13 exe.install();
14
15 const run_cmd = exe.run();
16 run_cmd.step.dependOn(b.getInstallStep());
17 if (b.args) |args| {
18 run_cmd.addArgs(args);
19 }
20
21 const run_step = b.step("run", "Run the app");
22 run_step.dependOn(&run_cmd.step);
23}
src/lib.zig created+11
......@@ -0,0 +1,11 @@
1const std = @import("std");
2
3const EngineType = enum {
4 sqlite3,
5};
6
7pub fn engine(etype: EngineType) type {
8 return switch (etype) {
9 .sqlite3 => @import("./sqlite3.zig"),
10 };
11}
src/main.zig created+34
......@@ -0,0 +1,34 @@
1const std = @import("std");
2const zorm = @import("zorm");
3
4const string = []const u8;
5const ULID = string;
6const Time = string;
7
8const Package = struct {
9 id: u64,
10 uuid: ULID,
11 owner: ULID,
12 name: string,
13 created_on: Time,
14 remote: u64,
15 remote_id: string,
16 remote_name: string,
17 description: string,
18 license: string,
19 latest_version: string,
20 hook_secret: string,
21 star_count: u64,
22};
23
24pub fn main() !void {
25 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
26 const alloc = &gpa.allocator;
27
28 var db = try zorm.engine(.sqlite3).connect("/home/snuc/dev/zig-zorm/access.db");
29 var list = try db.collect(alloc, Package, "select * from packages order by star_count desc limit 25");
30
31 for (list) |item| {
32 std.log.info("{s}", .{item.remote_name});
33 }
34}
src/sqlite3.zig created+38
......@@ -0,0 +1,38 @@
1const std = @import("std");
2const sqlite = @import("sqlite");
3
4const Self = @This();
5
6db: sqlite.Db = undefined,
7mutex: std.Thread.Mutex,
8
9pub fn connect(path: [:0]const u8) !Self {
10 return Self{
11 .db = try sqlite.Db.init(.{
12 .mode = sqlite.Db.Mode{ .File = path },
13 .open_flags = .{
14 .write = true,
15 .create = true,
16 },
17 .threading_mode = .SingleThread,
18 }),
19 .mutex = std.Thread.Mutex{},
20 };
21}
22
23pub fn collect(self: *Self, alloc: *std.mem.Allocator, comptime T: type, comptime query: []const u8) ![]const T {
24 var stmt = try self.db.prepare(query);
25 defer stmt.deinit();
26 var iter = try stmt.iterator(T, .{});
27 var list = std.ArrayList(T).init(alloc);
28 while (try iter.next(.{ .allocator = alloc })) |row| {
29 try list.append(row);
30 }
31 return list.toOwnedSlice();
32}
33
34pub fn exec(self: *Self, comptime query: []const u8, args: anytype) !void {
35 var stmt = try self.db.prepare(query);
36 defer stmt.deinit();
37 try stmt.exec(args);
38}
zig.mod created+7
......@@ -0,0 +1,7 @@
1id: c8lag7k2cr3lxodwor24az8j61nblulbw1oxw3tl191wgoje
2name: zorm
3main: src/lib.zig
4license: AGPL-3.0
5description: The ORM library for Zig.
6dependencies:
7 - src: git https://github.com/vrischmann/zig-sqlite
zigmod.lock created+2
......@@ -0,0 +1,2 @@
12
2git https://github.com/vrischmann/zig-sqlite commit-1adb900dcc816a419a4b67ccae0555ffe33f72c4