2017-09-03 04:50:31 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Gosora PostgreSQL Interface
|
|
|
|
* Under heavy development
|
|
|
|
* Copyright Azareal 2017 - 2018
|
|
|
|
*
|
|
|
|
*/
|
2017-07-12 11:05:18 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
import "strings"
|
|
|
|
import "database/sql"
|
|
|
|
import _ "github.com/go-sql-driver/mysql"
|
|
|
|
|
|
|
|
// We don't need SSL to run an installer... Do we?
|
2017-09-03 04:50:31 +00:00
|
|
|
var dbSslmode = "disable"
|
2017-07-12 11:05:18 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func _setPgsqlAdapter() {
|
|
|
|
dbPort = "5432"
|
|
|
|
initDatabase = _initPgsql
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func _initPgsql() (err error) {
|
|
|
|
_dbPassword := dbPassword
|
|
|
|
if _dbPassword != "" {
|
|
|
|
_dbPassword = " password=" + _pgEscapeBit(_dbPassword)
|
2017-07-12 11:05:18 +00:00
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
db, err = sql.Open("postgres", "host='"+_pgEscapeBit(dbHost)+"' port='"+_pgEscapeBit(dbPort)+"' user='"+_pgEscapeBit(dbUsername)+"' dbname='"+_pgEscapeBit(dbName)+"'"+_dbPassword+" sslmode='"+dbSslmode+"'")
|
2017-07-12 11:05:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println("Successfully connected to the database")
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Create the database, if it doesn't exist
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2017-07-12 11:05:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func _pgEscapeBit(bit string) string {
|
2017-09-10 16:57:22 +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?
|
2017-09-03 04:50:31 +00:00
|
|
|
return strings.Replace(bit, "'", "\\'", -1)
|
|
|
|
}
|