-
Notifications
You must be signed in to change notification settings - Fork 4
/
engine.i_dbconnector.go
152 lines (124 loc) · 4.21 KB
/
engine.i_dbconnector.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package mysql
import (
"errors"
"database/sql"
"time"
"github.com/ottemo/commerce/env"
"github.com/ottemo/commerce/utils"
)
// ------------------------------------------------------------------------------------
// InterfaceDBConnector implementation (package "github.com/ottemo/commerce/db/interfaces")
// ------------------------------------------------------------------------------------
// GetConnectionParams returns configured DB connection params
func (it *DBEngine) GetConnectionParams() interface{} {
var connectionParams = connectionParamsType{
uri: "/",
dbName: "ottemo",
poolConnections: 10,
maxConnections: 0,
}
if iniConfig := env.GetIniConfig(); iniConfig != nil {
if iniValue := iniConfig.GetValue("db.mysql.uri", connectionParams.uri); iniValue != "" {
connectionParams.uri = iniValue
}
if iniValue := iniConfig.GetValue("db.mysql.db", connectionParams.dbName); iniValue != "" {
connectionParams.dbName = iniValue
}
if iniValue := iniConfig.GetValue("db.mysql.maxConnections", ""); iniValue != "" {
connectionParams.maxConnections = utils.InterfaceToInt(iniValue)
}
if iniValue := iniConfig.GetValue("db.mysql.poolConnections", ""); iniValue != "" {
connectionParams.poolConnections = utils.InterfaceToInt(iniValue)
}
}
return connectionParams
}
// Connect establishes DB connection
func (it *DBEngine) Connect(srcConnectionParams interface{}) error {
connectionParams, ok := srcConnectionParams.(connectionParamsType)
if !ok {
return errors.New("Wrong connection parameters type.")
}
if newConnection, err := sql.Open("mysql", connectionParams.uri); err == nil {
it.connection = newConnection
} else {
return err
}
if connectionParams.poolConnections > 0 {
it.connection.SetMaxIdleConns(connectionParams.poolConnections)
}
if connectionParams.maxConnections > 0 {
it.connection.SetMaxOpenConns(connectionParams.maxConnections)
}
// making sure DB selected otherwise trying to obtain DB
rows, err := it.connection.Query("SELECT DATABASE()")
if err != nil {
return err
}
if !rows.Next() || rows.Scan(connectionParams.dbName) != nil || connectionParams.dbName == "" {
if _, err := it.connection.Exec("USE " + connectionParams.dbName); err != nil {
if _, err = it.connection.Exec("CREATE DATABASE " + connectionParams.dbName); err != nil {
return err
}
if _, err = it.connection.Exec("USE " + connectionParams.dbName); err != nil {
return err
}
}
}
return nil
}
// AfterConnect makes initialization of DB engine
func (it *DBEngine) AfterConnect(srcConnectionParams interface{}) error {
SQL := "CREATE TABLE IF NOT EXISTS `" + ConstCollectionNameColumnInfo + "` ( " +
"`_id` INTEGER NOT NULL AUTO_INCREMENT," +
"`collection` VARCHAR(255)," +
"`column` VARCHAR(255)," +
"`type` VARCHAR(255)," +
"`indexed` TINYINT(1)," +
"PRIMARY KEY(`_id`) )"
_, err := it.connection.Exec(SQL)
if err != nil {
return sqlError(SQL, err)
}
return nil
}
// Reconnect tries to reconnect to DB
func (it *DBEngine) Reconnect(connectionParams interface{}) error {
// Call Connect to "select database".
// Simple it.Ping is not enough - it restores connection, but doesn't "select database".
return it.Connect(connectionParams)
}
// IsConnected returns connection status
func (it *DBEngine) IsConnected() bool {
return it.isConnected
}
// SetConnected sets connection status
func (it *DBEngine) SetConnected(connected bool) {
it.isConnected = connected
}
// Ping checks connection alive
func (it *DBEngine) Ping() error {
// Better way to check connection is alive and "database is selected"
rows, err := it.connection.Query("SELECT * from " + ConstCollectionNameColumnInfo);
defer func(rows *sql.Rows){
if rows != nil {
err := rows.Close()
if err != nil {
it.LogConnection(err.Error())
}
}
}(rows)
return err
}
// GetValidationInterval returns delay between Ping
func (it *DBEngine) GetValidationInterval() time.Duration {
return ConstConnectionValidateInterval
}
// GetEngineName returns DBEngine name (InterfaceDBConnector)
func (it *DBEngine) GetEngineName() string {
return it.GetName()
}
// LogConnection outputs message to log
func (it *DBEngine) LogConnection(message string) {
env.Log("mysql.log", "DEBUG", message)
}