Compare commits

...

7 Commits

Author SHA1 Message Date
hoteas 47c8736f34 数据库错误操作优化 2022-08-18 12:33:01 +08:00
hoteas dcce8ace9e 数据库事务操作优化 2022-08-18 11:36:23 +08:00
hoteas 125ccc5c3b 数据库操作优化 2022-08-17 17:10:12 +08:00
hoteas 432d59d483 数据库操作优化 2022-08-17 16:42:46 +08:00
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 109 additions and 31 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{}
+107 -24
View File
@@ -70,7 +70,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 +115,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 +175,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 +205,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
}
@@ -193,7 +271,11 @@ func (that *HoTimeDB) GetType() string {
// Action 事务,如果action返回true则执行成功;false则回滚
func (that *HoTimeDB) Action(action func(db HoTimeDB) (isSuccess bool)) (isSuccess bool) {
db := HoTimeDB{DB: that.DB, HoTimeCache: that.HoTimeCache, Prefix: that.Prefix}
db := HoTimeDB{that.DB, that.ContextBase, that.DBName,
that.HoTimeCache, that.Log, that.Type,
that.Prefix, that.LastQuery, that.LastData,
that.ConnectFunc, that.LastErr, that.limit, that.Tx,
that.SlaveDB, that.Mode}
tx, err := db.Begin()
if err != nil {
that.LastErr.SetError(err)
@@ -236,7 +318,7 @@ func (that *HoTimeDB) InitDb(err ...*Error) *Error {
e := that.SlaveDB.Ping()
that.LastErr.SetError(e)
}
//that.DB.SetConnMaxLifetime(time.Second)
return that.LastErr
}
@@ -456,7 +538,7 @@ func (that *HoTimeDB) md5(query string, args ...interface{}) string {
func (that *HoTimeDB) Query(query string, args ...interface{}) []Map {
defer func() {
if that.Mode == 2 {
if that.Mode != 0 {
that.Log.Info("SQL:"+that.LastQuery, " DATA:", that.LastData, " ERROR:", that.LastErr.GetError())
}
}()
@@ -484,16 +566,18 @@ func (that *HoTimeDB) Query(query string, args ...interface{}) []Map {
resl, err = db.Query(query, args...)
}
if err != nil && that.LastErr.GetError() != nil &&
that.LastErr.GetError().Error() == err.Error() {
return nil
}
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...)
}
that.LastErr.SetError(err)
return nil
}
@@ -502,7 +586,7 @@ func (that *HoTimeDB) Query(query string, args ...interface{}) []Map {
func (that *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, *Error) {
defer func() {
if that.Mode == 2 {
if that.Mode != 0 {
that.Log.Info("SQL: "+that.LastQuery, " DATA: ", that.LastData, " ERROR: ", that.LastErr.GetError())
}
}()
@@ -523,19 +607,18 @@ func (that *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, *Erro
resl, e = that.DB.Exec(query, args...)
}
if e != nil && that.LastErr.GetError() != nil &&
that.LastErr.GetError().Error() == e.Error() {
return resl, that.LastErr
}
that.LastErr.SetError(e)
//判断是否连接断开了
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...)
}
that.LastErr.SetError(e)
return resl, that.LastErr
}