summaryrefslogtreecommitdiff
path: root/src/duckdb
diff options
context:
space:
mode:
Diffstat (limited to 'src/duckdb')
-rw-r--r--src/duckdb/Connection.zig47
-rw-r--r--src/duckdb/Database.zig (renamed from src/duckdb/db.zig)74
-rw-r--r--src/duckdb/PreparedStatement.zig4
3 files changed, 69 insertions, 56 deletions
diff --git a/src/duckdb/Connection.zig b/src/duckdb/Connection.zig
new file mode 100644
index 0000000..e62b2db
--- /dev/null
+++ b/src/duckdb/Connection.zig
@@ -0,0 +1,47 @@
+const c = @cImport({
+ @cInclude("duckdb.h");
+});
+const Result = @import("Result.zig").Result;
+const PreparedStatement = @import("PreparedStatement.zig");
+
+_conn: c.duckdb_connection,
+
+const Self = @This();
+
+pub fn init(conn: c.duckdb_connection) Self {
+ return .{
+ ._conn = conn
+ };
+}
+
+pub fn deinit(self: *Self) void {
+ c.duckdb_disconnect(&self._conn);
+}
+
+/// Query returning a result. Caller needs to call result.deinit()
+pub fn query(self: *Self, q: [:0]const u8, comptime res_type: type)
+ !Result(res_type) {
+ var result: c.duckdb_result = undefined;
+ const state = c.duckdb_query(self._conn, q, &result);
+ if ( state == c.DuckDBError ){
+ return error.DuckDBError;
+ }
+ return try Result(res_type).init(result);
+}
+
+/// Query with no results and autoclean
+pub fn run(self: *Self, q: [:0]const u8) !void{
+ var x = try self.query(q, void);
+ defer x.deinit();
+}
+
+/// Make a prepared query. Caller needs to call prepared_query.deinit()
+pub fn prepareStatement(self: *Self, q: [:0]const u8)
+ !PreparedStatement {
+ var stmt: c.duckdb_prepared_statement = undefined;
+ const state = c.duckdb_prepare(self._conn, q, &stmt);
+ if ( state == c.DuckDBError ){
+ return error.DuckDBError;
+ }
+ return PreparedStatement.init(stmt);
+}
diff --git a/src/duckdb/db.zig b/src/duckdb/Database.zig
index 4fad7cb..ca2a03a 100644
--- a/src/duckdb/db.zig
+++ b/src/duckdb/Database.zig
@@ -4,82 +4,44 @@ const c = @cImport({
@cInclude("duckdb.h");
});
const Result = @import("Result.zig").Result;
+const Connection = @import("Connection.zig");
const PreparedStatement = @import("PreparedStatement.zig");
-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);
- }
-
- /// Query returning a result. Caller needs to call result.deinit()
- pub fn query(self: *Connection, q: [:0]const u8, comptime res_type: type)
- !Result(res_type) {
- var result: c.duckdb_result = undefined;
- const state = c.duckdb_query(self._conn, q, &result);
- if ( state == c.DuckDBError ){
- return error.DuckDBError;
- }
- return try Result(res_type).init(result);
- }
-
- /// Query with no results and autoclean
- pub fn run(self: *Connection, q: [:0]const u8) !void{
- var x = try self.query(q, void);
- defer x.deinit();
- }
-
- /// Make a prepared query. Caller needs to call prepared_query.deinit()
- pub fn prepareStatement(self: *Connection, q: [:0]const u8)
- !PreparedStatement {
- var stmt: c.duckdb_prepared_statement = undefined;
- const state = c.duckdb_prepare(self._conn, q, &stmt);
- if ( state == c.DuckDBError ){
- return error.DuckDBError;
- }
- return PreparedStatement.init(stmt);
- }
-};
-
-
-pub const Database = @This();
+const Self = @This();
_db: c.duckdb_database,
-pub fn init(file: [*c]const u8) !Database{
- var db : Database = undefined;
+/// Creates (opens) a new database that needs to call .deinit() later
+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: *Database) void{
+pub fn deinit(self: *Self) void{
c.duckdb_close(&self._db);
}
-pub fn connect(self: Database) !Connection {
- return Connection.init(self);
+/// Returns a new Connection that needs to call .deinit() later
+pub fn connect(self: Self) !Connection {
+ var conn: c.duckdb_connection = undefined;
+ if( c.duckdb_connect(self._db, &conn) == c.DuckDBError ){
+ return error.DuckDBError;
+ }
+ return Connection.init(conn);
}
test "Open and connect" {
- var database = try Database.init(":memory:");
+ var database = try Self.init(":memory:");
defer database.deinit();
var connection = try database.connect();
defer connection.deinit();
}
test "Simple querying" {
- var database = try Database.init(":memory:");
+ var database = try Self.init(":memory:");
defer database.deinit();
var connection = try database.connect();
defer connection.deinit();
@@ -111,7 +73,7 @@ test "Simple querying" {
}
test "Checks if all fields are captured" {
- var database = try Database.init(":memory:");
+ var database = try Self.init(":memory:");
defer database.deinit();
var connection = try database.connect();
defer connection.deinit();
@@ -130,7 +92,7 @@ test "Checks if all fields are captured" {
}
test "String queries" {
- var database = try Database.init(":memory:");
+ var database = try Self.init(":memory:");
defer database.deinit();
var connection = try database.connect();
defer connection.deinit();
@@ -155,7 +117,7 @@ test "String queries" {
}
test "Prepared queries" {
- var database = try Database.init(":memory:");
+ var database = try Self.init(":memory:");
defer database.deinit();
var connection = try database.connect();
defer connection.deinit();
diff --git a/src/duckdb/PreparedStatement.zig b/src/duckdb/PreparedStatement.zig
index e81ac4c..d6b60b2 100644
--- a/src/duckdb/PreparedStatement.zig
+++ b/src/duckdb/PreparedStatement.zig
@@ -136,6 +136,10 @@ pub fn exec(self: *Self, comptime T: type) !Result(T){
return try Result(T).init(result);
}
+pub fn clear(self: *Self) void {
+ c.duckdb_clear_bindings(self._q);
+}
+
pub fn deinit(self: *Self) void{
c.duckdb_destroy_prepare(&self._q);
}