gosora/install/install/pgsql.go
Azareal fdb6304e32 Mostly a mid-way technical commit to make reading diffs easier on me.
Split the relativeTime function into relativeTime and relativeTimeFromString.
Refactored the installer.
Made a lot of progress with PgSQL and MSSQL support, mostly MSSQL.
Made a lot of progress on the automated testing suite.
Added eqcss to the file extension list.
..depressed..
Fixed various bugs here and there.
gen_mysql.go is now properly excluded when the pgsql or mssql build tag is passed.
The BBCode plugin is now always initialised prior to it's test.
We've begun transitioning towards deserializing database times directly into time.Times rather than doing it every time on the spot.
Added more dev flags.
The tests now make use of a test database rather than the production database.
2017-10-14 08:39:22 +01:00

118 lines
2.5 KiB
Go

/*
*
* Gosora PostgreSQL Interface
* Under heavy development
* Copyright Azareal 2017 - 2018
*
*/
package install
import (
"database/sql"
"errors"
"fmt"
"strings"
"../../query_gen/lib"
_ "github.com/go-sql-driver/mysql"
)
// We don't need SSL to run an installer... Do we?
var dbSslmode = "disable"
func init() {
adapters["pgsql"] = &PgsqlInstaller{dbHost: ""}
}
type PgsqlInstaller struct {
db *sql.DB
dbHost string
dbUsername string
dbPassword string
dbName string
dbPort string
}
func (ins *PgsqlInstaller) SetConfig(dbHost string, dbUsername string, dbPassword string, dbName string, dbPort string) {
ins.dbHost = dbHost
ins.dbUsername = dbUsername
ins.dbPassword = dbPassword
ins.dbName = dbName
ins.dbPort = dbPort
}
func (ins *PgsqlInstaller) Name() string {
return "pgsql"
}
func (ins *PgsqlInstaller) DefaultPort() string {
return "5432"
}
func (ins *PgsqlInstaller) InitDatabase() (err error) {
_dbPassword := ins.dbPassword
if _dbPassword != "" {
_dbPassword = " password=" + pgEscapeBit(_dbPassword)
}
db, err := sql.Open("postgres", "host='"+pgEscapeBit(ins.dbHost)+"' port='"+pgEscapeBit(ins.dbPort)+"' user='"+pgEscapeBit(ins.dbUsername)+"' dbname='"+pgEscapeBit(ins.dbName)+"'"+_dbPassword+" sslmode='"+dbSslmode+"'")
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
qgen.Builder.SetConn(db)
err = qgen.Builder.SetAdapter("pgsql")
if err != nil {
return err
}
ins.db = db
return nil
}
func (ins *PgsqlInstaller) TableDefs() (err error) {
return errors.New("TableDefs() not implemented")
}
func (ins *PgsqlInstaller) InitialData() (err error) {
return errors.New("InitialData() not implemented")
}
func (ins *PgsqlInstaller) CreateAdmin() error {
return createAdmin()
}
func (ins *PgsqlInstaller) DBHost() string {
return ins.dbHost
}
func (ins *PgsqlInstaller) DBUsername() string {
return ins.dbUsername
}
func (ins *PgsqlInstaller) DBPassword() string {
return ins.dbPassword
}
func (ins *PgsqlInstaller) DBName() string {
return ins.dbName
}
func (ins *PgsqlInstaller) DBPort() string {
return ins.dbPort
}
func pgEscapeBit(bit string) string {
// TODO: Write a custom parser, so that backslashes work properly in the sql.Open string. Do something similar for the database driver, if possible?
return strings.Replace(bit, "'", "\\'", -1)
}