|
1 | 1 | const std = @import("std");
|
2 |
| -const assert = std.debug.assert; |
3 |
| -const print = std.debug.print; |
| 2 | +const expect = std.testing.expect; |
4 | 3 |
|
5 | 4 | const pg = @cImport({
|
6 | 5 | @cInclude("libpq-fe.h");
|
7 | 6 | });
|
8 | 7 |
|
9 |
| -pub fn main() void { |
| 8 | +pub fn main() !void { |
10 | 9 | const conn = pg.PQconnectdb("postgres://localhost/pgvector_zig_test");
|
11 |
| - assert(pg.PQstatus(conn) == pg.CONNECTION_OK); |
| 10 | + try expect(pg.PQstatus(conn) == pg.CONNECTION_OK); |
12 | 11 |
|
13 | 12 | var res = pg.PQexec(conn, "CREATE EXTENSION IF NOT EXISTS vector");
|
14 |
| - assert(pg.PQresultStatus(res) == pg.PGRES_COMMAND_OK); |
| 13 | + try expect(pg.PQresultStatus(res) == pg.PGRES_COMMAND_OK); |
15 | 14 | pg.PQclear(res);
|
16 | 15 |
|
17 | 16 | res = pg.PQexec(conn, "DROP TABLE IF EXISTS items");
|
18 |
| - assert(pg.PQresultStatus(res) == pg.PGRES_COMMAND_OK); |
| 17 | + try expect(pg.PQresultStatus(res) == pg.PGRES_COMMAND_OK); |
19 | 18 | pg.PQclear(res);
|
20 | 19 |
|
21 | 20 | res = pg.PQexec(conn, "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))");
|
22 |
| - assert(pg.PQresultStatus(res) == pg.PGRES_COMMAND_OK); |
| 21 | + try expect(pg.PQresultStatus(res) == pg.PGRES_COMMAND_OK); |
23 | 22 | pg.PQclear(res);
|
24 | 23 |
|
25 | 24 | const paramValues = [3:0][*c]const u8{ "[1,1,1]", "[2,2,2]", "[1,1,2]" };
|
26 | 25 | res = pg.PQexecParams(conn, "INSERT INTO items (embedding) VALUES ($1), ($2), ($3)", 3, null, ¶mValues, null, null, 0);
|
27 |
| - assert(pg.PQresultStatus(res) == pg.PGRES_COMMAND_OK); |
| 26 | + try expect(pg.PQresultStatus(res) == pg.PGRES_COMMAND_OK); |
28 | 27 | pg.PQclear(res);
|
29 | 28 |
|
30 | 29 | const paramValues2 = [1:0][*c]const u8{"[1,1,1]"};
|
31 | 30 | res = pg.PQexecParams(conn, "SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", 1, null, ¶mValues2, null, null, 0);
|
32 |
| - assert(pg.PQresultStatus(res) == pg.PGRES_TUPLES_OK); |
| 31 | + try expect(pg.PQresultStatus(res) == pg.PGRES_TUPLES_OK); |
33 | 32 | const ntuples = pg.PQntuples(res);
|
34 | 33 | var i: c_int = 0;
|
35 | 34 | while (i < ntuples) {
|
36 |
| - print("{s}: {s}\n", .{ pg.PQgetvalue(res, i, 0), pg.PQgetvalue(res, i, 1) }); |
| 35 | + std.debug.print("{s}: {s}\n", .{ pg.PQgetvalue(res, i, 0), pg.PQgetvalue(res, i, 1) }); |
37 | 36 | i += 1;
|
38 | 37 | }
|
39 | 38 | pg.PQclear(res);
|
|
0 commit comments