-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLConnection.go
71 lines (59 loc) · 1.6 KB
/
SQLConnection.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
71
package infrastructures
import (
"os"
_ "github.com/go-sql-driver/mysql"
dbr "github.com/gocraft/dbr/v2"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// ISQLConnection is
type ISQLConnection interface {
EsignRead() *dbr.Session
EsignWrite() *dbr.Session
}
// SQLConnection define sql connection.
type SQLConnection struct{}
var (
dbEsignRead, dbEsignWrite *dbr.Connection
err error
)
// Connection open a new database connection
func Connection(db, descriptor string, maxIdle, maxConns int) *dbr.Connection {
conn, err := dbr.Open(db, descriptor, nil)
if err != nil {
log.WithFields(log.Fields{
"action": "connection for " + db,
"event": db + "_error_connection",
}).Error(err)
os.Exit(0)
}
conn.SetMaxOpenConns(maxConns)
conn.SetMaxIdleConns(maxIdle)
return conn
}
// EsignRead create a new bareksa_marketdata session
func (s *SQLConnection) EsignRead() *dbr.Session {
if dbEsignRead == nil {
dbEsignRead = Connection(
viper.GetString("database.client.driver"),
viper.GetString("database.client.read"),
viper.GetInt("database.client.max_idle"),
viper.GetInt("database.client.max_cons"),
)
}
sess := dbEsignRead.NewSession(nil)
return sess
}
// EsignWrite create a new bareksa_marketdata session
func (s *SQLConnection) EsignWrite() *dbr.Session {
if dbEsignWrite == nil {
dbEsignWrite = Connection(
viper.GetString("database.client.driver"),
viper.GetString("database.client.write"),
viper.GetInt("database.client.max_idle"),
viper.GetInt("database.client.max_cons"),
)
}
sess := dbEsignWrite.NewSession(nil)
return sess
}