created half baked database interface
This commit is contained in:
parent
7a4b8b5584
commit
217bdb1c1e
1 changed files with 75 additions and 0 deletions
75
ZboardDB.zig
Normal file
75
ZboardDB.zig
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const c = @cImport({
|
||||||
|
@cInclude("sqlite3.h");
|
||||||
|
});
|
||||||
|
|
||||||
|
const ZBoardDB = struct {
|
||||||
|
db: *c.sqlite3,
|
||||||
|
|
||||||
|
pub fn init() !ZBoardDB {
|
||||||
|
var c_db: ?*c.sqlite3 = undefined;
|
||||||
|
if (c.SQLITE_OK != c.sqlite3_open(":memory:", &c_db)) {
|
||||||
|
return error.DBInitError;
|
||||||
|
}
|
||||||
|
|
||||||
|
const db: ZBoardDB = .{ .db = c_db };
|
||||||
|
|
||||||
|
try db.execute(
|
||||||
|
\\ CREATE TABLE IF NOT EXISTS users (
|
||||||
|
\\ id INTEGER PRIMARY KEY,
|
||||||
|
\\ name VARCHAR(255) NOT NULL UNIQUE
|
||||||
|
\\ );
|
||||||
|
\\ CREATE TABLE IF NOT EXISTS topics (
|
||||||
|
\\ id INTEGER PRIMARY KEY,
|
||||||
|
\\ topic VARCHAR(255) NOT NULL UNIQUE,
|
||||||
|
\\ user_id INTEGER NOT NULL REFERENCES users(id)
|
||||||
|
\\ );
|
||||||
|
\\ id INTEGER PRIMARY KEY,
|
||||||
|
\\ title VARCHAR(255) NOT NULL UNIQUE,
|
||||||
|
\\ topic_id INTEGER NOT NULL REFERENCES topics(id),
|
||||||
|
\\ user_id INTEGER NOT NULL REFERENCES users(id)
|
||||||
|
\\ );
|
||||||
|
\\ CREATE TABLE IF NOT EXISTS posts (
|
||||||
|
\\ id INTEGER PRIMARY KEY,
|
||||||
|
\\ content TEXT NOT NULL,
|
||||||
|
\\ thread_id INTEGER NOT NULL REFERENCES threads(id),
|
||||||
|
\\ user_id INTEGER NOT NULL REFERENCES users(id),
|
||||||
|
\\ epoch INTEGER NOT NULL
|
||||||
|
\\ );
|
||||||
|
);
|
||||||
|
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: ZBoardDB) !void {
|
||||||
|
_ = c.sqlite3_close(self.db);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn execute(self: ZBoardDB, query: [:0]const u8) !void {
|
||||||
|
var errmsg: [*c]u8 = undefined;
|
||||||
|
if (c.SQLITE_OK != c.sqlite3_exec(self.db, query, null, null, &errmsg)) {
|
||||||
|
defer c.sqlite3_free(errmsg);
|
||||||
|
std.debug.print("Exec query failed: {s}\n", .{errmsg});
|
||||||
|
return error.execError;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn getCurrentUser() !void {}
|
||||||
|
|
||||||
|
fn createUser() !void {}
|
||||||
|
fn deleteUser() !void {}
|
||||||
|
|
||||||
|
fn createTopic() !void {}
|
||||||
|
fn deleteTopic() !void {}
|
||||||
|
|
||||||
|
fn createThread() !void {}
|
||||||
|
fn deleteThread() !void {}
|
||||||
|
|
||||||
|
fn createPost() !void {}
|
||||||
|
fn deletePost() !void {}
|
||||||
|
|
||||||
|
fn getTopics() !void {}
|
||||||
|
fn getThreadsByTopic() !void {}
|
||||||
|
fn getPostsByThread() !void {}
|
||||||
|
};
|
||||||
Loading…
Reference in a new issue