自动设置数据库
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
})
|
||||
}
|
||||
@@ -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
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user