2019-02-02 16:18:25 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-04-20 23:23:42 +08:00
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-04-20 23:23:42 +08:00
|
|
|
|
|
|
|
|
|
package gdb
|
|
|
|
|
|
|
|
|
|
import (
|
2018-08-15 18:14:21 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
"errors"
|
|
|
|
|
"database/sql"
|
2019-02-02 16:18:25 +08:00
|
|
|
|
"github.com/gogf/gf/g/util/gconv"
|
|
|
|
|
_ "github.com/gogf/gf/third/github.com/go-sql-driver/mysql"
|
2018-12-16 22:22:07 +08:00
|
|
|
|
"reflect"
|
2018-12-14 10:09:45 +08:00
|
|
|
|
"strings"
|
2018-04-20 23:23:42 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 数据库链式操作模型对象
|
|
|
|
|
type Model struct {
|
2018-12-14 18:35:51 +08:00
|
|
|
|
db DB // 数据库操作对象
|
2018-12-15 15:50:39 +08:00
|
|
|
|
tx *TX // 数据库事务对象
|
2018-12-14 10:09:45 +08:00
|
|
|
|
tablesInit string // 初始化Model时的表名称(可以是多个)
|
2018-08-15 18:14:21 +08:00
|
|
|
|
tables string // 数据库操作表
|
|
|
|
|
fields string // 操作字段
|
|
|
|
|
where string // 操作条件
|
|
|
|
|
whereArgs []interface{} // 操作条件参数
|
|
|
|
|
groupBy string // 分组语句
|
|
|
|
|
orderBy string // 排序语句
|
|
|
|
|
start int // 分页开始
|
|
|
|
|
limit int // 分页条数
|
|
|
|
|
data interface{} // 操作记录(支持Map/List/string类型)
|
|
|
|
|
batch int // 批量操作条数
|
2018-12-16 22:22:07 +08:00
|
|
|
|
filter bool // 是否按照表字段过滤data参数
|
2018-08-15 18:14:21 +08:00
|
|
|
|
cacheEnabled bool // 当前SQL操作是否开启查询缓存功能
|
|
|
|
|
cacheTime int // 查询缓存时间
|
|
|
|
|
cacheName string // 查询缓存名称
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作,数据表字段,可支持多个表,以半角逗号连接
|
2018-12-15 15:50:39 +08:00
|
|
|
|
func (bs *dbBase) Table(tables string) (*Model) {
|
|
|
|
|
return &Model {
|
|
|
|
|
db : bs.db,
|
2018-12-14 10:09:45 +08:00
|
|
|
|
tablesInit : tables,
|
|
|
|
|
tables : tables,
|
|
|
|
|
fields : "*",
|
2018-08-15 18:14:21 +08:00
|
|
|
|
}
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作,数据表字段,可支持多个表,以半角逗号连接
|
2018-12-15 15:50:39 +08:00
|
|
|
|
func (bs *dbBase) From(tables string) (*Model) {
|
|
|
|
|
return bs.db.Table(tables)
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// (事务)链式操作,数据表字段,可支持多个表,以半角逗号连接
|
2018-12-15 15:50:39 +08:00
|
|
|
|
func (tx *TX) Table(tables string) (*Model) {
|
2018-08-15 18:14:21 +08:00
|
|
|
|
return &Model{
|
2018-12-14 10:09:45 +08:00
|
|
|
|
db : tx.db,
|
|
|
|
|
tx : tx,
|
|
|
|
|
tablesInit : tables,
|
|
|
|
|
tables : tables,
|
2018-08-15 18:14:21 +08:00
|
|
|
|
}
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// (事务)链式操作,数据表字段,可支持多个表,以半角逗号连接
|
2018-12-15 15:50:39 +08:00
|
|
|
|
func (tx *TX) From(tables string) (*Model) {
|
2018-08-15 18:14:21 +08:00
|
|
|
|
return tx.Table(tables)
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-18 10:10:14 +08:00
|
|
|
|
// 克隆一个当前对象
|
|
|
|
|
func (md *Model) Clone() *Model {
|
|
|
|
|
newModel := (*Model)(nil)
|
|
|
|
|
if md.tx != nil {
|
|
|
|
|
newModel = md.tx.Table(md.tablesInit)
|
|
|
|
|
} else {
|
|
|
|
|
newModel = md.db.Table(md.tablesInit)
|
|
|
|
|
}
|
|
|
|
|
*newModel = *md
|
|
|
|
|
return newModel
|
2018-12-14 10:09:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 23:23:42 +08:00
|
|
|
|
// 链式操作,左联表
|
|
|
|
|
func (md *Model) LeftJoin(joinTable string, on string) (*Model) {
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model := md.Clone()
|
|
|
|
|
model.tables += fmt.Sprintf(" LEFT JOIN %s ON (%s)", joinTable, on)
|
|
|
|
|
return model
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作,右联表
|
|
|
|
|
func (md *Model) RightJoin(joinTable string, on string) (*Model) {
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model := md.Clone()
|
|
|
|
|
model.tables += fmt.Sprintf(" RIGHT JOIN %s ON (%s)", joinTable, on)
|
|
|
|
|
return model
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作,内联表
|
|
|
|
|
func (md *Model) InnerJoin(joinTable string, on string) (*Model) {
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model := md.Clone()
|
|
|
|
|
model.tables += fmt.Sprintf(" INNER JOIN %s ON (%s)", joinTable, on)
|
|
|
|
|
return model
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作,查询字段
|
|
|
|
|
func (md *Model) Fields(fields string) (*Model) {
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model := md.Clone()
|
|
|
|
|
model.fields = fields
|
|
|
|
|
return model
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-16 22:22:07 +08:00
|
|
|
|
// 链式操作,过滤字段
|
|
|
|
|
func (md *Model) Filter() (*Model) {
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model := md.Clone()
|
|
|
|
|
model.filter = true
|
|
|
|
|
return model
|
2018-12-16 22:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 23:23:42 +08:00
|
|
|
|
// 链式操作,condition,支持string & gdb.Map
|
2018-08-15 18:14:21 +08:00
|
|
|
|
func (md *Model) Where(where interface{}, args ...interface{}) (*Model) {
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model := md.Clone()
|
2018-12-17 10:52:44 +08:00
|
|
|
|
newWhere, newArgs := formatCondition(where, args)
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model.where = newWhere
|
|
|
|
|
model.whereArgs = append(model.whereArgs, newArgs...)
|
2018-12-14 10:09:45 +08:00
|
|
|
|
// 支持 Where("uid", 1)这种格式
|
2018-12-22 21:03:03 +08:00
|
|
|
|
if len(args) == 1 && strings.Index(model.where , "?") < 0 {
|
|
|
|
|
model.where += "=?"
|
2018-12-14 10:09:45 +08:00
|
|
|
|
}
|
2018-12-22 21:03:03 +08:00
|
|
|
|
return model
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作,添加AND条件到Where中
|
2018-08-15 18:14:21 +08:00
|
|
|
|
func (md *Model) And(where interface{}, args ...interface{}) (*Model) {
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model := md.Clone()
|
2018-12-17 10:52:44 +08:00
|
|
|
|
newWhere, newArgs := formatCondition(where, args)
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model.where += " AND " + newWhere
|
|
|
|
|
model.whereArgs = append(model.whereArgs, newArgs...)
|
|
|
|
|
return model
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作,添加OR条件到Where中
|
2018-08-15 18:14:21 +08:00
|
|
|
|
func (md *Model) Or(where interface{}, args ...interface{}) (*Model) {
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model := md.Clone()
|
2018-12-17 10:52:44 +08:00
|
|
|
|
newWhere, newArgs := formatCondition(where, args)
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model.where += " OR " + newWhere
|
|
|
|
|
model.whereArgs = append(model.whereArgs, newArgs...)
|
|
|
|
|
return model
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作,group by
|
|
|
|
|
func (md *Model) GroupBy(groupBy string) (*Model) {
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model := md.Clone()
|
|
|
|
|
model.groupBy = groupBy
|
|
|
|
|
return model
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作,order by
|
|
|
|
|
func (md *Model) OrderBy(orderBy string) (*Model) {
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model := md.Clone()
|
|
|
|
|
model.orderBy = orderBy
|
|
|
|
|
return model
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作,limit
|
|
|
|
|
func (md *Model) Limit(start int, limit int) (*Model) {
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model := md.Clone()
|
|
|
|
|
model.start = start
|
|
|
|
|
model.limit = limit
|
|
|
|
|
return model
|
2018-08-15 18:14:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作,翻页
|
|
|
|
|
// @author ymrjqyy
|
|
|
|
|
func (md *Model) ForPage(page, limit int) (*Model) {
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model := md.Clone()
|
|
|
|
|
model.start = (page - 1) * limit
|
|
|
|
|
model.limit = limit
|
|
|
|
|
return model
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置批处理的大小
|
|
|
|
|
func (md *Model) Batch(batch int) *Model {
|
|
|
|
|
model := md.Clone()
|
|
|
|
|
model.batch = batch
|
|
|
|
|
return model
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询缓存/清除缓存操作,需要注意的是,事务查询不支持缓存。
|
|
|
|
|
// 当time < 0时表示清除缓存, time=0时表示不过期, time > 0时表示过期时间,time过期时间单位:秒;
|
|
|
|
|
// name表示自定义的缓存名称,便于业务层精准定位缓存项(如果业务层需要手动清理时,必须指定缓存名称),
|
|
|
|
|
// 例如:查询缓存时设置名称,清理缓存时可以给定清理的缓存名称进行精准清理。
|
|
|
|
|
func (md *Model) Cache(time int, name ... string) *Model {
|
|
|
|
|
model := md.Clone()
|
|
|
|
|
model.cacheTime = time
|
|
|
|
|
if len(name) > 0 {
|
|
|
|
|
model.cacheName = name[0]
|
|
|
|
|
}
|
|
|
|
|
// 查询缓存特性不支持事务操作
|
|
|
|
|
if model.tx == nil {
|
|
|
|
|
model.cacheEnabled = true
|
|
|
|
|
}
|
|
|
|
|
return model
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 12:38:12 +08:00
|
|
|
|
// 链式操作,操作数据项,参数data类型支持 string/map/slice/struct/*struct ,
|
|
|
|
|
// 也可以是:key,value,key,value,...。
|
|
|
|
|
func (md *Model) Data(data ...interface{}) *Model {
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model := md.Clone()
|
2018-08-15 18:14:21 +08:00
|
|
|
|
if len(data) > 1 {
|
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
|
for i := 0; i < len(data); i += 2 {
|
2019-02-26 01:19:01 +08:00
|
|
|
|
m[gconv.String(data[i])] = data[i + 1]
|
2018-08-15 18:14:21 +08:00
|
|
|
|
}
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model.data = m
|
2018-08-15 18:14:21 +08:00
|
|
|
|
} else {
|
2018-12-16 22:22:07 +08:00
|
|
|
|
switch data[0].(type) {
|
|
|
|
|
case List:
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model.data = data[0]
|
2018-12-16 22:22:07 +08:00
|
|
|
|
case Map:
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model.data = data[0]
|
2018-12-16 22:22:07 +08:00
|
|
|
|
default:
|
|
|
|
|
rv := reflect.ValueOf(data[0])
|
|
|
|
|
kind := rv.Kind()
|
|
|
|
|
if kind == reflect.Ptr {
|
|
|
|
|
rv = rv.Elem()
|
|
|
|
|
kind = rv.Kind()
|
|
|
|
|
}
|
|
|
|
|
switch kind {
|
2019-02-26 01:19:01 +08:00
|
|
|
|
// 如果是slice,那么转换为List类型
|
2018-12-16 22:22:07 +08:00
|
|
|
|
case reflect.Slice: fallthrough
|
|
|
|
|
case reflect.Array:
|
|
|
|
|
list := make(List, rv.Len())
|
|
|
|
|
for i := 0; i < rv.Len(); i++ {
|
|
|
|
|
list[i] = gconv.Map(rv.Index(i).Interface())
|
|
|
|
|
}
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model.data = list
|
2019-02-26 01:19:01 +08:00
|
|
|
|
case reflect.Map: fallthrough
|
|
|
|
|
case reflect.Struct:
|
|
|
|
|
model.data = Map(gconv.Map(data[0]))
|
2018-12-16 22:22:07 +08:00
|
|
|
|
default:
|
2018-12-22 21:03:03 +08:00
|
|
|
|
model.data = data[0]
|
2018-12-16 22:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-15 18:14:21 +08:00
|
|
|
|
}
|
2018-12-22 21:03:03 +08:00
|
|
|
|
return model
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 12:38:12 +08:00
|
|
|
|
// 链式操作, CURD - Insert/BatchInsert。
|
|
|
|
|
// 根据Data方法传递的参数类型决定该操作是单条操作还是批量操作,
|
|
|
|
|
// 如果Data方法传递的是slice类型,那么为批量操作。
|
2018-07-19 22:11:05 +08:00
|
|
|
|
func (md *Model) Insert() (result sql.Result, err error) {
|
2018-08-15 18:14:21 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
if err == nil {
|
|
|
|
|
md.checkAndRemoveCache()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
if md.data == nil {
|
|
|
|
|
return nil, errors.New("inserting into table with empty data")
|
|
|
|
|
}
|
|
|
|
|
// 批量操作
|
|
|
|
|
if list, ok := md.data.(List); ok {
|
|
|
|
|
batch := 10
|
|
|
|
|
if md.batch > 0 {
|
|
|
|
|
batch = md.batch
|
|
|
|
|
}
|
2018-12-16 22:22:07 +08:00
|
|
|
|
if md.filter {
|
|
|
|
|
for k, m := range list {
|
|
|
|
|
list[k] = md.db.filterFields(md.tables, m)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-15 18:14:21 +08:00
|
|
|
|
if md.tx == nil {
|
|
|
|
|
return md.db.BatchInsert(md.tables, list, batch)
|
|
|
|
|
} else {
|
|
|
|
|
return md.tx.BatchInsert(md.tables, list, batch)
|
|
|
|
|
}
|
2018-12-16 22:22:07 +08:00
|
|
|
|
} else if data, ok := md.data.(Map); ok {
|
|
|
|
|
if md.filter {
|
|
|
|
|
data = md.db.filterFields(md.tables, data)
|
|
|
|
|
}
|
2018-08-15 18:14:21 +08:00
|
|
|
|
if md.tx == nil {
|
2018-12-16 22:22:07 +08:00
|
|
|
|
return md.db.Insert(md.tables, data)
|
2018-08-15 18:14:21 +08:00
|
|
|
|
} else {
|
2018-12-16 22:22:07 +08:00
|
|
|
|
return md.tx.Insert(md.tables, data)
|
2018-08-15 18:14:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil, errors.New("inserting into table with invalid data type")
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 12:38:12 +08:00
|
|
|
|
// 链式操作, CURD - Replace/BatchReplace。
|
|
|
|
|
// 根据Data方法传递的参数类型决定该操作是单条操作还是批量操作,
|
|
|
|
|
// 如果Data方法传递的是slice类型,那么为批量操作。
|
2018-07-19 22:11:05 +08:00
|
|
|
|
func (md *Model) Replace() (result sql.Result, err error) {
|
2018-08-15 18:14:21 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
if err == nil {
|
|
|
|
|
md.checkAndRemoveCache()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
if md.data == nil {
|
|
|
|
|
return nil, errors.New("replacing into table with empty data")
|
|
|
|
|
}
|
|
|
|
|
// 批量操作
|
|
|
|
|
if list, ok := md.data.(List); ok {
|
|
|
|
|
batch := 10
|
|
|
|
|
if md.batch > 0 {
|
|
|
|
|
batch = md.batch
|
|
|
|
|
}
|
2018-12-16 22:22:07 +08:00
|
|
|
|
if md.filter {
|
|
|
|
|
for k, m := range list {
|
|
|
|
|
list[k] = md.db.filterFields(md.tables, m)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-15 18:14:21 +08:00
|
|
|
|
if md.tx == nil {
|
|
|
|
|
return md.db.BatchReplace(md.tables, list, batch)
|
|
|
|
|
} else {
|
|
|
|
|
return md.tx.BatchReplace(md.tables, list, batch)
|
|
|
|
|
}
|
2018-12-16 22:22:07 +08:00
|
|
|
|
} else if data, ok := md.data.(Map); ok {
|
|
|
|
|
if md.filter {
|
|
|
|
|
data = md.db.filterFields(md.tables, data)
|
|
|
|
|
}
|
2018-08-15 18:14:21 +08:00
|
|
|
|
if md.tx == nil {
|
2018-12-16 22:22:07 +08:00
|
|
|
|
return md.db.Replace(md.tables, data)
|
2018-08-15 18:14:21 +08:00
|
|
|
|
} else {
|
2018-12-16 22:22:07 +08:00
|
|
|
|
return md.tx.Replace(md.tables, data)
|
2018-08-15 18:14:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil, errors.New("replacing into table with invalid data type")
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 12:38:12 +08:00
|
|
|
|
// 链式操作, CURD - Save/BatchSave。
|
|
|
|
|
// 根据Data方法传递的参数类型决定该操作是单条操作还是批量操作,
|
|
|
|
|
// 如果Data方法传递的是slice类型,那么为批量操作。
|
2018-07-19 22:11:05 +08:00
|
|
|
|
func (md *Model) Save() (result sql.Result, err error) {
|
2018-08-15 18:14:21 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
if err == nil {
|
|
|
|
|
md.checkAndRemoveCache()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
if md.data == nil {
|
|
|
|
|
return nil, errors.New("replacing into table with empty data")
|
|
|
|
|
}
|
|
|
|
|
// 批量操作
|
|
|
|
|
if list, ok := md.data.(List); ok {
|
2019-02-27 09:38:10 +08:00
|
|
|
|
batch := gDEFAULT_BATCH_NUM
|
2018-08-15 18:14:21 +08:00
|
|
|
|
if md.batch > 0 {
|
|
|
|
|
batch = md.batch
|
|
|
|
|
}
|
2018-12-16 22:22:07 +08:00
|
|
|
|
if md.filter {
|
|
|
|
|
for k, m := range list {
|
|
|
|
|
list[k] = md.db.filterFields(md.tables, m)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-15 18:14:21 +08:00
|
|
|
|
if md.tx == nil {
|
|
|
|
|
return md.db.BatchSave(md.tables, list, batch)
|
|
|
|
|
} else {
|
|
|
|
|
return md.tx.BatchSave(md.tables, list, batch)
|
|
|
|
|
}
|
2018-12-16 22:22:07 +08:00
|
|
|
|
} else if data, ok := md.data.(Map); ok {
|
|
|
|
|
if md.filter {
|
|
|
|
|
data = md.db.filterFields(md.tables, data)
|
|
|
|
|
}
|
2018-08-15 18:14:21 +08:00
|
|
|
|
if md.tx == nil {
|
2018-12-16 22:22:07 +08:00
|
|
|
|
return md.db.Save(md.tables, data)
|
2018-08-15 18:14:21 +08:00
|
|
|
|
} else {
|
2018-12-16 22:22:07 +08:00
|
|
|
|
return md.tx.Save(md.tables, data)
|
2018-08-15 18:14:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil, errors.New("saving into table with invalid data type")
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作, CURD - Update
|
2018-07-19 22:11:05 +08:00
|
|
|
|
func (md *Model) Update() (result sql.Result, err error) {
|
2018-08-15 18:14:21 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
if err == nil {
|
|
|
|
|
md.checkAndRemoveCache()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
if md.data == nil {
|
|
|
|
|
return nil, errors.New("updating table with empty data")
|
|
|
|
|
}
|
2018-12-16 22:22:07 +08:00
|
|
|
|
if md.filter {
|
|
|
|
|
if data, ok := md.data.(Map); ok {
|
|
|
|
|
if md.filter {
|
|
|
|
|
md.data = md.db.filterFields(md.tables, data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-15 18:14:21 +08:00
|
|
|
|
if md.tx == nil {
|
|
|
|
|
return md.db.Update(md.tables, md.data, md.where, md.whereArgs ...)
|
|
|
|
|
} else {
|
|
|
|
|
return md.tx.Update(md.tables, md.data, md.where, md.whereArgs ...)
|
|
|
|
|
}
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作, CURD - Delete
|
2018-07-19 22:11:05 +08:00
|
|
|
|
func (md *Model) Delete() (result sql.Result, err error) {
|
2018-08-15 18:14:21 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
if err == nil {
|
|
|
|
|
md.checkAndRemoveCache()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
if md.tx == nil {
|
|
|
|
|
return md.db.Delete(md.tables, md.where, md.whereArgs...)
|
|
|
|
|
} else {
|
|
|
|
|
return md.tx.Delete(md.tables, md.where, md.whereArgs...)
|
|
|
|
|
}
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作,select
|
2018-05-01 10:09:57 +08:00
|
|
|
|
func (md *Model) Select() (Result, error) {
|
2018-12-15 15:50:39 +08:00
|
|
|
|
return md.All()
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作,查询所有记录
|
2018-05-01 10:09:57 +08:00
|
|
|
|
func (md *Model) All() (Result, error) {
|
2018-12-15 15:50:39 +08:00
|
|
|
|
return md.getAll(md.getFormattedSql(), md.whereArgs...)
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作,查询单条记录
|
2018-05-01 10:09:57 +08:00
|
|
|
|
func (md *Model) One() (Record, error) {
|
2018-08-15 18:14:21 +08:00
|
|
|
|
list, err := md.All()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if len(list) > 0 {
|
|
|
|
|
return list[0], nil
|
|
|
|
|
}
|
|
|
|
|
return nil, nil
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 链式操作,查询字段值
|
2018-05-01 13:02:20 +08:00
|
|
|
|
func (md *Model) Value() (Value, error) {
|
2018-08-15 18:14:21 +08:00
|
|
|
|
one, err := md.One()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
for _, v := range one {
|
|
|
|
|
return v, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, nil
|
2018-04-20 23:23:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-19 15:33:17 +08:00
|
|
|
|
// 链式操作,查询单条记录,并自动转换为struct对象
|
|
|
|
|
func (md *Model) Struct(obj interface{}) error {
|
2018-08-15 18:14:21 +08:00
|
|
|
|
one, err := md.One()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return one.ToStruct(obj)
|
2018-07-19 15:33:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-24 10:38:08 +08:00
|
|
|
|
// 链式操作,查询数量,fields可以为空,也可以自定义查询字段,
|
2018-07-19 15:33:17 +08:00
|
|
|
|
// 当给定自定义查询字段时,该字段必须为数量结果,否则会引起歧义,使用如:md.Fields("COUNT(id)")
|
2018-04-24 10:38:08 +08:00
|
|
|
|
func (md *Model) Count() (int, error) {
|
2018-12-18 10:10:14 +08:00
|
|
|
|
defer func(fields string) {
|
|
|
|
|
md.fields = fields
|
|
|
|
|
}(md.fields)
|
2018-08-15 18:14:21 +08:00
|
|
|
|
if md.fields == "" || md.fields == "*" {
|
|
|
|
|
md.fields = "COUNT(1)"
|
2018-12-05 13:52:34 +08:00
|
|
|
|
} else {
|
|
|
|
|
md.fields = fmt.Sprintf(`COUNT(%s)`, md.fields)
|
2018-08-15 18:14:21 +08:00
|
|
|
|
}
|
|
|
|
|
s := md.getFormattedSql()
|
|
|
|
|
if len(md.groupBy) > 0 {
|
|
|
|
|
s = fmt.Sprintf("SELECT COUNT(1) FROM (%s) count_alias", s)
|
|
|
|
|
}
|
|
|
|
|
list, err := md.getAll(s, md.whereArgs...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
if len(list) > 0 {
|
|
|
|
|
for _, v := range list[0] {
|
|
|
|
|
return v.Int(), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0, nil
|
2018-07-19 15:33:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询操作,对底层SQL操作的封装
|
2018-12-17 19:26:59 +08:00
|
|
|
|
func (md *Model) getAll(query string, args ...interface{}) (result Result, err error) {
|
|
|
|
|
cacheKey := ""
|
2018-08-15 18:14:21 +08:00
|
|
|
|
// 查询缓存查询处理
|
|
|
|
|
if md.cacheEnabled {
|
|
|
|
|
cacheKey = md.cacheName
|
|
|
|
|
if len(cacheKey) == 0 {
|
2018-12-17 19:26:59 +08:00
|
|
|
|
cacheKey = query + "/" + gconv.String(args)
|
2018-08-15 18:14:21 +08:00
|
|
|
|
}
|
2018-12-14 18:35:51 +08:00
|
|
|
|
if v := md.db.getCache().Get(cacheKey); v != nil {
|
2018-08-15 18:14:21 +08:00
|
|
|
|
return v.(Result), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-17 19:26:59 +08:00
|
|
|
|
|
2018-08-15 18:14:21 +08:00
|
|
|
|
if md.tx == nil {
|
2018-12-17 19:26:59 +08:00
|
|
|
|
result, err = md.db.GetAll(query, args...)
|
2018-08-15 18:14:21 +08:00
|
|
|
|
} else {
|
2018-12-17 19:26:59 +08:00
|
|
|
|
result, err = md.tx.GetAll(query, args...)
|
2018-08-15 18:14:21 +08:00
|
|
|
|
}
|
|
|
|
|
// 查询缓存保存处理
|
|
|
|
|
if len(cacheKey) > 0 && err == nil {
|
|
|
|
|
if md.cacheTime < 0 {
|
2018-12-14 18:35:51 +08:00
|
|
|
|
md.db.getCache().Remove(cacheKey)
|
2018-08-15 18:14:21 +08:00
|
|
|
|
} else {
|
2018-12-14 18:35:51 +08:00
|
|
|
|
md.db.getCache().Set(cacheKey, result, md.cacheTime*1000)
|
2018-08-15 18:14:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result, err
|
2018-04-24 10:38:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-19 22:11:05 +08:00
|
|
|
|
// 检查是否需要查询查询缓存
|
|
|
|
|
func (md *Model) checkAndRemoveCache() {
|
2018-08-15 18:14:21 +08:00
|
|
|
|
if md.cacheEnabled && md.cacheTime < 0 && len(md.cacheName) > 0 {
|
2018-12-14 18:35:51 +08:00
|
|
|
|
md.db.getCache().Remove(md.cacheName)
|
2018-08-15 18:14:21 +08:00
|
|
|
|
}
|
2018-07-19 22:11:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-24 10:38:08 +08:00
|
|
|
|
// 格式化当前输入参数,返回可执行的SQL语句(不带参数)
|
|
|
|
|
func (md *Model) getFormattedSql() string {
|
2018-08-15 18:14:21 +08:00
|
|
|
|
if md.fields == "" {
|
|
|
|
|
md.fields = "*"
|
|
|
|
|
}
|
|
|
|
|
s := fmt.Sprintf("SELECT %s FROM %s", md.fields, md.tables)
|
|
|
|
|
if md.where != "" {
|
|
|
|
|
s += " WHERE " + md.where
|
|
|
|
|
}
|
|
|
|
|
if md.groupBy != "" {
|
|
|
|
|
s += " GROUP BY " + md.groupBy
|
|
|
|
|
}
|
|
|
|
|
if md.orderBy != "" {
|
|
|
|
|
s += " ORDER BY " + md.orderBy
|
|
|
|
|
}
|
|
|
|
|
if md.limit != 0 {
|
|
|
|
|
s += fmt.Sprintf(" LIMIT %d, %d", md.start, md.limit)
|
|
|
|
|
}
|
|
|
|
|
return s
|
2018-07-19 15:33:17 +08:00
|
|
|
|
}
|
2018-08-13 10:29:01 +08:00
|
|
|
|
|
|
|
|
|
// 组块结果集
|
|
|
|
|
// @author ymrjqyy
|
2018-08-15 18:17:29 +08:00
|
|
|
|
// @author 2018-08-15
|
2018-08-15 18:16:36 +08:00
|
|
|
|
func (md *Model) Chunk(limit int, callback func(result Result, err error) bool) {
|
2018-12-14 10:09:45 +08:00
|
|
|
|
page := 1
|
2018-08-13 10:29:01 +08:00
|
|
|
|
for {
|
2018-08-15 18:14:21 +08:00
|
|
|
|
md.ForPage(page, limit)
|
2018-12-14 10:09:45 +08:00
|
|
|
|
data, err := md.getAll(md.getFormattedSql(), md.whereArgs...)
|
2018-08-13 10:29:01 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
callback(nil, err)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
2018-08-14 10:28:52 +08:00
|
|
|
|
if callback(data, err) == false {
|
|
|
|
|
break
|
|
|
|
|
}
|
2018-08-13 10:29:01 +08:00
|
|
|
|
if len(data) < limit {
|
|
|
|
|
break
|
|
|
|
|
}
|
2018-08-15 18:14:21 +08:00
|
|
|
|
page++
|
2018-08-13 10:29:01 +08:00
|
|
|
|
}
|
|
|
|
|
}
|