2017-10-14 07:39:22 +00:00
/ *
*
* Gosora MSSQL Interface
* Copyright Azareal 2017 - 2018
*
* /
package install
import (
2022-02-21 03:16:15 +00:00
"bytes"
"database/sql"
"fmt"
"io/ioutil"
"log"
"net/url"
"path/filepath"
"strconv"
"strings"
"github.com/Azareal/Gosora/query_gen"
_ "github.com/denisenkom/go-mssqldb"
2017-10-14 07:39:22 +00:00
)
func init ( ) {
2022-02-21 03:16:15 +00:00
adapters [ "mssql" ] = & MssqlInstaller { dbHost : "" }
2017-10-14 07:39:22 +00:00
}
type MssqlInstaller struct {
2022-02-21 03:16:15 +00:00
db * sql . DB
dbHost string
dbUsername string
dbPassword string
dbName string
dbInstance string
dbPort string
2017-10-14 07:39:22 +00:00
}
func ( ins * MssqlInstaller ) SetConfig ( dbHost string , dbUsername string , dbPassword string , dbName string , dbPort string ) {
2022-02-21 03:16:15 +00:00
ins . dbHost = dbHost
ins . dbUsername = dbUsername
ins . dbPassword = dbPassword
ins . dbName = dbName
ins . dbInstance = "" // You can't set this from the installer right now, it allows you to connect to a named instance instead of a port
ins . dbPort = dbPort
2017-10-14 07:39:22 +00:00
}
func ( ins * MssqlInstaller ) Name ( ) string {
2022-02-21 03:16:15 +00:00
return "mssql"
2017-10-14 07:39:22 +00:00
}
func ( ins * MssqlInstaller ) DefaultPort ( ) string {
2022-02-21 03:16:15 +00:00
return "1433"
2017-10-14 07:39:22 +00:00
}
func ( ins * MssqlInstaller ) InitDatabase ( ) ( err error ) {
2022-02-21 03:16:15 +00:00
query := url . Values { }
query . Add ( "database" , ins . dbName )
u := & url . URL {
Scheme : "sqlserver" ,
User : url . UserPassword ( ins . dbUsername , ins . dbPassword ) ,
Host : ins . dbHost + ":" + ins . dbPort ,
Path : ins . dbInstance ,
RawQuery : query . Encode ( ) ,
}
log . Print ( "u.String() " , u . String ( ) )
db , err := sql . Open ( "mssql" , u . String ( ) )
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" )
// TODO: Create the database, if it doesn't exist
// Ready the query builder
ins . db = db
qgen . Builder . SetConn ( db )
return qgen . Builder . SetAdapter ( "mssql" )
2017-10-14 07:39:22 +00:00
}
func ( ins * MssqlInstaller ) TableDefs ( ) ( err error ) {
2022-02-21 03:16:15 +00:00
//fmt.Println("Creating the tables")
files , _ := ioutil . ReadDir ( "./schema/mssql/" )
for _ , f := range files {
if ! strings . HasPrefix ( f . Name ( ) , "query_" ) {
continue
}
var table , ext string
table = strings . TrimPrefix ( f . Name ( ) , "query_" )
ext = filepath . Ext ( table )
if ext != ".sql" {
continue
}
table = strings . TrimSuffix ( table , ext )
// ? - This is mainly here for tests, although it might allow the installer to overwrite a production database, so we might want to proceed with caution
_ , err = ins . db . Exec ( "DROP TABLE IF EXISTS [" + table + "];" )
if err != nil {
fmt . Println ( "Failed query:" , "DROP TABLE IF EXISTS [" + table + "]" )
return err
}
fmt . Println ( "Creating table '" + table + "'" )
data , err := ioutil . ReadFile ( "./schema/mssql/" + f . Name ( ) )
if err != nil {
return err
}
data = bytes . TrimSpace ( data )
_ , err = ins . db . Exec ( string ( data ) )
if err != nil {
fmt . Println ( "Failed query:" , string ( data ) )
return err
}
}
return nil
2017-10-14 07:39:22 +00:00
}
func ( ins * MssqlInstaller ) InitialData ( ) ( err error ) {
2022-02-21 03:16:15 +00:00
//fmt.Println("Seeding the tables")
data , err := ioutil . ReadFile ( "./schema/mssql/inserts.sql" )
if err != nil {
return err
}
data = bytes . TrimSpace ( data )
statements := bytes . Split ( data , [ ] byte ( ";" ) )
for key , statement := range statements {
if len ( statement ) == 0 {
continue
}
fmt . Println ( "Executing query #" + strconv . Itoa ( key ) + " " + string ( statement ) )
_ , err = ins . db . Exec ( string ( statement ) )
if err != nil {
return err
}
}
return nil
2017-10-14 07:39:22 +00:00
}
func ( ins * MssqlInstaller ) CreateAdmin ( ) error {
2022-02-21 03:16:15 +00:00
return createAdmin ( )
2017-10-14 07:39:22 +00:00
}
func ( ins * MssqlInstaller ) DBHost ( ) string {
2022-02-21 03:16:15 +00:00
return ins . dbHost
2017-10-14 07:39:22 +00:00
}
func ( ins * MssqlInstaller ) DBUsername ( ) string {
2022-02-21 03:16:15 +00:00
return ins . dbUsername
2017-10-14 07:39:22 +00:00
}
func ( ins * MssqlInstaller ) DBPassword ( ) string {
2022-02-21 03:16:15 +00:00
return ins . dbPassword
2017-10-14 07:39:22 +00:00
}
func ( ins * MssqlInstaller ) DBName ( ) string {
2022-02-21 03:16:15 +00:00
return ins . dbName
2017-10-14 07:39:22 +00:00
}
func ( ins * MssqlInstaller ) DBPort ( ) string {
2022-02-21 03:16:15 +00:00
return ins . dbPort
2017-10-14 07:39:22 +00:00
}