2017-09-03 04:50:31 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Gosora MySQL Interface
|
|
|
|
* Copyright Azareal 2017 - 2018
|
|
|
|
*
|
|
|
|
*/
|
2017-07-12 11:05:18 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-09-03 04:50:31 +00:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2017-07-12 11:05:18 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2017-09-03 04:50:31 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-08-13 11:22:34 +00:00
|
|
|
|
2017-07-12 11:05:18 +00:00
|
|
|
"../query_gen/lib"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
)
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
//var dbCollation string = "utf8mb4_general_ci"
|
2017-07-12 11:05:18 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func _setMysqlAdapter() {
|
|
|
|
dbPort = "3306"
|
|
|
|
initDatabase = _initMysql
|
|
|
|
tableDefs = _tableDefsMysql
|
|
|
|
initialData = _initialDataMysql
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func _initMysql() (err error) {
|
|
|
|
_dbPassword := dbPassword
|
|
|
|
if _dbPassword != "" {
|
|
|
|
_dbPassword = ":" + _dbPassword
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
db, err = sql.Open("mysql", dbUsername+_dbPassword+"@tcp("+dbHost+":"+dbPort+")/")
|
2017-07-12 11:05:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the connection is alive..
|
|
|
|
err = db.Ping()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println("Successfully connected to the database")
|
2017-08-13 11:22:34 +00:00
|
|
|
|
2017-07-12 11:05:18 +00:00
|
|
|
var waste string
|
2017-09-03 04:50:31 +00:00
|
|
|
err = db.QueryRow("SHOW DATABASES LIKE '" + dbName + "'").Scan(&waste)
|
2017-07-12 11:05:18 +00:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
fmt.Println("Unable to find the database. Attempting to create it")
|
2017-09-13 15:09:13 +00:00
|
|
|
_, err = db.Exec("CREATE DATABASE IF NOT EXISTS " + dbName)
|
2017-07-12 11:05:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println("The database was successfully created")
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
fmt.Println("Switching to database " + dbName)
|
|
|
|
_, err = db.Exec("USE " + dbName)
|
2017-07-12 11:05:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-13 11:22:34 +00:00
|
|
|
|
2017-07-12 11:05:18 +00:00
|
|
|
// Ready the query builder
|
|
|
|
qgen.Builder.SetConn(db)
|
|
|
|
err = qgen.Builder.SetAdapter("mysql")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-13 11:22:34 +00:00
|
|
|
|
2017-07-12 11:05:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func _tableDefsMysql() error {
|
2017-07-12 11:05:18 +00:00
|
|
|
//fmt.Println("Creating the tables")
|
|
|
|
files, _ := ioutil.ReadDir("./schema/mysql/")
|
|
|
|
for _, f := range files {
|
2017-09-03 04:50:31 +00:00
|
|
|
if !strings.HasPrefix(f.Name(), "query_") {
|
2017-07-12 11:05:18 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-08-13 11:22:34 +00:00
|
|
|
|
2017-09-22 05:36:15 +00:00
|
|
|
var table, ext string
|
2017-09-03 04:50:31 +00:00
|
|
|
table = strings.TrimPrefix(f.Name(), "query_")
|
2017-07-12 11:05:18 +00:00
|
|
|
ext = filepath.Ext(table)
|
|
|
|
if ext != ".sql" {
|
|
|
|
continue
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
table = strings.TrimSuffix(table, ext)
|
2017-08-13 11:22:34 +00:00
|
|
|
|
2017-07-12 11:05:18 +00:00
|
|
|
fmt.Println("Creating table '" + table + "'")
|
|
|
|
data, err := ioutil.ReadFile("./schema/mysql/" + f.Name())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data = bytes.TrimSpace(data)
|
2017-08-13 11:22:34 +00:00
|
|
|
|
2017-07-12 11:05:18 +00:00
|
|
|
_, err = db.Exec(string(data))
|
|
|
|
if err != nil {
|
2017-09-03 04:50:31 +00:00
|
|
|
fmt.Println("Failed query:", string(data))
|
2017-07-12 11:05:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//fmt.Println("Finished creating the tables")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-22 05:36:15 +00:00
|
|
|
// ? - Moved this here since it was breaking the installer, we need to add this at some point
|
|
|
|
/* TODO: Implement the html-attribute setting type before deploying this */
|
|
|
|
/*INSERT INTO settings(`name`,`content`,`type`) VALUES ('meta_desc','','html-attribute');*/
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func _initialDataMysql() error {
|
2017-07-12 11:05:18 +00:00
|
|
|
return nil // Coming Soon
|
2017-08-13 11:22:34 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
/*fmt.Println("Seeding the tables")
|
2017-07-12 11:05:18 +00:00
|
|
|
data, err := ioutil.ReadFile("./schema/mysql/inserts.sql")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data = bytes.TrimSpace(data)
|
2017-08-13 11:22:34 +00:00
|
|
|
|
2017-07-12 11:05:18 +00:00
|
|
|
fmt.Println("Executing query",string(data))
|
|
|
|
_, err = db.Exec(string(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-13 11:22:34 +00:00
|
|
|
|
2017-07-12 11:05:18 +00:00
|
|
|
//fmt.Println("Finished inserting the database data")
|
2017-09-03 04:50:31 +00:00
|
|
|
return nil*/
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func _mysqlSeedDatabase() error {
|
2017-07-12 11:05:18 +00:00
|
|
|
fmt.Println("Opening the database seed file")
|
|
|
|
sqlContents, err := ioutil.ReadFile("./mysql.sql")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Preparing installation queries")
|
|
|
|
sqlContents = bytes.TrimSpace(sqlContents)
|
|
|
|
statements := bytes.Split(sqlContents, []byte(";"))
|
|
|
|
for key, statement := range statements {
|
|
|
|
if len(statement) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Executing query #" + strconv.Itoa(key) + " " + string(statement))
|
|
|
|
_, err = db.Exec(string(statement))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("Finished inserting the database data")
|
|
|
|
return nil
|
|
|
|
}
|