gosora/install/mssql.go

172 lines
3.7 KiB
Go
Raw Normal View History

/*
*
* Gosora MSSQL Interface
* Copyright Azareal 2017 - 2018
*
*/
package install
import (
"bytes"
"database/sql"
"fmt"
"io/ioutil"
"log"
"net/url"
"path/filepath"
"strconv"
"strings"
"github.com/Azareal/Gosora/query_gen"
_ "github.com/denisenkom/go-mssqldb"
)
func init() {
adapters["mssql"] = &MssqlInstaller{dbHost: ""}
}
type MssqlInstaller struct {
db *sql.DB
dbHost string
dbUsername string
dbPassword string
dbName string
dbInstance string
dbPort string
}
func (ins *MssqlInstaller) SetConfig(dbHost string, dbUsername string, dbPassword string, dbName string, dbPort string) {
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
}
func (ins *MssqlInstaller) Name() string {
return "mssql"
}
func (ins *MssqlInstaller) DefaultPort() string {
return "1433"
}
func (ins *MssqlInstaller) InitDatabase() (err error) {
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
Added the AboutSegment feature, you can see this in use on Cosora, it's a little raw right now, but I'm planning to polish it in the next commit. Refactored the code to use switches instead of if blocks in some places. Refactored the Dashboard to make it easier to add icons to it like I did with Cosora. You can now use maps in transpiled templates. Made progress on Cosora's footer. Swapped out the ThemeName property in the HeaderVars struct for a more general and flexible Theme property. Added the colstack CSS class to make it easier to style the layouts for the Control Panel and profile. Renamed the FStore variable to Forums. Renamed the Fpstore variable to FPStore. Renamed the Gstore variable to Groups. Split the MemoryTopicStore into DefaultTopicStore and MemoryTopicCache. Split the MemoryUserStore into DefaultUserStore and MemoryUserCache. Removed the NullUserStore, SQLUserStore, and SQLTopicStore. Added the NullTopicCache and NullUserCache. Moved the Reload method out of the TopicCache interface and into the TopicStore one. Moved the Reload method out of the UserCache interface and into the UserStore one. Added the SetCache and GetCache methods to the TopicStore and UserStore. Added the BypassGetAll method to the WordFilterMap type. Renamed routePanelSetting to routePanelSettingEdit. Renamed routePanelSettingEdit to routePanelSettingEditSubmit. Moved the page titles into the english language pack. Split main() into main and afterDBInit to avoid code duplication in general_test.go Added the ReqIsJson method so that we don't have to sniff the headers every time. Added the LogStore interface. Added the SQLModLogStore and the SQLAdminLogStore. Refactored the phrase system to use getPhrasePlaceholder instead of hard-coding the string to return in a bunch of functions. Removed a redundant rank check. Added the GuildStore to plugin_guilds. Added the about_segment_title and about_segment_body settings. Refactored the setting system to use predefined errors to make it easier for an upstream caller to filter out sensitive error messages as opposed to safe errors. Added the BypassGetAll method to the SettingMap type. Added the Update method to the SettingMap type. BulkGet is now exposed via the MemoryUserCache. Refactored more logs in the template transpiler to reduce the amount of indentation. Refactored the tests to take up fewer lines. Further improved the Cosora theme's colours, padding, and profiles. Added styling for the Control Panel Dashboard to the Cosora Theme. Reduced the amount of code duplication in the installer query generator and opened the door to certain types of auto-migrations. Refactored the Control Panel Dashboard to reduce the amount of code duplication. Refactored the modlog route to reduce the amount of code duplication and string concatenation.
2017-11-23 05:37:08 +00:00
qgen.Builder.SetConn(db)
return qgen.Builder.SetAdapter("mssql")
}
func (ins *MssqlInstaller) TableDefs() (err error) {
//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
}
func (ins *MssqlInstaller) InitialData() (err error) {
//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
}
func (ins *MssqlInstaller) CreateAdmin() error {
return createAdmin()
}
func (ins *MssqlInstaller) DBHost() string {
return ins.dbHost
}
func (ins *MssqlInstaller) DBUsername() string {
return ins.dbUsername
}
func (ins *MssqlInstaller) DBPassword() string {
return ins.dbPassword
}
func (ins *MssqlInstaller) DBName() string {
return ins.dbName
}
func (ins *MssqlInstaller) DBPort() string {
return ins.dbPort
}