diff --git a/application.go b/application.go index 3270015..14ef40f 100644 --- a/application.go +++ b/application.go @@ -475,6 +475,7 @@ func SetDB(appIns *Application) { func SetMysqlDB(appIns *Application, config Map) { appIns.Db.Type = "mysql" + appIns.Db.Prefix = config.GetString("prefix") appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) { //master数据库配置 query := config.GetString("user") + ":" + config.GetString("password") + @@ -503,6 +504,7 @@ func SetMysqlDB(appIns *Application, config Map) { func SetSqliteDB(appIns *Application, config Map) { appIns.Db.Type = "sqlite" + appIns.Db.Prefix = config.GetString("prefix") appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) { db, e := sql.Open("sqlite3", config.GetString("path")) if e != nil && len(err) != 0 { diff --git a/cache/cache_db.go b/cache/cache_db.go index 4fbe234..3c9b983 100644 --- a/cache/cache_db.go +++ b/cache/cache_db.go @@ -9,6 +9,7 @@ import ( ) type HoTimeDBInterface interface { + GetPrefix() string Query(query string, args ...interface{}) []Map Exec(query string, args ...interface{}) (sql.Result, *Error) Get(table string, qu ...interface{}) Map @@ -51,13 +52,13 @@ func (that *CacheDb) initDbTable() { return } 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 { that.isInit = true 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 { that.isInit = true } @@ -65,13 +66,13 @@ func (that *CacheDb) initDbTable() { } 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 { that.isInit = true 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, "ckey" TEXT(60), "cvalue" TEXT(2000), diff --git a/db/db.go b/db/db.go index e6de390..dcc257c 100644 --- a/db/db.go +++ b/db/db.go @@ -18,6 +18,7 @@ type HoTimeDB struct { ContextBase *cache.HoTimeCache Type string + Prefix string LastQuery string LastData []interface{} ConnectFunc func(err ...*Error) (*sql.DB, *sql.DB) @@ -438,7 +439,7 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map { query += " *" } - query += " FROM " + table + query += " FROM " + that.Prefix + table if join { for k, v := range qu[0].(Map) { @@ -515,6 +516,9 @@ func (that *HoTimeDB) Get(table string, qu ...interface{}) Map { } return data[0] } +func (that *HoTimeDB) GetPrefix() string { + return that.Prefix +} // Count 计数 func (that *HoTimeDB) Count(table string, qu ...interface{}) int { @@ -853,7 +857,7 @@ func (that *HoTimeDB) cond(tag string, data Map) (string, []interface{}) { // Update 更新数据 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' qs := make([]interface{}, 0) 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 { - query := "DELETE FROM " + table + " " + query := "DELETE FROM " + that.Prefix + table + " " temp, resWhere := that.where(data) 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...)