-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegisterDataBase.go
52 lines (41 loc) · 1.33 KB
/
RegisterDataBase.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
package register
import (
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
)
// RegisterDataBase 注册数据库
func RegisterDataBase() {
driver := beego.AppConfig.String("database::db_driver")
logs.Debug(">> driver:"+driver)
switch driver {
case "mysql":
mysql()
case "sqlite3":
sqlite3()
}
if beego.AppConfig.String("runmode") == "dev" {
orm.Debug = true
}
}
func mysql(){
alias := beego.AppConfig.String("mysql::db_alias")
host := beego.AppConfig.String("mysql::db_host")
port := beego.AppConfig.String("mysql::db_port")
database := beego.AppConfig.String("mysql::db_database")
username := beego.AppConfig.String("mysql::db_username")
password := beego.AppConfig.String("mysql::db_password")
charset := beego.AppConfig.String("mysql::db_charset")
dataSource := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=true&loc=Local", username, password, host, port, database, charset)
orm.RegisterDriver("mysql", orm.DRMySQL)
orm.RegisterDataBase(alias, "mysql", dataSource)
}
func sqlite3(){
alias := beego.AppConfig.String("sqlite3::db_alias")
database := beego.AppConfig.String("sqlite3::db_database")
orm.RegisterDriver("sqlite3", orm.DRSqlite)
orm.RegisterDataBase(alias, "sqlite3", database)
}