Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6d9af149ad | |||
| 253cad35ef | |||
| 47c8736f34 | |||
| dcce8ace9e | |||
| 125ccc5c3b | |||
| 432d59d483 | |||
| 60f222b011 |
+33
-27
@@ -13,7 +13,6 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type HoTimeDB struct {
|
type HoTimeDB struct {
|
||||||
@@ -82,7 +81,7 @@ func (that *HotimeDBBuilder) Update(qu ...interface{}) int64 {
|
|||||||
}
|
}
|
||||||
if lth > 1 {
|
if lth > 1 {
|
||||||
for k := 1; k < lth; k++ {
|
for k := 1; k < lth; k++ {
|
||||||
data[ObjToStr(k-1)] = qu[k]
|
data[ObjToStr(qu[k-1])] = qu[k]
|
||||||
k++
|
k++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -127,7 +126,7 @@ func (that *HotimeDBBuilder) Join(qu ...interface{}) *HotimeDBBuilder {
|
|||||||
}
|
}
|
||||||
if lth > 1 {
|
if lth > 1 {
|
||||||
for k := 1; k < lth; k++ {
|
for k := 1; k < lth; k++ {
|
||||||
data[ObjToStr(k-1)] = qu[k]
|
data[ObjToStr(qu[k-1])] = qu[k]
|
||||||
k++
|
k++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -156,7 +155,7 @@ func (that *HotimeDBBuilder) And(qu ...interface{}) *HotimeDBBuilder {
|
|||||||
if lth > 1 {
|
if lth > 1 {
|
||||||
where = Map{}
|
where = Map{}
|
||||||
for k := 1; k < lth; k++ {
|
for k := 1; k < lth; k++ {
|
||||||
where[ObjToStr(k-1)] = qu[k]
|
where[ObjToStr(qu[k-1])] = qu[k]
|
||||||
k++
|
k++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -188,7 +187,7 @@ func (that *HotimeDBBuilder) Or(qu ...interface{}) *HotimeDBBuilder {
|
|||||||
if lth > 1 {
|
if lth > 1 {
|
||||||
where = Map{}
|
where = Map{}
|
||||||
for k := 1; k < lth; k++ {
|
for k := 1; k < lth; k++ {
|
||||||
where[ObjToStr(k-1)] = qu[k]
|
where[ObjToStr(qu[k-1])] = qu[k]
|
||||||
k++
|
k++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -219,7 +218,7 @@ func (that *HotimeDBBuilder) Where(qu ...interface{}) *HotimeDBBuilder {
|
|||||||
if lth > 1 {
|
if lth > 1 {
|
||||||
where = Map{}
|
where = Map{}
|
||||||
for k := 1; k < lth; k++ {
|
for k := 1; k < lth; k++ {
|
||||||
where[ObjToStr(k-1)] = qu[k]
|
where[ObjToStr(qu[k-1])] = qu[k]
|
||||||
k++
|
k++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -272,7 +271,11 @@ func (that *HoTimeDB) GetType() string {
|
|||||||
|
|
||||||
// Action 事务,如果action返回true则执行成功;false则回滚
|
// Action 事务,如果action返回true则执行成功;false则回滚
|
||||||
func (that *HoTimeDB) Action(action func(db HoTimeDB) (isSuccess bool)) (isSuccess bool) {
|
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()
|
tx, err := db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
that.LastErr.SetError(err)
|
that.LastErr.SetError(err)
|
||||||
@@ -315,7 +318,7 @@ func (that *HoTimeDB) InitDb(err ...*Error) *Error {
|
|||||||
e := that.SlaveDB.Ping()
|
e := that.SlaveDB.Ping()
|
||||||
that.LastErr.SetError(e)
|
that.LastErr.SetError(e)
|
||||||
}
|
}
|
||||||
that.DB.SetConnMaxLifetime(time.Second)
|
//that.DB.SetConnMaxLifetime(time.Second)
|
||||||
return that.LastErr
|
return that.LastErr
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -535,7 +538,7 @@ func (that *HoTimeDB) md5(query string, args ...interface{}) string {
|
|||||||
|
|
||||||
func (that *HoTimeDB) Query(query string, args ...interface{}) []Map {
|
func (that *HoTimeDB) Query(query string, args ...interface{}) []Map {
|
||||||
defer func() {
|
defer func() {
|
||||||
if that.Mode == 2 {
|
if that.Mode != 0 {
|
||||||
that.Log.Info("SQL:"+that.LastQuery, " DATA:", that.LastData, " ERROR:", that.LastErr.GetError())
|
that.Log.Info("SQL:"+that.LastQuery, " DATA:", that.LastData, " ERROR:", that.LastErr.GetError())
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -563,16 +566,18 @@ func (that *HoTimeDB) Query(query string, args ...interface{}) []Map {
|
|||||||
resl, err = db.Query(query, args...)
|
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)
|
that.LastErr.SetError(err)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err = db.Ping(); err != nil {
|
|
||||||
that.LastErr.SetError(err)
|
if err = db.Ping(); err == nil {
|
||||||
_ = that.InitDb()
|
|
||||||
if that.LastErr.GetError() != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return that.Query(query, args...)
|
return that.Query(query, args...)
|
||||||
}
|
}
|
||||||
|
that.LastErr.SetError(err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -581,7 +586,7 @@ func (that *HoTimeDB) Query(query string, args ...interface{}) []Map {
|
|||||||
|
|
||||||
func (that *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, *Error) {
|
func (that *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, *Error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if that.Mode == 2 {
|
if that.Mode != 0 {
|
||||||
that.Log.Info("SQL: "+that.LastQuery, " DATA: ", that.LastData, " ERROR: ", that.LastErr.GetError())
|
that.Log.Info("SQL: "+that.LastQuery, " DATA: ", that.LastData, " ERROR: ", that.LastErr.GetError())
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -602,19 +607,18 @@ func (that *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, *Erro
|
|||||||
resl, e = that.DB.Exec(query, args...)
|
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)
|
that.LastErr.SetError(e)
|
||||||
|
|
||||||
//判断是否连接断开了
|
//判断是否连接断开了
|
||||||
if e != nil {
|
if e != nil {
|
||||||
|
|
||||||
if e = that.DB.Ping(); e != nil {
|
if e = that.DB.Ping(); e == nil {
|
||||||
that.LastErr.SetError(e)
|
|
||||||
_ = that.InitDb()
|
|
||||||
if that.LastErr.GetError() != nil {
|
|
||||||
return resl, that.LastErr
|
|
||||||
}
|
|
||||||
return that.Exec(query, args...)
|
return that.Exec(query, args...)
|
||||||
}
|
}
|
||||||
|
that.LastErr.SetError(e)
|
||||||
return resl, that.LastErr
|
return resl, that.LastErr
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -661,11 +665,13 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map {
|
|||||||
data := ObjToSlice(qu[intQs])
|
data := ObjToSlice(qu[intQs])
|
||||||
for i := 0; i < len(data); i++ {
|
for i := 0; i < len(data); i++ {
|
||||||
k := data.GetString(i)
|
k := data.GetString(i)
|
||||||
if strings.Contains(k, " AS ") {
|
if strings.Contains(k, " AS ") ||
|
||||||
|
strings.Contains(k, ".") {
|
||||||
|
|
||||||
query += " " + k + " "
|
query += " " + k + " "
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
query += " `" + k + "` "
|
query += " `" + k + "` "
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -697,9 +703,9 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if reflect.ValueOf(qu[0]).Type().String() == "common.Slice" {
|
if reflect.ValueOf(qu[0]).Type().String() == "common.Slice" {
|
||||||
|
qu0 := ObjToSlice(qu[0])
|
||||||
for key, _ := range testQuData {
|
for key, _ := range qu0 {
|
||||||
v := testQuData.GetMap(key)
|
v := qu0.GetMap(key)
|
||||||
for k1, v1 := range v {
|
for k1, v1 := range v {
|
||||||
testQu = append(testQu, k1)
|
testQu = append(testQu, k1)
|
||||||
testQuData[k1] = v1
|
testQuData[k1] = v1
|
||||||
|
|||||||
Reference in New Issue
Block a user