2022-05-01 22:47:00 +02:00
|
|
|
module db
|
|
|
|
|
2022-05-19 07:54:33 +02:00
|
|
|
import models { GitRepo, GitRepoArch, GitRepoFilter }
|
2022-05-01 22:47:00 +02:00
|
|
|
|
2022-05-03 19:55:52 +02:00
|
|
|
// get_git_repos returns all GitRepo's in the database.
|
2022-05-17 13:50:46 +02:00
|
|
|
pub fn (db &VieterDb) get_git_repos(filter GitRepoFilter) []GitRepo {
|
|
|
|
// This seems to currently be blocked by a bug in the ORM, I'll have to ask
|
|
|
|
// around.
|
|
|
|
if filter.repo != '' {
|
|
|
|
res := sql db.conn {
|
|
|
|
select from GitRepo where repo == filter.repo order by id limit filter.limit offset filter.offset
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-05-01 22:47:00 +02:00
|
|
|
res := sql db.conn {
|
2022-05-17 13:50:46 +02:00
|
|
|
select from GitRepo order by id limit filter.limit offset filter.offset
|
2022-05-01 22:47:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-05-03 19:55:52 +02:00
|
|
|
// get_git_repo tries to return a specific GitRepo.
|
2022-05-01 22:47:00 +02:00
|
|
|
pub fn (db &VieterDb) get_git_repo(repo_id int) ?GitRepo {
|
|
|
|
res := sql db.conn {
|
|
|
|
select from GitRepo where id == repo_id
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2022-05-07 14:16:30 +02:00
|
|
|
if res.id == 0 {
|
2022-05-01 22:47:00 +02:00
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-05-03 19:55:52 +02:00
|
|
|
// add_git_repo inserts the given GitRepo into the database.
|
2022-05-01 22:47:00 +02:00
|
|
|
pub fn (db &VieterDb) add_git_repo(repo GitRepo) {
|
|
|
|
sql db.conn {
|
|
|
|
insert repo into GitRepo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 19:55:52 +02:00
|
|
|
// delete_git_repo deletes the repo with the given ID from the database.
|
2022-05-01 22:47:00 +02:00
|
|
|
pub fn (db &VieterDb) delete_git_repo(repo_id int) {
|
|
|
|
sql db.conn {
|
|
|
|
delete from GitRepo where id == repo_id
|
2022-05-02 20:59:53 +02:00
|
|
|
delete from GitRepoArch where repo_id == repo_id
|
2022-05-01 22:47:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 19:55:52 +02:00
|
|
|
// update_git_repo updates any non-array values for a given GitRepo.
|
2022-05-02 20:59:53 +02:00
|
|
|
pub fn (db &VieterDb) update_git_repo(repo_id int, params map[string]string) {
|
|
|
|
mut values := []string{}
|
|
|
|
|
2022-05-05 09:35:19 +02:00
|
|
|
// TODO does this allow for SQL injection?
|
2022-05-02 20:59:53 +02:00
|
|
|
$for field in GitRepo.fields {
|
|
|
|
if field.name in params {
|
|
|
|
// Any fields that are array types require their own update method
|
|
|
|
$if field.typ is string {
|
2022-05-03 16:16:56 +02:00
|
|
|
values << "$field.name = '${params[field.name]}'"
|
2022-05-02 20:59:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
values_str := values.join(', ')
|
2022-05-19 08:20:11 +02:00
|
|
|
// I think this is actual SQL & not the ORM language
|
2022-05-03 16:16:56 +02:00
|
|
|
query := 'update GitRepo set $values_str where id == $repo_id'
|
2022-05-03 16:55:50 +02:00
|
|
|
|
2022-05-02 20:59:53 +02:00
|
|
|
db.conn.exec_none(query)
|
|
|
|
}
|
|
|
|
|
2022-05-03 19:55:52 +02:00
|
|
|
// update_git_repo_archs updates a given GitRepo's arch value.
|
2022-05-02 20:59:53 +02:00
|
|
|
pub fn (db &VieterDb) update_git_repo_archs(repo_id int, archs []GitRepoArch) {
|
2022-05-03 16:16:56 +02:00
|
|
|
archs_with_id := archs.map(GitRepoArch{
|
|
|
|
...it
|
|
|
|
repo_id: repo_id
|
|
|
|
})
|
2022-05-02 20:59:53 +02:00
|
|
|
|
|
|
|
sql db.conn {
|
|
|
|
delete from GitRepoArch where repo_id == repo_id
|
|
|
|
}
|
|
|
|
|
|
|
|
for arch in archs_with_id {
|
|
|
|
sql db.conn {
|
|
|
|
insert arch into GitRepoArch
|
|
|
|
}
|
|
|
|
}
|
2022-05-01 22:47:00 +02:00
|
|
|
}
|
2022-05-07 15:44:59 +02:00
|
|
|
|
2022-05-07 22:06:17 +02:00
|
|
|
// git_repo_exists is a utility function that checks whether a repo with the
|
|
|
|
// given id exists.
|
2022-05-07 15:44:59 +02:00
|
|
|
pub fn (db &VieterDb) git_repo_exists(repo_id int) bool {
|
2022-05-07 19:38:28 +02:00
|
|
|
db.get_git_repo(repo_id) or { return false }
|
|
|
|
|
2022-05-07 15:44:59 +02:00
|
|
|
return true
|
|
|
|
}
|