From a3a5e3722554415fefd9a49037d0891fc5c1c0e7 Mon Sep 17 00:00:00 2001 From: Samuel Webb Date: Sun, 7 Apr 2024 13:58:52 -0400 Subject: [PATCH 1/5] Add sqlite/update jetzig --- build.zig | 12 +++++++++++- build.zig.zon | 8 ++++++-- src/app/views/root/_content.zmpl | 4 +++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/build.zig b/build.zig index d80454e..e488390 100644 --- a/build.zig +++ b/build.zig @@ -15,10 +15,20 @@ pub fn build(b: *std.Build) !void { // Example dependency: const iguanas_dep = b.dependency("iguanas", .{ .optimize = optimize, .target = target }); exe.root_module.addImport("iguanas", iguanas_dep.module("iguanas")); + + const sqlite = b.dependency("sqlite", .{ + .target = target, + .optimize = optimize, + }); + + 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")); // All dependencies **must** be added to imports above this line. - try jetzig.jetzigInit(b, exe, .{}); + try jetzig.jetzigInit(b, exe, .{.zmpl_version = .v2}); b.installArtifact(exe); diff --git a/build.zig.zon b/build.zig.zon index e14a6d7..bb50052 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -16,13 +16,17 @@ // internet connectivity. .dependencies = .{ .jetzig = .{ - .url = "https://github.com/jetzig-framework/jetzig/archive/31e8ae36e1d7f68166114eefb867c23df352d537.tar.gz", - .hash = "1220d368ce21549ccd721d5f303888986fff5714835b1f266a62e329f85eabec8f19", + .url = "https://github.com/jetzig-framework/jetzig/archive/68ca4d3bbad05880e6e38fc6d10e7a24e772081e.tar.gz", + .hash = "122047ebabab1dfe1bb7f96ffdac98410ffb4850b06dc6b1670ce74aaeaa49bdc08e", }, .iguanas = .{ .url = "https://github.com/jetzig-framework/iguanas/archive/89c2abf29de0bc31054a9a6feac5a6a83bab0459.tar.gz", .hash = "12202fd319a5ab4e124b00e8ddea474d07c19c4e005d77b6c29fc44860904ea01a5c", }, + .sqlite = .{ + .url = "https://github.com/vrischmann/zig-sqlite/archive/a3095ac.tar.gz", + .hash = "12203f7d14722debe8f1a2168f28a262aaa5ec2457a7a975fc7dcc1123ed11597c98", + } }, .paths = .{ // This makes *all* files, recursively, included in this package. It is generally diff --git a/src/app/views/root/_content.zmpl b/src/app/views/root/_content.zmpl index 6d3a665..39bf503 100644 --- a/src/app/views/root/_content.zmpl +++ b/src/app/views/root/_content.zmpl @@ -13,6 +13,8 @@
Visit jetzig.dev to get started.
Join our Discord server and introduce yourself:
- https://discord.gg/eufqssz7X6 + Search for an artist, album, or song: + +
From fee65c4b9d510a085be8ca153cfe989f19c4d7de Mon Sep 17 00:00:00 2001 From: Samuel Webb Date: Sun, 7 Apr 2024 15:41:57 -0400 Subject: [PATCH 2/5] Begin SQL testing --- build.zig.zon | 4 ++-- src/db.zig | 0 src/main.zig | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/db.zig diff --git a/build.zig.zon b/build.zig.zon index bb50052..578092d 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -24,8 +24,8 @@ .hash = "12202fd319a5ab4e124b00e8ddea474d07c19c4e005d77b6c29fc44860904ea01a5c", }, .sqlite = .{ - .url = "https://github.com/vrischmann/zig-sqlite/archive/a3095ac.tar.gz", - .hash = "12203f7d14722debe8f1a2168f28a262aaa5ec2457a7a975fc7dcc1123ed11597c98", + .url = "https://github.com/vrischmann/zig-sqlite/archive/6af21bb.tar.gz", + .hash = "12202799861d92c7880d1a74f95be099752d7f0420a0c9628dc923ca2b1a22b7bda8", } }, .paths = .{ diff --git a/src/db.zig b/src/db.zig new file mode 100644 index 0000000..e69de29 diff --git a/src/main.zig b/src/main.zig index c7438a3..922472f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4,6 +4,8 @@ pub const jetzig = @import("jetzig"); pub const routes = @import("routes"); +pub const sqlite = @import("sqlite"); + // Override default settings in `jetzig.config` here: pub const jetzig_options = struct { /// Middleware chain. Add any custom middleware here, or use middleware provided in @@ -85,6 +87,37 @@ pub const jetzig_options = struct { }; pub fn main() !void { + var db = try sqlite.Db.init(.{ + .mode = sqlite.Db.Mode{ .File = "/home/swebb/Source/zuletzt/src/app/database/data.db" }, + .open_flags = .{ + .write = true, + .create = true, + }, + .threading_mode = .MultiThread, + }); + + const create = + \\CREATE TABLE artists ('artist', 'plays') + ; + + const query = + \\INSERT INTO artists ('artist', 'plays') VALUES (?,?) + ; + + var build = try db.prepare(create); + defer build.deinit(); + + try build.exec(.{},.{}); + + var stmt = try db.prepare(query); + defer stmt.deinit(); + + try stmt.exec(.{}, .{ + .artist = "Wilco", + .plays = 2500, + }); + + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer std.debug.assert(gpa.deinit() == .ok); const allocator = gpa.allocator(); From 199dc0ca49cd9c30d5e2628e1a0b2e254ecc9e93 Mon Sep 17 00:00:00 2001 From: Samuel Webb Date: Sun, 7 Apr 2024 16:19:35 -0400 Subject: [PATCH 3/5] Partial upgrade to ZMPL v2 --- .gitignore | 1 + src/app/views/root/_content.zmpl | 2 +- src/app/views/root/index.zmpl | 10 ++++++---- src/main.zig | 12 ++++++------ 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index cbcae4c..1b3afe7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ zig-cache/ *.core static/ .jetzig +src/app/database/ \ No newline at end of file diff --git a/src/app/views/root/_content.zmpl b/src/app/views/root/_content.zmpl index 39bf503..088ab6b 100644 --- a/src/app/views/root/_content.zmpl +++ b/src/app/views/root/_content.zmpl @@ -1,4 +1,4 @@ -// Renders the `message` response data value. +

{.message}

diff --git a/src/app/views/root/index.zmpl b/src/app/views/root/index.zmpl index cccb8d8..9473e7a 100644 --- a/src/app/views/root/index.zmpl +++ b/src/app/views/root/index.zmpl @@ -9,12 +9,14 @@
- // If present, renders the `message_param` response data value, add `?message=hello` to the - // URL to see the output: +

{.message_param}

- // Renders `src/app/views/root/_content.zmpl` with the same template data available: -
{^root/content}
+ +
+ @partial root/content +
diff --git a/src/main.zig b/src/main.zig index 922472f..9d5b43a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -96,18 +96,18 @@ pub fn main() !void { .threading_mode = .MultiThread, }); - const create = - \\CREATE TABLE artists ('artist', 'plays') - ; + //const create = + // \\CREATE TABLE artists ('artist', 'plays') + //; const query = \\INSERT INTO artists ('artist', 'plays') VALUES (?,?) ; - var build = try db.prepare(create); - defer build.deinit(); + //var build = try db.prepare(create); + //defer build.deinit(); - try build.exec(.{},.{}); + //try build.exec(.{},.{}); var stmt = try db.prepare(query); defer stmt.deinit(); From 46f0b5038c76c10b547873104a5d92b3e9c75aca Mon Sep 17 00:00:00 2001 From: Samuel Webb Date: Sun, 7 Apr 2024 16:29:33 -0400 Subject: [PATCH 4/5] Update to ZMPL v2 --- src/app/views/root.zig | 7 +++---- src/app/views/root/_content.zmpl | 5 +++-- src/app/views/root/index.zmpl | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/app/views/root.zig b/src/app/views/root.zig index 0fa146c..d567fc5 100644 --- a/src/app/views/root.zig +++ b/src/app/views/root.zig @@ -1,3 +1,4 @@ +const std = @import("std"); const jetzig = @import("jetzig"); /// `src/app/views/root.zig` represents the root URL `/` @@ -18,7 +19,7 @@ pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View { var root = try data.object(); // Add a string to the root object. - try root.put("message", data.string("Welcome to Jetzig!")); + try root.put("welcome_message", data.string("Welcome to Jetzig!")); // Request params have the same type as a `data.object()` so they can be inserted them // directly into the response data. Fetch `http://localhost:8080/?message=hello` to set the @@ -26,9 +27,7 @@ pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View { // present. const params = try request.params(); - if (params.get("message")) |value| { - try root.put("message_param", value); - } + try root.put("message_param", params.get("message")); // Set arbitrary response headers as required. `content-type` is automatically assigned for // HTML, JSON responses. diff --git a/src/app/views/root/_content.zmpl b/src/app/views/root/_content.zmpl index 088ab6b..89ce5ae 100644 --- a/src/app/views/root/_content.zmpl +++ b/src/app/views/root/_content.zmpl @@ -1,5 +1,6 @@ -

{.message}

+@args message: *ZmplValue +

{{message}}

@@ -15,6 +16,6 @@
Search for an artist, album, or song: - +
diff --git a/src/app/views/root/index.zmpl b/src/app/views/root/index.zmpl index 9473e7a..38e3ef7 100644 --- a/src/app/views/root/index.zmpl +++ b/src/app/views/root/index.zmpl @@ -11,11 +11,11 @@
-

{.message_param}

+

{{.message_param}}

- @partial root/content + @partial root/content(message: .welcome_message)
From dc959607f916a6db924fa603828ba3c2202674a8 Mon Sep 17 00:00:00 2001 From: Samuel Webb Date: Mon, 8 Apr 2024 20:19:24 -0400 Subject: [PATCH 5/5] Building db stuff --- .gitignore | 3 +-- src/app/database/data.db | Bin 0 -> 8192 bytes src/app/views/search.zig | 12 +++++++++ src/db.zig | 54 +++++++++++++++++++++++++++++++++++++++ src/main.zig | 34 ++++++++++++------------ 5 files changed, 84 insertions(+), 19 deletions(-) create mode 100644 src/app/database/data.db create mode 100644 src/app/views/search.zig diff --git a/.gitignore b/.gitignore index 1b3afe7..7932178 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ zig-out/ zig-cache/ *.core static/ -.jetzig -src/app/database/ \ No newline at end of file +.jetzig \ No newline at end of file diff --git a/src/app/database/data.db b/src/app/database/data.db new file mode 100644 index 0000000000000000000000000000000000000000..6ba499c9458a58ca7ba31db1d16cf47b60c79684 GIT binary patch literal 8192 zcmeI%F-yZh6bJCTiwX+y3~DPB>02ZUqJ#YaZ9^stMHA>C4pB~Xk~SqdkbV|dXFq{% z4vzg8{Q!Oduh?#aWN;Av@9uGTk2gGi>qQqmZ4}+*#V|2+#CCqrtCDa+9f~ zFuFA5;t;`Q*oi}mJKbJLiy|HR<1Cq!{xSLACI85kZMG^~Ot$1yzR4H)wE0|ZRU8`a z5P$##AOHafKmY;|fB*y_0D*rZu;DqPzWzSY*=;Uo?COuk_G*qe;GgGubWcH{hh;u; zcO9|sQ2#NbK%0rTV>{SKoGL~Alp>|d+O{K3otZ7sAf5m0N2btYRoWT%ewy