feat(db): added function to convert sqlite output to struct
parent
31e903ebeb
commit
4f32dec5b5
19
src/db/db.v
19
src/db/db.v
|
@ -1,6 +1,7 @@
|
|||
module db
|
||||
|
||||
import sqlite
|
||||
import time
|
||||
|
||||
struct VieterDb {
|
||||
conn sqlite.DB
|
||||
|
@ -66,3 +67,21 @@ pub fn init(db_path string) ?VieterDb {
|
|||
conn: conn
|
||||
}
|
||||
}
|
||||
|
||||
pub fn row_into<T>(row sqlite.Row) T {
|
||||
mut i := 0
|
||||
mut out := T{}
|
||||
|
||||
$for field in T.fields {
|
||||
$if field.typ is string {
|
||||
out.$(field.name) = row.vals[i]
|
||||
} $else $if field.typ is int {
|
||||
out.$(field.name) = row.vals[i].int()
|
||||
} $else $if field.typ is time.Time {
|
||||
out.$(field.name) = time.unix(row.vals[i].int())
|
||||
}
|
||||
|
||||
i += 1
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
|
|
@ -19,18 +19,19 @@ pub fn (db &VieterDb) get_build_logs(filter BuildLogFilter) []BuildLog {
|
|||
where_parts << 'start_time < $filter.after.unix_time()'
|
||||
}
|
||||
|
||||
where_str := if where_parts.len > 0 {
|
||||
' where ' + where_parts.map('($it)').join(' and ')
|
||||
} else {
|
||||
''
|
||||
mut where_str := ''
|
||||
|
||||
if where_parts.len > 0 {
|
||||
where_str = ' where ' + where_parts.map('($it)').join(' and ')
|
||||
}
|
||||
|
||||
query := 'select from BuildLog' + where_str
|
||||
res := db.conn.exec(query)
|
||||
rows, _ := db.conn.exec(query)
|
||||
res := rows.map(row_into<BuildLog>(it))
|
||||
|
||||
/* res := sql db.conn { */
|
||||
/* select from BuildLog where filter.repo == 0 || repo_id == filter.repo order by id */
|
||||
/* } */
|
||||
// res := sql db.conn {
|
||||
// select from BuildLog where filter.repo == 0 || repo_id == filter.repo order by id
|
||||
// }
|
||||
|
||||
return res
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ module models
|
|||
import time
|
||||
|
||||
pub struct BuildLog {
|
||||
pub:
|
||||
pub mut:
|
||||
id int [primary; sql: serial]
|
||||
repo_id int [nonull]
|
||||
start_time time.Time [nonull]
|
||||
|
|
Loading…
Reference in New Issue