-
Notifications
You must be signed in to change notification settings - Fork 16
/
pgsql.go
70 lines (56 loc) · 1.91 KB
/
pgsql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// +build pgsql
/* Copyright Azareal 2016 - 2020 */
/* Super experimental and incomplete. DON'T USE IT YET! */
package main
import (
"database/sql"
"strings"
c "github.com/Azareal/Gosora/common"
qgen "github.com/Azareal/Gosora/query_gen"
_ "github.com/lib/pq"
)
// TODO: Add support for SSL for all database drivers, not just pgsql
var dbSslmode = "disable" // verify-full
func init() {
dbAdapter = "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 c.DbConfig.Password != "" {
_dbpassword = " password='" + _escape_bit(c.DbConfig.Password) + "'"
}
// TODO: Move this bit to the query gen lib
db, err = sql.Open("postgres", "host='"+_escape_bit(c.DbConfig.Host)+"' port='"+_escape_bit(c.DbConfig.Port)+"' user='"+_escape_bit(c.DbConfig.Username)+"' dbname='"+_escape_bit(c.DbConfig.Dbname)+"'"+_dbpassword+" sslmode='"+dbSslmode+"'")
if err != nil {
return err
}
// Make sure that the connection is alive
err = db.Ping()
if err != nil {
return err
}
// 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)
db.SetConnMaxLifetime(c.DBTimeout())
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)
}