Add QueryTable

This commit is contained in:
jaina heartles 2022-08-03 21:52:07 -07:00
parent 80ded57904
commit f28046bd79
1 changed files with 22 additions and 0 deletions

View File

@ -4,6 +4,26 @@ const util = @import("util");
const String = []const u8;
const comptimePrint = std.fmt.comptimePrint;
pub const QueryTable = struct {
Model: type,
as: String,
// Gets a fully qualified field from a literal
pub fn field(comptime self: QueryTable, comptime lit: @Type(.EnumLiteral)) String {
const f = @as(std.meta.FieldEnum(self.Model), lit);
return comptimePrint("{s}.{s}", .{ self.as, @tagName(f) });
}
};
test "QueryTable.field" {
const tbl = QueryTable{
.Model = struct { id: i32 },
.as = "my_type",
};
try std.testing.expectEqualStrings("my_type.id", tbl.field(.id));
}
// Combines an array/tuple of strings into a single string, with a copy of
// joiner in between each one
fn join(comptime vals: []const String, comptime joiner: String) String {
@ -17,12 +37,14 @@ fn join(comptime vals: []const String, comptime joiner: String) String {
return result[joiner.len..];
}
// Stringifies and joins an array of conditions into a single string
fn joinConditions(comptime cs: []const Condition, comptime joiner: String) String {
var strs: [cs.len]String = undefined;
for (cs) |v, i| strs[i] = v.str();
return join(&strs, joiner);
}
// Represents a condition in a SQL statement
pub const Condition = union(enum) {
const BinaryOp = struct {
lhs: String,