mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
完成数据库ORM链式操作的Where参数类型修改:string -> interface{}
This commit is contained in:
8
TODO
8
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{};
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
@ -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"))
|
||||
}
|
||||
Reference in New Issue
Block a user