Compare commits

..

5 Commits

Author SHA1 Message Date
hoteas 6d9af149ad 数据库错误操作优化 2022-08-18 13:40:02 +08:00
hoteas 253cad35ef 数据库错误操作优化 2022-08-18 13:28:02 +08:00
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
+30 -26
View File
@@ -81,7 +81,7 @@ func (that *HotimeDBBuilder) Update(qu ...interface{}) int64 {
}
if lth > 1 {
for k := 1; k < lth; k++ {
data[ObjToStr(k-1)] = qu[k]
data[ObjToStr(qu[k-1])] = qu[k]
k++
}
}
@@ -126,7 +126,7 @@ func (that *HotimeDBBuilder) Join(qu ...interface{}) *HotimeDBBuilder {
}
if lth > 1 {
for k := 1; k < lth; k++ {
data[ObjToStr(k-1)] = qu[k]
data[ObjToStr(qu[k-1])] = qu[k]
k++
}
}
@@ -155,7 +155,7 @@ func (that *HotimeDBBuilder) And(qu ...interface{}) *HotimeDBBuilder {
if lth > 1 {
where = Map{}
for k := 1; k < lth; k++ {
where[ObjToStr(k-1)] = qu[k]
where[ObjToStr(qu[k-1])] = qu[k]
k++
}
}
@@ -187,7 +187,7 @@ func (that *HotimeDBBuilder) Or(qu ...interface{}) *HotimeDBBuilder {
if lth > 1 {
where = Map{}
for k := 1; k < lth; k++ {
where[ObjToStr(k-1)] = qu[k]
where[ObjToStr(qu[k-1])] = qu[k]
k++
}
}
@@ -218,7 +218,7 @@ func (that *HotimeDBBuilder) Where(qu ...interface{}) *HotimeDBBuilder {
if lth > 1 {
where = Map{}
for k := 1; k < lth; k++ {
where[ObjToStr(k-1)] = qu[k]
where[ObjToStr(qu[k-1])] = qu[k]
k++
}
}
@@ -271,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)
@@ -534,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())
}
}()
@@ -562,18 +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 {
return that.Query(query, args...)
} else {
err = that.InitDb()
that.LastErr.SetError(err)
if err == nil {
return that.Query(query, args...)
}
}
that.LastErr.SetError(err)
return nil
}
@@ -582,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())
}
}()
@@ -603,20 +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 {
return that.Exec(query, args...)
} else {
e = that.InitDb()
that.LastErr.SetError(e)
if e == nil {
return that.Exec(query, args...)
}
}
that.LastErr.SetError(e)
return resl, that.LastErr
}
@@ -663,11 +665,13 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map {
data := ObjToSlice(qu[intQs])
for i := 0; i < len(data); i++ {
k := data.GetString(i)
if strings.Contains(k, " AS ") {
if strings.Contains(k, " AS ") ||
strings.Contains(k, ".") {
query += " " + k + " "
} else {
query += " `" + k + "` "
}
@@ -699,9 +703,9 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map {
}
if reflect.ValueOf(qu[0]).Type().String() == "common.Slice" {
for key, _ := range testQuData {
v := testQuData.GetMap(key)
qu0 := ObjToSlice(qu[0])
for key, _ := range qu0 {
v := qu0.GetMap(key)
for k1, v1 := range v {
testQu = append(testQu, k1)
testQuData[k1] = v1