gosora/install/pgsql.go

113 lines
2.6 KiB
Go
Raw Normal View History

/*
*
* Gosora PostgreSQL Interface
* Under heavy development
Added the In-Progress Widget Manager UI. Added the IsoCode field to phrase files. Rewrote a good portion of the widget system logic. Added some tests for the widget system. Added the Online Users widget. Added a few sealed incomplete widgets like the Search & Filter Widget. Added the AllUsers method to WsHubImpl for Online Users. Please don't abuse it. Added the optional *DBTableKey field to AddColumn. Added the panel_analytics_time_range template to reduce the amount of duplication. Failed registrations now show up in red in the registration logs for Nox. Failed logins now show up in red in the login logs for Nox. Added basic h2 CSS to the other themes. Added .show_on_block_edit and .hide_on_block_edit to the other themes. Updated contributing. Updated a bunch of dates to 2019. Replaced tblKey{} with nil where possible. Switched out some &s for &s to reduce the number of possible bugs. Fixed a bug with selector messages where the inspector would get really jittery due to unnecessary DOM updates. Moved header.Zone and associated fields to the bottom of ViewTopic to reduce the chances of problems arising. Added the ZoneData field to *Header. Added IDs to the items in the forum list template. Split the fetchPhrases function into the initPhrases and fetchPhrases functions in init.js Added .colstack_sub_head. Fixed the CSS in the menu list. Removed an inline style from the simple topic like and unlike buttons. Removed an inline style from the simple topic IP button. Simplified the LoginRequired error handler. Fixed a typo in the comment prior to DatabaseError() Reduce the number of false leaves for WebSocket page transitions. Added the error zone. De-duped the logic in WsHubImpl.getUsers. Fixed a potential widget security issue. Added twenty new phrases. Added the wid column to the widgets table. You will need to run the patcher / updater for this commit.
2019-01-21 12:27:59 +00:00
* Copyright Azareal 2017 - 2019
*
*/
package install
import (
2022-02-21 03:16:15 +00:00
"database/sql"
"errors"
"fmt"
"strings"
2022-02-21 03:16:15 +00:00
"github.com/Azareal/Gosora/query_gen"
_ "github.com/go-sql-driver/mysql"
)
// We don't need SSL to run an installer... Do we?
var dbSslmode = "disable"
func init() {
2022-02-21 03:16:15 +00:00
adapters["pgsql"] = &PgsqlInstaller{dbHost: ""}
}
type PgsqlInstaller struct {
2022-02-21 03:16:15 +00:00
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) {
2022-02-21 03:16:15 +00:00
ins.dbHost = dbHost
ins.dbUsername = dbUsername
ins.dbPassword = dbPassword
ins.dbName = dbName
ins.dbPort = dbPort
}
func (ins *PgsqlInstaller) Name() string {
2022-02-21 03:16:15 +00:00
return "pgsql"
}
func (ins *PgsqlInstaller) DefaultPort() string {
2022-02-21 03:16:15 +00:00
return "5432"
}
func (ins *PgsqlInstaller) InitDatabase() (err error) {
2022-02-21 03:16:15 +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")
}
func (ins *PgsqlInstaller) TableDefs() (err error) {
2022-02-21 03:16:15 +00:00
return errors.New("TableDefs() not implemented")
}
func (ins *PgsqlInstaller) InitialData() (err error) {
2022-02-21 03:16:15 +00:00
return errors.New("InitialData() not implemented")
}
func (ins *PgsqlInstaller) CreateAdmin() error {
2022-02-21 03:16:15 +00:00
return createAdmin()
}
func (ins *PgsqlInstaller) DBHost() string {
2022-02-21 03:16:15 +00:00
return ins.dbHost
}
func (ins *PgsqlInstaller) DBUsername() string {
2022-02-21 03:16:15 +00:00
return ins.dbUsername
}
func (ins *PgsqlInstaller) DBPassword() string {
2022-02-21 03:16:15 +00:00
return ins.dbPassword
}
func (ins *PgsqlInstaller) DBName() string {
2022-02-21 03:16:15 +00:00
return ins.dbName
}
func (ins *PgsqlInstaller) DBPort() string {
2022-02-21 03:16:15 +00:00
return ins.dbPort
}
func pgEscapeBit(bit string) string {
2022-02-21 03:16:15 +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)
}