Creation of database
This commit is contained in:
parent
c62478603c
commit
339fa3de27
26
src/main.zig
26
src/main.zig
|
@ -6,6 +6,8 @@ const Client = @import("client.zig").Client;
|
||||||
const webhook = @import("webhook.zig");
|
const webhook = @import("webhook.zig");
|
||||||
const sqlite = @import("sqlite.zig");
|
const sqlite = @import("sqlite.zig");
|
||||||
|
|
||||||
|
const DATABASE_VERSION_CODE = 1;
|
||||||
|
|
||||||
const CREATE_TABLES =
|
const CREATE_TABLES =
|
||||||
\\ CREATE TABLE VERSION
|
\\ CREATE TABLE VERSION
|
||||||
\\ (
|
\\ (
|
||||||
|
@ -100,7 +102,7 @@ pub fn main() anyerror!void {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
try db.exec(CREATE_TABLES);
|
try createTables(&db);
|
||||||
|
|
||||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||||
var allocator = arena.allocator();
|
var allocator = arena.allocator();
|
||||||
|
@ -123,6 +125,28 @@ pub fn main() anyerror!void {
|
||||||
arena.deinit();
|
arena.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn createTables(db: *sqlite.Database) anyerror!void {
|
||||||
|
var stm = db.prepare("SELECT versionCode FROM VERSION ORDER BY versionCode DESC") catch {
|
||||||
|
std.log.debug("Creating database", .{});
|
||||||
|
|
||||||
|
try db.exec(CREATE_TABLES);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (stm.next()) {
|
||||||
|
var code: isize = 0;
|
||||||
|
|
||||||
|
try stm.fetch(.{&code});
|
||||||
|
|
||||||
|
if (DATABASE_VERSION_CODE == code) {
|
||||||
|
std.log.debug("Database already created", .{});
|
||||||
|
stm.finalize();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stm.finalize();
|
||||||
|
}
|
||||||
|
|
||||||
pub fn updateAlert(allocator: std.mem.Allocator, client: *Client, config: *Config, headers: *std.StringHashMap([]const u8)) anyerror!void {
|
pub fn updateAlert(allocator: std.mem.Allocator, client: *Client, config: *Config, headers: *std.StringHashMap([]const u8)) anyerror!void {
|
||||||
var request = std.ArrayList(u8).init(allocator);
|
var request = std.ArrayList(u8).init(allocator);
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ pub const Database = struct {
|
||||||
|
|
||||||
var rc = sqlite3.sqlite3_open(path, &ptr);
|
var rc = sqlite3.sqlite3_open(path, &ptr);
|
||||||
if (rc > 0) {
|
if (rc > 0) {
|
||||||
std.log.err("Can't open database: {s}\n", .{sqlite3.sqlite3_errmsg(ptr)});
|
std.log.err("Can't open database: {s}", .{sqlite3.sqlite3_errmsg(ptr)});
|
||||||
return error.FailedToOpenDatabase;
|
return error.FailedToOpenDatabase;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ pub const Database = struct {
|
||||||
pub fn close(self: *@This()) anyerror!void {
|
pub fn close(self: *@This()) anyerror!void {
|
||||||
var rc = sqlite3.sqlite3_close(self.db);
|
var rc = sqlite3.sqlite3_close(self.db);
|
||||||
if (rc > 0) {
|
if (rc > 0) {
|
||||||
std.log.err("Can't close database: {s}\n", .{sqlite3.sqlite3_errmsg(self.db)});
|
std.log.err("Can't close database: {s}", .{sqlite3.sqlite3_errmsg(self.db)});
|
||||||
}
|
}
|
||||||
|
|
||||||
self.db = null;
|
self.db = null;
|
||||||
|
@ -35,7 +35,7 @@ pub const Database = struct {
|
||||||
var rc = sqlite3.sqlite3_prepare_v2(self.db, query.ptr, @intCast(c_int, query.len), &res, 0);
|
var rc = sqlite3.sqlite3_prepare_v2(self.db, query.ptr, @intCast(c_int, query.len), &res, 0);
|
||||||
|
|
||||||
if (rc != sqlite3.SQLITE_OK) {
|
if (rc != sqlite3.SQLITE_OK) {
|
||||||
std.log.err("failed to fetch data: {s}\n", .{sqlite3.sqlite3_errmsg(self.db)});
|
std.log.err("failed to fetch data: {s}", .{sqlite3.sqlite3_errmsg(self.db)});
|
||||||
return error.FailedToFetchData;
|
return error.FailedToFetchData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ pub const Database = struct {
|
||||||
|
|
||||||
var rc = sqlite3.sqlite3_exec(self.db, queries, null, null, &errorMsg);
|
var rc = sqlite3.sqlite3_exec(self.db, queries, null, null, &errorMsg);
|
||||||
if (rc != sqlite3.SQLITE_OK) {
|
if (rc != sqlite3.SQLITE_OK) {
|
||||||
std.log.err("failed to execute queries: {s}\n", .{errorMsg}); // TODO
|
std.log.err("failed to execute queries: {s}", .{errorMsg}); // TODO
|
||||||
sqlite3.sqlite3_free(errorMsg);
|
sqlite3.sqlite3_free(errorMsg);
|
||||||
return error.FailedToExecuteQueries;
|
return error.FailedToExecuteQueries;
|
||||||
}
|
}
|
||||||
|
@ -70,14 +70,14 @@ pub const Statement = struct {
|
||||||
.Float, .ComptimeFloat => {
|
.Float, .ComptimeFloat => {
|
||||||
var rc = sqlite3.sqlite3_bind_double(self.statement, @intCast(c_int, index), value);
|
var rc = sqlite3.sqlite3_bind_double(self.statement, @intCast(c_int, index), value);
|
||||||
if (rc != sqlite3.SQLITE_OK) {
|
if (rc != sqlite3.SQLITE_OK) {
|
||||||
std.log.err("failed to bind parameter: {s}\n", .{sqlite3.sqlite3_errmsg(self.db.db)});
|
std.log.err("failed to bind parameter: {s}", .{sqlite3.sqlite3_errmsg(self.db.db)});
|
||||||
return error.FailedToBindParameter;
|
return error.FailedToBindParameter;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
.Int, .ComptimeInt => {
|
.Int, .ComptimeInt => {
|
||||||
var rc = sqlite3.sqlite3_bind_int(self.statement, @intCast(c_int, index), value);
|
var rc = sqlite3.sqlite3_bind_int(self.statement, @intCast(c_int, index), value);
|
||||||
if (rc != sqlite3.SQLITE_OK) {
|
if (rc != sqlite3.SQLITE_OK) {
|
||||||
std.log.err("failed to bind parameter: {s}\n", .{sqlite3.sqlite3_errmsg(self.db.db)});
|
std.log.err("failed to bind parameter: {s}", .{sqlite3.sqlite3_errmsg(self.db.db)});
|
||||||
return error.FailedToBindParameter;
|
return error.FailedToBindParameter;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -87,14 +87,14 @@ pub const Statement = struct {
|
||||||
U8ArrayTypeTag.text => {
|
U8ArrayTypeTag.text => {
|
||||||
var rc = sqlite3.sqlite3_bind_text(self.statement, @intCast(c_int, index), value.text.ptr, @intCast(c_int, value.text.len), sqlite3.SQLITE_TRANSIENT);
|
var rc = sqlite3.sqlite3_bind_text(self.statement, @intCast(c_int, index), value.text.ptr, @intCast(c_int, value.text.len), sqlite3.SQLITE_TRANSIENT);
|
||||||
if (rc != sqlite3.SQLITE_OK) {
|
if (rc != sqlite3.SQLITE_OK) {
|
||||||
std.log.err("failed to bind parameter: {s}\n", .{sqlite3.sqlite3_errmsg(self.db.db)});
|
std.log.err("failed to bind parameter: {s}", .{sqlite3.sqlite3_errmsg(self.db.db)});
|
||||||
return error.FailedToBindParameter;
|
return error.FailedToBindParameter;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
U8ArrayTypeTag.bytes => {
|
U8ArrayTypeTag.bytes => {
|
||||||
var rc = sqlite3.sqlite3_bind_blob(self.statement, @intCast(c_int, index), value.bytes.ptr, @intCast(c_int, value.bytes.len), sqlite3.SQLITE_TRANSIENT);
|
var rc = sqlite3.sqlite3_bind_blob(self.statement, @intCast(c_int, index), value.bytes.ptr, @intCast(c_int, value.bytes.len), sqlite3.SQLITE_TRANSIENT);
|
||||||
if (rc != sqlite3.SQLITE_OK) {
|
if (rc != sqlite3.SQLITE_OK) {
|
||||||
std.log.err("failed to bind parameter: {s}\n", .{sqlite3.sqlite3_errmsg(self.db.db)});
|
std.log.err("failed to bind parameter: {s}", .{sqlite3.sqlite3_errmsg(self.db.db)});
|
||||||
return error.FailedToBindParameter;
|
return error.FailedToBindParameter;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue