gosora/pgsql.go
Azareal 2047fed0b9 Renamed a few querygen structs.
Moved the accumulator sub-builders into their own file.
Implemented OO methods for the prebuilders.

Began converting the generated queries over to the OO methods.
2017-11-12 05:25:04 +00:00

73 lines
1.9 KiB
Go

// +build pgsql
/* Copyright Azareal 2016 - 2018 */
/* Super experimental and incomplete. DON'T USE IT YET! */
package main
import (
"database/sql"
"strings"
"./common"
"./query_gen/lib"
_ "github.com/lib/pq"
)
// TODO: Add support for SSL for all database drivers, not just pgsql
var db_sslmode = "disable" // verify-full
func init() {
db_adapter = "pgsql"
_initDatabase = initPgsql
}
func initPgsql() (err error) {
// TODO: Investigate connect_timeout to see what it does exactly and whether it's relevant to us
var _dbpassword string
if dbpassword != "" {
_dbpassword = " password='" + _escape_bit(common.DbConfig.Password) + "'"
}
// TODO: Move this bit to the query gen lib
db, err = sql.Open("postgres", "host='"+_escape_bit(common.DbConfig.Host)+"' port='"+_escape_bit(common.DbConfig.Port)+"' user='"+_escape_bit(common.DbConfig.Username)+"' dbname='"+_escape_bit(common.Config.Dbname)+"'"+_dbpassword+" sslmode='"+db_sslmode+"'")
if err != nil {
return err
}
// Make sure that the connection is alive
err = db.Ping()
if err != nil {
return err
}
// Fetch the database version
db.QueryRow("SELECT VERSION()").Scan(&db_version)
// Set the number of max open connections. How many do we need? Might need to do some tests.
db.SetMaxOpenConns(64)
db.SetMaxIdleConns(32)
// Only hold connections open for five seconds to avoid accumulating a large number of stale connections
//db.SetConnMaxLifetime(5 * time.Second)
err = _gen_pgsql()
if err != nil {
return err
}
// Ready the query builder
qgen.Builder.SetConn(db)
err = qgen.Builder.SetAdapter("pgsql")
if err != nil {
return err
}
// TO-DO Handle the queries which the query generator can't handle yet
return nil
}
func _escape_bit(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)
}