summaryrefslogtreecommitdiff
path: root/src/duckdb/db.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/duckdb/db.zig')
-rw-r--r--src/duckdb/db.zig76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/duckdb/db.zig b/src/duckdb/db.zig
new file mode 100644
index 0000000..134a1f4
--- /dev/null
+++ b/src/duckdb/db.zig
@@ -0,0 +1,76 @@
+const std = @import("std");
+const assert = std.debug.assert;
+const c = @cImport({
+ @cInclude("duckdb.h");
+});
+const Result = @import("Result.zig").Result;
+
+
+const Connection = struct {
+ _conn: c.duckdb_connection,
+
+ pub fn init(db: Database) !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 fn query(self: *Connection, q: [:0]const u8, comptime res_type: type)
+ !Result(res_type) {
+ return try Result(res_type).init(self._conn, q);
+ }
+};
+
+
+pub const Database = @This();
+
+_db: c.duckdb_database,
+
+pub fn init(file: [*c]const u8) !Database{
+ var db : Database = undefined;
+ if (c.duckdb_open(file, &db._db) == c.DuckDBError) {
+ return error.DuckDBError;
+ }
+ return db;
+}
+
+pub fn deinit(self: *Database) void{
+ c.duckdb_close(&self._db);
+}
+
+pub fn connect(self: Database) !Connection {
+ return Connection.init(self);
+}
+
+test "Open and connect" {
+ var database = try Database.init(":memory:");
+ defer database.deinit();
+ var connection = try database.connect();
+ defer connection.deinit();
+}
+
+test "Query size" {
+ var database = try Database.init(":memory:");
+ defer database.deinit();
+ var connection = try database.connect();
+ defer connection.deinit();
+
+ const s : type = comptime struct {
+ primer: u8,
+ segund: u8
+ };
+
+ _ = try connection.query("CREATE TABLE integers (i INTEGER, j INTEGER);", void);
+ _ = try connection.query("INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);", void);
+ var result = try connection.query("SELECT * FROM integers;", s);
+ _ = result.next();
+ defer result.deinit();
+
+ try std.testing.expect(2 == result.getColumnCount());
+}