Hi all
Im having some Serious trouble trying to wrap a function with another function, because of the return type.
In particular a function from
(And i guess its possible that its a problem how the library is written, but i belive its more likely that its a problem with my lack of zig knowledge)
Following the githubs README example works fine, but wanting to create some logic separation, i try to create some functions that abstract the db calls, but for the sql query calls i hit a wall with the return type.
The minimun working example
const std = @import("std");
const sqlite = @import("sqlite");
const DB_PATH = "db_file.db";
const FETCH =
\\SELECT id, name FROM Foo
;
const Row = struct {
id: u32,
name: sqlite.Text,
};
pub fn main(init: std.process.Init) !void {
const gpa = init.gpa;
// Open db connection
var db_conn: sqlite.Database = try connect(DB_PATH, gpa);
defer db_conn.close();
// Cant get to wrap the statement preparation into a function
const db_sta = try listing(db_conn);
// By commenting the prev line and uncommenting the next one it works, ofc
//const db_stament = try db_conn.prepare(struct {}, Row, FETCH);
defer db_stament.finalize();
while (try db_stament.step()) |row| {
std.debug.print("{d} {s}", .{ row.id, row.name });
}
}
pub fn connect(db_path: []const u8, gpa: std.mem.Allocator) !sqlite.Database {
// Convert db_path string to C compatible string
const db_path_s = try gpa.dupeSentinel(u8, db_path, 0);
defer gpa.free(db_path_s);
std.debug.print("DEBUG: opening database \"{s}\"\n", .{db_path_s});
return try sqlite.Database.open(.{ .path = db_path_s });
}
// None of this (and Many More attempts) work
// pub fn listing(conn: sqlite.Database) !sqlite.Statement(struct {}, Row) {
pub fn listing(conn: sqlite.Database) !@TypeOf(sqlite.Statement) {
return try conn.prepare(struct {}, Row, FETCH);
}
Minimum sqlite db creation
CREATE TABLE IF NOT EXISTS Foo (
id INTEGER PRIMARY KEY NOT NULL,
name TEXT UNIQUE NOT NULL,
data TEXT
);
INSERT INTO Row (name, data) VALUES
('bar', 'some text')
;
For the attemps in the minimal example up there i get:
src/main.zig:41:29: error: expected type 'type', found 'sqlite.Statement(main.main__struct_32813,Row)'
Ive also tried
type: it makes it the function comptime wich fails with the database connection parameteranytypein all possible placessqlite.Statementcomplains that its a function- many more, and nothing works
Ive looked at the source zig-sqlite/src/sqlite.zig at main · nDimensional/zig-sqlite · GitHub but i dont have the Params in the function definition so cant use it for the return type
And Statement zig-sqlite/src/sqlite.zig at main · nDimensional/zig-sqlite · GitHub just has return type as type and in the code it returns a struct defined in place, so i cant reference to the struct it uses as return…
Besides im also expecting even more problems as soon as i want to make a more general function that calls different queries based on some logic
Another slightly related question: using the other zig-sqlite library?
I see it says its in hiatus, and tried it and it failed to build for release. missing symbols.
But it had many more contributors, meanwhile this one just has the single maintainer (also i wanna extend my thanks to them for keeping the module)
This is seriously testing my sanity ![]()