2022-05-07 14:16:30 +02:00
|
|
|
module db
|
|
|
|
|
2022-05-19 22:43:38 +02:00
|
|
|
import models { BuildLog, BuildLogFilter }
|
|
|
|
import time
|
2022-05-07 21:50:20 +02:00
|
|
|
|
2022-05-07 14:16:30 +02:00
|
|
|
// get_build_logs returns all BuildLog's in the database.
|
2022-05-19 22:43:38 +02:00
|
|
|
pub fn (db &VieterDb) get_build_logs(filter BuildLogFilter) []BuildLog {
|
|
|
|
mut where_parts := []string{}
|
|
|
|
|
2022-06-14 22:25:40 +02:00
|
|
|
if filter.target != 0 {
|
|
|
|
where_parts << 'target_id == $filter.target'
|
2022-05-19 22:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if filter.before != time.Time{} {
|
|
|
|
where_parts << 'start_time < $filter.before.unix_time()'
|
|
|
|
}
|
|
|
|
|
|
|
|
if filter.after != time.Time{} {
|
2022-05-29 20:08:21 +02:00
|
|
|
where_parts << 'start_time > $filter.after.unix_time()'
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: possible SQL injection
|
|
|
|
if filter.arch != '' {
|
|
|
|
where_parts << "arch == '$filter.arch'"
|
|
|
|
}
|
|
|
|
|
|
|
|
mut parts := []string{}
|
|
|
|
|
|
|
|
for exp in filter.exit_codes {
|
|
|
|
if exp[0] == `!` {
|
|
|
|
code := exp[1..].int()
|
|
|
|
|
|
|
|
parts << 'exit_code != $code'
|
2022-05-29 21:07:46 +02:00
|
|
|
} else {
|
2022-05-29 20:08:21 +02:00
|
|
|
code := exp.int()
|
|
|
|
|
|
|
|
parts << 'exit_code == $code'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if parts.len > 0 {
|
|
|
|
where_parts << parts.map('($it)').join(' or ')
|
2022-05-19 22:43:38 +02:00
|
|
|
}
|
|
|
|
|
2022-05-28 22:22:55 +02:00
|
|
|
mut where_str := ''
|
|
|
|
|
|
|
|
if where_parts.len > 0 {
|
2022-05-29 20:08:21 +02:00
|
|
|
where_str = 'where ' + where_parts.map('($it)').join(' and ')
|
2022-05-07 14:16:30 +02:00
|
|
|
}
|
|
|
|
|
2022-05-29 20:08:21 +02:00
|
|
|
query := 'select * from BuildLog $where_str limit $filter.limit offset $filter.offset'
|
2022-05-28 22:22:55 +02:00
|
|
|
rows, _ := db.conn.exec(query)
|
|
|
|
res := rows.map(row_into<BuildLog>(it))
|
2022-05-19 22:43:38 +02:00
|
|
|
|
2022-05-07 14:16:30 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-06-14 22:25:40 +02:00
|
|
|
// get_build_logs_for_target returns all BuildLog's in the database for a given
|
|
|
|
// target.
|
|
|
|
pub fn (db &VieterDb) get_build_logs_for_target(target_id int) []BuildLog {
|
2022-05-07 15:31:01 +02:00
|
|
|
res := sql db.conn {
|
2022-06-14 22:25:40 +02:00
|
|
|
select from BuildLog where target_id == target_id order by id
|
2022-05-07 15:31:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-05-07 14:16:30 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|