完成数据库ORM链式操作的Where参数类型修改:string -> interface{}

This commit is contained in:
John
2018-04-20 10:53:30 +08:00
parent e4844a2fdd
commit 32bb5c79ef
6 changed files with 71 additions and 32 deletions

View File

@ -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
}

View File

@ -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)