Add QueryTable
This commit is contained in:
parent
80ded57904
commit
f28046bd79
1 changed files with 22 additions and 0 deletions
|
@ -4,6 +4,26 @@ const util = @import("util");
|
||||||
const String = []const u8;
|
const String = []const u8;
|
||||||
const comptimePrint = std.fmt.comptimePrint;
|
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
|
// Combines an array/tuple of strings into a single string, with a copy of
|
||||||
// joiner in between each one
|
// joiner in between each one
|
||||||
fn join(comptime vals: []const String, comptime joiner: String) String {
|
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..];
|
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 {
|
fn joinConditions(comptime cs: []const Condition, comptime joiner: String) String {
|
||||||
var strs: [cs.len]String = undefined;
|
var strs: [cs.len]String = undefined;
|
||||||
for (cs) |v, i| strs[i] = v.str();
|
for (cs) |v, i| strs[i] = v.str();
|
||||||
return join(&strs, joiner);
|
return join(&strs, joiner);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Represents a condition in a SQL statement
|
||||||
pub const Condition = union(enum) {
|
pub const Condition = union(enum) {
|
||||||
const BinaryOp = struct {
|
const BinaryOp = struct {
|
||||||
lhs: String,
|
lhs: String,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue