自动设置数据库

This commit is contained in:
2019-11-10 16:42:49 +08:00
parent 177eb86eae
commit 58b908f8ba
6 changed files with 114 additions and 21 deletions
+16
View File
@@ -0,0 +1,16 @@
package db
import (
"code.hoteas.com/hoteas/hotime"
"code.hoteas.com/hoteas/hotime/dri/mysql"
"code.hoteas.com/hoteas/hotime/dri/sqlite"
"strings"
)
func SetDB(appIns *hotime.Application) {
if appIns.Config.GetString("dbType") == "sqlite" || strings.Contains(appIns.Config.GetString("dbName"), ".db") {
sqlite.SetDB(appIns)
} else {
mysql.SetDB(appIns)
}
}
+19
View File
@@ -0,0 +1,19 @@
package mysql
import (
"code.hoteas.com/hoteas/hotime"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
func SetDB(appIns *hotime.Application) {
appIns.SetConnectDB(func(err ...*hotime.Error) *sql.DB {
query := appIns.Config.GetString("dbUser") + ":" + appIns.Config.GetString("dbPwd") +
"@tcp(" + appIns.Config.GetString("dbHost") + ":" + appIns.Config.GetString("dbPort") + ")/" + appIns.Config.GetString("dbName") + "?charset=utf8"
DB, e := sql.Open("mysql", query)
if e != nil && len(err) != 0 {
err[0].SetError(e)
}
return DB
})
}
+17
View File
@@ -0,0 +1,17 @@
package sqlite
import (
"code.hoteas.com/hoteas/hotime"
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
func SetDB(appIns *hotime.Application) {
appIns.SetConnectDB(func(err ...*hotime.Error) *sql.DB {
db, e := sql.Open("sqlite3", appIns.Config.GetString("dbName"))
if e != nil && len(err) != 0 {
err[0].SetError(e)
}
return db
})
}