2017-10-14 07:39:22 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Gosora PostgreSQL Interface
|
|
|
|
* Under heavy development
|
2019-01-21 12:27:59 +00:00
|
|
|
* Copyright Azareal 2017 - 2019
|
2017-10-14 07:39:22 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
package install
|
|
|
|
|
|
|
|
import (
|
2022-02-21 03:53:13 +00:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2017-10-14 07:39:22 +00:00
|
|
|
|
2022-02-21 03:53:13 +00:00
|
|
|
qgen "git.tuxpa.in/a/gosora/query_gen"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2017-10-14 07:39:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// We don't need SSL to run an installer... Do we?
|
|
|
|
var dbSslmode = "disable"
|
|
|
|
|
|
|
|
func init() {
|
2022-02-21 03:53:13 +00:00
|
|
|
adapters["pgsql"] = &PgsqlInstaller{dbHost: ""}
|
2017-10-14 07:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PgsqlInstaller struct {
|
2022-02-21 03:53:13 +00:00
|
|
|
db *sql.DB
|
|
|
|
dbHost string
|
|
|
|
dbUsername string
|
|
|
|
dbPassword string
|
|
|
|
dbName string
|
|
|
|
dbPort string
|
2017-10-14 07:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ins *PgsqlInstaller) SetConfig(dbHost string, dbUsername string, dbPassword string, dbName string, dbPort string) {
|
2022-02-21 03:53:13 +00:00
|
|
|
ins.dbHost = dbHost
|
|
|
|
ins.dbUsername = dbUsername
|
|
|
|
ins.dbPassword = dbPassword
|
|
|
|
ins.dbName = dbName
|
|
|
|
ins.dbPort = dbPort
|
2017-10-14 07:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ins *PgsqlInstaller) Name() string {
|
2022-02-21 03:53:13 +00:00
|
|
|
return "pgsql"
|
2017-10-14 07:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ins *PgsqlInstaller) DefaultPort() string {
|
2022-02-21 03:53:13 +00:00
|
|
|
return "5432"
|
2017-10-14 07:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ins *PgsqlInstaller) InitDatabase() (err error) {
|
2022-02-21 03:53:13 +00:00
|
|
|
_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
|
|
|
|
ins.db = db
|
|
|
|
qgen.Builder.SetConn(db)
|
|
|
|
return qgen.Builder.SetAdapter("pgsql")
|
2017-10-14 07:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ins *PgsqlInstaller) TableDefs() (err error) {
|
2022-02-21 03:53:13 +00:00
|
|
|
return errors.New("TableDefs() not implemented")
|
2017-10-14 07:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ins *PgsqlInstaller) InitialData() (err error) {
|
2022-02-21 03:53:13 +00:00
|
|
|
return errors.New("InitialData() not implemented")
|
2017-10-14 07:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ins *PgsqlInstaller) CreateAdmin() error {
|
2022-02-21 03:53:13 +00:00
|
|
|
return createAdmin()
|
2017-10-14 07:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ins *PgsqlInstaller) DBHost() string {
|
2022-02-21 03:53:13 +00:00
|
|
|
return ins.dbHost
|
2017-10-14 07:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ins *PgsqlInstaller) DBUsername() string {
|
2022-02-21 03:53:13 +00:00
|
|
|
return ins.dbUsername
|
2017-10-14 07:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ins *PgsqlInstaller) DBPassword() string {
|
2022-02-21 03:53:13 +00:00
|
|
|
return ins.dbPassword
|
2017-10-14 07:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ins *PgsqlInstaller) DBName() string {
|
2022-02-21 03:53:13 +00:00
|
|
|
return ins.dbName
|
2017-10-14 07:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ins *PgsqlInstaller) DBPort() string {
|
2022-02-21 03:53:13 +00:00
|
|
|
return ins.dbPort
|
2017-10-14 07:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func pgEscapeBit(bit string) string {
|
2022-02-21 03:53:13 +00:00
|
|
|
// 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)
|
2017-10-14 07:39:22 +00:00
|
|
|
}
|