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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
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 "Simple querying" {
var database = try Database.init(":memory:");
defer database.deinit();
var connection = try database.connect();
defer connection.deinit();
const s : type = comptime struct {
primer: i32, // This is safe because the first column is NOT NULL
segund: ?i32
};
var x = try connection.query("CREATE TABLE integers (i INTEGER NOT NULL, j INTEGER);", void);
x.deinit();
var y = try connection.query("INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);", void);
y.deinit();
var result = try connection.query("SELECT * FROM integers;", s);
defer result.deinit();
try std.testing.expect(2 == result.getColumnCount());
var z = try result.next();
try std.testing.expect(z.primer == 3);
try std.testing.expect(z.segund.? == 4);
z = try result.next();
try std.testing.expect(z.primer == 5);
try std.testing.expect(z.segund.? == 6);
z = try result.next();
try std.testing.expect(z.primer == 7);
try std.testing.expect(z.segund == null);
}
test "Checks if all fields are captured" {
var database = try Database.init(":memory:");
defer database.deinit();
var connection = try database.connect();
defer connection.deinit();
const s : type = comptime struct {
primer: ?i32,
segund: ?i32,
tercer: ?i32,
};
var x = try connection.query("CREATE TABLE integers (i INTEGER NOT NULL, j INTEGER);", void);
x.deinit();
var y = try connection.query("INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);", void);
y.deinit();
try std.testing.expectError(error.QueryColumnCountCapture,
connection.query("SELECT * FROM integers;", s));
}
test "String queries" {
var database = try Database.init(":memory:");
defer database.deinit();
var connection = try database.connect();
defer connection.deinit();
const s : type = comptime struct {
primer: []const u8
};
var x = try connection.query("CREATE TABLE text (i VARCHAR NOT NULL );", void);
x.deinit();
var y = try connection.query("INSERT INTO text VALUES ('Inlined');", void);
y.deinit();
var z = try connection.query("INSERT INTO text VALUES ('A very long string that is not inlined');", void);
z.deinit();
var result = try connection.query("SELECT * FROM text;", s);
defer result.deinit();
try std.testing.expect(1 == result.getColumnCount());
var w = try result.next();
try std.testing.expect(std.mem.eql(u8, w.primer, "Inlined"));
w = try result.next();
try std.testing.expect(std.mem.eql(u8, w.primer, "A very long string that is not inlined"));
}
|