summaryrefslogtreecommitdiff
path: root/src/db.zig
blob: 751a50d9fdac2103c10486e4c59805105da2f779 (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 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);
}

test "Open and connect" {
    var database = try Self.init(":memory:");
    defer database.deinit();
    var connection = try database.connect();
    defer connection.deinit();
}