This commit is contained in:
Omer Sabic 2024-02-06 12:39:30 +01:00
parent d7b6642e92
commit 782c72fb16
2 changed files with 35 additions and 6 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
zig-cache/
./build.zig.zon

View File

@ -13,8 +13,10 @@ const COLUMN_EMAIL_SIZE = 255;
const Row = struct { id: i16, username: [COLUMN_USERNAME_SIZE]u8, email: [COLUMN_EMAIL_SIZE]u8 }; const Row = struct { id: i16, username: [COLUMN_USERNAME_SIZE]u8, email: [COLUMN_EMAIL_SIZE]u8 };
var Table: [8]Row = undefined;
const StatementType = enum { INSERT, SELECT }; const StatementType = enum { INSERT, SELECT };
const Statement = struct { type: StatementType, row_to_insert: Row }; const Statement = struct { type: StatementType, data_to_insert: [*]anyopaque, data_inserted: usize = 0 };
pub fn new_input_buffer() !*InputBuffer { pub fn new_input_buffer() !*InputBuffer {
const input_buffers = try allocator.alloc(InputBuffer, 1); const input_buffers = try allocator.alloc(InputBuffer, 1);
@ -27,6 +29,14 @@ pub fn new_input_buffer() !*InputBuffer {
} }
pub fn main() !void { pub fn main() !void {
// DEBUG: Temporary data before insert is implemented
Table[0] = Row{ .id = 1, .username = undefined, .email = undefined };
std.mem.copyForwards(u8, &Table[0].username, "example");
std.mem.copyForwards(u8, &Table[0].email, "example@gml.com");
// END DEBUG
const input_buffer: *InputBuffer = try new_input_buffer(); const input_buffer: *InputBuffer = try new_input_buffer();
// defer std.heap.page_allocator.free(input_buffer); // defer std.heap.page_allocator.free(input_buffer);
@ -60,6 +70,10 @@ pub fn main() !void {
std.debug.print("Unrecognized keyword at start of {s}\n", .{command}); std.debug.print("Unrecognized keyword at start of {s}\n", .{command});
continue; continue;
}, },
PrepareResultError.SYNTAX_ERROR => {
std.debug.print("Unrecognized syntax near {s}\n", .{command});
continue;
},
} }
}; };
@ -103,7 +117,12 @@ pub fn prepare_statement(input_buffer: InputBuffer, statement: *Statement) Prepa
if (std.mem.eql(u8, input_buffer.buffer[0..6], "insert")) { if (std.mem.eql(u8, input_buffer.buffer[0..6], "insert")) {
statement.type = StatementType.INSERT; statement.type = StatementType.INSERT;
var data = std.mem.split(u8, input_buffer.buffer[7 .. input_buffer.buffer_length - 1], " ");
while (data.next()) |field| {
statement.data_to_insert[statement.data_inserted] = &field;
statement.data_inserted += 1;
}
return; return;
} }
@ -117,11 +136,19 @@ pub fn prepare_statement(input_buffer: InputBuffer, statement: *Statement) Prepa
pub fn execute_statement(statement: *Statement) void { pub fn execute_statement(statement: *Statement) void {
switch (statement.type) { switch (statement.type) {
StatementType.INSERT => { StatementType.INSERT => {},
std.debug.print("Your insert command would go here\n", .{});
},
StatementType.SELECT => { StatementType.SELECT => {
std.debug.print("Your select command would go here\n", .{}); select_all();
}, },
} }
} }
pub fn select_all() void {
std.debug.print("id | username | email\n", .{});
for (Table) |rowItem| {
if (rowItem.id == 0) return;
std.debug.print("{} {s} {s}\n", .{ rowItem.id, rowItem.username[0..], rowItem.email[0..] });
}
}
pub fn insert() !void {}