From 2e88e0825388b9122615f3bb32b0a8aec96dfd35 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Fri, 2 Aug 2024 14:25:05 +0200 Subject: Add connection.run --- src/duckdb/db.zig | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/duckdb/db.zig b/src/duckdb/db.zig index f662ee0..41cfe67 100644 --- a/src/duckdb/db.zig +++ b/src/duckdb/db.zig @@ -21,10 +21,17 @@ const Connection = struct { 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) { return try Result(res_type).init(self._conn, q); } + + /// Query with no results and autoclean + pub fn run(self: *Connection, q: [:0]const u8) !void{ + var x = try Result(void).init(self._conn, q); + defer x.deinit(); + } }; @@ -66,10 +73,8 @@ test "Simple querying" { 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(); + try connection.run("CREATE TABLE integers (i INTEGER NOT NULL, j INTEGER);"); + try connection.run("INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);"); var result = try connection.query("SELECT * FROM integers;", s); defer result.deinit(); @@ -101,10 +106,8 @@ test "Checks if all fields are captured" { 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 connection.run("CREATE TABLE integers (i INTEGER NOT NULL, j INTEGER);"); + try connection.run("INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);"); try std.testing.expectError(error.QueryColumnCountCapture, connection.query("SELECT * FROM integers;", s)); @@ -120,12 +123,9 @@ test "String queries" { 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(); + try connection.run("CREATE TABLE text (i VARCHAR NOT NULL );"); + try connection.run("INSERT INTO text VALUES ('Inlined');"); + try connection.run("INSERT INTO text VALUES ('A very long string that is not inlined');"); var result = try connection.query("SELECT * FROM text;", s); defer result.deinit(); -- cgit v1.2.3