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