From 5fc727a119cbe75eea8c774b157d84b92abbc3b0 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 20 Apr 2018 10:53:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=95=B0=E6=8D=AE=E5=BA=93OR?= =?UTF-8?q?M=E9=93=BE=E5=BC=8F=E6=93=8D=E4=BD=9C=E7=9A=84Where=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E7=B1=BB=E5=9E=8B=E4=BF=AE=E6=94=B9=EF=BC=9Astring=20?= =?UTF-8?q?->=20interface{}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO | 8 ++++---- g/database/gdb/gdb_base.go | 30 ++++++++++++++++++++++++++++-- g/database/gdb/gdb_linkop.go | 26 +++++++++++++------------- g/util/gutil/gutil.go | 10 ++++++++++ geg/database/mysql/mysql.go | 18 +++++++++++++----- geg/other/test.go | 11 +++-------- 6 files changed, 71 insertions(+), 32 deletions(-) diff --git a/TODO b/TODO index b344faa0e..053b2292f 100644 --- a/TODO +++ b/TODO @@ -1,8 +1,6 @@ ON THE WAY: -1. gdb Where方法参数的改进,研究是否可以将string参数类型修改为interface{}; -2. 增加对于数据表Model的封装; -3. 跟踪第三方mxj包的issue问题:https://github.com/clbanning/mxj/issues/48; -4. ghttp.Server平滑重启方案; +1. 增加对于数据表Model的封装; +2. ghttp.Server平滑重启方案; DONE: @@ -13,3 +11,5 @@ DONE: 5. 增加fsnotify包支持; 6. 改进gcfg和gview的文件自动更新机制; 7. 将模板变量进行暴露,以便应用端可以进行灵活控制; +8. 跟踪第三方mxj包的issue问题:https://github.com/clbanning/mxj/issues/48; +9. gdb Where方法参数的改进,研究是否可以将string参数类型修改为interface{}; \ No newline at end of file diff --git a/g/database/gdb/gdb_base.go b/g/database/gdb/gdb_base.go index 5e720a5fd..ec1167152 100644 --- a/g/database/gdb/gdb_base.go +++ b/g/database/gdb/gdb_base.go @@ -11,8 +11,10 @@ import ( "fmt" "errors" "strings" + "reflect" "database/sql" "gitee.com/johng/gf/g/util/gconv" + "gitee.com/johng/gf/g/util/gutil" ) // 关闭链接 @@ -315,11 +317,35 @@ func (db *Db) Update(table string, data interface{}, condition interface{}, args for _, v := range args { params = append(params, gconv.String(v)) } - return db.Exec(fmt.Sprintf("UPDATE %s%s%s SET %s WHERE %s", db.charl, table, db.charr, updates, condition), params...) + return db.Exec(fmt.Sprintf("UPDATE %s%s%s SET %s WHERE %s", db.charl, table, db.charr, updates, db.formatCondition(condition)), params...) } // CURD操作:删除数据 func (db *Db) Delete(table string, condition interface{}, args ...interface{}) (sql.Result, error) { - return db.Exec(fmt.Sprintf("DELETE FROM %s WHERE %s", db.charl, table, db.charr, condition), args...) + return db.Exec(fmt.Sprintf("DELETE FROM %s WHERE %s", db.charl, table, db.charr, db.formatCondition(condition)), args...) +} + +// 格式化SQL查询条件 +func (db *Db) formatCondition(condition interface{}) (where string) { + if reflect.ValueOf(condition).Kind() == reflect.Map { + ks := reflect.ValueOf(condition).MapKeys() + vs := reflect.ValueOf(condition) + for _, k := range ks { + key := gconv.String(k.Interface()) + value := gconv.String(vs.MapIndex(k).Interface()) + isNum := gutil.IsNumeric(value) + if len(where) > 0 { + where += " and " + } + if isNum || value == "?" { + where += key + "=" + value + } else { + where += key + "='" + value + "'" + } + } + } else { + where += gconv.String(condition) + } + return } diff --git a/g/database/gdb/gdb_linkop.go b/g/database/gdb/gdb_linkop.go index 3abb61fa1..7fd75313b 100644 --- a/g/database/gdb/gdb_linkop.go +++ b/g/database/gdb/gdb_linkop.go @@ -21,8 +21,8 @@ type DbOp struct { fields string // 操作字段 where string // 操作条件 whereArgs []interface{} // 操作条件参数 - groupby string // 分组语句 - orderby string // 排序语句 + groupBy string // 分组语句 + orderBy string // 排序语句 start int // 分页开始 limit int // 分页条数 data interface{} // 操作记录(支持Map/List/string类型) @@ -79,22 +79,22 @@ func (op *DbOp) Fields(fields string) (*DbOp) { return op } -// 链式操作,consition -func (op *DbOp) Where(where string, args...interface{}) (*DbOp) { - op.where = where +// 链式操作,condition,支持string & gdb.Map +func (op *DbOp) Where(where interface{}, args...interface{}) (*DbOp) { + op.where = op.db.formatCondition(where) op.whereArgs = args return op } // 链式操作,group by -func (op *DbOp) GroupBy(groupby string) (*DbOp) { - op.groupby = groupby +func (op *DbOp) GroupBy(groupBy string) (*DbOp) { + op.groupBy = groupBy return op } // 链式操作,order by -func (op *DbOp) OrderBy(orderby string) (*DbOp) { - op.orderby = orderby +func (op *DbOp) OrderBy(orderBy string) (*DbOp) { + op.orderBy = orderBy return op } @@ -234,11 +234,11 @@ func (op *DbOp) Select() (List, error) { if op.where != "" { s += " WHERE " + op.where } - if op.groupby != "" { - s += " GROUP BY " + op.groupby + if op.groupBy != "" { + s += " GROUP BY " + op.groupBy } - if op.orderby != "" { - s += " ORDER BY " + op.orderby + if op.orderBy != "" { + s += " ORDER BY " + op.orderBy } if op.limit != 0 { s += fmt.Sprintf(" LIMIT %d, %d", op.start, op.limit) diff --git a/g/util/gutil/gutil.go b/g/util/gutil/gutil.go index 6563a6724..5eed0b98e 100644 --- a/g/util/gutil/gutil.go +++ b/g/util/gutil/gutil.go @@ -37,3 +37,13 @@ func IsLetterUpper(b byte) bool { } return false } + +// 判断锁给字符串是否为数字 +func IsNumeric(s string) bool { + for i := 0; i < len(s); i++ { + if s[i] < byte('0') && s[i] > byte('9') { + return false + } + } + return true +} diff --git a/geg/database/mysql/mysql.go b/geg/database/mysql/mysql.go index 4fb6bdb64..02fdfd0b1 100644 --- a/geg/database/mysql/mysql.go +++ b/geg/database/mysql/mysql.go @@ -4,6 +4,7 @@ import ( "fmt" "time" "gitee.com/johng/gf/g/database/gdb" + "gitee.com/johng/gf/g" ) // 本文件用于gf框架的mysql数据库操作示例,不作为单元测试使用 @@ -248,7 +249,6 @@ func update3() { fmt.Println() } - // 链式查询操作1 func linkopSelect1() { fmt.Println("linkopSelect1:") @@ -321,6 +321,17 @@ func linkopUpdate3() { fmt.Println() } +// Where条件使用Map +func linkopUpdate4() { + fmt.Println("linkopUpdate4:") + r, err := db.Table("user").Data(gdb.Map{"name" : "john11111"}).Where(g.Map{"uid" : 1}).Update() + if err == nil { + fmt.Println(r.RowsAffected()) + } else { + fmt.Println(err) + } + fmt.Println() +} // 链式批量写入 func linkopBatchInsert1() { @@ -454,11 +465,8 @@ func main() { //linkopUpdate1() //linkopUpdate2() //linkopUpdate3() + linkopUpdate4() //keepPing() //transaction1() //transaction2() - - m, e := db.Table("user").Fields("uid,name").Where("uid = ?", 4).One() - fmt.Println(e) - fmt.Println(m) } \ No newline at end of file diff --git a/geg/other/test.go b/geg/other/test.go index 1ae2f0de4..b20f9b843 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,15 +1,10 @@ package main import ( - "gitee.com/johng/gf/g/net/ghttp" + "fmt" + "strconv" ) func main() { - s := ghttp.GetServer() - s.BindHandler("/template2", func(r *ghttp.Request){ - //panic("123") - }) - s.SetAccessLogEnabled(true) - s.SetPort(8199) - s.Run() + fmt.Println(strconv.Atoi("11")) } \ No newline at end of file