-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
52 lines (47 loc) · 1.26 KB
/
driver.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
package database
import (
"errors"
"github.com/sohaha/zlsgo/zfile"
"github.com/zlsgo/zdb/driver"
"github.com/zlsgo/zdb/driver/mysql"
"github.com/zlsgo/zdb/driver/sqlite3"
)
var (
drivers = map[string]func(Options) (driver.IfeConfig, error){
"mysql": func(db Options) (dbConf driver.IfeConfig, err error) {
if db.MySQL == nil {
return nil, errors.New("初始化数据库失败: mysql 未配置")
}
dbConf = &mysql.Config{
Host: db.MySQL.Host,
Port: db.MySQL.Port,
User: db.MySQL.User,
Password: db.MySQL.Password,
DBName: db.MySQL.DBName,
Parameters: db.MySQL.Parameters,
Charset: db.MySQL.Charset,
// Zone: db.MySQL.Zone,
}
return
},
"sqlite": func(db Options) (dbConf driver.IfeConfig, err error) {
if db.Sqlite == nil {
return nil, errors.New("初始化数据库失败: sqlite 未配置")
}
if db.Sqlite.Path == "" {
return nil, errors.New("初始化数据库失败: sqlite path 未配置")
}
dbConf = &sqlite3.Config{
File: db.Sqlite.Path,
Parameters: db.Sqlite.Parameters,
}
if !zfile.FileExist(db.Sqlite.Path) {
err := zfile.WriteFile(db.Sqlite.Path, []byte(""))
if err != nil {
return nil, err
}
}
return
},
}
)