完善error库

This commit is contained in:
hoteas 2021-05-29 00:37:20 +08:00
parent 764d88894e
commit 0e2932ca89
3 changed files with 15 additions and 8 deletions

View File

@ -475,6 +475,7 @@ func SetDB(appIns *Application) {
func SetMysqlDB(appIns *Application, config Map) { func SetMysqlDB(appIns *Application, config Map) {
appIns.Db.Type = "mysql" appIns.Db.Type = "mysql"
appIns.Db.Prefix = config.GetString("prefix")
appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) { appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) {
//master数据库配置 //master数据库配置
query := config.GetString("user") + ":" + config.GetString("password") + query := config.GetString("user") + ":" + config.GetString("password") +
@ -503,6 +504,7 @@ func SetMysqlDB(appIns *Application, config Map) {
func SetSqliteDB(appIns *Application, config Map) { func SetSqliteDB(appIns *Application, config Map) {
appIns.Db.Type = "sqlite" appIns.Db.Type = "sqlite"
appIns.Db.Prefix = config.GetString("prefix")
appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) { appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) {
db, e := sql.Open("sqlite3", config.GetString("path")) db, e := sql.Open("sqlite3", config.GetString("path"))
if e != nil && len(err) != 0 { if e != nil && len(err) != 0 {

9
cache/cache_db.go vendored
View File

@ -9,6 +9,7 @@ import (
) )
type HoTimeDBInterface interface { type HoTimeDBInterface interface {
GetPrefix() string
Query(query string, args ...interface{}) []Map Query(query string, args ...interface{}) []Map
Exec(query string, args ...interface{}) (sql.Result, *Error) Exec(query string, args ...interface{}) (sql.Result, *Error)
Get(table string, qu ...interface{}) Map Get(table string, qu ...interface{}) Map
@ -51,13 +52,13 @@ func (that *CacheDb) initDbTable() {
return return
} }
dbName := dbNames[0].GetString("DATABASE()") dbName := dbNames[0].GetString("DATABASE()")
res := that.Db.Query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='" + dbName + "' AND TABLE_NAME='cached'") res := that.Db.Query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='" + dbName + "' AND TABLE_NAME='" + that.Db.GetPrefix() + "cached'")
if len(res) != 0 { if len(res) != 0 {
that.isInit = true that.isInit = true
return return
} }
_, e := that.Db.Exec("CREATE TABLE `cached` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `ckey` varchar(60) DEFAULT NULL, `cvalue` varchar(2000) DEFAULT NULL, `time` bigint(20) DEFAULT NULL, `endtime` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=198740 DEFAULT CHARSET=utf8") _, e := that.Db.Exec("CREATE TABLE `" + that.Db.GetPrefix() + "cached` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `ckey` varchar(60) DEFAULT NULL, `cvalue` varchar(2000) DEFAULT NULL, `time` bigint(20) DEFAULT NULL, `endtime` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=198740 DEFAULT CHARSET=utf8")
if e.GetError() == nil { if e.GetError() == nil {
that.isInit = true that.isInit = true
} }
@ -65,13 +66,13 @@ func (that *CacheDb) initDbTable() {
} }
if that.Db.GetType() == "sqlite" { if that.Db.GetType() == "sqlite" {
res := that.Db.Query(`select * from sqlite_master where type = 'table' and name = 'cached'`) res := that.Db.Query(`select * from sqlite_master where type = 'table' and name = '` + that.Db.GetPrefix() + `cached'`)
if len(res) != 0 { if len(res) != 0 {
that.isInit = true that.isInit = true
return return
} }
_, e := that.Db.Exec(`CREATE TABLE "cached" ( _, e := that.Db.Exec(`CREATE TABLE "` + that.Db.GetPrefix() + `cached" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"ckey" TEXT(60), "ckey" TEXT(60),
"cvalue" TEXT(2000), "cvalue" TEXT(2000),

View File

@ -18,6 +18,7 @@ type HoTimeDB struct {
ContextBase ContextBase
*cache.HoTimeCache *cache.HoTimeCache
Type string Type string
Prefix string
LastQuery string LastQuery string
LastData []interface{} LastData []interface{}
ConnectFunc func(err ...*Error) (*sql.DB, *sql.DB) ConnectFunc func(err ...*Error) (*sql.DB, *sql.DB)
@ -438,7 +439,7 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map {
query += " *" query += " *"
} }
query += " FROM " + table query += " FROM " + that.Prefix + table
if join { if join {
for k, v := range qu[0].(Map) { for k, v := range qu[0].(Map) {
@ -515,6 +516,9 @@ func (that *HoTimeDB) Get(table string, qu ...interface{}) Map {
} }
return data[0] return data[0]
} }
func (that *HoTimeDB) GetPrefix() string {
return that.Prefix
}
// Count 计数 // Count 计数
func (that *HoTimeDB) Count(table string, qu ...interface{}) int { func (that *HoTimeDB) Count(table string, qu ...interface{}) int {
@ -853,7 +857,7 @@ func (that *HoTimeDB) cond(tag string, data Map) (string, []interface{}) {
// Update 更新数据 // Update 更新数据
func (that *HoTimeDB) Update(table string, data Map, where Map) int64 { func (that *HoTimeDB) Update(table string, data Map, where Map) int64 {
query := "UPDATE " + table + " SET " query := "UPDATE " + that.Prefix + table + " SET "
//UPDATE Person SET Address = 'Zhongshan 23', City = 'Nanjing' WHERE LastName = 'Wilson' //UPDATE Person SET Address = 'Zhongshan 23', City = 'Nanjing' WHERE LastName = 'Wilson'
qs := make([]interface{}, 0) qs := make([]interface{}, 0)
tp := len(data) tp := len(data)
@ -898,7 +902,7 @@ func (that *HoTimeDB) Update(table string, data Map, where Map) int64 {
func (that *HoTimeDB) Delete(table string, data map[string]interface{}) int64 { func (that *HoTimeDB) Delete(table string, data map[string]interface{}) int64 {
query := "DELETE FROM " + table + " " query := "DELETE FROM " + that.Prefix + table + " "
temp, resWhere := that.where(data) temp, resWhere := that.where(data)
query += temp query += temp
@ -941,7 +945,7 @@ func (that *HoTimeDB) Insert(table string, data map[string]interface{}) int64 {
} }
} }
query := "INSERT INTO " + table + queryString + "VALUES" + valueString query := "INSERT INTO " + that.Prefix + table + queryString + "VALUES" + valueString
res, err := that.Exec(query, values...) res, err := that.Exec(query, values...)