-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathgorose.go
More file actions
127 lines (114 loc) · 2.89 KB
/
gorose.go
File metadata and controls
127 lines (114 loc) · 2.89 KB
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
package gorose
import (
"database/sql"
"errors"
"github.com/gohouse/gorose/drivers"
)
var (
DB *sql.DB // origin DB
Tx *sql.Tx // transaction
//Stmt *sql.Stmt
Connect Connection
SetMaxOpenConns int = 0
SetMaxIdleConns int = 1
)
type Connection struct {
DbConfig map[string]interface{}
Default string
CurrentConfig map[string]string
}
func Open(args ...interface{}) (Database, error) {
if len(args) == 1 {
// continue
} else if len(args) == 2 {
if confReal, ok := args[1].(string); ok {
Connect.Default = confReal
} else {
return Database{}, errors.New("指定默认数据库只能位字符串!")
}
} else {
return Database{},errors.New("Open方法只接收1个或2个参数!")
}
// 解析config
err := Connect.parseConfig(args[0])
if err!=nil{
return Database{},err
}
// 驱动数据库
errs := Connect.boot()
return Dbstruct, errs
}
func (this *Connection) parseConfig(args interface{}) error {
if confReal, ok := args.(map[string]string); ok {
Connect.CurrentConfig = confReal
} else if confReal, ok := args.(map[string]interface{}); ok {
Connect.DbConfig = confReal
if defaultDb,ok := confReal["default"]; ok{
if Connect.Default == "" {
Connect.Default = defaultDb.(string)
}
}
if Connect.Default == ""{
return errors.New("配置文件默认数据库链接未设置!")
}
// 获取指定的默认数据库链接信息
if defaultDbConnection,ok := confReal[Connect.Default]; ok{
if configs,ok := defaultDbConnection.(map[string]string);ok{
Connect.CurrentConfig = configs
} else {
return errors.New("数据库配置格式有误!")
}
} else {
return errors.New("指定的数据库链接不存在!")
}
// 设置连接池信息
if mo,ok := confReal["SetMaxOpenConns"];ok{
if moInt,ok := mo.(int);ok{
SetMaxOpenConns = moInt
} else {
return errors.New("连接池信息配置的值只能是数字")
}
}
if mi,ok := confReal["SetMaxIdleConns"];ok{
if miInt,ok := mi.(int);ok{
SetMaxIdleConns = miInt
} else {
return errors.New("连接池信息配置的值只能是数字")
}
}
} else {
return errors.New("配置文件格式有误2!")
}
return nil
}
func (this *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(SetMaxOpenConns)
DB.SetMaxIdleConns(SetMaxIdleConns)
if err != nil {
return err
}
// 检查是否可以ping通
err2 := DB.Ping()
return err2
}
func GetDB() *sql.DB {
return DB
}