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 @@...@@ -1,6 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const sqlite = @import("sqlite");3const sqlite = @import("sqlite");
4const tracer = @import("tracer");
45
5const Self = @This();6const Self = @This();
67
...@@ -33,6 +34,9 @@ fn prepare(self: *Self, comptime query: string) !sqlite.StatementType(.{}, query...@@ -33,6 +34,9 @@ fn prepare(self: *Self, comptime query: string) !sqlite.StatementType(.{}, query
33}34}
3435
35pub fn collect(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) ![]T {36pub 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
36 var stmt = try self.prepare(query);40 var stmt = try self.prepare(query);
37 defer stmt.deinit();41 defer stmt.deinit();
38 var iter = try stmt.iteratorAlloc(T, alloc, args);42 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...@@ -45,12 +49,18 @@ pub fn collect(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime
45}49}
4650
47pub fn exec(self: *Self, alloc: std.mem.Allocator, comptime query: string, args: anytype) !void {51pub 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
48 var stmt = try self.prepare(query);55 var stmt = try self.prepare(query);
49 defer stmt.deinit();56 defer stmt.deinit();
50 try stmt.execAlloc(alloc, .{}, args);57 try stmt.execAlloc(alloc, .{}, args);
51}58}
5259
53pub fn first(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) !?T {60pub 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
54 var stmt = try self.prepare(query);64 var stmt = try self.prepare(query);
55 defer stmt.deinit();65 defer stmt.deinit();
56 return try stmt.oneAlloc(T, alloc, .{}, args);66 return try stmt.oneAlloc(T, alloc, .{}, args);
...@@ -61,6 +71,9 @@ pub fn prepareDynamic(self: *Self, query: string) !sqlite.DynamicStatement {...@@ -61,6 +71,9 @@ pub fn prepareDynamic(self: *Self, query: string) !sqlite.DynamicStatement {
61}71}
6272
63pub fn doesTableExist(self: *Self, alloc: std.mem.Allocator, name: string) !bool {73pub fn doesTableExist(self: *Self, alloc: std.mem.Allocator, name: string) !bool {
74 const t = tracer.trace(@src(), " {s}", .{name});
75 defer t.end();
76
64 for (try self.collect(alloc, string, "select name from sqlite_master where type=? AND name=?", .{ .type = "table", .name = name })) |item| {77 for (try self.collect(alloc, string, "select name from sqlite_master where type=? AND name=?", .{ .type = "table", .name = name })) |item| {
65 if (std.mem.eql(u8, item, name)) {78 if (std.mem.eql(u8, item, name)) {
66 return true;79 return true;
...@@ -70,6 +83,9 @@ pub fn doesTableExist(self: *Self, alloc: std.mem.Allocator, name: string) !bool...@@ -70,6 +83,9 @@ pub fn doesTableExist(self: *Self, alloc: std.mem.Allocator, name: string) !bool
70}83}
7184
72pub fn hasColumnWithName(self: *Self, alloc: std.mem.Allocator, comptime table: string, comptime column: string) !bool {85pub 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
73 for (try pragma.table_info(self, alloc, table)) |item| {89 for (try pragma.table_info(self, alloc, table)) |item| {
74 if (std.mem.eql(u8, item.name, column)) {90 if (std.mem.eql(u8, item.name, column)) {
75 return true;91 return true;
...@@ -91,6 +107,9 @@ pub const Pragma = struct {...@@ -91,6 +107,9 @@ pub const Pragma = struct {
91107
92pub const pragma = struct {108pub const pragma = struct {
93 pub fn table_info(self: *Self, alloc: std.mem.Allocator, comptime name: string) ![]const Pragma.TableInfo {109 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
94 return try self.collect(alloc, Pragma.TableInfo, "pragma table_info(" ++ name ++ ")", .{});113 return try self.collect(alloc, Pragma.TableInfo, "pragma table_info(" ++ name ++ ")", .{});
95 }114 }
96};115};
zig.mod+1
...@@ -5,3 +5,4 @@ license: AGPL-3.0...@@ -5,3 +5,4 @@ license: AGPL-3.0
5description: The ORM library for Zig.5description: The ORM library for Zig.
6dependencies:6dependencies:
7 - src: git https://github.com/vrischmann/zig-sqlite7 - src: git https://github.com/vrischmann/zig-sqlite
8 - src: git https://github.com/nektro/zig-tracer