Use MySQL instead of SQLite

This commit is contained in:
nemunaire 2016-01-23 13:16:31 +01:00
parent 32e8f931b9
commit 8788eea4f0
4 changed files with 72 additions and 49 deletions

View File

@ -20,7 +20,7 @@ var CloudPassword string
func main() {
var bind = flag.String("bind", "0.0.0.0:8081", "Bind port/socket")
var dbfile = flag.String("db", "fic.db", "Path to the DB")
var dsn = flag.String("dsn", "fic:fic@/fic", "DSN to connect to the MySQL server")
flag.StringVar(&BaseURL, "baseurl", "http://fic.srs.epita.fr/", "URL prepended to each URL")
flag.StringVar(&SubmissionDir, "submission", "./submissions/", "Base directory where save submissions")
flag.StringVar(&PKIDir, "pki", "./pki/", "Base directory where found PKI scripts")
@ -56,7 +56,7 @@ func main() {
}
log.Println("Opening database...")
if err := fic.DBInit(*dbfile); err != nil {
if err := fic.DBInit(fmt.Sprintf("%s?parseTime=true", *dsn)); err != nil {
log.Fatal("Cannot open the database: ", err)
os.Exit(1)

View File

@ -2,6 +2,7 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
@ -42,7 +43,7 @@ func watchsubdir(watcher *inotify.Watcher, pathname string) error {
}
func main() {
var dbfile = flag.String("db", "fic.db", "Path to the DB")
var dsn = flag.String("dsn", "fic:fic@/fic", "DSN to connect to the MySQL server")
flag.StringVar(&BaseURL, "baseurl", "http://fic.srs.epita.fr/", "URL prepended to each URL")
flag.StringVar(&SubmissionDir, "submission", "./submissions", "Base directory where save submissions")
flag.StringVar(&TeamsDir, "teams", "../TEAMS", "Base directory where save teams JSON files")
@ -60,7 +61,7 @@ func main() {
}
log.Println("Opening DB...")
if err := fic.DBInit(*dbfile); err != nil {
if err := fic.DBInit(fmt.Sprintf("%s?parseTime=true", *dsn)); err != nil {
log.Fatal("Cannot open the database: ", err)
os.Exit(1)

View File

@ -2,78 +2,93 @@ package fic
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
func DBInit(path string) error {
func DBInit(dsn string) error {
var err error
if db, err = sql.Open("sqlite3", path); err != nil {
if db, err = sql.Open("mysql", dsn); err != nil {
return err
}
_, err = db.Exec(`
PRAGMA foreign_keys=ON;
`)
return err
return nil
}
func DBCreate() error {
_, err := db.Exec(`
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS themes(
id_theme INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
authors TEXT NOT NULL
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,
name TEXT NOT NULL,
id_team INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
color INTEGER 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)
);
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS team_members(
id_member INTEGER NOT NULL PRIMARY KEY,
id_member INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_team INTEGER,
firstname TEXT NOT NULL,
lastname TEXT NOT NULL,
nickname TEXT NOT NULL,
company TEXT NOT NULL,
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,
id_exercice INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_theme INTEGER NOT NULL,
title TEXT NOT NULL,
title VARCHAR(255) NOT NULL,
statement TEXT NOT NULL,
hint TEXT NOT NULL,
depend INTEGER,
gain INTEGER NOT NULL,
video_uri TEXT 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,
origin TEXT NOT NULL,
path TEXT NOT NULL UNIQUE,
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 TEXT NOT NULL,
sha1 BLOB 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_keys(
id_key INTEGER NOT NULL PRIMARY KEY,
id_key INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_exercice INTEGER NOT NULL,
type TEXT NOT NULL,
value BLOB 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,
@ -81,6 +96,10 @@ CREATE TABLE IF NOT EXISTS exercice_solved(
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,
@ -88,9 +107,11 @@ CREATE TABLE IF NOT EXISTS exercice_tries(
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice),
FOREIGN KEY(id_team) REFERENCES teams(id_team)
);
`)
`); err != nil {
return err
}
return nil
}
func DBClose() error {
return db.Close()

View File

@ -119,18 +119,19 @@ func (t Team) HasAccess(e Exercice) bool {
}
func (t Team) HasSolved(e Exercice) (bool, time.Time, int64) {
var nb int64
var tm int64
var mt *time.Time
if DBQueryRow("SELECT MIN(time) FROM exercice_solved WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&mt); mt == nil {
if err := DBQueryRow("SELECT COUNT(id_exercice), MAX(time) FROM exercice_tries WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&nb, &mt); err != nil {
var nb *int64
var tm *time.Time
if DBQueryRow("SELECT MIN(time) FROM exercice_solved WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&tm); tm == nil {
if DBQueryRow("SELECT COUNT(id_exercice), MAX(time) FROM exercice_tries WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&nb, &tm); tm == nil {
return false, time.Unix(0, 0), 0
} else if nb == nil {
return false, *tm, 0
} else {
return false, *mt, nb
return false, *tm, *nb
}
} else if err := DBQueryRow("SELECT COUNT(id_exercice) FROM exercice_solved WHERE id_exercice = ? AND time < ?", e.Id, tm).Scan(&nb); err != nil {
return true, time.Unix(tm, 0), 0
} else if DBQueryRow("SELECT COUNT(id_exercice) FROM exercice_solved WHERE id_exercice = ? AND time < ?", e.Id, tm).Scan(&nb); nb == nil {
return true, *tm, 0
} else {
return true, time.Unix(tm, 0), nb + 1
return true, *tm, *nb + 1
}
}