2021-05-23 23:27:41 +00:00
|
|
|
|
package db
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
|
|
import (
|
2022-03-12 17:12:29 +00:00
|
|
|
|
"code.hoteas.com/golang/hotime/cache"
|
|
|
|
|
. "code.hoteas.com/golang/hotime/common"
|
2017-08-04 08:20:59 +00:00
|
|
|
|
"database/sql"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
2022-07-31 19:40:09 +00:00
|
|
|
|
"fmt"
|
2021-05-25 11:53:34 +00:00
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2022-07-11 03:07:17 +00:00
|
|
|
|
"github.com/sirupsen/logrus"
|
2017-08-04 08:20:59 +00:00
|
|
|
|
"os"
|
|
|
|
|
"reflect"
|
2022-03-12 21:06:28 +00:00
|
|
|
|
"sort"
|
2017-08-04 08:20:59 +00:00
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type HoTimeDB struct {
|
|
|
|
|
*sql.DB
|
2021-05-23 23:27:41 +00:00
|
|
|
|
ContextBase
|
2021-06-04 18:18:56 +00:00
|
|
|
|
DBName string
|
2021-05-28 14:52:22 +00:00
|
|
|
|
*cache.HoTimeCache
|
2022-07-11 03:07:17 +00:00
|
|
|
|
Log *logrus.Logger
|
2019-11-10 10:00:45 +00:00
|
|
|
|
Type string
|
2021-05-28 16:37:20 +00:00
|
|
|
|
Prefix string
|
2017-08-04 08:20:59 +00:00
|
|
|
|
LastQuery string
|
|
|
|
|
LastData []interface{}
|
2021-05-23 22:14:58 +00:00
|
|
|
|
ConnectFunc func(err ...*Error) (*sql.DB, *sql.DB)
|
2021-05-25 11:53:34 +00:00
|
|
|
|
LastErr *Error
|
2017-09-19 07:39:35 +00:00
|
|
|
|
limit Slice
|
2021-05-24 21:08:17 +00:00
|
|
|
|
*sql.Tx //事务对象
|
2021-05-23 22:14:58 +00:00
|
|
|
|
SlaveDB *sql.DB
|
2022-07-11 03:07:17 +00:00
|
|
|
|
Mode int //mode为0生产模式,1、为测试模式、2为开发模式
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
// SetConnect 设置数据库配置连接
|
|
|
|
|
func (that *HoTimeDB) SetConnect(connect func(err ...*Error) (master, slave *sql.DB), err ...*Error) {
|
|
|
|
|
that.ConnectFunc = connect
|
|
|
|
|
_ = that.InitDb(err...)
|
|
|
|
|
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
// GetType 设置数据库配置连接
|
|
|
|
|
func (that *HoTimeDB) GetType() string {
|
|
|
|
|
return that.Type
|
2021-05-23 23:27:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
// Action 事务,如果action返回true则执行成功;false则回滚
|
2022-03-13 09:02:19 +00:00
|
|
|
|
func (that *HoTimeDB) Action(action func(db HoTimeDB) (isSuccess bool)) (isSuccess bool) {
|
2021-05-28 16:41:17 +00:00
|
|
|
|
db := HoTimeDB{DB: that.DB, HoTimeCache: that.HoTimeCache, Prefix: that.Prefix}
|
2017-08-23 17:09:11 +00:00
|
|
|
|
tx, err := db.Begin()
|
2017-08-23 01:40:54 +00:00
|
|
|
|
if err != nil {
|
2021-05-24 21:08:17 +00:00
|
|
|
|
that.LastErr.SetError(err)
|
2022-03-13 09:02:19 +00:00
|
|
|
|
return isSuccess
|
2017-08-23 01:40:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 07:39:35 +00:00
|
|
|
|
db.Tx = tx
|
2017-08-23 16:57:48 +00:00
|
|
|
|
|
2022-03-13 09:02:19 +00:00
|
|
|
|
isSuccess = action(db)
|
2017-08-23 01:40:54 +00:00
|
|
|
|
|
2022-03-13 09:02:19 +00:00
|
|
|
|
if !isSuccess {
|
2021-05-24 21:08:17 +00:00
|
|
|
|
err = db.Tx.Rollback()
|
|
|
|
|
if err != nil {
|
|
|
|
|
that.LastErr.SetError(err)
|
2022-03-13 09:02:19 +00:00
|
|
|
|
return isSuccess
|
2021-05-24 21:08:17 +00:00
|
|
|
|
}
|
2022-03-13 09:02:19 +00:00
|
|
|
|
return isSuccess
|
2017-08-23 01:40:54 +00:00
|
|
|
|
}
|
2021-05-24 21:08:17 +00:00
|
|
|
|
err = db.Tx.Commit()
|
|
|
|
|
if err != nil {
|
|
|
|
|
that.LastErr.SetError(err)
|
|
|
|
|
return false
|
|
|
|
|
}
|
2022-03-13 09:02:19 +00:00
|
|
|
|
return true
|
2017-08-23 01:40:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-25 11:53:34 +00:00
|
|
|
|
func (that *HoTimeDB) InitDb(err ...*Error) *Error {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
if len(err) != 0 {
|
2021-05-25 11:53:34 +00:00
|
|
|
|
that.LastErr = err[0]
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
2021-05-25 11:53:34 +00:00
|
|
|
|
that.DB, that.SlaveDB = that.ConnectFunc(that.LastErr)
|
2021-05-24 21:08:17 +00:00
|
|
|
|
if that.DB == nil {
|
|
|
|
|
return that.LastErr
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
2021-05-24 21:08:17 +00:00
|
|
|
|
e := that.DB.Ping()
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
that.LastErr.SetError(e)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
if that.SlaveDB != nil {
|
|
|
|
|
e := that.SlaveDB.Ping()
|
|
|
|
|
that.LastErr.SetError(e)
|
2021-05-23 22:14:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
return that.LastErr
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
func (that *HoTimeDB) Page(page, pageRow int) *HoTimeDB {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
page = (page - 1) * pageRow
|
|
|
|
|
if page < 0 {
|
|
|
|
|
page = 1
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
that.limit = Slice{page, pageRow}
|
|
|
|
|
return that
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
func (that *HoTimeDB) PageSelect(table string, qu ...interface{}) []Map {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
|
|
if len(qu) == 1 {
|
2021-05-24 21:08:17 +00:00
|
|
|
|
qu = append(qu, Map{"LIMIT": that.limit})
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
if len(qu) == 2 {
|
2021-06-11 01:41:23 +00:00
|
|
|
|
temp := DeepCopyMap(qu[1]).(Map)
|
2021-05-24 21:08:17 +00:00
|
|
|
|
temp["LIMIT"] = that.limit
|
2017-08-04 08:20:59 +00:00
|
|
|
|
qu[1] = temp
|
|
|
|
|
}
|
|
|
|
|
if len(qu) == 3 {
|
2021-06-11 01:41:23 +00:00
|
|
|
|
temp := DeepCopyMap(qu[2]).(Map)
|
2021-05-24 21:08:17 +00:00
|
|
|
|
temp["LIMIT"] = that.limit
|
2017-08-04 08:20:59 +00:00
|
|
|
|
qu[2] = temp
|
|
|
|
|
}
|
|
|
|
|
//fmt.Println(qu)
|
2021-05-24 21:08:17 +00:00
|
|
|
|
data := that.Select(table, qu...)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
// Row 数据库数据解析
|
|
|
|
|
func (that *HoTimeDB) Row(resl *sql.Rows) []Map {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
dest := make([]Map, 0)
|
|
|
|
|
strs, _ := resl.Columns()
|
|
|
|
|
|
|
|
|
|
for i := 0; resl.Next(); i++ {
|
|
|
|
|
lis := make(Map, 0)
|
|
|
|
|
a := make([]interface{}, len(strs))
|
|
|
|
|
|
|
|
|
|
b := make([]interface{}, len(a))
|
|
|
|
|
for j := 0; j < len(a); j++ {
|
|
|
|
|
b[j] = &a[j]
|
|
|
|
|
}
|
2021-05-24 21:08:17 +00:00
|
|
|
|
err := resl.Scan(b...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
that.LastErr.SetError(err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
for j := 0; j < len(a); j++ {
|
2021-05-29 16:10:07 +00:00
|
|
|
|
//fmt.Println(reflect.ValueOf(a[j]).Type().String() )
|
2017-08-04 08:20:59 +00:00
|
|
|
|
if a[j] != nil && reflect.ValueOf(a[j]).Type().String() == "[]uint8" {
|
|
|
|
|
lis[strs[j]] = string(a[j].([]byte))
|
|
|
|
|
} else {
|
2021-05-29 16:01:15 +00:00
|
|
|
|
lis[strs[j]] = a[j] //取实际类型
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2019-09-14 09:01:45 +00:00
|
|
|
|
//防止int被误读为float64
|
2021-05-29 16:10:07 +00:00
|
|
|
|
//jlis, e := json.Marshal(lis)
|
|
|
|
|
//if e != nil {
|
|
|
|
|
// that.LastErr.SetError(e)
|
|
|
|
|
//} else {
|
|
|
|
|
// lis.JsonToMap(string(jlis), that.LastErr)
|
|
|
|
|
//}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
|
|
dest = append(dest, lis)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dest
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
////code=0,1,2 0 backup all,1 backup data,2 backup ddl
|
2022-03-12 17:48:54 +00:00
|
|
|
|
//func (that *HoTimeDB) Backup(path string, code int) {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
// var cmd *exec.Cmd
|
|
|
|
|
// switch code {
|
|
|
|
|
// case 0:cmd= exec.Command("mysqldump","-h"+ObjToStr(Config["dbHost"]), "-P"+ObjToStr(Config["dbPort"]),"-u"+ObjToStr(Config["dbUser"]), "-p"+ObjToStr(Config["dbPwd"]),ObjToStr(Config["dbName"]))
|
|
|
|
|
// case 1:cmd= exec.Command("mysqldump","-h"+ObjToStr(Config["dbHost"]), "-P"+ObjToStr(Config["dbPort"]),"-u"+ObjToStr(Config["dbUser"]), "-p"+ObjToStr(Config["dbPwd"]), ObjToStr(Config["dbName"]))
|
|
|
|
|
// case 2:cmd= exec.Command("mysqldump","--no-data","-h"+ObjToStr(Config["dbHost"]), "-P"+ObjToStr(Config["dbPort"]),"-u"+ObjToStr(Config["dbUser"]), "-p"+ObjToStr(Config["dbPwd"]),ObjToStr(Config["dbName"]))
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// stdout, err := cmd.StdoutPipe()
|
|
|
|
|
// if err != nil {
|
2019-07-01 04:35:04 +00:00
|
|
|
|
// log.Println(err)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// if err := cmd.Start(); err != nil {
|
2019-07-01 04:35:04 +00:00
|
|
|
|
// log.Println(err)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// bytes, err := ioutil.ReadAll(stdout)
|
|
|
|
|
// if err != nil {
|
2019-07-01 04:35:04 +00:00
|
|
|
|
// log.Println(err)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
// }
|
|
|
|
|
// err = ioutil.WriteFile(path, bytes, 0644)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// panic(err)
|
|
|
|
|
// }
|
|
|
|
|
// return ;
|
|
|
|
|
// //
|
|
|
|
|
// //db := ``
|
|
|
|
|
// //fmt.Println(db)
|
|
|
|
|
// //
|
2022-03-12 17:48:54 +00:00
|
|
|
|
// //tables := that.Query("show tables")
|
2017-08-04 08:20:59 +00:00
|
|
|
|
// //lth := len(tables)
|
|
|
|
|
// //if lth == 0 {
|
|
|
|
|
// // return
|
|
|
|
|
// //}
|
|
|
|
|
// //for k, _ := range tables[0] {
|
|
|
|
|
// // db = Substr(k, 10, len(k))
|
|
|
|
|
// //}
|
|
|
|
|
// //
|
|
|
|
|
// //fd, _ := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
|
|
|
|
|
// //fd.Write([]byte("/*datetime " + time.Now().Format("2006-01-02 15:04:05") + " */ \r\n"))
|
|
|
|
|
// //fd.Close()
|
|
|
|
|
// //
|
|
|
|
|
// //for i := 0; i < lth; i++ {
|
|
|
|
|
// // tt := tables[i]["Tables_in_"+db].(string)
|
2022-03-12 17:48:54 +00:00
|
|
|
|
// // that.backupSave(path, tt, code)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
// // debug.FreeOSMemory()
|
|
|
|
|
// //}
|
|
|
|
|
//
|
|
|
|
|
//}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
func (that *HoTimeDB) backupSave(path string, tt string, code int) {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
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 {
|
2021-05-24 21:08:17 +00:00
|
|
|
|
str += that.backupDdl(tt)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if code == 0 || code == 1 {
|
|
|
|
|
str += "insert into `" + tt + "`\r\n\r\n("
|
2021-05-24 21:08:17 +00:00
|
|
|
|
str += that.backupCol(tt)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
_, _ = fd.Write([]byte(str))
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
2021-05-24 21:08:17 +00:00
|
|
|
|
func (that *HoTimeDB) backupDdl(tt string) string {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
data := that.Query("show create table " + tt)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
if len(data) == 0 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ObjToStr(data[0]["Create Table"]) + ";\r\n\r\n"
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
func (that *HoTimeDB) backupCol(tt string) string {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
str := ""
|
2021-05-24 21:08:17 +00:00
|
|
|
|
data := that.Select(tt, "*")
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
|
|
lthData := len(data)
|
|
|
|
|
|
|
|
|
|
if lthData == 0 {
|
|
|
|
|
return str
|
|
|
|
|
}
|
|
|
|
|
lthCol := len(data[0])
|
|
|
|
|
col := make([]string, lthCol)
|
|
|
|
|
tempLthData := 0
|
2021-05-24 21:08:17 +00:00
|
|
|
|
for k := range data[0] {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
|
|
if tempLthData == lthCol-1 {
|
|
|
|
|
str += "`" + k + "`)"
|
|
|
|
|
} else {
|
|
|
|
|
str += "`" + k + "`,"
|
|
|
|
|
}
|
|
|
|
|
col[tempLthData] = k
|
|
|
|
|
tempLthData++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
str += " values"
|
|
|
|
|
|
|
|
|
|
for j := 0; j < lthData; j++ {
|
|
|
|
|
|
|
|
|
|
for m := 0; m < lthCol; m++ {
|
|
|
|
|
|
|
|
|
|
if m == 0 {
|
|
|
|
|
str += "("
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v := "NULL"
|
|
|
|
|
if data[j][col[m]] != nil {
|
|
|
|
|
v = "'" + strings.Replace(ObjToStr(data[j][col[m]]), "'", `\'`, -1) + "'"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if m == lthCol-1 {
|
|
|
|
|
str += v + ")"
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
str += v + ","
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if j == lthData-1 {
|
|
|
|
|
str += ";\r\n\r\n"
|
|
|
|
|
} else {
|
|
|
|
|
str += ",\r\n\r\n"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return str
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
func (that *HoTimeDB) md5(query string, args ...interface{}) string {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
strByte, _ := json.Marshal(args)
|
|
|
|
|
str := Md5(query + ":" + string(strByte))
|
|
|
|
|
return str
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
func (that *HoTimeDB) Query(query string, args ...interface{}) []Map {
|
2022-07-11 03:07:17 +00:00
|
|
|
|
defer func() {
|
|
|
|
|
if that.Mode == 2 {
|
|
|
|
|
that.Log.Info("SQL:"+that.LastQuery, " DATA:", that.LastData, " ERROR:", that.LastErr.GetError())
|
|
|
|
|
}
|
|
|
|
|
}()
|
2017-08-04 08:20:59 +00:00
|
|
|
|
//fmt.Println(query)
|
|
|
|
|
var err error
|
|
|
|
|
var resl *sql.Rows
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
that.LastQuery = query
|
|
|
|
|
that.LastData = args
|
2021-05-23 22:14:58 +00:00
|
|
|
|
//主从数据库切换,只有select语句有从数据库
|
2021-05-24 21:08:17 +00:00
|
|
|
|
db := that.DB
|
|
|
|
|
if that.SlaveDB != nil {
|
|
|
|
|
db = that.SlaveDB
|
2021-05-23 22:14:58 +00:00
|
|
|
|
}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
2021-05-23 22:14:58 +00:00
|
|
|
|
if db == nil {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
err = errors.New("没有初始化数据库")
|
2021-05-24 21:08:17 +00:00
|
|
|
|
that.LastErr.SetError(err)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-08-23 01:40:54 +00:00
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
if that.Tx != nil {
|
|
|
|
|
resl, err = that.Tx.Query(query, args...)
|
2017-09-19 07:39:35 +00:00
|
|
|
|
} else {
|
2021-05-23 22:14:58 +00:00
|
|
|
|
resl, err = db.Query(query, args...)
|
2017-08-23 01:40:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
that.LastErr.SetError(err)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
if err != nil {
|
2021-05-23 22:14:58 +00:00
|
|
|
|
if err = db.Ping(); err != nil {
|
2021-05-24 21:08:17 +00:00
|
|
|
|
that.LastErr.SetError(err)
|
|
|
|
|
_ = that.InitDb()
|
|
|
|
|
if that.LastErr.GetError() != nil {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-05-24 21:08:17 +00:00
|
|
|
|
return that.Query(query, args...)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
2022-06-20 16:06:34 +00:00
|
|
|
|
return nil
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
return that.Row(resl)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-25 11:53:34 +00:00
|
|
|
|
func (that *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, *Error) {
|
2022-07-11 03:07:17 +00:00
|
|
|
|
defer func() {
|
|
|
|
|
if that.Mode == 2 {
|
|
|
|
|
that.Log.Info("SQL: "+that.LastQuery, " DATA: ", that.LastData, " ERROR: ", that.LastErr.GetError())
|
|
|
|
|
}
|
|
|
|
|
}()
|
2021-05-24 21:08:17 +00:00
|
|
|
|
that.LastQuery = query
|
|
|
|
|
that.LastData = args
|
2017-08-23 01:40:54 +00:00
|
|
|
|
var e error
|
|
|
|
|
var resl sql.Result
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
if that.DB == nil {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
err := errors.New("没有初始化数据库")
|
2021-05-24 21:08:17 +00:00
|
|
|
|
that.LastErr.SetError(err)
|
|
|
|
|
return nil, that.LastErr
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
if that.Tx != nil {
|
|
|
|
|
resl, e = that.Tx.Exec(query, args...)
|
2017-09-19 07:39:35 +00:00
|
|
|
|
} else {
|
2021-05-24 21:08:17 +00:00
|
|
|
|
resl, e = that.DB.Exec(query, args...)
|
2017-08-23 01:40:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
that.LastErr.SetError(e)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
|
|
//判断是否连接断开了
|
|
|
|
|
if e != nil {
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
if e = that.DB.Ping(); e != nil {
|
|
|
|
|
that.LastErr.SetError(e)
|
|
|
|
|
_ = that.InitDb()
|
|
|
|
|
if that.LastErr.GetError() != nil {
|
|
|
|
|
return resl, that.LastErr
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
2021-05-24 21:08:17 +00:00
|
|
|
|
return that.Exec(query, args...)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
2022-06-20 16:06:34 +00:00
|
|
|
|
return resl, that.LastErr
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
return resl, that.LastErr
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-12 17:48:54 +00:00
|
|
|
|
//func (that *HoTimeDB)copy(data []Map)[]Map{
|
2017-08-04 08:20:59 +00:00
|
|
|
|
// if data==nil{
|
|
|
|
|
// return nil
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// lth:=len(data)
|
|
|
|
|
//
|
|
|
|
|
// res:=make([]Map,lth)
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// for i:=0;i<lth;i++{
|
|
|
|
|
//
|
|
|
|
|
// res[i]=DeepCopy(data[i]).(Map)
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// return res
|
|
|
|
|
//
|
|
|
|
|
//}
|
2021-05-24 21:08:17 +00:00
|
|
|
|
|
|
|
|
|
func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
|
|
query := "SELECT"
|
|
|
|
|
where := Map{}
|
|
|
|
|
qs := make([]interface{}, 0)
|
|
|
|
|
intQs, intWhere := 0, 1
|
|
|
|
|
join := false
|
|
|
|
|
if len(qu) == 3 {
|
|
|
|
|
intQs = 1
|
|
|
|
|
intWhere = 2
|
|
|
|
|
join = true
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(qu) > 0 {
|
|
|
|
|
if reflect.ValueOf(qu[intQs]).Type().String() == "string" {
|
|
|
|
|
query += " " + qu[intQs].(string)
|
|
|
|
|
} else {
|
|
|
|
|
for i := 0; i < len(qu[intQs].(Slice)); i++ {
|
2017-09-19 07:39:35 +00:00
|
|
|
|
k := qu[intQs].(Slice)[i].(string)
|
|
|
|
|
if strings.Contains(k, " AS ") {
|
|
|
|
|
|
|
|
|
|
query += " " + k + " "
|
|
|
|
|
|
2017-08-04 08:20:59 +00:00
|
|
|
|
} else {
|
2017-09-19 07:39:35 +00:00
|
|
|
|
query += " `" + k + "` "
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if i+1 != len(qu[intQs].(Slice)) {
|
|
|
|
|
query = query + ", "
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
query += " *"
|
|
|
|
|
}
|
2021-08-28 05:05:12 +00:00
|
|
|
|
if !strings.Contains(table, ".") && !strings.Contains(table, " AS ") {
|
|
|
|
|
query += " FROM `" + that.Prefix + table + "`"
|
|
|
|
|
} else {
|
|
|
|
|
query += " FROM " + that.Prefix + table
|
|
|
|
|
}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
|
|
if join {
|
2022-03-12 21:06:28 +00:00
|
|
|
|
var testQu = []string{}
|
|
|
|
|
testQuData := qu[0].(Map)
|
|
|
|
|
for key, _ := range testQuData {
|
|
|
|
|
//fmt.Println(key, ":", value)
|
|
|
|
|
testQu = append(testQu, key)
|
|
|
|
|
}
|
|
|
|
|
sort.Strings(testQu)
|
|
|
|
|
|
|
|
|
|
for _, k := range testQu {
|
|
|
|
|
v := testQuData[k]
|
2017-08-04 08:20:59 +00:00
|
|
|
|
switch Substr(k, 0, 3) {
|
|
|
|
|
case "[>]":
|
2022-05-05 10:33:27 +00:00
|
|
|
|
query += " LEFT JOIN `" + Substr(k, 3, len(k)-3) + "` ON " + v.(string)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
case "[<]":
|
2022-05-05 10:33:27 +00:00
|
|
|
|
query += " RIGHT JOIN `" + Substr(k, 3, len(k)-3) + "` ON " + v.(string)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
switch Substr(k, 0, 4) {
|
|
|
|
|
case "[<>]":
|
2022-05-05 10:33:27 +00:00
|
|
|
|
query += " FULL JOIN `" + Substr(k, 4, len(k)-4) + "` ON " + v.(string)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
case "[><]":
|
2022-05-05 10:33:27 +00:00
|
|
|
|
query += " INNER JOIN `" + Substr(k, 4, len(k)-4) + "` ON " + v.(string)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(qu) > 1 {
|
|
|
|
|
where = qu[intWhere].(Map)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
temp, resWhere := that.where(where)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
2022-07-11 03:07:17 +00:00
|
|
|
|
query += temp + ";"
|
2017-08-04 08:20:59 +00:00
|
|
|
|
qs = append(qs, resWhere...)
|
2021-05-24 21:08:17 +00:00
|
|
|
|
md5 := that.md5(query, qs...)
|
2018-04-09 17:16:24 +00:00
|
|
|
|
|
2021-05-28 15:48:33 +00:00
|
|
|
|
if that.HoTimeCache != nil && table != "cached" {
|
2018-04-10 15:17:10 +00:00
|
|
|
|
//如果缓存有则从缓存取
|
2021-05-28 14:52:22 +00:00
|
|
|
|
cacheData := that.HoTimeCache.Db(table + ":" + md5)
|
2018-04-10 15:17:10 +00:00
|
|
|
|
|
2021-05-29 16:01:15 +00:00
|
|
|
|
if cacheData != nil && cacheData.Data != nil {
|
2018-04-10 15:17:10 +00:00
|
|
|
|
return cacheData.ToMapArray()
|
|
|
|
|
}
|
2018-04-09 17:16:24 +00:00
|
|
|
|
}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
2018-04-09 17:16:24 +00:00
|
|
|
|
//无缓存则数据库取
|
2021-05-24 21:08:17 +00:00
|
|
|
|
res := that.Query(query, qs...)
|
2018-04-09 17:16:24 +00:00
|
|
|
|
|
2017-11-07 01:21:48 +00:00
|
|
|
|
if res == nil {
|
2019-11-10 10:00:45 +00:00
|
|
|
|
res = []Map{}
|
2018-04-09 17:16:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//缓存
|
2021-05-28 15:48:33 +00:00
|
|
|
|
if that.HoTimeCache != nil && table != "cached" {
|
2018-04-09 17:16:24 +00:00
|
|
|
|
|
2021-05-28 14:52:22 +00:00
|
|
|
|
_ = that.HoTimeCache.Db(table+":"+md5, res)
|
2017-11-07 01:21:48 +00:00
|
|
|
|
}
|
2018-04-09 17:16:24 +00:00
|
|
|
|
|
2017-08-04 08:20:59 +00:00
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
func (that *HoTimeDB) Get(table string, qu ...interface{}) Map {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
//fmt.Println(qu)
|
|
|
|
|
if len(qu) == 1 {
|
|
|
|
|
qu = append(qu, Map{"LIMIT": 1})
|
|
|
|
|
}
|
|
|
|
|
if len(qu) == 2 {
|
|
|
|
|
temp := qu[1].(Map)
|
|
|
|
|
temp["LIMIT"] = 1
|
|
|
|
|
qu[1] = temp
|
|
|
|
|
}
|
|
|
|
|
if len(qu) == 3 {
|
|
|
|
|
temp := qu[2].(Map)
|
|
|
|
|
temp["LIMIT"] = 1
|
|
|
|
|
qu[2] = temp
|
|
|
|
|
}
|
|
|
|
|
//fmt.Println(qu)
|
2021-05-24 21:08:17 +00:00
|
|
|
|
data := that.Select(table, qu...)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
if len(data) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return data[0]
|
|
|
|
|
}
|
2021-05-28 16:37:20 +00:00
|
|
|
|
func (that *HoTimeDB) GetPrefix() string {
|
|
|
|
|
return that.Prefix
|
|
|
|
|
}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
// Count 计数
|
|
|
|
|
func (that *HoTimeDB) Count(table string, qu ...interface{}) int {
|
|
|
|
|
var req = []interface{}{}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
if len(qu) == 2 {
|
|
|
|
|
req = append(req, qu[0])
|
|
|
|
|
req = append(req, "COUNT(*)")
|
|
|
|
|
req = append(req, qu[1])
|
|
|
|
|
} else {
|
|
|
|
|
req = append(req, "COUNT(*)")
|
|
|
|
|
req = append(req, qu...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//req=append(req,qu...)
|
2021-05-24 21:08:17 +00:00
|
|
|
|
data := that.Select(table, req...)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
//fmt.Println(data)
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
//res,_:=StrToInt(data[0]["COUNT(*)"].(string))
|
|
|
|
|
res := ObjToStr(data[0]["COUNT(*)"])
|
|
|
|
|
count, _ := StrToInt(res)
|
|
|
|
|
return count
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var condition = []string{"AND", "OR"}
|
|
|
|
|
var vcond = []string{"GROUP", "ORDER", "LIMIT"}
|
|
|
|
|
|
|
|
|
|
//where语句解析
|
2021-05-24 21:08:17 +00:00
|
|
|
|
func (that *HoTimeDB) where(data Map) (string, []interface{}) {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
|
|
where := ""
|
|
|
|
|
|
|
|
|
|
res := make([]interface{}, 0)
|
|
|
|
|
//AND OR判断
|
2022-03-12 21:06:28 +00:00
|
|
|
|
testQu := []string{}
|
|
|
|
|
//testQuData:= qu[0].(Map)
|
|
|
|
|
for key, _ := range data {
|
|
|
|
|
//fmt.Println(key, ":", value)
|
|
|
|
|
testQu = append(testQu, key)
|
|
|
|
|
}
|
|
|
|
|
sort.Strings(testQu)
|
|
|
|
|
for _, k := range testQu {
|
|
|
|
|
v := data[k]
|
2017-08-04 08:20:59 +00:00
|
|
|
|
x := 0
|
|
|
|
|
for i := 0; i < len(condition); i++ {
|
|
|
|
|
|
|
|
|
|
if condition[i] == k {
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
tw, ts := that.cond(k, v.(Map))
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
|
|
where += tw
|
|
|
|
|
res = append(res, ts...)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
x++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
y := 0
|
|
|
|
|
for j := 0; j < len(vcond); j++ {
|
|
|
|
|
|
|
|
|
|
if vcond[j] == k {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
y++
|
|
|
|
|
}
|
|
|
|
|
if x == len(condition) && y == len(vcond) {
|
2021-06-13 01:53:37 +00:00
|
|
|
|
|
2021-07-06 20:08:40 +00:00
|
|
|
|
if v != nil && reflect.ValueOf(v).Type().String() == "common.Slice" && len(v.(Slice)) == 0 {
|
2021-06-13 01:53:37 +00:00
|
|
|
|
continue
|
|
|
|
|
}
|
2022-07-31 19:40:09 +00:00
|
|
|
|
fmt.Println(reflect.ValueOf(v).Type().String())
|
|
|
|
|
if v != nil && strings.Contains(reflect.ValueOf(v).Type().String(), "[]") && len(ObjToSlice(v)) == 0 {
|
2021-06-13 01:53:37 +00:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
tv, vv := that.varCond(k, v)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
|
|
where += tv
|
|
|
|
|
res = append(res, vv...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(where) != 0 {
|
|
|
|
|
where = " WHERE " + where
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//特殊字符
|
|
|
|
|
for j := 0; j < len(vcond); j++ {
|
2022-03-12 21:06:28 +00:00
|
|
|
|
testQu := []string{}
|
|
|
|
|
//testQuData:= qu[0].(Map)
|
|
|
|
|
for key, _ := range data {
|
|
|
|
|
//fmt.Println(key, ":", value)
|
|
|
|
|
testQu = append(testQu, key)
|
|
|
|
|
}
|
|
|
|
|
sort.Strings(testQu)
|
|
|
|
|
for _, k := range testQu {
|
|
|
|
|
v := data[k]
|
2017-08-04 08:20:59 +00:00
|
|
|
|
if vcond[j] == k {
|
|
|
|
|
if k == "ORDER" {
|
|
|
|
|
where += " " + k + " BY "
|
|
|
|
|
//fmt.Println(reflect.ValueOf(v).Type())
|
|
|
|
|
|
|
|
|
|
//break
|
|
|
|
|
} else if k == "GROUP" {
|
|
|
|
|
|
|
|
|
|
where += " " + k + " BY "
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
where += " " + k
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 00:06:44 +00:00
|
|
|
|
if reflect.ValueOf(v).Type().String() == "common.Slice" {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
for i := 0; i < len(v.(Slice)); i++ {
|
|
|
|
|
where += " " + ObjToStr(v.(Slice)[i])
|
|
|
|
|
|
|
|
|
|
if len(v.(Slice)) != i+1 {
|
|
|
|
|
where += ","
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2017-12-03 19:54:46 +00:00
|
|
|
|
|
2017-08-04 08:20:59 +00:00
|
|
|
|
} else {
|
|
|
|
|
//fmt.Println(v)
|
|
|
|
|
where += " " + ObjToStr(v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return where, res
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) {
|
2021-06-13 01:53:37 +00:00
|
|
|
|
|
2017-08-04 08:20:59 +00:00
|
|
|
|
where := ""
|
|
|
|
|
res := make([]interface{}, 0)
|
|
|
|
|
length := len(k)
|
2021-10-26 16:27:24 +00:00
|
|
|
|
if k == "[#]" {
|
|
|
|
|
k = strings.Replace(k, "[#]", "", -1)
|
|
|
|
|
where += " " + ObjToStr(v) + " "
|
|
|
|
|
} else if length > 0 && strings.Contains(k, "[") && k[length-1] == ']' {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
def := false
|
2017-09-19 07:39:35 +00:00
|
|
|
|
|
2017-08-04 08:20:59 +00:00
|
|
|
|
switch Substr(k, length-3, 3) {
|
|
|
|
|
case "[>]":
|
|
|
|
|
k = strings.Replace(k, "[>]", "", -1)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
if !strings.Contains(k, ".") {
|
|
|
|
|
k = "`" + k + "`"
|
|
|
|
|
}
|
|
|
|
|
where += k + ">? "
|
2017-08-04 08:20:59 +00:00
|
|
|
|
res = append(res, v)
|
|
|
|
|
case "[<]":
|
|
|
|
|
k = strings.Replace(k, "[<]", "", -1)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
if !strings.Contains(k, ".") {
|
|
|
|
|
k = "`" + k + "`"
|
|
|
|
|
}
|
|
|
|
|
where += k + "<? "
|
2017-08-04 08:20:59 +00:00
|
|
|
|
res = append(res, v)
|
|
|
|
|
case "[!]":
|
|
|
|
|
k = strings.Replace(k, "[!]", "", -1)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
if !strings.Contains(k, ".") {
|
|
|
|
|
k = "`" + k + "`"
|
|
|
|
|
}
|
2021-05-24 21:08:17 +00:00
|
|
|
|
where, res = that.notIn(k, v, where, res)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
case "[#]":
|
|
|
|
|
k = strings.Replace(k, "[#]", "", -1)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
if !strings.Contains(k, ".") {
|
|
|
|
|
k = "`" + k + "`"
|
|
|
|
|
}
|
2017-09-19 07:39:35 +00:00
|
|
|
|
where += " " + k + "=" + ObjToStr(v)
|
2022-05-27 07:00:44 +00:00
|
|
|
|
case "[##]": //直接添加value到sql,需要考虑防注入,value比如:"a>b"
|
|
|
|
|
|
|
|
|
|
where += " " + ObjToStr(v)
|
2019-07-16 10:15:00 +00:00
|
|
|
|
case "[#!]":
|
|
|
|
|
k = strings.Replace(k, "[#!]", "", -1)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
if !strings.Contains(k, ".") {
|
|
|
|
|
k = "`" + k + "`"
|
|
|
|
|
}
|
2019-07-16 10:15:00 +00:00
|
|
|
|
where += " " + k + "!=" + ObjToStr(v)
|
|
|
|
|
case "[!#]":
|
|
|
|
|
k = strings.Replace(k, "[!#]", "", -1)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
if !strings.Contains(k, ".") {
|
|
|
|
|
k = "`" + k + "`"
|
|
|
|
|
}
|
2019-07-16 10:15:00 +00:00
|
|
|
|
where += " " + k + "!=" + ObjToStr(v)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
case "[~]":
|
|
|
|
|
k = strings.Replace(k, "[~]", "", -1)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
if !strings.Contains(k, ".") {
|
|
|
|
|
k = "`" + k + "`"
|
|
|
|
|
}
|
|
|
|
|
where += k + " LIKE ? "
|
2017-11-09 10:15:55 +00:00
|
|
|
|
v = "%" + ObjToStr(v) + "%"
|
2017-08-04 08:20:59 +00:00
|
|
|
|
res = append(res, v)
|
2019-11-10 10:00:45 +00:00
|
|
|
|
case "[!~]": //左边任意
|
2018-07-30 18:09:42 +00:00
|
|
|
|
k = strings.Replace(k, "[~]", "", -1)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
if !strings.Contains(k, ".") {
|
|
|
|
|
k = "`" + k + "`"
|
|
|
|
|
}
|
|
|
|
|
where += k + " LIKE ? "
|
2018-07-30 18:09:42 +00:00
|
|
|
|
v = "%" + ObjToStr(v)
|
|
|
|
|
res = append(res, v)
|
2019-11-10 10:00:45 +00:00
|
|
|
|
case "[~!]": //右边任意
|
2018-07-30 18:09:42 +00:00
|
|
|
|
k = strings.Replace(k, "[~]", "", -1)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
if !strings.Contains(k, ".") {
|
|
|
|
|
k = "`" + k + "`"
|
|
|
|
|
}
|
|
|
|
|
where += k + " LIKE ? "
|
2019-11-10 10:00:45 +00:00
|
|
|
|
v = ObjToStr(v) + "%"
|
2018-07-30 18:09:42 +00:00
|
|
|
|
res = append(res, v)
|
2019-11-10 10:00:45 +00:00
|
|
|
|
case "[~~]": //手动任意
|
2018-07-30 18:09:42 +00:00
|
|
|
|
k = strings.Replace(k, "[~]", "", -1)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
if !strings.Contains(k, ".") {
|
|
|
|
|
k = "`" + k + "`"
|
|
|
|
|
}
|
|
|
|
|
where += k + " LIKE ? "
|
2018-07-30 18:09:42 +00:00
|
|
|
|
//v = ObjToStr(v)
|
|
|
|
|
res = append(res, v)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
default:
|
|
|
|
|
def = true
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if def {
|
|
|
|
|
switch Substr(k, length-4, 4) {
|
|
|
|
|
case "[>=]":
|
|
|
|
|
k = strings.Replace(k, "[>=]", "", -1)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
if !strings.Contains(k, ".") {
|
|
|
|
|
k = "`" + k + "`"
|
|
|
|
|
}
|
|
|
|
|
where += k + ">=? "
|
2017-08-04 08:20:59 +00:00
|
|
|
|
res = append(res, v)
|
|
|
|
|
case "[<=]":
|
2017-12-03 19:54:46 +00:00
|
|
|
|
k = strings.Replace(k, "[<=]", "", -1)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
if !strings.Contains(k, ".") {
|
|
|
|
|
k = "`" + k + "`"
|
|
|
|
|
}
|
|
|
|
|
where += k + "<=? "
|
2017-08-04 08:20:59 +00:00
|
|
|
|
res = append(res, v)
|
|
|
|
|
case "[><]":
|
|
|
|
|
k = strings.Replace(k, "[><]", "", -1)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
if !strings.Contains(k, ".") {
|
|
|
|
|
k = "`" + k + "`"
|
|
|
|
|
}
|
|
|
|
|
where += k + " NOT BETWEEN ? AND ? "
|
2017-08-04 08:20:59 +00:00
|
|
|
|
res = append(res, v.(Slice)[0])
|
|
|
|
|
res = append(res, v.(Slice)[1])
|
|
|
|
|
case "[<>]":
|
|
|
|
|
k = strings.Replace(k, "[<>]", "", -1)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
if !strings.Contains(k, ".") {
|
|
|
|
|
k = "`" + k + "`"
|
|
|
|
|
}
|
|
|
|
|
where += k + " BETWEEN ? AND ? "
|
2017-08-04 08:20:59 +00:00
|
|
|
|
res = append(res, v.(Slice)[0])
|
|
|
|
|
res = append(res, v.(Slice)[1])
|
|
|
|
|
default:
|
2021-06-13 01:53:37 +00:00
|
|
|
|
if !strings.Contains(k, ".") {
|
|
|
|
|
k = "`" + k + "`"
|
|
|
|
|
}
|
2022-07-24 21:37:19 +00:00
|
|
|
|
if reflect.ValueOf(v).Type().String() == "common.Slice" || strings.Contains(reflect.ValueOf(v).Type().String(), "[]") {
|
|
|
|
|
vs := ObjToSlice(v)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
where += k + " IN ("
|
2022-07-24 21:37:19 +00:00
|
|
|
|
res = append(res, vs...)
|
2019-11-10 10:00:45 +00:00
|
|
|
|
if len(v.(Slice)) == 0 {
|
|
|
|
|
where += ") "
|
|
|
|
|
} else {
|
2022-07-24 21:37:19 +00:00
|
|
|
|
for i := 0; i < len(vs); i++ {
|
|
|
|
|
if i+1 != len(vs) {
|
2017-12-03 18:38:03 +00:00
|
|
|
|
where += "?,"
|
|
|
|
|
} else {
|
|
|
|
|
where += "?) "
|
|
|
|
|
}
|
|
|
|
|
//res=append(res,(v.(Slice))[i])
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-03 18:38:03 +00:00
|
|
|
|
|
2017-08-04 08:20:59 +00:00
|
|
|
|
} else {
|
2021-06-13 01:53:37 +00:00
|
|
|
|
where += k + "=? "
|
2017-08-04 08:20:59 +00:00
|
|
|
|
res = append(res, v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-10 10:00:45 +00:00
|
|
|
|
} else {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
//fmt.Println(reflect.ValueOf(v).Type().String())
|
2021-06-13 01:53:37 +00:00
|
|
|
|
if !strings.Contains(k, ".") {
|
|
|
|
|
k = "`" + k + "`"
|
|
|
|
|
}
|
2019-11-10 10:00:45 +00:00
|
|
|
|
if v == nil {
|
2021-06-13 01:53:37 +00:00
|
|
|
|
where += k + " IS NULL"
|
2022-07-24 21:37:19 +00:00
|
|
|
|
} else if reflect.ValueOf(v).Type().String() == "common.Slice" || strings.Contains(reflect.ValueOf(v).Type().String(), "[]") {
|
|
|
|
|
vs := ObjToSlice(v)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
//fmt.Println(v)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
where += k + " IN ("
|
2022-07-24 21:37:19 +00:00
|
|
|
|
res = append(res, vs...)
|
|
|
|
|
for i := 0; i < len(vs); i++ {
|
|
|
|
|
if i+1 != len(vs) {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
where += "?,"
|
|
|
|
|
} else {
|
|
|
|
|
where += "?) "
|
|
|
|
|
}
|
|
|
|
|
//res=append(res,(v.(Slice))[i])
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-07-30 19:21:14 +00:00
|
|
|
|
|
2021-06-13 01:53:37 +00:00
|
|
|
|
where += k + "=? "
|
2019-11-10 10:00:45 +00:00
|
|
|
|
res = append(res, v)
|
2018-07-30 18:09:42 +00:00
|
|
|
|
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return where, res
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-12 17:48:54 +00:00
|
|
|
|
// that.Db.Update("user",hotime.Map{"ustate":"1"},hotime.Map{"AND":hotime.Map{"OR":hotime.Map{"uid":4,"uname":"dasda"}},"ustate":1})
|
2021-05-24 21:08:17 +00:00
|
|
|
|
func (that *HoTimeDB) notIn(k string, v interface{}, where string, res []interface{}) (string, []interface{}) {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
//where:=""
|
|
|
|
|
//fmt.Println(reflect.ValueOf(v).Type().String())
|
2019-11-10 10:00:45 +00:00
|
|
|
|
if v == nil {
|
2018-07-30 19:21:14 +00:00
|
|
|
|
|
2021-06-13 01:53:37 +00:00
|
|
|
|
where += k + " IS NOT NULL "
|
2018-07-30 19:21:14 +00:00
|
|
|
|
|
2022-07-24 21:37:19 +00:00
|
|
|
|
} else if reflect.ValueOf(v).Type().String() == "common.Slice" || strings.Contains(reflect.ValueOf(v).Type().String(), "[]") {
|
|
|
|
|
vs := ObjToSlice(v)
|
2021-06-13 01:53:37 +00:00
|
|
|
|
where += k + " NOT IN ("
|
2022-07-24 21:37:19 +00:00
|
|
|
|
res = append(res, vs...)
|
|
|
|
|
for i := 0; i < len(vs); i++ {
|
|
|
|
|
if i+1 != len(vs) {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
where += "?,"
|
|
|
|
|
} else {
|
|
|
|
|
where += "?) "
|
|
|
|
|
}
|
|
|
|
|
//res=append(res,(v.(Slice))[i])
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-07-30 19:21:14 +00:00
|
|
|
|
|
2021-06-13 01:53:37 +00:00
|
|
|
|
where += k + " !=? "
|
2019-11-10 10:00:45 +00:00
|
|
|
|
res = append(res, v)
|
2018-07-30 18:09:42 +00:00
|
|
|
|
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return where, res
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
func (that *HoTimeDB) cond(tag string, data Map) (string, []interface{}) {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
where := " "
|
|
|
|
|
res := make([]interface{}, 0)
|
|
|
|
|
lens := len(data)
|
|
|
|
|
//fmt.Println(lens)
|
2022-03-12 21:06:28 +00:00
|
|
|
|
testQu := []string{}
|
|
|
|
|
//testQuData:= qu[0].(Map)
|
|
|
|
|
for key, _ := range data {
|
|
|
|
|
//fmt.Println(key, ":", value)
|
|
|
|
|
testQu = append(testQu, key)
|
|
|
|
|
}
|
|
|
|
|
sort.Strings(testQu)
|
|
|
|
|
for _, k := range testQu {
|
|
|
|
|
v := data[k]
|
2017-08-04 08:20:59 +00:00
|
|
|
|
x := 0
|
|
|
|
|
for i := 0; i < len(condition); i++ {
|
|
|
|
|
|
|
|
|
|
if condition[i] == k {
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
tw, ts := that.cond(k, v.(Map))
|
2017-08-04 08:20:59 +00:00
|
|
|
|
if lens--; lens <= 0 {
|
|
|
|
|
//fmt.Println(lens)
|
|
|
|
|
where += "(" + tw + ") "
|
|
|
|
|
} else {
|
|
|
|
|
where += "(" + tw + ") " + tag + " "
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res = append(res, ts...)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
x++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if x == len(condition) {
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
tv, vv := that.varCond(k, v)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
|
|
res = append(res, vv...)
|
|
|
|
|
if lens--; lens <= 0 {
|
|
|
|
|
where += tv + ""
|
|
|
|
|
} else {
|
|
|
|
|
where += tv + " " + tag + " "
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return where, res
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
// Update 更新数据
|
|
|
|
|
func (that *HoTimeDB) Update(table string, data Map, where Map) int64 {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
2021-08-28 05:05:12 +00:00
|
|
|
|
query := "UPDATE `" + that.Prefix + table + "` SET "
|
2017-08-04 08:20:59 +00:00
|
|
|
|
//UPDATE Person SET Address = 'Zhongshan 23', City = 'Nanjing' WHERE LastName = 'Wilson'
|
|
|
|
|
qs := make([]interface{}, 0)
|
|
|
|
|
tp := len(data)
|
|
|
|
|
|
|
|
|
|
for k, v := range data {
|
|
|
|
|
vstr := "?"
|
|
|
|
|
if Substr(k, len(k)-3, 3) == "[#]" {
|
|
|
|
|
k = strings.Replace(k, "[#]", "", -1)
|
|
|
|
|
|
|
|
|
|
vstr = ObjToStr(v)
|
|
|
|
|
} else {
|
|
|
|
|
qs = append(qs, v)
|
|
|
|
|
}
|
|
|
|
|
query += "`" + k + "`=" + vstr + ""
|
|
|
|
|
if tp--; tp != 0 {
|
|
|
|
|
query += ", "
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
temp, resWhere := that.where(where)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
//fmt.Println(resWhere)
|
|
|
|
|
|
2022-07-11 03:07:17 +00:00
|
|
|
|
query += temp + ";"
|
2017-08-04 08:20:59 +00:00
|
|
|
|
qs = append(qs, resWhere...)
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
res, err := that.Exec(query, qs...)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
2019-11-10 10:00:45 +00:00
|
|
|
|
rows := int64(0)
|
|
|
|
|
if err.GetError() == nil && res != nil {
|
2018-04-09 17:16:24 +00:00
|
|
|
|
rows, _ = res.RowsAffected()
|
|
|
|
|
}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
2018-04-09 17:16:24 +00:00
|
|
|
|
//如果更新成功,则删除缓存
|
2019-11-10 10:00:45 +00:00
|
|
|
|
if rows != 0 {
|
2021-05-28 15:48:33 +00:00
|
|
|
|
if that.HoTimeCache != nil && table != "cached" {
|
2021-05-28 14:52:22 +00:00
|
|
|
|
_ = that.HoTimeCache.Db(table+"*", nil)
|
2018-04-09 17:16:24 +00:00
|
|
|
|
}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rows
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
func (that *HoTimeDB) Delete(table string, data map[string]interface{}) int64 {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
2021-05-28 16:37:20 +00:00
|
|
|
|
query := "DELETE FROM " + that.Prefix + table + " "
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
temp, resWhere := that.where(data)
|
2022-07-11 03:07:17 +00:00
|
|
|
|
query += temp + ";"
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
res, err := that.Exec(query, resWhere...)
|
2019-11-10 10:00:45 +00:00
|
|
|
|
rows := int64(0)
|
|
|
|
|
if err.GetError() == nil && res != nil {
|
2018-04-09 17:16:24 +00:00
|
|
|
|
rows, _ = res.RowsAffected()
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-09 17:16:24 +00:00
|
|
|
|
//如果删除成功,删除对应缓存
|
2019-11-10 10:00:45 +00:00
|
|
|
|
if rows != 0 {
|
2021-05-28 15:48:33 +00:00
|
|
|
|
if that.HoTimeCache != nil && table != "cached" {
|
2021-05-28 14:52:22 +00:00
|
|
|
|
_ = that.HoTimeCache.Db(table+"*", nil)
|
2018-04-09 17:16:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
//return 0
|
2018-04-09 17:16:24 +00:00
|
|
|
|
|
2017-08-04 08:20:59 +00:00
|
|
|
|
return rows
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
// Insert 插入新数据
|
|
|
|
|
func (that *HoTimeDB) Insert(table string, data map[string]interface{}) int64 {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
|
|
values := make([]interface{}, 0)
|
|
|
|
|
queryString := " ("
|
|
|
|
|
valueString := " ("
|
|
|
|
|
|
|
|
|
|
lens := len(data)
|
|
|
|
|
tempLen := 0
|
|
|
|
|
for k, v := range data {
|
|
|
|
|
tempLen++
|
2022-05-03 07:17:27 +00:00
|
|
|
|
|
|
|
|
|
vstr := "?"
|
|
|
|
|
if Substr(k, len(k)-3, 3) == "[#]" {
|
|
|
|
|
k = strings.Replace(k, "[#]", "", -1)
|
|
|
|
|
|
|
|
|
|
vstr = ObjToStr(v)
|
|
|
|
|
if tempLen < lens {
|
|
|
|
|
queryString += "`" + k + "`,"
|
|
|
|
|
valueString += vstr + ","
|
|
|
|
|
} else {
|
|
|
|
|
queryString += "`" + k + "`) "
|
|
|
|
|
valueString += vstr + ");"
|
|
|
|
|
}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
} else {
|
2022-05-03 07:17:27 +00:00
|
|
|
|
|
|
|
|
|
values = append(values, v)
|
|
|
|
|
if tempLen < lens {
|
|
|
|
|
queryString += "`" + k + "`,"
|
|
|
|
|
valueString += "?,"
|
|
|
|
|
} else {
|
|
|
|
|
queryString += "`" + k + "`) "
|
|
|
|
|
valueString += "?);"
|
|
|
|
|
}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2021-08-28 05:05:12 +00:00
|
|
|
|
query := "INSERT INTO `" + that.Prefix + table + "` " + queryString + "VALUES" + valueString
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
2021-05-24 21:08:17 +00:00
|
|
|
|
res, err := that.Exec(query, values...)
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
2019-11-10 10:00:45 +00:00
|
|
|
|
id := int64(0)
|
|
|
|
|
if err.GetError() == nil && res != nil {
|
2021-07-04 18:20:10 +00:00
|
|
|
|
id1, err := res.LastInsertId()
|
|
|
|
|
that.LastErr.SetError(err)
|
|
|
|
|
id = id1
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-09 17:16:24 +00:00
|
|
|
|
//如果插入成功,删除缓存
|
2019-11-10 10:00:45 +00:00
|
|
|
|
if id != 0 {
|
2021-05-28 15:48:33 +00:00
|
|
|
|
if that.HoTimeCache != nil && table != "cached" {
|
2021-05-28 14:52:22 +00:00
|
|
|
|
_ = that.HoTimeCache.Db(table+"*", nil)
|
2018-04-09 17:16:24 +00:00
|
|
|
|
}
|
2017-08-04 08:20:59 +00:00
|
|
|
|
}
|
2018-04-09 17:16:24 +00:00
|
|
|
|
|
2017-08-04 08:20:59 +00:00
|
|
|
|
//fmt.Println(id)
|
|
|
|
|
return id
|
|
|
|
|
}
|