authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-04-11 03:22:50 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-04-11 03:22:50 -07:00
log0aa231a02edae4c9723eab1a314267c116255fa2
treee59d5ef3b8710d0327c3905cad69f7cdba35c709
parent1a53a152fd3ea5e8a6a9410e7712c4e497f5d867

add tracer instrumentation


2 files changed, 20 insertions(+), 0 deletions(-)

src/sqlite3.zig+19
......@@ -1,6 +1,7 @@
11const std = @import("std");
22const string = []const u8;
33const sqlite = @import("sqlite");
4const tracer = @import("tracer");
45
56const Self = @This();
67
......@@ -33,6 +34,9 @@ fn prepare(self: *Self, comptime query: string) !sqlite.StatementType(.{}, query
3334}
3435
3536pub fn collect(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) ![]T {
37 const t = tracer.trace(@src(), " {s}", .{query});
38 defer t.end();
39
3640 var stmt = try self.prepare(query);
3741 defer stmt.deinit();
3842 var iter = try stmt.iteratorAlloc(T, alloc, args);
......@@ -45,12 +49,18 @@ pub fn collect(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime
4549}
4650
4751pub fn exec(self: *Self, alloc: std.mem.Allocator, comptime query: string, args: anytype) !void {
52 const t = tracer.trace(@src(), " {s}", .{query});
53 defer t.end();
54
4855 var stmt = try self.prepare(query);
4956 defer stmt.deinit();
5057 try stmt.execAlloc(alloc, .{}, args);
5158}
5259
5360pub fn first(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) !?T {
61 const t = tracer.trace(@src(), " {s}", .{query});
62 defer t.end();
63
5464 var stmt = try self.prepare(query);
5565 defer stmt.deinit();
5666 return try stmt.oneAlloc(T, alloc, .{}, args);
......@@ -61,6 +71,9 @@ pub fn prepareDynamic(self: *Self, query: string) !sqlite.DynamicStatement {
6171}
6272
6373pub fn doesTableExist(self: *Self, alloc: std.mem.Allocator, name: string) !bool {
74 const t = tracer.trace(@src(), " {s}", .{name});
75 defer t.end();
76
6477 for (try self.collect(alloc, string, "select name from sqlite_master where type=? AND name=?", .{ .type = "table", .name = name })) |item| {
6578 if (std.mem.eql(u8, item, name)) {
6679 return true;
......@@ -70,6 +83,9 @@ pub fn doesTableExist(self: *Self, alloc: std.mem.Allocator, name: string) !bool
7083}
7184
7285pub fn hasColumnWithName(self: *Self, alloc: std.mem.Allocator, comptime table: string, comptime column: string) !bool {
86 const t = tracer.trace(@src(), " {s}.{s}", .{ table, column });
87 defer t.end();
88
7389 for (try pragma.table_info(self, alloc, table)) |item| {
7490 if (std.mem.eql(u8, item.name, column)) {
7591 return true;
......@@ -91,6 +107,9 @@ pub const Pragma = struct {
91107
92108pub const pragma = struct {
93109 pub fn table_info(self: *Self, alloc: std.mem.Allocator, comptime name: string) ![]const Pragma.TableInfo {
110 const t = tracer.trace(@src(), " {s}", .{name});
111 defer t.end();
112
94113 return try self.collect(alloc, Pragma.TableInfo, "pragma table_info(" ++ name ++ ")", .{});
95114 }
96115};
zig.mod+1
......@@ -5,3 +5,4 @@ license: AGPL-3.0
55description: The ORM library for Zig.
66dependencies:
77 - src: git https://github.com/vrischmann/zig-sqlite
8 - src: git https://github.com/nektro/zig-tracer