From 79eac7d4f010f1e7b9f059ad7c6d9fe233f37f17 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Sun, 22 Oct 2023 16:56:02 -0700 Subject: [PATCH] add sqlite and a test command (lose WASI support, see note) With this commit, we lose WASI. This is ok here, because it's not like we're going to be saving our data on sqlite inside a Cloudflare worker. The problem actually doesn't seem to be compilation, but runtime. wasmtime complains that too many locals are defined when sqlite is compiled into this --- build.zig | 9 +++++++++ build.zig.zon | 4 ++++ src/createtable.zig | 12 +++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/build.zig b/build.zig index 24f07a9..e424926 100644 --- a/build.zig +++ b/build.zig @@ -76,7 +76,16 @@ pub fn build(b: *std.Build) !void { .optimize = optimize, }); const aws_signing_module = aws_dep.module("aws-signing"); + const sqlite_dep = b.dependency("sqlite", .{ + .target = target, + .optimize = optimize, + .use_bundled = true, + }); + const sqlite_module = sqlite_dep.module("sqlite"); for (&[_]*std.Build.Step.Compile{ exe, unit_tests }) |cs| { cs.addModule("aws-signing", aws_signing_module); + cs.addModule("sqlite", sqlite_module); + cs.addIncludePath(.{ .path = "c" }); + cs.linkLibrary(sqlite_dep.artifact("sqlite")); } } diff --git a/build.zig.zon b/build.zig.zon index 98b1a66..fbe94b6 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -7,6 +7,10 @@ .url = "https://git.lerch.org/lobo/aws-sdk-for-zig/archive/825d93720a92bcaedb3d00cd04764469fdec0c86.tar.gz", .hash = "122038e86ca453cbb0b4d5534380470eeb0656fdbab9aca2b7d2dc77756ab659204a", }, + .sqlite = .{ + .url = "https://github.com/vrischmann/zig-sqlite/archive/19535aab5760eeaf2979a9dadfca3bb21d1594b9.tar.gz", + .hash = "12208c654deea149cee27eaa45d0e6515c3d8f97d775a4156cbcce0ff424b5d26ea3", + }, .universal_lambda_build = .{ .url = "https://git.lerch.org/lobo/universal-lambda-zig/archive/d8b536651531ee95ceb4fae65ca5f29c5ed6ef29.tar.gz", .hash = "1220de5b5f23fddb794e2e735ee8312b9cd0d1302d5b8e3902f785e904f515506ccf", diff --git a/src/createtable.zig b/src/createtable.zig index a7c97fa..26d4d9f 100644 --- a/src/createtable.zig +++ b/src/createtable.zig @@ -1,9 +1,19 @@ const std = @import("std"); +const sqlite = @import("sqlite"); pub fn handler(allocator: std.mem.Allocator, event_data: []const u8) ![]const u8 { _ = event_data; + var db = try sqlite.Db.init(.{ + .mode = sqlite.Db.Mode{ .File = "donotuse.db" }, + .open_flags = .{ + .write = true, + .create = true, + }, + .threading_mode = .MultiThread, + }); + try db.exec("CREATE TABLE user(id integer primary key, age integer, name text)", .{}, .{}); var al = std.ArrayList(u8).init(allocator); var writer = al.writer(); - try writer.print("hello\n", .{}); + try writer.print("table created\n", .{}); return al.items; }