Added authentication and shared notes endpoints

This commit is contained in:
Nathan Anderson
2023-07-27 01:23:10 -06:00
parent 81863ae912
commit cdabcf61f3
10 changed files with 1014 additions and 269 deletions
+107 -12
View File
@@ -31,21 +31,85 @@ pub const Db = struct {
self: *Db,
comptime Type: type,
allocator: Allocator,
comptime whereClause: []const u8,
comptime where_clause: []const u8,
values: anytype,
comptime limit: ?u32,
comptime order_by_field: ?[]const u8,
comptime order: ?[]const u8,
comptime limit: ?bool,
limit_val: ?u32,
) !?[]Type {
_ = limit;
comptime {
if (order == null and order_by_field != null or order != null and order_by_field == null) {
@compileError("Must provide both order and order_by or neither, with select " ++ where_clause);
}
if (order != null) {
if (!(std.mem.eql(u8, order.?, "DESC") or std.mem.eql(u8, order.?, "ASC"))) {
@compileError("Must use ASC or DESC for order_by, used: " ++ order.?);
}
}
if (!std.mem.containsAtLeast(u8, where_clause, 1, "?")) {
@compileError("where_clause missing '?', no possible values to insert " ++ where_clause);
}
// Check that where_clause only contains fields in struct
var query_objs_iter = std.mem.split(u8, where_clause, "?");
inline for (@typeInfo(@TypeOf(values)).Struct.fields) |struct_field| {
const name = struct_field.name;
const query_obj = query_objs_iter.next();
if (query_obj == null) {
@compileError("Query does not have enough clauses for passed in data: " ++ where_clause);
}
if (std.mem.containsAtLeast(u8, query_obj.?, 1, name)) {
continue;
} else {
@compileError("Missing field or messed up order in select:\n" ++ where_clause ++ "\n" ++ query_obj.?);
}
}
const last = query_objs_iter.next();
if (last != null and !std.mem.eql(u8, last.?, "")) {
@compileError("Values is lacking, query contains more ? than data provided: " ++ where_clause ++ "\nLeft with: " ++ last.?);
}
}
if (limit == null and limit_val != null or limit != null and limit_val == null) {
std.log.err("Must provide both limit and limit_val or neither, with select: {s}", .{where_clause});
return null;
}
var res_array: std.ArrayList(Type) = std.ArrayList(Type).init(allocator);
if (order_by_field == null and limit == null) {
const query = "SELECT * FROM " ++ models.getTypeTableName(Type) ++ " " ++ where_clause ++ ";";
var stmt = try self._sql_db.prepare(query);
defer stmt.deinit();
const query = "SELECT * FROM " ++ models.getTypeTableName(Type) ++ " " ++ whereClause ++ ";";
var stmt = try self._sql_db.prepare(query);
defer stmt.deinit();
var iter = try stmt.iteratorAlloc(Type, allocator, values);
while (try iter.nextAlloc(allocator, .{})) |row| {
try res_array.append(row);
}
} else if (order_by_field == null and limit != null) {
const query = "SELECT * FROM " ++ models.getTypeTableName(Type) ++ " " ++ where_clause ++ " LIMIT ?;";
var stmt = try self._sql_db.prepare(query);
defer stmt.deinit();
var iter = try stmt.iteratorAlloc(Type, allocator, values);
while (try iter.nextAlloc(allocator, .{})) |row| {
try res_array.append(row);
var iter = try stmt.iteratorAlloc(Type, allocator, utils.structConcatFields(values, .{ .limit = limit_val.? }));
while (try iter.nextAlloc(allocator, .{})) |row| {
try res_array.append(row);
}
} else if (order_by_field != null and limit == null) {
const query = "SELECT * FROM " ++ models.getTypeTableName(Type) ++ " " ++ where_clause ++ " ORDER BY " ++ order_by_field.? ++ " " ++ order.? ++ ";";
var stmt = try self._sql_db.prepare(query);
defer stmt.deinit();
var iter = try stmt.iteratorAlloc(Type, allocator, values);
while (try iter.nextAlloc(allocator, .{})) |row| {
try res_array.append(row);
}
} else {
const query = "SELECT * FROM " ++ models.getTypeTableName(Type) ++ " " ++ where_clause ++ " ORDER BY " ++ order_by_field.? ++ " " ++ order.? ++ " LIMIT ?;";
var stmt = try self._sql_db.prepare(query);
defer stmt.deinit();
var iter = try stmt.iteratorAlloc(Type, allocator, utils.structConcatFields(values, .{ .limit = limit_val.? }));
while (try iter.nextAlloc(allocator, .{})) |row| {
try res_array.append(row);
}
}
return try res_array.toOwnedSlice();
@@ -57,6 +121,21 @@ pub const Db = struct {
}
pub fn selectOne(self: *Db, comptime Type: type, allocator: Allocator, comptime query: []const u8, values: anytype) !?Type {
comptime {
var query_objs_iter = std.mem.split(u8, query, "=");
inline for (@typeInfo(@TypeOf(values)).Struct.fields) |struct_field| {
const name = struct_field.name;
const query_obj = query_objs_iter.next();
if (query_obj == null) {
@compileError("Query does not have enough clauses for passed in data:\n" ++ "Type: " ++ @typeName(Type) ++ "\n" ++ query ++ "\n");
}
if (std.mem.containsAtLeast(u8, query_obj.?, 1, name)) {
continue;
} else {
@compileError("Missing field or messed up order in select:\n" ++ query ++ "\n" ++ query_obj.?);
}
}
}
const row = try self._sql_db.oneAlloc(Type, allocator, query, .{}, values);
// std.debug.print("{any}", .{row});
return row;
@@ -72,9 +151,25 @@ pub const Db = struct {
}
pub fn insert(self: *Db, comptime Type: type, values: anytype) !void {
// TODO check there is an ID field
comptime {
const query = models.createInsertQuery(Type);
// const InsertType = utils.removeStructFields(Type, &[_]u8{0});
var query_objs_iter = std.mem.split(u8, query, ",");
inline for (@typeInfo(@TypeOf(values)).Struct.fields) |struct_field| {
const name = struct_field.name;
const query_obj = query_objs_iter.next();
if (query_obj == null) {
@compileError("Query does not have enough clauses for passed in data:\n" ++ "Type: " ++ @typeName(Type) ++ "\n" ++ query ++ "\n");
}
if (std.mem.containsAtLeast(u8, query_obj.?, 1, name)) {
continue;
} else {
@compileError("Missing field or messed up order in insert:\n" ++ query ++ "\n" ++ query_obj.? ++ "\n");
}
}
}
self._sql_db.exec(models.createInsertQuery(Type), .{}, values) catch |err| {
std.debug.print("Encountered error while inserting data:\n{any}\n\tQuery:{s}\n{any}\n", .{ values, models.createInsertQuery(Type), err });
std.debug.print("Encountered error while inserting data:\n\t{any}\nQuery:\t{s}\n{any}\n", .{ values, models.createInsertQuery(Type), err });
return err;
};
}