package fic import ( "database/sql" _ "github.com/go-sql-driver/mysql" ) var db *sql.DB func DBInit(dsn string) error { var err error if db, err = sql.Open("mysql", dsn); err != nil { return err } return nil } func DBCreate() error { if _, err := db.Exec(` 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(` CREATE TABLE IF NOT EXISTS themes( id_theme INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL UNIQUE, authors VARCHAR(255) NOT NULL ); `); err != nil { return err } if _, err := db.Exec(` CREATE TABLE IF NOT EXISTS teams( id_team INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, initial_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, color INTEGER NOT NULL ); `); err != nil { return err } if _, err := db.Exec(` CREATE TABLE IF NOT EXISTS team_members( id_member INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, id_team INTEGER, firstname VARCHAR(255) NOT NULL, lastname VARCHAR(255) NOT NULL, nickname VARCHAR(255) NOT NULL, company VARCHAR(255) NOT NULL, FOREIGN KEY(id_team) REFERENCES teams(id_team) ); `); err != nil { return err } if _, err := db.Exec(` CREATE TABLE IF NOT EXISTS exercices( id_exercice INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, id_theme INTEGER NOT NULL, title VARCHAR(255) NOT NULL, statement TEXT NOT NULL, depend INTEGER, gain INTEGER NOT NULL, video_uri VARCHAR(255) NOT NULL, FOREIGN KEY(id_theme) REFERENCES themes(id_theme), FOREIGN KEY(depend) REFERENCES exercices(id_exercice) ); `); err != nil { return err } if _, err := db.Exec(` CREATE TABLE IF NOT EXISTS exercice_files( id_file INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, origin VARCHAR(255) NOT NULL, path VARCHAR(255) NOT NULL UNIQUE, id_exercice INTEGER NOT NULL, name VARCHAR(255) NOT NULL, sha1 BINARY(20) NOT NULL, size INTEGER NOT NULL, FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice) ); `); 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) ); `); err != nil { return err } if _, err := db.Exec(` CREATE TABLE IF NOT EXISTS exercice_keys( id_key INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, id_exercice INTEGER NOT NULL, type VARCHAR(255) NOT NULL, value BINARY(64) NOT NULL, FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice) ); `); err != nil { return err } if _, err := db.Exec(` 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) ); `); err != nil { return err } if _, err := db.Exec(` 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) ); `); 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, FOREIGN KEY(id_hint) REFERENCES exercice_hints(id_hint), FOREIGN KEY(id_team) REFERENCES teams(id_team) ); `); err != nil { return err } return nil } 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...) }