Compare commits

...

3 Commits

Author SHA1 Message Date
hoteas 60f222b011 数据库操作优化 2022-08-17 16:35:52 +08:00
hoteas 7a8b86ed12 数据库操作优化,优化系统管理后台代码生成逻辑 2022-08-17 16:02:36 +08:00
hoteas b940afa6b0 数据库操作优化 2022-08-15 05:59:06 +08:00
2 changed files with 104 additions and 27 deletions
+2 -7
View File
@@ -566,14 +566,9 @@ func Init(config string) *Application {
codeMake["name"] = codeMake.GetString("table")
}
if appIns.Config.GetInt("mode") > 0 {
appIns.MakeCodeRouter[codeMake.GetString("name")] = &code.MakeCode{Error: appIns.Error}
appIns.MakeCodeRouter[codeMake.GetString("name")] = &code.MakeCode{Error: appIns.Error}
appIns.MakeCodeRouter[codeMake.GetString("name")].Db2JSON(&appIns.Db, codeMake)
appIns.MakeCodeRouter[codeMake.GetString("name")].Db2JSON(&appIns.Db, codeMake)
} else {
appIns.MakeCodeRouter[codeMake.GetString("name")] = &code.MakeCode{Error: appIns.Error}
appIns.MakeCodeRouter[codeMake.GetString("name")].Db2JSON(nil, codeMake)
}
//接入动态代码层
if appIns.Router == nil {
appIns.Router = Router{}
+102 -20
View File
@@ -13,6 +13,7 @@ import (
"reflect"
"sort"
"strings"
"time"
)
type HoTimeDB struct {
@@ -70,7 +71,21 @@ func (that *HotimeDBBuilder) Select(qu ...interface{}) []Map {
return that.HoTimeDB.Select(that.table, that.join, qu, that.where)
}
func (that *HotimeDBBuilder) Update(data Map) int64 {
func (that *HotimeDBBuilder) Update(qu ...interface{}) int64 {
lth := len(qu)
if lth == 0 {
return 0
}
data := Map{}
if lth == 1 {
data = ObjToMap(qu[0])
}
if lth > 1 {
for k := 1; k < lth; k++ {
data[ObjToStr(k-1)] = qu[k]
k++
}
}
return that.HoTimeDB.Update(that.table, data, that.where)
}
@@ -101,19 +116,51 @@ func (that *HotimeDBBuilder) FullJoin(table, joinStr string) *HotimeDBBuilder {
return that
}
func (that *HotimeDBBuilder) Join(join Map) *HotimeDBBuilder {
func (that *HotimeDBBuilder) Join(qu ...interface{}) *HotimeDBBuilder {
lth := len(qu)
if lth == 0 {
return that
}
data := Map{}
if lth == 1 {
data = ObjToMap(qu[0])
}
if lth > 1 {
for k := 1; k < lth; k++ {
data[ObjToStr(k-1)] = qu[k]
k++
}
}
if that.join == nil {
that.join = Slice{}
}
if join == nil {
if data == nil {
return that
}
that.join = append(that.join, join)
that.join = append(that.join, data)
return that
}
func (that *HotimeDBBuilder) And(where Map) *HotimeDBBuilder {
func (that *HotimeDBBuilder) And(qu ...interface{}) *HotimeDBBuilder {
lth := len(qu)
if lth == 0 {
return that
}
var where Map
if lth == 1 {
where = ObjToMap(qu[0])
}
if lth > 1 {
where = Map{}
for k := 1; k < lth; k++ {
where[ObjToStr(k-1)] = qu[k]
k++
}
}
if where == nil {
return that
}
@@ -129,7 +176,22 @@ func (that *HotimeDBBuilder) And(where Map) *HotimeDBBuilder {
return that
}
func (that *HotimeDBBuilder) Or(where Map) *HotimeDBBuilder {
func (that *HotimeDBBuilder) Or(qu ...interface{}) *HotimeDBBuilder {
lth := len(qu)
if lth == 0 {
return that
}
var where Map
if lth == 1 {
where = ObjToMap(qu[0])
}
if lth > 1 {
where = Map{}
for k := 1; k < lth; k++ {
where[ObjToStr(k-1)] = qu[k]
k++
}
}
if where == nil {
return that
}
@@ -144,7 +206,24 @@ func (that *HotimeDBBuilder) Or(where Map) *HotimeDBBuilder {
return that
}
func (that *HotimeDBBuilder) Where(where Map) *HotimeDBBuilder {
func (that *HotimeDBBuilder) Where(qu ...interface{}) *HotimeDBBuilder {
lth := len(qu)
if lth == 0 {
return that
}
var where Map
if lth == 1 {
where = ObjToMap(qu[0])
}
if lth > 1 {
where = Map{}
for k := 1; k < lth; k++ {
where[ObjToStr(k-1)] = qu[k]
k++
}
}
if where == nil {
return that
}
@@ -236,7 +315,7 @@ func (that *HoTimeDB) InitDb(err ...*Error) *Error {
e := that.SlaveDB.Ping()
that.LastErr.SetError(e)
}
that.DB.SetConnMaxLifetime(time.Second)
return that.LastErr
}
@@ -486,13 +565,15 @@ func (that *HoTimeDB) Query(query string, args ...interface{}) []Map {
that.LastErr.SetError(err)
if err != nil {
if err = db.Ping(); err != nil {
that.LastErr.SetError(err)
_ = that.InitDb()
if that.LastErr.GetError() != nil {
return nil
}
if err = db.Ping(); err == nil {
return that.Query(query, args...)
} else {
err = that.InitDb()
that.LastErr.SetError(err)
if err == nil {
return that.Query(query, args...)
}
}
return nil
}
@@ -528,13 +609,14 @@ func (that *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, *Erro
//判断是否连接断开了
if e != nil {
if e = that.DB.Ping(); e != nil {
that.LastErr.SetError(e)
_ = that.InitDb()
if that.LastErr.GetError() != nil {
return resl, that.LastErr
}
if e = that.DB.Ping(); e == nil {
return that.Exec(query, args...)
} else {
e = that.InitDb()
that.LastErr.SetError(e)
if e == nil {
return that.Exec(query, args...)
}
}
return resl, that.LastErr
}