use logrus to project default log tools

This commit is contained in:
hoteas
2021-05-25 05:08:17 +08:00
parent 9bc930750d
commit df07afb744
15 changed files with 491 additions and 457 deletions
+139 -128
View File
@@ -22,27 +22,28 @@ type HoTimeDB struct {
ConnectFunc func(err ...*Error) (*sql.DB, *sql.DB)
LastErr Error
limit Slice
Tx *sql.Tx //事务对象
*sql.Tx //事务对象
SlaveDB *sql.DB
}
//设置数据库配置连接
func (this *HoTimeDB) SetConnect(connect func(err ...*Error) (master, slave *sql.DB), err ...*Error) {
this.ConnectFunc = connect
this.InitDb()
// SetConnect 设置数据库配置连接
func (that *HoTimeDB) SetConnect(connect func(err ...*Error) (master, slave *sql.DB), err ...*Error) {
that.ConnectFunc = connect
_ = that.InitDb(err...)
}
//设置数据库配置连接
func (this *HoTimeDB) GetType() string {
return this.Type
// GetType 设置数据库配置连接
func (that *HoTimeDB) GetType() string {
return that.Type
}
//事务,如果action返回true则执行成功;false则回滚
func (this *HoTimeDB) Action(action func(db HoTimeDB) bool) bool {
db := HoTimeDB{DB: this.DB, CacheIns: this.CacheIns, DBCached: this.DBCached}
// Action 事务,如果action返回true则执行成功;false则回滚
func (that *HoTimeDB) Action(action func(db HoTimeDB) bool) bool {
db := HoTimeDB{DB: that.DB, CacheIns: that.CacheIns, DBCached: that.DBCached}
tx, err := db.Begin()
if err != nil {
this.LastErr.SetError(err)
that.LastErr.SetError(err)
return false
}
@@ -51,66 +52,74 @@ func (this *HoTimeDB) Action(action func(db HoTimeDB) bool) bool {
result := action(db)
if !result {
db.Tx.Rollback()
err = db.Tx.Rollback()
if err != nil {
that.LastErr.SetError(err)
return false
}
return result
}
db.Tx.Commit()
err = db.Tx.Commit()
if err != nil {
that.LastErr.SetError(err)
return false
}
return result
}
func (this *HoTimeDB) InitDb(err ...*Error) Error {
func (that *HoTimeDB) InitDb(err ...*Error) Error {
if len(err) != 0 {
this.LastErr = *(err[0])
that.LastErr = *(err[0])
}
this.DB, this.SlaveDB = this.ConnectFunc(&this.LastErr)
if this.DB == nil {
return this.LastErr
that.DB, that.SlaveDB = that.ConnectFunc(&that.LastErr)
if that.DB == nil {
return that.LastErr
}
e := this.DB.Ping()
e := that.DB.Ping()
this.LastErr.SetError(e)
that.LastErr.SetError(e)
if this.SlaveDB != nil {
e := this.SlaveDB.Ping()
this.LastErr.SetError(e)
if that.SlaveDB != nil {
e := that.SlaveDB.Ping()
that.LastErr.SetError(e)
}
return this.LastErr
return that.LastErr
}
func (this *HoTimeDB) Page(page, pageRow int) *HoTimeDB {
func (that *HoTimeDB) Page(page, pageRow int) *HoTimeDB {
page = (page - 1) * pageRow
if page < 0 {
page = 1
}
this.limit = Slice{page, pageRow}
return this
that.limit = Slice{page, pageRow}
return that
}
func (this *HoTimeDB) PageSelect(table string, qu ...interface{}) []Map {
func (that *HoTimeDB) PageSelect(table string, qu ...interface{}) []Map {
if len(qu) == 1 {
qu = append(qu, Map{"LIMIT": this.limit})
qu = append(qu, Map{"LIMIT": that.limit})
}
if len(qu) == 2 {
temp := qu[1].(Map)
temp["LIMIT"] = this.limit
temp["LIMIT"] = that.limit
qu[1] = temp
}
if len(qu) == 3 {
temp := qu[2].(Map)
temp["LIMIT"] = this.limit
temp["LIMIT"] = that.limit
qu[2] = temp
}
//fmt.Println(qu)
data := this.Select(table, qu...)
data := that.Select(table, qu...)
return data
}
//数据库数据解析
func (this *HoTimeDB) Row(resl *sql.Rows) []Map {
// Row 数据库数据解析
func (that *HoTimeDB) Row(resl *sql.Rows) []Map {
dest := make([]Map, 0)
strs, _ := resl.Columns()
@@ -122,7 +131,11 @@ func (this *HoTimeDB) Row(resl *sql.Rows) []Map {
for j := 0; j < len(a); j++ {
b[j] = &a[j]
}
resl.Scan(b...)
err := resl.Scan(b...)
if err != nil {
that.LastErr.SetError(err)
return nil
}
for j := 0; j < len(a); j++ {
if a[j] != nil && reflect.ValueOf(a[j]).Type().String() == "[]uint8" {
lis[strs[j]] = string(a[j].([]byte))
@@ -134,9 +147,9 @@ func (this *HoTimeDB) Row(resl *sql.Rows) []Map {
//防止int被误读为float64
jlis, e := json.Marshal(lis)
if e != nil {
this.LastErr.SetError(e)
that.LastErr.SetError(e)
} else {
lis.JsonToMap(string(jlis), &this.LastErr)
lis.JsonToMap(string(jlis), &that.LastErr)
}
dest = append(dest, lis)
@@ -201,25 +214,25 @@ func (this *HoTimeDB) Row(resl *sql.Rows) []Map {
//
//}
func (this *HoTimeDB) backupSave(path string, tt string, code int) {
func (that *HoTimeDB) backupSave(path string, tt string, code int) {
fd, _ := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
defer fd.Close()
str := "\r\n"
if code == 0 || code == 2 {
str += this.backupDdl(tt)
str += that.backupDdl(tt)
}
if code == 0 || code == 1 {
str += "insert into `" + tt + "`\r\n\r\n("
str += this.backupCol(tt)
str += that.backupCol(tt)
}
fd.Write([]byte(str))
_, _ = fd.Write([]byte(str))
}
func (this *HoTimeDB) backupDdl(tt string) string {
func (that *HoTimeDB) backupDdl(tt string) string {
data := this.Query("show create table " + tt)
data := that.Query("show create table " + tt)
if len(data) == 0 {
return ""
}
@@ -227,9 +240,9 @@ func (this *HoTimeDB) backupDdl(tt string) string {
return ObjToStr(data[0]["Create Table"]) + ";\r\n\r\n"
}
func (this *HoTimeDB) backupCol(tt string) string {
func (that *HoTimeDB) backupCol(tt string) string {
str := ""
data := this.Select(tt, "*")
data := that.Select(tt, "*")
lthData := len(data)
@@ -239,7 +252,7 @@ func (this *HoTimeDB) backupCol(tt string) string {
lthCol := len(data[0])
col := make([]string, lthCol)
tempLthData := 0
for k, _ := range data[0] {
for k := range data[0] {
if tempLthData == lthCol-1 {
str += "`" + k + "`)"
@@ -281,89 +294,89 @@ func (this *HoTimeDB) backupCol(tt string) string {
return str
}
func (this *HoTimeDB) md5(query string, args ...interface{}) string {
func (that *HoTimeDB) md5(query string, args ...interface{}) string {
strByte, _ := json.Marshal(args)
str := Md5(query + ":" + string(strByte))
return str
}
func (this *HoTimeDB) Query(query string, args ...interface{}) []Map {
func (that *HoTimeDB) Query(query string, args ...interface{}) []Map {
//fmt.Println(query)
var err error
var resl *sql.Rows
this.LastQuery = query
this.LastData = args
that.LastQuery = query
that.LastData = args
//主从数据库切换,只有select语句有从数据库
db := this.DB
if this.SlaveDB != nil {
db = this.SlaveDB
db := that.DB
if that.SlaveDB != nil {
db = that.SlaveDB
}
if db == nil {
err = errors.New("没有初始化数据库")
this.LastErr.SetError(err)
that.LastErr.SetError(err)
return nil
}
if this.Tx != nil {
resl, err = this.Tx.Query(query, args...)
if that.Tx != nil {
resl, err = that.Tx.Query(query, args...)
} else {
resl, err = db.Query(query, args...)
}
this.LastErr.SetError(err)
that.LastErr.SetError(err)
if err != nil {
if err = db.Ping(); err != nil {
this.LastErr.SetError(err)
this.InitDb()
if this.LastErr.GetError() != nil {
that.LastErr.SetError(err)
_ = that.InitDb()
if that.LastErr.GetError() != nil {
return nil
}
return this.Query(query, args...)
return that.Query(query, args...)
}
return nil
}
return this.Row(resl)
return that.Row(resl)
}
func (this *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, Error) {
func (that *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, Error) {
this.LastQuery = query
this.LastData = args
that.LastQuery = query
that.LastData = args
var e error
var resl sql.Result
if this.DB == nil {
if that.DB == nil {
err := errors.New("没有初始化数据库")
this.LastErr.SetError(err)
return nil, this.LastErr
that.LastErr.SetError(err)
return nil, that.LastErr
}
if this.Tx != nil {
resl, e = this.Tx.Exec(query, args...)
if that.Tx != nil {
resl, e = that.Tx.Exec(query, args...)
} else {
resl, e = this.DB.Exec(query, args...)
resl, e = that.DB.Exec(query, args...)
}
this.LastErr.SetError(e)
that.LastErr.SetError(e)
//判断是否连接断开了
if e != nil {
if e = this.DB.Ping(); e != nil {
this.LastErr.SetError(e)
this.InitDb()
if this.LastErr.GetError() != nil {
return resl, this.LastErr
if e = that.DB.Ping(); e != nil {
that.LastErr.SetError(e)
_ = that.InitDb()
if that.LastErr.GetError() != nil {
return resl, that.LastErr
}
return this.Exec(query, args...)
return that.Exec(query, args...)
}
}
return resl, this.LastErr
return resl, that.LastErr
}
//func (this *HoTimeDB)copy(data []Map)[]Map{
@@ -384,7 +397,8 @@ func (this *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, Error
// return res
//
//}
func (this *HoTimeDB) Select(table string, qu ...interface{}) []Map {
func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map {
query := "SELECT"
where := Map{}
@@ -446,15 +460,15 @@ func (this *HoTimeDB) Select(table string, qu ...interface{}) []Map {
where = qu[intWhere].(Map)
}
temp, resWhere := this.where(where)
temp, resWhere := that.where(where)
query += temp
qs = append(qs, resWhere...)
md5 := this.md5(query, qs...)
md5 := that.md5(query, qs...)
if this.DBCached && this.CacheIns != nil {
if that.DBCached && that.CacheIns != nil {
//如果缓存有则从缓存取
cacheData := this.Cache(table + ":" + md5)
cacheData := that.Cache(table + ":" + md5)
if cacheData.Data != nil {
return cacheData.ToMapArray()
@@ -462,23 +476,23 @@ func (this *HoTimeDB) Select(table string, qu ...interface{}) []Map {
}
//无缓存则数据库取
res := this.Query(query, qs...)
res := that.Query(query, qs...)
if res == nil {
res = []Map{}
}
//缓存
if this.DBCached && this.CacheIns != nil {
if that.DBCached && that.CacheIns != nil {
this.Cache(table+":"+md5, res)
_ = that.Cache(table+":"+md5, res)
}
return res
}
func (this *HoTimeDB) Get(table string, qu ...interface{}) Map {
func (that *HoTimeDB) Get(table string, qu ...interface{}) Map {
//fmt.Println(qu)
if len(qu) == 1 {
qu = append(qu, Map{"LIMIT": 1})
@@ -494,19 +508,16 @@ func (this *HoTimeDB) Get(table string, qu ...interface{}) Map {
qu[2] = temp
}
//fmt.Println(qu)
data := this.Select(table, qu...)
data := that.Select(table, qu...)
if len(data) == 0 {
return nil
}
return data[0]
}
/**
** 计数
*/
func (this *HoTimeDB) Count(table string, qu ...interface{}) int {
req := []interface{}{}
// Count 计数
func (that *HoTimeDB) Count(table string, qu ...interface{}) int {
var req = []interface{}{}
if len(qu) == 2 {
req = append(req, qu[0])
req = append(req, "COUNT(*)")
@@ -517,7 +528,7 @@ func (this *HoTimeDB) Count(table string, qu ...interface{}) int {
}
//req=append(req,qu...)
data := this.Select(table, req...)
data := that.Select(table, req...)
//fmt.Println(data)
if len(data) == 0 {
return 0
@@ -533,7 +544,7 @@ var condition = []string{"AND", "OR"}
var vcond = []string{"GROUP", "ORDER", "LIMIT"}
//where语句解析
func (this *HoTimeDB) where(data Map) (string, []interface{}) {
func (that *HoTimeDB) where(data Map) (string, []interface{}) {
where := ""
@@ -545,7 +556,7 @@ func (this *HoTimeDB) where(data Map) (string, []interface{}) {
if condition[i] == k {
tw, ts := this.cond(k, v.(Map))
tw, ts := that.cond(k, v.(Map))
where += tw
res = append(res, ts...)
@@ -563,7 +574,7 @@ func (this *HoTimeDB) where(data Map) (string, []interface{}) {
y++
}
if x == len(condition) && y == len(vcond) {
tv, vv := this.varCond(k, v)
tv, vv := that.varCond(k, v)
where += tv
res = append(res, vv...)
@@ -617,7 +628,7 @@ func (this *HoTimeDB) where(data Map) (string, []interface{}) {
}
func (this *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) {
func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) {
where := ""
res := make([]interface{}, 0)
length := len(k)
@@ -635,7 +646,7 @@ func (this *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) {
res = append(res, v)
case "[!]":
k = strings.Replace(k, "[!]", "", -1)
where, res = this.notIn(k, v, where, res)
where, res = that.notIn(k, v, where, res)
case "[#]":
k = strings.Replace(k, "[#]", "", -1)
where += " " + k + "=" + ObjToStr(v)
@@ -692,7 +703,7 @@ func (this *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) {
default:
if reflect.ValueOf(v).Type().String() == "hotime.Slice" {
where += "`" + k + "` IN ("
res = append(res, (v.(Slice))...)
res = append(res, v.(Slice)...)
if len(v.(Slice)) == 0 {
where += ") "
} else {
@@ -725,7 +736,7 @@ func (this *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) {
//fmt.Println(v)
where += "`" + k + "` IN ("
res = append(res, (v.(Slice))...)
res = append(res, v.(Slice)...)
for i := 0; i < len(v.(Slice)); i++ {
if i+1 != len(v.(Slice)) {
where += "?,"
@@ -736,7 +747,7 @@ func (this *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) {
}
} else if reflect.ValueOf(v).Type().String() == "[]interface {}" {
where += "`" + k + "` IN ("
res = append(res, (v.([]interface{}))...)
res = append(res, v.([]interface{})...)
for i := 0; i < len(v.([]interface{})); i++ {
if i+1 != len(v.([]interface{})) {
where += "?,"
@@ -757,7 +768,7 @@ func (this *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) {
}
// this.Db.Update("user",hotime.Map{"ustate":"1"},hotime.Map{"AND":hotime.Map{"OR":hotime.Map{"uid":4,"uname":"dasda"}},"ustate":1})
func (this *HoTimeDB) notIn(k string, v interface{}, where string, res []interface{}) (string, []interface{}) {
func (that *HoTimeDB) notIn(k string, v interface{}, where string, res []interface{}) (string, []interface{}) {
//where:=""
//fmt.Println(reflect.ValueOf(v).Type().String())
if v == nil {
@@ -766,7 +777,7 @@ func (this *HoTimeDB) notIn(k string, v interface{}, where string, res []interfa
} else if reflect.ValueOf(v).Type().String() == "hotime.Slice" {
where += "`" + k + "` NOT IN ("
res = append(res, (v.(Slice))...)
res = append(res, v.(Slice)...)
for i := 0; i < len(v.(Slice)); i++ {
if i+1 != len(v.(Slice)) {
where += "?,"
@@ -777,7 +788,7 @@ func (this *HoTimeDB) notIn(k string, v interface{}, where string, res []interfa
}
} else if reflect.ValueOf(v).Type().String() == "[]interface {}" {
where += "`" + k + "` NOT IN ("
res = append(res, (v.([]interface{}))...)
res = append(res, v.([]interface{})...)
for i := 0; i < len(v.([]interface{})); i++ {
if i+1 != len(v.([]interface{})) {
where += "?,"
@@ -796,7 +807,7 @@ func (this *HoTimeDB) notIn(k string, v interface{}, where string, res []interfa
return where, res
}
func (this *HoTimeDB) cond(tag string, data Map) (string, []interface{}) {
func (that *HoTimeDB) cond(tag string, data Map) (string, []interface{}) {
where := " "
res := make([]interface{}, 0)
lens := len(data)
@@ -807,7 +818,7 @@ func (this *HoTimeDB) cond(tag string, data Map) (string, []interface{}) {
if condition[i] == k {
tw, ts := this.cond(k, v.(Map))
tw, ts := that.cond(k, v.(Map))
if lens--; lens <= 0 {
//fmt.Println(lens)
where += "(" + tw + ") "
@@ -823,7 +834,7 @@ func (this *HoTimeDB) cond(tag string, data Map) (string, []interface{}) {
if x == len(condition) {
tv, vv := this.varCond(k, v)
tv, vv := that.varCond(k, v)
res = append(res, vv...)
if lens--; lens <= 0 {
@@ -838,8 +849,8 @@ func (this *HoTimeDB) cond(tag string, data Map) (string, []interface{}) {
return where, res
}
//更新数据
func (this *HoTimeDB) Update(table string, data Map, where Map) int64 {
// Update 更新数据
func (that *HoTimeDB) Update(table string, data Map, where Map) int64 {
query := "UPDATE " + table + " SET "
//UPDATE Person SET Address = 'Zhongshan 23', City = 'Nanjing' WHERE LastName = 'Wilson'
@@ -861,13 +872,13 @@ func (this *HoTimeDB) Update(table string, data Map, where Map) int64 {
}
}
temp, resWhere := this.where(where)
temp, resWhere := that.where(where)
//fmt.Println(resWhere)
query += temp
qs = append(qs, resWhere...)
res, err := this.Exec(query, qs...)
res, err := that.Exec(query, qs...)
rows := int64(0)
if err.GetError() == nil && res != nil {
@@ -876,22 +887,22 @@ func (this *HoTimeDB) Update(table string, data Map, where Map) int64 {
//如果更新成功,则删除缓存
if rows != 0 {
if this.DBCached && this.CacheIns != nil {
this.Cache(table+"*", nil)
if that.DBCached && that.CacheIns != nil {
_ = that.Cache(table+"*", nil)
}
}
return rows
}
func (this *HoTimeDB) Delete(table string, data map[string]interface{}) int64 {
func (that *HoTimeDB) Delete(table string, data map[string]interface{}) int64 {
query := "DELETE FROM " + table + " "
temp, resWhere := this.where(data)
temp, resWhere := that.where(data)
query += temp
res, err := this.Exec(query, resWhere...)
res, err := that.Exec(query, resWhere...)
rows := int64(0)
if err.GetError() == nil && res != nil {
rows, _ = res.RowsAffected()
@@ -899,8 +910,8 @@ func (this *HoTimeDB) Delete(table string, data map[string]interface{}) int64 {
//如果删除成功,删除对应缓存
if rows != 0 {
if this.DBCached && this.CacheIns != nil {
this.Cache(table+"*", nil)
if that.DBCached && that.CacheIns != nil {
_ = that.Cache(table+"*", nil)
}
}
//return 0
@@ -908,8 +919,8 @@ func (this *HoTimeDB) Delete(table string, data map[string]interface{}) int64 {
return rows
}
//插入新数据
func (this *HoTimeDB) Insert(table string, data map[string]interface{}) int64 {
// Insert 插入新数据
func (that *HoTimeDB) Insert(table string, data map[string]interface{}) int64 {
values := make([]interface{}, 0)
queryString := " ("
@@ -931,18 +942,18 @@ func (this *HoTimeDB) Insert(table string, data map[string]interface{}) int64 {
}
query := "INSERT INTO " + table + queryString + "VALUES" + valueString
res, err := this.Exec(query, values...)
res, err := that.Exec(query, values...)
id := int64(0)
if err.GetError() == nil && res != nil {
//id, this.LastErr.err = res.LastInsertId()
//id, that.LastErr.err = res.LastInsertId()
}
//如果插入成功,删除缓存
if id != 0 {
if this.DBCached && this.CacheIns != nil {
this.Cache(table+"*", nil)
if that.DBCached && that.CacheIns != nil {
_ = that.Cache(table+"*", nil)
}
}