forked from gohouse/gorose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgorose.go
259 lines (235 loc) · 6.16 KB
/
gorose.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package gorose
import (
"database/sql"
"errors"
"github.com/gohouse/gorose/drivers"
)
var (
// DB is origin DB
DB *sql.DB
// Tx is transaction DB
//Tx *sql.Tx
// Connect is the Connection Object
Connect Connection
//conn.SetMaxOpenConns int = 0
//conn.SetMaxIdleConns int = -1
)
func init() {
Connect.SetMaxOpenConns = 0
Connect.SetMaxIdleConns = -1
}
// Open instance of sql.DB.Oper
// if args has 1 param , it will be derect connection or with default config set
// if args has 2 params , the second param will be the default dirver key
func Open(args ...interface{}) (Connection, error) {
//fmt.Println(args)
//return Connect, errors.New("dsf")
if len(args) == 1 {
// continue
} else if len(args) == 2 {
if confReal, ok := args[1].(string); ok {
Connect.Default = confReal
} else {
// 指定默认数据库只能为字符串!
return Connect, errors.New("only str allowed of default database name")
}
} else {
// Open方法只接收1个或2个参数!
return Connect, errors.New("1 or 2 params need in Open() method")
}
// 解析config
err := Connect.parseConfig(args[0])
if err != nil {
return Connect, err
}
// 驱动数据库
errs := Connect.boot()
return Connect, errs
}
// Connection is the database pre handle
type Connection struct {
// all config sets
DbConfig map[string]interface{}
// default database
Default string
// current config on use
CurrentConfig map[string]string
//// all sql logs
//SqlLog []string
//// if in transaction, the code auto change
//Trans bool
// max open connections
SetMaxOpenConns int
// max freedom connections leave
SetMaxIdleConns int
}
// Parse input config
func (conn *Connection) parseConfig(args interface{}) error {
if confReal, ok := args.(map[string]string); ok { // direct connection
Connect.CurrentConfig = confReal
} else if confReal, ok := args.(map[string]interface{}); ok {
// store the full connection
Connect.DbConfig = confReal
// if set the Default conf, store it
if defaultDb, ok := confReal["Default"]; ok {
// judge if seted
if Connect.Default == "" {
Connect.Default = defaultDb.(string)
}
}
if Connect.Default == "" {
// 配置文件默认数据库链接未设置
return errors.New("the default database is missing in config!")
}
// 获取指定的默认数据库链接信息
var connections map[string]map[string]string
if connectionsInterface, ok := confReal["Connections"]; ok {
switch connectionsInterface.(type) {
case map[string]map[string]string:
connections = connectionsInterface.(map[string]map[string]string)
default:
return errors.New("the database connections format error !")
}
} else {
return errors.New("the database connections missing !")
}
if defaultDbConnection, ok := connections[Connect.Default]; ok {
Connect.CurrentConfig = defaultDbConnection
} else {
// 指定的数据库链接不存在!
return errors.New("the database for using is missing!")
}
// 设置连接池信息
if mo, ok := confReal["SetMaxOpenConns"]; ok {
if moInt, ok := mo.(int); ok {
conn.SetMaxOpenConns = moInt
} else {
// 连接池信息配置的值只能是数字
return errors.New("the value of connection pool config need int")
}
}
if mi, ok := confReal["SetMaxIdleConns"]; ok {
if miInt, ok := mi.(int); ok {
conn.SetMaxIdleConns = miInt
} else {
return errors.New("the value of connection pool config need int")
}
}
} else {
return errors.New("format error in database config!")
}
return nil
}
// Boot sql driver
func (conn *Connection) boot() error {
dbObj := Connect.CurrentConfig
var driver, dsn string
var err error
//DB, err = sql.Open("mysql", "root:@tcp(localhost:3306)/test?charset=utf8")
switch dbObj["driver"] {
case "mysql":
driver, dsn = drivers.MySQL(dbObj)
case "sqlite3":
driver, dsn = drivers.Sqlite3(dbObj)
case "postgres":
driver, dsn = drivers.Postgres(dbObj)
case "oracle":
driver, dsn = drivers.Oracle(dbObj)
case "mssql":
driver, dsn = drivers.MsSQL(dbObj)
}
// 开始驱动
DB, err = sql.Open(driver, dsn)
DB.SetMaxOpenConns(conn.SetMaxOpenConns)
DB.SetMaxIdleConns(conn.SetMaxIdleConns)
if err != nil {
return err
}
// 检查是否可以ping通
err2 := DB.Ping()
return err2
}
// Close database
func (conn *Connection) Close() error {
//Connect.SqlLog = []string{}
return DB.Close()
}
// Ping db
func (conn *Connection) Ping() error {
return DB.Ping()
}
// Table is set table from database
func (conn *Connection) Table(table string) *Database {
return conn.GetInstance().Table(table)
}
//// Begin transaction begin
//func (conn *Connection) Begin() {
// Tx, _ = DB.Begin()
// Connect.Trans = true
//}
//
//// Commit is transaction commit
//func (conn *Connection) Commit() {
// Tx.Commit()
// Connect.Trans = false
//}
//
//// Rollback is transaction rollback
//func (conn *Connection) Rollback() {
// Tx.Rollback()
// Connect.Trans = false
//}
//
//// Transaction is simple transaction
//func (conn *Connection) Transaction(closure func() error) bool {
// //defer func() {
// // if err := recover(); err != nil {
// // conn.Rollback()
// // panic(err)
// // }
// //}()
//
// conn.Begin()
// err := closure()
// if err != nil {
// conn.Rollback()
// return false
// }
// conn.Commit()
//
// return true
//}
// Query str
func (conn *Connection) Query(args ...interface{}) ([]map[string]interface{}, error) {
return conn.GetInstance().Query(args...)
}
// Execute str
func (conn *Connection) Execute(args ...interface{}) (int64, error) {
return conn.GetInstance().Execute(args...)
}
// GetInstance , get the database object
func (conn *Connection) GetInstance() *Database {
//var database *Database
//return database
return &Database{}
}
// JsonEncode : parse json
func (conn *Connection) JsonEncode(arg interface{}) string {
return conn.GetInstance().JsonEncode(arg)
}
//// LastSql is get last query sql
//func (conn *Connection) LastSql() string {
// if len(Connect.SqlLog) > 0 {
// return Connect.SqlLog[len(Connect.SqlLog)-1:][0]
// }
// return ""
//}
//
//// SqlLogs is all sql query logs in this request
//func (conn *Connection) SqlLogs() []string {
// return Connect.SqlLog
//}
// GetDB is get origin *sql.DB
func GetDB() *sql.DB {
return DB
}