From 58c1ecd25e82f4466093151525454cf420ecfc6e Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 7 May 2022 14:16:30 +0200 Subject: [PATCH] db: added BuildLog & required methods --- src/db/db.v | 1 + src/db/git.v | 2 +- src/db/logs.v | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/db/logs.v diff --git a/src/db/db.v b/src/db/db.v index a75c34c..5ec240d 100644 --- a/src/db/db.v +++ b/src/db/db.v @@ -12,6 +12,7 @@ pub fn init(db_path string) ?VieterDb { sql conn { create table GitRepo + create table BuildLog } return VieterDb{ diff --git a/src/db/git.v b/src/db/git.v index c40086b..b779140 100644 --- a/src/db/git.v +++ b/src/db/git.v @@ -94,7 +94,7 @@ pub fn (db &VieterDb) get_git_repo(repo_id int) ?GitRepo { // If a select statement fails, it returns a zeroed object. By // checking one of the required fields, we can see whether the query // returned a result or not. - if res.url == '' { + if res.id == 0 { return none } diff --git a/src/db/logs.v b/src/db/logs.v new file mode 100644 index 0000000..3e5b600 --- /dev/null +++ b/src/db/logs.v @@ -0,0 +1,47 @@ +module db + +import time + +pub struct BuildLog { + id int [primary; sql: serial] + repo GitRepo [nonull] + start_time time.Time [nonull] + end_time time.Time [nonull] + exit_code int [nonull] +} + +// get_build_logs returns all BuildLog's in the database. +pub fn (db &VieterDb) get_build_logs() []BuildLog { + res := sql db.conn { + select from BuildLog order by id + } + + return res +} + +// get_build_log tries to return a specific BuildLog. +pub fn (db &VieterDb) get_build_log(id int) ?BuildLog { + res := sql db.conn { + select from BuildLog where id == id + } + + if res.id == 0 { + return none + } + + return res +} + +// add_build_log inserts the given BuildLog into the database. +pub fn (db &VieterDb) add_build_log(log BuildLog) { + sql db.conn { + insert log into BuildLog + } +} + +// delete_build_log delete the BuildLog with the given ID from the database. +pub fn (db &VieterDb) delete_build_log(id int) { + sql db.conn { + delete from BuildLog where id == id + } +}