From adfb2236dcd98034cbe627169282e21d79cd18d1 Mon Sep 17 00:00:00 2001 From: hoteas Date: Tue, 2 Aug 2022 03:02:57 +0800 Subject: [PATCH] =?UTF-8?q?V2.0=E5=B0=81=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/hotimedb.go | 182 +++++++++++++++++++++++++++++++++++++++++--- example/app/init.go | 10 +++ example/main.go | 1 + 3 files changed, 183 insertions(+), 10 deletions(-) diff --git a/db/hotimedb.go b/db/hotimedb.go index 41a66de..8c4f955 100644 --- a/db/hotimedb.go +++ b/db/hotimedb.go @@ -33,6 +33,152 @@ type HoTimeDB struct { Mode int //mode为0生产模式,1、为测试模式、2为开发模式 } +type HotimeDBBuilder struct { + *HoTimeDB + table string + selects []interface{} + join Slice + where Map + lastWhere Map + page int + pageRow int +} + +func (that *HoTimeDB) Table(table string) *HotimeDBBuilder { + + return &HotimeDBBuilder{HoTimeDB: that, table: table, where: Map{}} +} + +func (that *HotimeDBBuilder) Get(qu ...interface{}) Map { + + return that.HoTimeDB.Get(that.table, that.join, qu, that.where) +} + +func (that *HotimeDBBuilder) Count() int { + + return that.HoTimeDB.Count(that.table, that.join, that.where) +} +func (that *HotimeDBBuilder) Page(page, pageRow int) *HotimeDBBuilder { + that.page = page + that.pageRow = pageRow + return that +} +func (that *HotimeDBBuilder) Select(qu ...interface{}) []Map { + if that.page != 0 { + return that.HoTimeDB.Page(that.page, that.pageRow).PageSelect(that.table, that.join, qu, that.where) + } + return that.HoTimeDB.Select(that.table, that.join, qu, that.where) +} + +func (that *HotimeDBBuilder) Update(data Map) int64 { + + return that.HoTimeDB.Update(that.table, data, that.where) +} + +func (that *HotimeDBBuilder) Delete() int64 { + + return that.HoTimeDB.Delete(that.table, that.where) +} + +func (that *HotimeDBBuilder) LeftJoin(table, joinStr string) *HotimeDBBuilder { + that.Join(Map{"[>]" + table: joinStr}) + return that + +} +func (that *HotimeDBBuilder) RightJoin(table, joinStr string) *HotimeDBBuilder { + that.Join(Map{"[<]" + table: joinStr}) + return that + +} +func (that *HotimeDBBuilder) InnerJoin(table, joinStr string) *HotimeDBBuilder { + that.Join(Map{"[><]" + table: joinStr}) + return that + +} + +func (that *HotimeDBBuilder) FullJoin(table, joinStr string) *HotimeDBBuilder { + that.Join(Map{"[<>]" + table: joinStr}) + return that +} + +func (that *HotimeDBBuilder) Join(join Map) *HotimeDBBuilder { + if that.join == nil { + that.join = Slice{} + } + if join == nil { + return that + } + + that.join = append(that.join, join) + return that +} + +func (that *HotimeDBBuilder) And(where Map) *HotimeDBBuilder { + if where == nil { + return that + } + + if that.lastWhere != nil { + that.lastWhere["AND"] = where + that.lastWhere = where + return that + } + that.lastWhere = where + that.where = Map{"AND": where} + + return that +} + +func (that *HotimeDBBuilder) Or(where Map) *HotimeDBBuilder { + if where == nil { + return that + } + + if that.lastWhere != nil { + that.lastWhere["OR"] = where + that.lastWhere = where + return that + } + that.lastWhere = where + that.where = Map{"Or": where} + + return that +} +func (that *HotimeDBBuilder) Where(where Map) *HotimeDBBuilder { + if where == nil { + return that + } + + if that.lastWhere != nil { + that.lastWhere["AND"] = where + that.lastWhere = where + return that + } + that.lastWhere = where + that.where = Map{"AND": that.lastWhere} + + return that +} + +func (that *HotimeDBBuilder) From(table string) *HotimeDBBuilder { + that.table = table + return that +} + +func (that *HotimeDBBuilder) Order(qu ...interface{}) *HotimeDBBuilder { + that.where["ORDER"] = ObjToSlice(qu) + return that +} +func (that *HotimeDBBuilder) Limit(qu ...interface{}) *HotimeDBBuilder { + that.where["LIMIT"] = ObjToSlice(qu) + return that +} + +func (that *HotimeDBBuilder) Group(qu ...interface{}) *HotimeDBBuilder { + that.where["GROUP"] = ObjToSlice(qu) + return that +} + // SetConnect 设置数据库配置连接 func (that *HoTimeDB) SetConnect(connect func(err ...*Error) (master, slave *sql.DB), err ...*Error) { that.ConnectFunc = connect @@ -433,8 +579,9 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map { if reflect.ValueOf(qu[intQs]).Type().String() == "string" { query += " " + qu[intQs].(string) } else { - for i := 0; i < len(qu[intQs].(Slice)); i++ { - k := qu[intQs].(Slice)[i].(string) + data := ObjToSlice(qu[intQs]) + for i := 0; i < len(data); i++ { + k := data.GetString(i) if strings.Contains(k, " AS ") { query += " " + k + " " @@ -443,7 +590,7 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map { query += " `" + k + "` " } - if i+1 != len(qu[intQs].(Slice)) { + if i+1 != len(data) { query = query + ", " } @@ -461,11 +608,26 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map { if join { var testQu = []string{} - testQuData := qu[0].(Map) - for key, _ := range testQuData { - //fmt.Println(key, ":", value) - testQu = append(testQu, key) + testQuData := Map{} + if reflect.ValueOf(qu[0]).Type().String() == "common.Map" { + testQuData = qu[0].(Map) + for key, _ := range testQuData { + //fmt.Println(key, ":", value) + testQu = append(testQu, key) + } } + + if reflect.ValueOf(qu[0]).Type().String() == "common.Slice" { + + for key, _ := range testQuData { + v := testQuData.GetMap(key) + for k1, v1 := range v { + testQu = append(testQu, k1) + testQuData[k1] = v1 + } + } + } + sort.Strings(testQu) for _, k := range testQu { @@ -752,7 +914,7 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) { k = "`" + k + "` " } where += k + " LIKE ? " - v = "%" + ObjToStr(v) + "% " + v = "%" + ObjToStr(v) + "%" res = append(res, v) case "[!~]": //左边任意 k = strings.Replace(k, "[~]", "", -1) @@ -760,7 +922,7 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) { k = "`" + k + "` " } where += k + " LIKE ? " - v = "%" + ObjToStr(v) + " " + v = "%" + ObjToStr(v) + "" res = append(res, v) case "[~!]": //右边任意 k = strings.Replace(k, "[~]", "", -1) @@ -768,7 +930,7 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) { k = "`" + k + "` " } where += k + " LIKE ? " - v = ObjToStr(v) + "% " + v = ObjToStr(v) + "%" res = append(res, v) case "[~~]": //手动任意 k = strings.Replace(k, "[~]", "", -1) diff --git a/example/app/init.go b/example/app/init.go index f507825..51cbb00 100644 --- a/example/app/init.go +++ b/example/app/init.go @@ -12,6 +12,16 @@ var AppProj = Proj{ "ctg": CtgCtr, "mail": MailCtr, "test": { + "test": func(that *Context) { + //data:=that.Db.Table("admin").Order("id DESC").Select("*") + //data1:=that.Db.Table("admin").Where(Map{"name[~]":"m"}).Order("id DESC").Select("*") + // + //data3:=that.Db.Select("admin","*",Map{"name[~]":"m"}) + data2 := that.Db.Table("article").Where(Map{"title[~]": "m"}).Order("id DESC").Page(1, 10).Select("*") + c := that.Db.Table("article").Where(Map{"title[~]": "m"}).Order("id DESC").Group("title").Select("*") + //that.Display(0,Slice{data1,data,data3,data2}) + that.Display(0, Slice{data2, c}) + }, "res": func(that *Context) { ebw_res := that.Db.Select("ebw_res", "*") diff --git a/example/main.go b/example/main.go index 1d05c33..5995203 100644 --- a/example/main.go +++ b/example/main.go @@ -12,6 +12,7 @@ func main() { return isFinished }) + appIns.Run(Router{"app": app.AppProj}) }