110 lines
2.7 KiB
Go
110 lines
2.7 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"database/sql"
|
||
|
_ "github.com/mattn/go-sqlite3"
|
||
|
)
|
||
|
|
||
|
var db *sql.DB
|
||
|
|
||
|
func DBInit(path string) error {
|
||
|
var err error
|
||
|
if db, err = sql.Open("sqlite3", path); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
_, err = db.Exec(`
|
||
|
PRAGMA foreign_keys=ON;
|
||
|
`)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func DBCreate() error {
|
||
|
_, err := db.Exec(`
|
||
|
CREATE TABLE IF NOT EXISTS themes(
|
||
|
id_theme INTEGER NOT NULL PRIMARY KEY,
|
||
|
name TEXT NOT NULL
|
||
|
);
|
||
|
CREATE TABLE IF NOT EXISTS teams(
|
||
|
id_team INTEGER NOT NULL PRIMARY KEY,
|
||
|
name TEXT NOT NULL
|
||
|
);
|
||
|
CREATE TABLE IF NOT EXISTS team_certificates(
|
||
|
id_team INTEGER NOT NULL PRIMARY KEY,
|
||
|
revoked INTEGER NOT NULL,
|
||
|
FOREIGN KEY(id_team) REFERENCES teams(id_team)
|
||
|
);
|
||
|
CREATE TABLE IF NOT EXISTS team_members(
|
||
|
id_member INTEGER NOT NULL PRIMARY KEY,
|
||
|
id_team INTEGER,
|
||
|
firstname TEXT NOT NULL,
|
||
|
lastname TEXT NOT NULL,
|
||
|
nickname TEXT NOT NULL,
|
||
|
company TEXT NOT NULL,
|
||
|
FOREIGN KEY(id_team) REFERENCES teams(id_team)
|
||
|
);
|
||
|
CREATE TABLE IF NOT EXISTS exercices(
|
||
|
id_exercice INTEGER NOT NULL PRIMARY KEY,
|
||
|
id_theme INTEGER NOT NULL,
|
||
|
title TEXT NOT NULL,
|
||
|
statement TEXT NOT NULL,
|
||
|
hint TEXT NOT NULL,
|
||
|
depend INTEGER,
|
||
|
gain INTEGER NOT NULL,
|
||
|
video_uri TEXT NOT NULL,
|
||
|
FOREIGN KEY(id_theme) REFERENCES themes(id_theme),
|
||
|
FOREIGN KEY(depend) REFERENCES exercices(id_exercice)
|
||
|
);
|
||
|
CREATE TABLE IF NOT EXISTS exercice_files(
|
||
|
id_file INTEGER NOT NULL PRIMARY KEY,
|
||
|
path TEXT NOT NULL UNIQUE,
|
||
|
id_exercice INTEGER NOT NULL,
|
||
|
name TEXT NOT NULL,
|
||
|
sha1 BLOB NOT NULL,
|
||
|
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
|
||
|
);
|
||
|
CREATE TABLE IF NOT EXISTS exercice_keys(
|
||
|
id_key INTEGER NOT NULL PRIMARY KEY,
|
||
|
id_exercice INTEGER NOT NULL,
|
||
|
type TEXT NOT NULL,
|
||
|
value BLOB NOT NULL,
|
||
|
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
|
||
|
);
|
||
|
CREATE TABLE IF NOT EXISTS exercice_solved(
|
||
|
id_exercice INTEGER NOT NULL,
|
||
|
id_team INTEGER NOT NULL,
|
||
|
time TIMESTAMP NOT NULL,
|
||
|
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice),
|
||
|
FOREIGN KEY(id_team) REFERENCES teams(id_team)
|
||
|
);
|
||
|
CREATE TABLE IF NOT EXISTS exercice_tries(
|
||
|
id_exercice INTEGER NOT NULL,
|
||
|
id_team INTEGER NOT NULL,
|
||
|
time TIMESTAMP NOT NULL,
|
||
|
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice),
|
||
|
FOREIGN KEY(id_team) REFERENCES teams(id_team)
|
||
|
);
|
||
|
`)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func DBClose() error {
|
||
|
return db.Close()
|
||
|
}
|
||
|
|
||
|
func DBPrepare(query string) (*sql.Stmt, error) {
|
||
|
return db.Prepare(query)
|
||
|
}
|
||
|
|
||
|
func DBQuery(query string, args ...interface{}) (*sql.Rows, error) {
|
||
|
return db.Query(query, args...)
|
||
|
}
|
||
|
|
||
|
func DBExec(query string, args ...interface{}) (sql.Result, error) {
|
||
|
return db.Exec(query, args...)
|
||
|
}
|
||
|
|
||
|
func DBQueryRow(query string, args ...interface{}) *sql.Row {
|
||
|
return db.QueryRow(query, args...)
|
||
|
}
|