highscore-server/src/database.v

41 lines
788 B
V

module main
struct ScoreRes {
pub mut:
id int [primary; sql: serial]
player string
score int
time i64
}
fn (mut app App) create_tables() int {
return app.db.exec_none('CREATE TABLE IF NOT EXISTS ScoreRes (
id INTEGER PRIMARY KEY,
player TEXT NOT NULL,
score INTEGER NOT NULL,
time SQLITE_INT64_TYPE NOT NULL
)')
}
fn (mut app App) insert_score(score ScoreRes) ScoreRes {
sql app.db {
insert score into ScoreRes
}
last_row_id := app.db.last_insert_rowid()
return sql app.db {
select from ScoreRes where id == last_row_id
}
}
fn (mut app App) get_scores() []ScoreRes {
return sql app.db {
select from ScoreRes order by score desc
}
}
fn (mut app App) delete_score(score_id int) {
sql app.db {
delete from ScoreRes where id == score_id
}
}