summaryrefslogtreecommitdiff
path: root/src/duckdb/Connection.zig
blob: e62b2dbb0e42dc251ef002bbd9e3b3a3c21adf10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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);
}