From bfa9c50ff95f9e35c7ee3aca34bb3670ebfd73bf Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Thu, 21 Oct 2021 20:45:07 -0700 Subject: [PATCH] sqlite3- add hasColumnWithName --- src/sqlite3.zig | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/sqlite3.zig b/src/sqlite3.zig index 414fcfd9319bc7b4c7ef6506711bb91f1fa77d26..1fae53e2fc52f38c425ef173f9ae28a63ce7e389 100644 --- a/src/sqlite3.zig +++ b/src/sqlite3.zig @@ -63,3 +63,29 @@ pub fn doesTableExist(self: *Self, alloc: *std.mem.Allocator, name: string) !boo } return false; } + +pub fn hasColumnWithName(self: *Self, alloc: *std.mem.Allocator, comptime table: string, comptime column: string) !bool { + for (try pragma.table_info(self, alloc, table)) |item| { + if (std.mem.eql(u8, item.name, column)) { + return true; + } + } + return false; +} + +pub const Pragma = struct { + pub const TableInfo = struct { + cid: u16, + name: string, + type: string, + notnull: bool, + dflt_value: string, + pk: bool, + }; +}; + +pub const pragma = struct { + pub fn table_info(self: *Self, alloc: *std.mem.Allocator, comptime name: string) ![]const Pragma.TableInfo { + return try self.collect(alloc, Pragma.TableInfo, "pragma table_info(" ++ name ++ ")", .{}); + } +}; -- 2.54.0