Added db state command and fixed printError
I was using the same buffer at 2 place :/
This commit is contained in:
parent
3c01150e33
commit
12ae7f9a62
16
src/main.zig
16
src/main.zig
@ -76,11 +76,11 @@ pub fn myLog(
|
|||||||
writer.writeByte('\n') catch return;
|
writer.writeByte('\n') catch return;
|
||||||
file.close();
|
file.close();
|
||||||
} else {
|
} else {
|
||||||
const writer = std.io.getStdErr().writer();
|
//const writer = std.io.getStdErr().writer();
|
||||||
|
|
||||||
writer.print("{s}{s}Time: {s} - ", .{ level_txt, prefix, date_format_buffer.items }) catch return;
|
//writer.print("{s}{s}Time: {s} - ", .{ level_txt, prefix, date_format_buffer.items }) catch return;
|
||||||
writer.print(format, args) catch return;
|
//writer.print(format, args) catch return;
|
||||||
writer.writeByte('\n') catch return;
|
//writer.writeByte('\n') catch return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,7 +179,6 @@ pub const DBEngine = struct {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: If an argument is given when starting the binary, it is the db path
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var db_engine = DBEngine.init(null, null);
|
var db_engine = DBEngine.init(null, null);
|
||||||
defer db_engine.deinit();
|
defer db_engine.deinit();
|
||||||
@ -197,7 +196,6 @@ pub fn main() !void {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (line) |line_str| {
|
if (line) |line_str| {
|
||||||
const start_time = std.time.milliTimestamp();
|
|
||||||
log.debug("Query received: {s}", .{line_str});
|
log.debug("Query received: {s}", .{line_str});
|
||||||
|
|
||||||
const null_term_line_str = try std.fmt.bufPrintZ(&line_buffer, "{s}", .{line_str});
|
const null_term_line_str = try std.fmt.bufPrintZ(&line_buffer, "{s}", .{line_str});
|
||||||
@ -263,6 +261,10 @@ pub fn main() !void {
|
|||||||
send("{s}", .{buffer.items});
|
send("{s}", .{buffer.items});
|
||||||
state = .end;
|
state = .end;
|
||||||
},
|
},
|
||||||
|
.keyword_state => {
|
||||||
|
send("{any}", .{db_engine.state});
|
||||||
|
state = .end;
|
||||||
|
},
|
||||||
.keyword_help => {
|
.keyword_help => {
|
||||||
send("{s}", .{HELP_MESSAGE.db});
|
send("{s}", .{HELP_MESSAGE.db});
|
||||||
state = .end;
|
state = .end;
|
||||||
@ -344,8 +346,6 @@ pub fn main() !void {
|
|||||||
log.info("Bye bye\n", .{});
|
log.info("Bye bye\n", .{});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
const end_time = std.time.milliTimestamp();
|
|
||||||
std.debug.print("Finished in: {d}ms\n", .{end_time - start_time});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,10 +8,11 @@ var map_error_buffer: [1024 * 1024]u8 = undefined; // This is for map AND error,
|
|||||||
var value_buffer: [1024]u8 = undefined;
|
var value_buffer: [1024]u8 = undefined;
|
||||||
var path_buffer: [1024 * 1024]u8 = undefined;
|
var path_buffer: [1024 * 1024]u8 = undefined;
|
||||||
|
|
||||||
|
var fa = std.heap.FixedBufferAllocator.init(&map_error_buffer);
|
||||||
|
const allocator = fa.allocator();
|
||||||
|
|
||||||
pub fn getEnvVariable(variable: []const u8) ?[]const u8 {
|
pub fn getEnvVariable(variable: []const u8) ?[]const u8 {
|
||||||
var fa = std.heap.FixedBufferAllocator.init(&map_error_buffer);
|
fa.reset();
|
||||||
defer fa.reset();
|
|
||||||
const allocator = fa.allocator();
|
|
||||||
|
|
||||||
var env_map = std.process.getEnvMap(allocator) catch return null;
|
var env_map = std.process.getEnvMap(allocator) catch return null;
|
||||||
|
|
||||||
@ -56,39 +57,35 @@ pub fn send(comptime format: []const u8, args: anytype) void {
|
|||||||
|
|
||||||
/// Print an error and send it to the user pointing to the token
|
/// Print an error and send it to the user pointing to the token
|
||||||
pub fn printError(message: []const u8, err: ZipponError, query: ?[]const u8, start: ?usize, end: ?usize) ZipponError {
|
pub fn printError(message: []const u8, err: ZipponError, query: ?[]const u8, start: ?usize, end: ?usize) ZipponError {
|
||||||
var fa = std.heap.FixedBufferAllocator.init(&map_error_buffer);
|
fa.reset();
|
||||||
defer fa.reset();
|
|
||||||
const allocator = fa.allocator();
|
|
||||||
|
|
||||||
var buffer = std.ArrayList(u8).init(allocator);
|
var buffer = std.ArrayList(u8).init(allocator);
|
||||||
defer buffer.deinit();
|
defer buffer.deinit();
|
||||||
var writer = buffer.writer();
|
var writer = buffer.writer();
|
||||||
|
|
||||||
writer.print("{{\"error\": \"", .{}) catch {};
|
writer.writeAll("{\"error\": \"") catch {};
|
||||||
writer.print("\n", .{}) catch {}; // Maybe use write all, not sure if it affect performance in any considerable way
|
writer.writeAll("\n") catch {};
|
||||||
writer.print("{s}\n", .{message}) catch {};
|
writer.print("{s}\n", .{message}) catch {};
|
||||||
|
|
||||||
if ((start != null) and (end != null) and (query != null)) {
|
if ((start != null) and (end != null) and (query != null)) {
|
||||||
const query_buffer = std.fmt.bufPrint(&map_error_buffer, "{s}", .{query.?}) catch return ZipponError.MemoryError;
|
const query_buffer = std.fmt.bufPrint(&path_buffer, "{s}", .{query.?}) catch return ZipponError.MemoryError;
|
||||||
std.mem.replaceScalar(u8, query_buffer, '\n', ' ');
|
std.mem.replaceScalar(u8, query_buffer, '\n', ' ');
|
||||||
writer.print("{s}\n", .{query.?}) catch {};
|
writer.print("{s}\n", .{query.?}) catch {};
|
||||||
|
|
||||||
// Calculate the number of spaces needed to reach the start position.
|
// Calculate the number of spaces needed to reach the start position.
|
||||||
var spaces: usize = 0;
|
var spaces: usize = 0;
|
||||||
while (spaces < start.?) : (spaces += 1) {
|
while (spaces < start.?) : (spaces += 1) {
|
||||||
writer.print(" ", .{}) catch {};
|
writer.writeByte(' ') catch {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the '^' characters for the error span.
|
// Print the '^' characters for the error span.
|
||||||
var i: usize = start.?;
|
var i: usize = start.?;
|
||||||
while (i < end.?) : (i += 1) {
|
while (i < end.?) : (i += 1) {
|
||||||
writer.print("^", .{}) catch {};
|
writer.writeByte('^') catch {};
|
||||||
}
|
}
|
||||||
writer.print(" \n", .{}) catch {}; // Align with the message
|
writer.writeAll(" \n") catch {}; // Align with the message
|
||||||
}
|
}
|
||||||
writer.print("\"}}", .{}) catch {};
|
writer.writeAll("\"}") catch {};
|
||||||
|
|
||||||
// log.debug("Parsing error: {s}", .{buffer.items});
|
|
||||||
|
|
||||||
send("{s}", .{buffer.items});
|
send("{s}", .{buffer.items});
|
||||||
return err;
|
return err;
|
||||||
@ -103,39 +100,3 @@ pub fn printOpenFile(comptime format: []const u8, args: anytype, options: std.fs
|
|||||||
const path = std.fmt.bufPrint(&path_buffer, format, args) catch return ZipponError.CantOpenDir;
|
const path = std.fmt.bufPrint(&path_buffer, format, args) catch return ZipponError.CantOpenDir;
|
||||||
return std.fs.cwd().openFile(path, options) catch ZipponError.CantOpenFile;
|
return std.fs.cwd().openFile(path, options) catch ZipponError.CantOpenFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn printTable(writer: anytype, headers: []const []const u8, data: []const []const []const u8) !void {
|
|
||||||
// Calculate column widths
|
|
||||||
var col_widths = [_]usize{0} ** 10;
|
|
||||||
|
|
||||||
// Determine max width for each column
|
|
||||||
for (headers, 0..) |header, i| {
|
|
||||||
col_widths[i] = @max(col_widths[i], header.len);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (data) |row| {
|
|
||||||
for (row, 0..) |cell, i| {
|
|
||||||
col_widths[i] = @max(col_widths[i], cell.len);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print headers
|
|
||||||
for (headers, 0..) |header, i| {
|
|
||||||
try writer.print("{s:<[*]}", .{ header, col_widths[i] + 2 });
|
|
||||||
}
|
|
||||||
try writer.print("\n", .{});
|
|
||||||
|
|
||||||
// Print separator
|
|
||||||
for (headers, 0..) |_, i| {
|
|
||||||
try writer.print("{s:-<[*]}", .{ "-", col_widths[i] + 2 });
|
|
||||||
}
|
|
||||||
try writer.print("\n", .{});
|
|
||||||
|
|
||||||
// Print data rows
|
|
||||||
for (data) |row| {
|
|
||||||
for (row, 0..) |cell, i| {
|
|
||||||
try writer.print("{s:<[*]}", .{ cell, col_widths[i] + 2 });
|
|
||||||
}
|
|
||||||
try writer.print("\n", .{});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -17,6 +17,7 @@ pub const Token = struct {
|
|||||||
.{ "new", .keyword_new },
|
.{ "new", .keyword_new },
|
||||||
.{ "metrics", .keyword_metrics },
|
.{ "metrics", .keyword_metrics },
|
||||||
.{ "use", .keyword_use },
|
.{ "use", .keyword_use },
|
||||||
|
.{ "state", .keyword_state },
|
||||||
});
|
});
|
||||||
|
|
||||||
pub fn getKeyword(bytes: []const u8) ?Tag {
|
pub fn getKeyword(bytes: []const u8) ?Tag {
|
||||||
@ -37,6 +38,7 @@ pub const Token = struct {
|
|||||||
keyword_new,
|
keyword_new,
|
||||||
keyword_metrics,
|
keyword_metrics,
|
||||||
keyword_use,
|
keyword_use,
|
||||||
|
keyword_state,
|
||||||
|
|
||||||
string_literal,
|
string_literal,
|
||||||
identifier,
|
identifier,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user