server/libfic/db.go

135 lines
3.2 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"
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
2016-01-23 12:16:31 +00:00
func DBInit(dsn string) error {
2016-01-07 17:28:16 +00:00
var err error
2016-01-23 12:16:31 +00:00
if db, err = sql.Open("mysql", dsn); err != nil {
2016-01-07 17:28:16 +00:00
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-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,
authors VARCHAR(255) 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,
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,
2016-01-07 17:28:16 +00:00
statement TEXT NOT NULL,
hint TEXT NOT NULL,
depend INTEGER,
gain INTEGER NOT NULL,
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,
sha1 BINARY(20) 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-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,
value BINARY(64) NOT NULL,
2016-01-07 17:28:16 +00:00
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_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)
);
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,
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
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...)
}