From a392744cc995d7c26730d2f1626612ad4739ca9b Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Mon, 29 Jul 2024 21:18:40 +0200 Subject: First commit with working DuckDB connection --- .gitignore | 2 ++ build.zig | 37 +++++++++++++++++++++++++++++++++++++ src/db.zig | 40 ++++++++++++++++++++++++++++++++++++++++ src/main.zig | 9 +++++++++ 4 files changed, 88 insertions(+) create mode 100644 .gitignore create mode 100644 build.zig create mode 100644 src/db.zig create mode 100644 src/main.zig diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d8c8979 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.zig-cache +zig-out diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..6c85360 --- /dev/null +++ b/build.zig @@ -0,0 +1,37 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const exe = b.addExecutable(.{ + .name = "newsplane", + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + + exe.linkSystemLibrary("duckdb"); + exe.linkLibC(); + exe.linkLibCpp(); + b.installArtifact(exe); + + const run_cmd = b.addRunArtifact(exe); + + 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); + + const exe_unit_tests = b.addTest(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&run_exe_unit_tests.step); +} diff --git a/src/db.zig b/src/db.zig new file mode 100644 index 0000000..425629d --- /dev/null +++ b/src/db.zig @@ -0,0 +1,40 @@ +const c = @cImport({ + @cInclude("duckdb.h"); +}); + +const Connection = struct { + _conn: c.duckdb_connection, + + pub fn init(db: Self) !Connection{ + var conn: Connection = undefined; + if( c.duckdb_connect(db._db, &conn._conn) == c.DuckDBError ){ + return error.DuckDBError; + } + return conn; + } + + pub fn deinit(self: *Connection) void{ + c.duckdb_disconnect(&self._conn); + } +}; + + +pub const Self = @This(); + +_db: c.duckdb_database, + +pub fn init(file: [*c]const u8) !Self{ + var db : Self = undefined; + if (c.duckdb_open(file, &db._db) == c.DuckDBError) { + return error.DuckDBError; + } + return db; +} + +pub fn deinit(self: *Self) void{ + c.duckdb_close(&self._db); +} + +pub fn connect(self: Self) !Connection { + return Connection.init(self); +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..df11bea --- /dev/null +++ b/src/main.zig @@ -0,0 +1,9 @@ +const std = @import("std"); +const Db = @import("./db.zig"); + +pub fn main() !void { + var database = try Db.init(":memory:"); + defer database.deinit(); + var connection = try database.connect(); + defer connection.deinit(); +} -- cgit v1.2.3