server/libfic/db.go

249 lines
6.4 KiB
Go
Raw Normal View History

2016-01-13 19:25:25 +00:00
package fic
2016-01-07 17:28:16 +00:00
import (
"database/sql"
2017-10-17 04:47:10 +00:00
"os"
2016-01-23 12:16:31 +00:00
_ "github.com/go-sql-driver/mysql"
2016-01-07 17:28:16 +00:00
)
var db *sql.DB
2017-10-17 04:47:10 +00:00
func DSNGenerator() string {
db_user := "fic"
db_password := "fic"
db_host := ""
db_db := "fic"
if v, exists := os.LookupEnv("MYSQL_HOST"); exists {
db_host = v
}
if v, exists := os.LookupEnv("MYSQL_PASSWORD"); exists {
db_password = v
} else if v, exists := os.LookupEnv("MYSQL_ROOT_PASSWORD"); exists {
db_user = "root"
db_password = v
}
if v, exists := os.LookupEnv("MYSQL_USER"); exists {
db_user = v
}
if v, exists := os.LookupEnv("MYSQL_DATABASE"); exists {
db_db = v
}
return db_user + ":" + db_password + "@" + db_host + "/" + db_db
}
2016-01-23 12:16:31 +00:00
func DBInit(dsn string) error {
2016-01-07 17:28:16 +00:00
var err error
2017-10-17 04:47:10 +00:00
if db, err = sql.Open("mysql", dsn + "?parseTime=true&foreign_key_checks=1"); err != nil {
2016-01-07 17:28:16 +00:00
return err
}
if _, err := db.Exec(`SET SESSION sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';`); err != nil {
return err
}
2016-01-23 12:16:31 +00:00
return nil
2016-01-07 17:28:16 +00:00
}
func DBCreate() error {
2016-01-23 12:16:31 +00:00
if _, err := db.Exec(`
2016-01-25 02:09:22 +00:00
CREATE TABLE IF NOT EXISTS events(
id_event INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
txt VARCHAR(255) NOT NULL UNIQUE,
kind ENUM('alert-default', 'alert-primary', 'alert-info', 'alert-warning', 'alert-danger', 'alert-success') NOT NULL,
time TIMESTAMP NOT NULL
);
`); err != nil {
return err
}
if _, err := db.Exec(`
2016-01-07 17:28:16 +00:00
CREATE TABLE IF NOT EXISTS themes(
2016-01-23 12:16:31 +00:00
id_theme INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL UNIQUE,
2017-12-08 23:54:49 +00:00
authors TEXT NOT NULL
2016-01-07 17:28:16 +00:00
);
2016-01-23 12:16:31 +00:00
`); err != nil {
2016-01-23 12:19:28 +00:00
return err
}
2016-01-23 12:16:31 +00:00
if _, err := db.Exec(`
2016-01-07 17:28:16 +00:00
CREATE TABLE IF NOT EXISTS teams(
2016-01-23 12:16:31 +00:00
id_team INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
2016-04-28 14:11:19 +00:00
initial_name VARCHAR(255) NOT NULL,
2016-01-23 12:16:31 +00:00
name VARCHAR(255) NOT NULL,
2016-01-20 09:14:13 +00:00
color INTEGER NOT NULL
2016-01-07 17:28:16 +00:00
);
2016-01-23 12:16:31 +00:00
`); err != nil {
2016-01-23 12:19:28 +00:00
return err
}
2016-01-23 12:16:31 +00:00
if _, err := db.Exec(`
2016-01-07 17:28:16 +00:00
CREATE TABLE IF NOT EXISTS team_members(
2016-01-23 12:16:31 +00:00
id_member INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
2016-01-07 17:28:16 +00:00
id_team INTEGER,
2016-01-23 12:16:31 +00:00
firstname VARCHAR(255) NOT NULL,
lastname VARCHAR(255) NOT NULL,
nickname VARCHAR(255) NOT NULL,
company VARCHAR(255) NOT NULL,
2016-01-07 17:28:16 +00:00
FOREIGN KEY(id_team) REFERENCES teams(id_team)
);
2016-01-23 12:16:31 +00:00
`); err != nil {
2016-01-23 12:19:28 +00:00
return err
}
2016-01-23 12:16:31 +00:00
if _, err := db.Exec(`
2016-01-07 17:28:16 +00:00
CREATE TABLE IF NOT EXISTS exercices(
2016-01-23 12:16:31 +00:00
id_exercice INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
2016-01-07 17:28:16 +00:00
id_theme INTEGER NOT NULL,
2016-01-23 12:16:31 +00:00
title VARCHAR(255) NOT NULL,
path VARCHAR(255) NOT NULL,
2016-01-07 17:28:16 +00:00
statement TEXT NOT NULL,
depend INTEGER,
gain INTEGER NOT NULL,
coefficient_cur FLOAT NOT NULL DEFAULT 1.0,
2016-01-23 12:16:31 +00:00
video_uri VARCHAR(255) NOT NULL,
2016-01-07 17:28:16 +00:00
FOREIGN KEY(id_theme) REFERENCES themes(id_theme),
FOREIGN KEY(depend) REFERENCES exercices(id_exercice)
);
2016-01-23 12:16:31 +00:00
`); err != nil {
2016-01-23 12:19:28 +00:00
return err
}
2016-01-23 12:16:31 +00:00
if _, err := db.Exec(`
2016-01-07 17:28:16 +00:00
CREATE TABLE IF NOT EXISTS exercice_files(
2016-01-23 12:16:31 +00:00
id_file INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
origin VARCHAR(255) NOT NULL,
path VARCHAR(255) NOT NULL UNIQUE,
2016-01-07 17:28:16 +00:00
id_exercice INTEGER NOT NULL,
2016-01-23 12:16:31 +00:00
name VARCHAR(255) NOT NULL,
cksum BINARY(64) NOT NULL,
2016-01-18 18:38:59 +00:00
size INTEGER NOT NULL,
2016-01-07 17:28:16 +00:00
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
);
2016-12-04 18:15:39 +00:00
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS exercice_hints(
id_hint INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_exercice INTEGER NOT NULL,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
cost INTEGER NOT NULL,
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
);
2016-01-23 12:16:31 +00:00
`); err != nil {
2016-01-23 12:19:28 +00:00
return err
}
2016-01-23 12:16:31 +00:00
if _, err := db.Exec(`
2016-01-07 17:28:16 +00:00
CREATE TABLE IF NOT EXISTS exercice_keys(
2016-01-23 12:16:31 +00:00
id_key INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
2016-01-07 17:28:16 +00:00
id_exercice INTEGER NOT NULL,
2016-01-23 12:16:31 +00:00
type VARCHAR(255) NOT NULL,
cksum BINARY(64) NOT NULL,
2016-01-07 17:28:16 +00:00
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
);
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS exercice_mcq(
id_mcq INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_exercice INTEGER NOT NULL,
title VARCHAR(255) NOT NULL,
kind ENUM('checkbox', 'radio', 'select') NOT NULL,
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
);
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS mcq_entries(
id_mcq_entry INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_mcq INTEGER NOT NULL,
label VARCHAR(255) NOT NULL,
response BOOLEAN NOT NULL,
FOREIGN KEY(id_mcq) REFERENCES exercice_mcq(id_mcq)
);
2017-12-16 00:16:30 +00:00
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS mcq_found(
id_mcq INTEGER NOT NULL,
id_team INTEGER NOT NULL,
time TIMESTAMP NOT NULL,
CONSTRAINT uq_found UNIQUE (id_mcq,id_team),
FOREIGN KEY(id_mcq) REFERENCES exercice_mcq(id_mcq),
FOREIGN KEY(id_team) REFERENCES teams(id_team)
);
2016-12-04 18:08:46 +00:00
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS key_found(
id_key INTEGER NOT NULL,
id_team INTEGER NOT NULL,
time TIMESTAMP NOT NULL,
CONSTRAINT uc_found UNIQUE (id_key,id_team),
2016-12-04 18:08:46 +00:00
FOREIGN KEY(id_key) REFERENCES exercice_keys(id_key),
FOREIGN KEY(id_team) REFERENCES teams(id_team)
);
2016-01-23 12:16:31 +00:00
`); err != nil {
2016-01-23 12:19:28 +00:00
return err
}
2016-01-23 12:16:31 +00:00
if _, err := db.Exec(`
2016-01-07 17:28:16 +00:00
CREATE TABLE IF NOT EXISTS exercice_solved(
id_exercice INTEGER NOT NULL,
id_team INTEGER NOT NULL,
time TIMESTAMP NOT NULL,
coefficient FLOAT NOT NULL DEFAULT 1.0,
CONSTRAINT uc_solved UNIQUE (id_exercice,id_team),
2016-01-07 17:28:16 +00:00
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice),
FOREIGN KEY(id_team) REFERENCES teams(id_team)
);
2016-01-23 12:16:31 +00:00
`); err != nil {
2016-01-23 12:19:28 +00:00
return err
}
2016-01-23 12:16:31 +00:00
if _, err := db.Exec(`
2016-01-07 17:28:16 +00:00
CREATE TABLE IF NOT EXISTS exercice_tries(
id_exercice INTEGER NOT NULL,
id_team INTEGER NOT NULL,
time TIMESTAMP NOT NULL,
2017-12-17 01:48:02 +00:00
nbdiff INTEGER NOT NULL DEFAULT 0,
2016-01-07 17:28:16 +00:00
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice),
FOREIGN KEY(id_team) REFERENCES teams(id_team)
);
2016-12-04 18:15:39 +00:00
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS team_hints(
id_team INTEGER,
id_hint INTEGER,
time TIMESTAMP NOT NULL,
CONSTRAINT uc_displayed UNIQUE (id_team,id_hint),
2016-12-04 18:15:39 +00:00
FOREIGN KEY(id_hint) REFERENCES exercice_hints(id_hint),
FOREIGN KEY(id_team) REFERENCES teams(id_team)
);
2016-01-23 12:16:31 +00:00
`); err != nil {
2016-01-23 12:19:28 +00:00
return err
}
2016-01-23 12:16:31 +00:00
return nil
}
2016-01-07 17:28:16 +00:00
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...)
}