From 193183b6c27ac4c0019e6257397de43a2fa86e54 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Mon, 9 Aug 2021 01:38:46 -0700 Subject: [PATCH] add initial code implementation --- .gitignore | 4 ++++ build.zig | 23 +++++++++++++++++++++++ src/lib.zig | 11 +++++++++++ src/main.zig | 34 ++++++++++++++++++++++++++++++++++ src/sqlite3.zig | 38 ++++++++++++++++++++++++++++++++++++++ zig.mod | 7 +++++++ zigmod.lock | 2 ++ 7 files changed, 119 insertions(+) create mode 100644 .gitignore create mode 100644 build.zig create mode 100644 src/lib.zig create mode 100644 src/main.zig create mode 100644 src/sqlite3.zig create mode 100644 zig.mod create mode 100644 zigmod.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..fd26f70b854c526398b6062d58c1008254f8e159 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +zig-* +.zigmod +deps.zig +*.db diff --git a/build.zig b/build.zig new file mode 100644 index 0000000000000000000000000000000000000000..2f54914b1b510b5b630fe91258daf1e38e4dcad2 --- /dev/null +++ b/build.zig @@ -0,0 +1,23 @@ +const std = @import("std"); +const deps = @import("./deps.zig"); + +pub fn build(b: *std.build.Builder) void { + const target = b.standardTargetOptions(.{}); + + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("zig-zorm", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + deps.addAllTo(exe); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/src/lib.zig b/src/lib.zig new file mode 100644 index 0000000000000000000000000000000000000000..6399d1fff6b56c25e345013cce8b7d143be7aaed --- /dev/null +++ b/src/lib.zig @@ -0,0 +1,11 @@ +const std = @import("std"); + +const EngineType = enum { + sqlite3, +}; + +pub fn engine(etype: EngineType) type { + return switch (etype) { + .sqlite3 => @import("./sqlite3.zig"), + }; +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000000000000000000000000000000000000..4ba5eb69a3d068e2f442bd51629a1606f4f9fa38 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,34 @@ +const std = @import("std"); +const zorm = @import("zorm"); + +const string = []const u8; +const ULID = string; +const Time = string; + +const Package = struct { + id: u64, + uuid: ULID, + owner: ULID, + name: string, + created_on: Time, + remote: u64, + remote_id: string, + remote_name: string, + description: string, + license: string, + latest_version: string, + hook_secret: string, + star_count: u64, +}; + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + const alloc = &gpa.allocator; + + var db = try zorm.engine(.sqlite3).connect("/home/snuc/dev/zig-zorm/access.db"); + var list = try db.collect(alloc, Package, "select * from packages order by star_count desc limit 25"); + + for (list) |item| { + std.log.info("{s}", .{item.remote_name}); + } +} diff --git a/src/sqlite3.zig b/src/sqlite3.zig new file mode 100644 index 0000000000000000000000000000000000000000..0d002663b45ceeab83addb181ab0fdcfc7c869cc --- /dev/null +++ b/src/sqlite3.zig @@ -0,0 +1,38 @@ +const std = @import("std"); +const sqlite = @import("sqlite"); + +const Self = @This(); + +db: sqlite.Db = undefined, +mutex: std.Thread.Mutex, + +pub fn connect(path: [:0]const u8) !Self { + return Self{ + .db = try sqlite.Db.init(.{ + .mode = sqlite.Db.Mode{ .File = path }, + .open_flags = .{ + .write = true, + .create = true, + }, + .threading_mode = .SingleThread, + }), + .mutex = std.Thread.Mutex{}, + }; +} + +pub fn collect(self: *Self, alloc: *std.mem.Allocator, comptime T: type, comptime query: []const u8) ![]const T { + var stmt = try self.db.prepare(query); + defer stmt.deinit(); + var iter = try stmt.iterator(T, .{}); + var list = std.ArrayList(T).init(alloc); + while (try iter.next(.{ .allocator = alloc })) |row| { + try list.append(row); + } + return list.toOwnedSlice(); +} + +pub fn exec(self: *Self, comptime query: []const u8, args: anytype) !void { + var stmt = try self.db.prepare(query); + defer stmt.deinit(); + try stmt.exec(args); +} diff --git a/zig.mod b/zig.mod new file mode 100644 index 0000000000000000000000000000000000000000..c966ee115abdebdfe47166f88b48df2937837ae3 --- /dev/null +++ b/zig.mod @@ -0,0 +1,7 @@ +id: c8lag7k2cr3lxodwor24az8j61nblulbw1oxw3tl191wgoje +name: zorm +main: src/lib.zig +license: AGPL-3.0 +description: The ORM library for Zig. +dependencies: + - src: git https://github.com/vrischmann/zig-sqlite diff --git a/zigmod.lock b/zigmod.lock new file mode 100644 index 0000000000000000000000000000000000000000..07aecb5e56d82bbbf75e05f58d6783886bd73a00 --- /dev/null +++ b/zigmod.lock @@ -0,0 +1,2 @@ +2 +git https://github.com/vrischmann/zig-sqlite commit-1adb900dcc816a419a4b67ccae0555ffe33f72c4 -- 2.54.0