2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-03-13 17:21:30 +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,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-08-24 21:18:59 +08:00
|
|
|
"github.com/gogf/gf/errors/gcode"
|
2021-06-22 21:48:56 +08:00
|
|
|
"github.com/gogf/gf/errors/gerror"
|
2021-05-21 13:25:53 +08:00
|
|
|
"reflect"
|
|
|
|
|
2020-09-02 20:37:02 +08:00
|
|
|
"github.com/gogf/gf/container/gset"
|
2020-06-16 17:38:05 +08:00
|
|
|
"github.com/gogf/gf/container/gvar"
|
2021-01-26 14:33:35 +08:00
|
|
|
"github.com/gogf/gf/internal/intlog"
|
2020-09-27 00:15:11 +08:00
|
|
|
"github.com/gogf/gf/internal/json"
|
2020-09-02 20:37:02 +08:00
|
|
|
"github.com/gogf/gf/text/gstr"
|
2020-03-13 17:21:30 +08:00
|
|
|
"github.com/gogf/gf/util/gconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Select is alias of Model.All.
|
|
|
|
// See Model.All.
|
2021-05-02 12:17:06 +08:00
|
|
|
// Deprecated, use All instead.
|
2020-03-13 17:21:30 +08:00
|
|
|
func (m *Model) Select(where ...interface{}) (Result, error) {
|
|
|
|
return m.All(where...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// All does "SELECT FROM ..." statement for the model.
|
|
|
|
// It retrieves the records from table and returns the result as slice type.
|
|
|
|
// It returns nil if there's no record retrieved with the given conditions from table.
|
|
|
|
//
|
2021-02-08 17:57:21 +08:00
|
|
|
// The optional parameter `where` is the same as the parameter of Model.Where function,
|
2020-03-13 17:21:30 +08:00
|
|
|
// see Model.Where.
|
|
|
|
func (m *Model) All(where ...interface{}) (Result, error) {
|
2020-07-15 09:15:03 +08:00
|
|
|
return m.doGetAll(false, where...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// doGetAll does "SELECT FROM ..." statement for the model.
|
|
|
|
// It retrieves the records from table and returns the result as slice type.
|
|
|
|
// It returns nil if there's no record retrieved with the given conditions from table.
|
|
|
|
//
|
2021-02-08 17:57:21 +08:00
|
|
|
// The parameter `limit1` specifies whether limits querying only one record if m.limit is not set.
|
|
|
|
// The optional parameter `where` is the same as the parameter of Model.Where function,
|
2020-07-15 09:15:03 +08:00
|
|
|
// see Model.Where.
|
|
|
|
func (m *Model) doGetAll(limit1 bool, where ...interface{}) (Result, error) {
|
2020-03-13 17:21:30 +08:00
|
|
|
if len(where) > 0 {
|
|
|
|
return m.Where(where[0], where[1:]...).All()
|
|
|
|
}
|
2021-05-27 22:18:16 +08:00
|
|
|
sqlWithHolder, holderArgs := m.getFormattedSqlAndArgs(queryTypeNormal, limit1)
|
|
|
|
return m.doGetAllBySql(sqlWithHolder, holderArgs...)
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
|
|
|
|
2020-09-02 20:37:02 +08:00
|
|
|
// getFieldsFiltered checks the fields and fieldsEx attributes, filters and returns the fields that will
|
|
|
|
// really be committed to underlying database driver.
|
|
|
|
func (m *Model) getFieldsFiltered() string {
|
|
|
|
if m.fieldsEx == "" {
|
|
|
|
// No filtering.
|
2020-11-06 21:32:10 +08:00
|
|
|
if !gstr.Contains(m.fields, ".") && !gstr.Contains(m.fields, " ") {
|
2021-05-21 13:25:53 +08:00
|
|
|
return m.db.GetCore().QuoteString(m.fields)
|
2020-11-06 21:32:10 +08:00
|
|
|
}
|
2020-09-02 20:37:02 +08:00
|
|
|
return m.fields
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
fieldsArray []string
|
|
|
|
fieldsExSet = gset.NewStrSetFrom(gstr.SplitAndTrim(m.fieldsEx, ","))
|
|
|
|
)
|
|
|
|
if m.fields != "*" {
|
|
|
|
// Filter custom fields with fieldEx.
|
|
|
|
fieldsArray = make([]string, 0, 8)
|
|
|
|
for _, v := range gstr.SplitAndTrim(m.fields, ",") {
|
|
|
|
fieldsArray = append(fieldsArray, v[gstr.PosR(v, "-")+1:])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if gstr.Contains(m.tables, " ") {
|
|
|
|
panic("function FieldsEx supports only single table operations")
|
|
|
|
}
|
|
|
|
// Filter table fields with fieldEx.
|
2021-07-06 13:14:33 +08:00
|
|
|
tableFields, err := m.TableFields(m.tablesInit)
|
2020-09-02 20:37:02 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if len(tableFields) == 0 {
|
|
|
|
panic(fmt.Sprintf(`empty table fields for table "%s"`, m.tables))
|
|
|
|
}
|
|
|
|
fieldsArray = make([]string, len(tableFields))
|
|
|
|
for k, v := range tableFields {
|
|
|
|
fieldsArray[v.Index] = k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newFields := ""
|
|
|
|
for _, k := range fieldsArray {
|
|
|
|
if fieldsExSet.Contains(k) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(newFields) > 0 {
|
|
|
|
newFields += ","
|
|
|
|
}
|
2021-05-21 13:25:53 +08:00
|
|
|
newFields += m.db.GetCore().QuoteWord(k)
|
2020-09-02 20:37:02 +08:00
|
|
|
}
|
|
|
|
return newFields
|
|
|
|
}
|
|
|
|
|
2021-08-01 10:33:33 +08:00
|
|
|
// Chunk iterates the query result with given `size` and `handler` function.
|
|
|
|
func (m *Model) Chunk(size int, handler ChunkHandler) {
|
2020-03-13 17:21:30 +08:00
|
|
|
page := m.start
|
2020-11-26 14:26:32 +08:00
|
|
|
if page <= 0 {
|
2020-03-13 17:21:30 +08:00
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
model := m
|
|
|
|
for {
|
2021-08-01 10:33:33 +08:00
|
|
|
model = model.Page(page, size)
|
2020-03-13 17:21:30 +08:00
|
|
|
data, err := model.All()
|
|
|
|
if err != nil {
|
2021-08-01 10:33:33 +08:00
|
|
|
handler(nil, err)
|
2020-03-13 17:21:30 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(data) == 0 {
|
|
|
|
break
|
|
|
|
}
|
2021-08-01 10:33:33 +08:00
|
|
|
if handler(data, err) == false {
|
2020-03-13 17:21:30 +08:00
|
|
|
break
|
|
|
|
}
|
2021-08-01 10:33:33 +08:00
|
|
|
if len(data) < size {
|
2020-03-13 17:21:30 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
page++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// One retrieves one record from table and returns the result as map type.
|
|
|
|
// It returns nil if there's no record retrieved with the given conditions from table.
|
|
|
|
//
|
2021-02-08 17:57:21 +08:00
|
|
|
// The optional parameter `where` is the same as the parameter of Model.Where function,
|
2020-03-13 17:21:30 +08:00
|
|
|
// see Model.Where.
|
|
|
|
func (m *Model) One(where ...interface{}) (Record, error) {
|
|
|
|
if len(where) > 0 {
|
|
|
|
return m.Where(where[0], where[1:]...).One()
|
|
|
|
}
|
2020-07-15 09:15:03 +08:00
|
|
|
all, err := m.doGetAll(true)
|
2020-03-13 17:21:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(all) > 0 {
|
|
|
|
return all[0], nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value retrieves a specified record value from table and returns the result as interface type.
|
|
|
|
// It returns nil if there's no record found with the given conditions from table.
|
|
|
|
//
|
2021-02-08 17:57:21 +08:00
|
|
|
// If the optional parameter `fieldsAndWhere` is given, the fieldsAndWhere[0] is the selected fields
|
2020-03-13 17:21:30 +08:00
|
|
|
// and fieldsAndWhere[1:] is treated as where condition fields.
|
|
|
|
// Also see Model.Fields and Model.Where functions.
|
|
|
|
func (m *Model) Value(fieldsAndWhere ...interface{}) (Value, error) {
|
|
|
|
if len(fieldsAndWhere) > 0 {
|
|
|
|
if len(fieldsAndWhere) > 2 {
|
|
|
|
return m.Fields(gconv.String(fieldsAndWhere[0])).Where(fieldsAndWhere[1], fieldsAndWhere[2:]...).Value()
|
|
|
|
} else if len(fieldsAndWhere) == 2 {
|
|
|
|
return m.Fields(gconv.String(fieldsAndWhere[0])).Where(fieldsAndWhere[1]).Value()
|
|
|
|
} else {
|
|
|
|
return m.Fields(gconv.String(fieldsAndWhere[0])).Value()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
one, err := m.One()
|
|
|
|
if err != nil {
|
2020-06-16 17:38:05 +08:00
|
|
|
return gvar.New(nil), err
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
|
|
|
for _, v := range one {
|
|
|
|
return v, nil
|
|
|
|
}
|
2020-06-16 17:38:05 +08:00
|
|
|
return gvar.New(nil), nil
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Array queries and returns data values as slice from database.
|
2021-05-02 12:17:06 +08:00
|
|
|
// Note that if there are multiple columns in the result, it returns just one column values randomly.
|
2020-03-13 17:21:30 +08:00
|
|
|
//
|
2021-02-08 17:57:21 +08:00
|
|
|
// If the optional parameter `fieldsAndWhere` is given, the fieldsAndWhere[0] is the selected fields
|
2020-03-13 17:21:30 +08:00
|
|
|
// and fieldsAndWhere[1:] is treated as where condition fields.
|
|
|
|
// Also see Model.Fields and Model.Where functions.
|
|
|
|
func (m *Model) Array(fieldsAndWhere ...interface{}) ([]Value, error) {
|
|
|
|
if len(fieldsAndWhere) > 0 {
|
|
|
|
if len(fieldsAndWhere) > 2 {
|
|
|
|
return m.Fields(gconv.String(fieldsAndWhere[0])).Where(fieldsAndWhere[1], fieldsAndWhere[2:]...).Array()
|
|
|
|
} else if len(fieldsAndWhere) == 2 {
|
|
|
|
return m.Fields(gconv.String(fieldsAndWhere[0])).Where(fieldsAndWhere[1]).Array()
|
|
|
|
} else {
|
|
|
|
return m.Fields(gconv.String(fieldsAndWhere[0])).Array()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
all, err := m.All()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return all.Array(), nil
|
|
|
|
}
|
|
|
|
|
2021-06-22 21:48:56 +08:00
|
|
|
// Struct retrieves one record from table and converts it into given struct.
|
|
|
|
// The parameter `pointer` should be type of *struct/**struct. If type **struct is given,
|
|
|
|
// it can create the struct internally during converting.
|
|
|
|
//
|
|
|
|
// Deprecated, use Scan instead.
|
|
|
|
func (m *Model) Struct(pointer interface{}, where ...interface{}) error {
|
|
|
|
return m.doStruct(pointer, where...)
|
|
|
|
}
|
|
|
|
|
2020-03-13 17:21:30 +08:00
|
|
|
// Struct retrieves one record from table and converts it into given struct.
|
2021-02-08 17:57:21 +08:00
|
|
|
// The parameter `pointer` should be type of *struct/**struct. If type **struct is given,
|
2020-03-13 17:21:30 +08:00
|
|
|
// it can create the struct internally during converting.
|
|
|
|
//
|
2021-02-08 17:57:21 +08:00
|
|
|
// The optional parameter `where` is the same as the parameter of Model.Where function,
|
2020-03-13 17:21:30 +08:00
|
|
|
// see Model.Where.
|
|
|
|
//
|
2021-06-22 21:48:56 +08:00
|
|
|
// Note that it returns sql.ErrNoRows if the given parameter `pointer` pointed to a variable that has
|
|
|
|
// default value and there's no record retrieved with the given conditions from table.
|
2020-03-13 17:21:30 +08:00
|
|
|
//
|
2021-06-22 21:48:56 +08:00
|
|
|
// Example:
|
2020-03-13 17:21:30 +08:00
|
|
|
// user := new(User)
|
2021-06-22 21:48:56 +08:00
|
|
|
// err := db.Model("user").Where("id", 1).Scan(user)
|
2020-03-13 17:21:30 +08:00
|
|
|
//
|
|
|
|
// user := (*User)(nil)
|
2021-06-22 21:48:56 +08:00
|
|
|
// err := db.Model("user").Where("id", 1).Scan(&user)
|
|
|
|
func (m *Model) doStruct(pointer interface{}, where ...interface{}) error {
|
|
|
|
model := m
|
|
|
|
// Auto selecting fields by struct attributes.
|
|
|
|
if model.fieldsEx == "" && (model.fields == "" || model.fields == "*") {
|
|
|
|
model = m.Fields(pointer)
|
|
|
|
}
|
|
|
|
one, err := model.One(where...)
|
2020-03-13 17:21:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-08 17:57:21 +08:00
|
|
|
if err = one.Struct(pointer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-22 21:48:56 +08:00
|
|
|
return model.doWithScanStruct(pointer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Structs retrieves records from table and converts them into given struct slice.
|
|
|
|
// The parameter `pointer` should be type of *[]struct/*[]*struct. It can create and fill the struct
|
|
|
|
// slice internally during converting.
|
|
|
|
//
|
|
|
|
// Deprecated, use Scan instead.
|
|
|
|
func (m *Model) Structs(pointer interface{}, where ...interface{}) error {
|
|
|
|
return m.doStructs(pointer, where...)
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Structs retrieves records from table and converts them into given struct slice.
|
2021-02-08 17:57:21 +08:00
|
|
|
// The parameter `pointer` should be type of *[]struct/*[]*struct. It can create and fill the struct
|
2020-03-13 17:21:30 +08:00
|
|
|
// slice internally during converting.
|
|
|
|
//
|
2021-02-08 17:57:21 +08:00
|
|
|
// The optional parameter `where` is the same as the parameter of Model.Where function,
|
2020-03-13 17:21:30 +08:00
|
|
|
// see Model.Where.
|
|
|
|
//
|
2021-06-22 21:48:56 +08:00
|
|
|
// Note that it returns sql.ErrNoRows if the given parameter `pointer` pointed to a variable that has
|
|
|
|
// default value and there's no record retrieved with the given conditions from table.
|
2020-03-13 17:21:30 +08:00
|
|
|
//
|
2021-06-22 21:48:56 +08:00
|
|
|
// Example:
|
2020-03-13 17:21:30 +08:00
|
|
|
// users := ([]User)(nil)
|
2021-06-22 21:48:56 +08:00
|
|
|
// err := db.Model("user").Scan(&users)
|
2020-03-13 17:21:30 +08:00
|
|
|
//
|
|
|
|
// users := ([]*User)(nil)
|
2021-06-22 21:48:56 +08:00
|
|
|
// err := db.Model("user").Scan(&users)
|
|
|
|
func (m *Model) doStructs(pointer interface{}, where ...interface{}) error {
|
|
|
|
model := m
|
|
|
|
// Auto selecting fields by struct attributes.
|
|
|
|
if model.fieldsEx == "" && (model.fields == "" || model.fields == "*") {
|
|
|
|
model = m.Fields(
|
|
|
|
reflect.New(
|
|
|
|
reflect.ValueOf(pointer).Elem().Type().Elem(),
|
|
|
|
).Interface(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
all, err := model.All(where...)
|
2020-03-13 17:21:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-09 18:00:43 +08:00
|
|
|
if err = all.Structs(pointer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-22 21:48:56 +08:00
|
|
|
return model.doWithScanStructs(pointer)
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
|
|
|
|
2021-02-08 17:57:21 +08:00
|
|
|
// Scan automatically calls Struct or Structs function according to the type of parameter `pointer`.
|
2021-06-22 21:48:56 +08:00
|
|
|
// It calls function doStruct if `pointer` is type of *struct/**struct.
|
|
|
|
// It calls function doStructs if `pointer` is type of *[]struct/*[]*struct.
|
2020-03-13 17:21:30 +08:00
|
|
|
//
|
2021-06-22 21:48:56 +08:00
|
|
|
// The optional parameter `where` is the same as the parameter of Model.Where function, see Model.Where.
|
2020-03-13 17:21:30 +08:00
|
|
|
//
|
2021-06-22 21:48:56 +08:00
|
|
|
// Note that it returns sql.ErrNoRows if the given parameter `pointer` pointed to a variable that has
|
|
|
|
// default value and there's no record retrieved with the given conditions from table.
|
2020-03-13 17:21:30 +08:00
|
|
|
//
|
2021-06-22 21:48:56 +08:00
|
|
|
// Example:
|
2020-03-13 17:21:30 +08:00
|
|
|
// user := new(User)
|
2020-12-29 13:30:15 +08:00
|
|
|
// err := db.Model("user").Where("id", 1).Scan(user)
|
2020-03-13 17:21:30 +08:00
|
|
|
//
|
|
|
|
// user := (*User)(nil)
|
2020-12-29 13:30:15 +08:00
|
|
|
// err := db.Model("user").Where("id", 1).Scan(&user)
|
2020-03-13 17:21:30 +08:00
|
|
|
//
|
|
|
|
// users := ([]User)(nil)
|
2020-12-29 13:30:15 +08:00
|
|
|
// err := db.Model("user").Scan(&users)
|
2020-03-13 17:21:30 +08:00
|
|
|
//
|
|
|
|
// users := ([]*User)(nil)
|
2020-12-29 13:30:15 +08:00
|
|
|
// err := db.Model("user").Scan(&users)
|
2020-03-13 17:21:30 +08:00
|
|
|
func (m *Model) Scan(pointer interface{}, where ...interface{}) error {
|
2021-06-22 21:48:56 +08:00
|
|
|
var (
|
|
|
|
reflectValue reflect.Value
|
|
|
|
reflectKind reflect.Kind
|
|
|
|
)
|
2021-02-08 17:57:21 +08:00
|
|
|
if v, ok := pointer.(reflect.Value); ok {
|
2021-06-22 21:48:56 +08:00
|
|
|
reflectValue = v
|
2021-02-08 17:57:21 +08:00
|
|
|
} else {
|
2021-06-22 21:48:56 +08:00
|
|
|
reflectValue = reflect.ValueOf(pointer)
|
|
|
|
}
|
|
|
|
|
|
|
|
reflectKind = reflectValue.Kind()
|
|
|
|
if reflectKind != reflect.Ptr {
|
2021-08-24 21:18:59 +08:00
|
|
|
return gerror.NewCode(gcode.CodeInvalidParameter, `the parameter "pointer" for function Scan should type of pointer`)
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
2021-06-22 21:48:56 +08:00
|
|
|
for reflectKind == reflect.Ptr {
|
|
|
|
reflectValue = reflectValue.Elem()
|
|
|
|
reflectKind = reflectValue.Kind()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch reflectKind {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
return m.doStructs(pointer, where...)
|
|
|
|
|
|
|
|
case reflect.Struct, reflect.Invalid:
|
|
|
|
return m.doStruct(pointer, where...)
|
|
|
|
|
|
|
|
default:
|
2021-07-20 23:02:02 +08:00
|
|
|
return gerror.NewCode(
|
2021-08-24 21:18:59 +08:00
|
|
|
gcode.CodeInvalidParameter,
|
2021-07-20 23:02:02 +08:00
|
|
|
`element of parameter "pointer" for function Scan should type of struct/*struct/[]struct/[]*struct`,
|
|
|
|
)
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 17:57:21 +08:00
|
|
|
// ScanList converts `r` to struct slice which contains other complex struct attributes.
|
|
|
|
// Note that the parameter `listPointer` should be type of *[]struct/*[]*struct.
|
2020-07-05 11:54:37 +08:00
|
|
|
// Usage example:
|
|
|
|
//
|
|
|
|
// type Entity struct {
|
|
|
|
// User *EntityUser
|
|
|
|
// UserDetail *EntityUserDetail
|
|
|
|
// UserScores []*EntityUserScores
|
|
|
|
// }
|
|
|
|
// var users []*Entity
|
|
|
|
// or
|
|
|
|
// var users []Entity
|
|
|
|
//
|
|
|
|
// ScanList(&users, "User")
|
|
|
|
// ScanList(&users, "UserDetail", "User", "uid:Uid")
|
|
|
|
// ScanList(&users, "UserScores", "User", "uid:Uid")
|
|
|
|
// The parameters "User"/"UserDetail"/"UserScores" in the example codes specify the target attribute struct
|
|
|
|
// that current result will be bound to.
|
|
|
|
// The "uid" in the example codes is the table field name of the result, and the "Uid" is the relational
|
2021-02-08 17:57:21 +08:00
|
|
|
// struct attribute name. It automatically calculates the HasOne/HasMany relationship with given `relation`
|
2020-07-05 11:54:37 +08:00
|
|
|
// parameter.
|
|
|
|
// See the example or unit testing cases for clear understanding for this function.
|
|
|
|
func (m *Model) ScanList(listPointer interface{}, attributeName string, relation ...string) (err error) {
|
2021-07-08 21:02:36 +08:00
|
|
|
result, err := m.All()
|
2020-07-05 11:54:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-08 21:02:36 +08:00
|
|
|
return doScanList(m, result, listPointer, attributeName, relation...)
|
2020-07-05 11:54:37 +08:00
|
|
|
}
|
|
|
|
|
2020-03-13 17:21:30 +08:00
|
|
|
// Count does "SELECT COUNT(x) FROM ..." statement for the model.
|
2021-02-08 17:57:21 +08:00
|
|
|
// The optional parameter `where` is the same as the parameter of Model.Where function,
|
2020-03-13 17:21:30 +08:00
|
|
|
// see Model.Where.
|
|
|
|
func (m *Model) Count(where ...interface{}) (int, error) {
|
|
|
|
if len(where) > 0 {
|
|
|
|
return m.Where(where[0], where[1:]...).Count()
|
|
|
|
}
|
2021-05-27 22:18:16 +08:00
|
|
|
var (
|
|
|
|
sqlWithHolder, holderArgs = m.getFormattedSqlAndArgs(queryTypeCount, false)
|
|
|
|
list, err = m.doGetAllBySql(sqlWithHolder, holderArgs...)
|
|
|
|
)
|
2020-03-13 17:21:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if len(list) > 0 {
|
|
|
|
for _, v := range list[0] {
|
|
|
|
return v.Int(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2021-05-02 12:17:06 +08:00
|
|
|
// CountColumn does "SELECT COUNT(x) FROM ..." statement for the model.
|
|
|
|
func (m *Model) CountColumn(column string) (int, error) {
|
|
|
|
if len(column) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return m.Fields(column).Count()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Min does "SELECT MIN(x) FROM ..." statement for the model.
|
|
|
|
func (m *Model) Min(column string) (float64, error) {
|
|
|
|
if len(column) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2021-05-21 13:25:53 +08:00
|
|
|
value, err := m.Fields(fmt.Sprintf(`MIN(%s)`, m.db.GetCore().QuoteWord(column))).Value()
|
2021-05-02 12:17:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return value.Float64(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Max does "SELECT MAX(x) FROM ..." statement for the model.
|
|
|
|
func (m *Model) Max(column string) (float64, error) {
|
|
|
|
if len(column) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2021-05-21 13:25:53 +08:00
|
|
|
value, err := m.Fields(fmt.Sprintf(`MAX(%s)`, m.db.GetCore().QuoteWord(column))).Value()
|
2021-05-02 12:17:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return value.Float64(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avg does "SELECT AVG(x) FROM ..." statement for the model.
|
|
|
|
func (m *Model) Avg(column string) (float64, error) {
|
|
|
|
if len(column) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2021-05-21 13:25:53 +08:00
|
|
|
value, err := m.Fields(fmt.Sprintf(`AVG(%s)`, m.db.GetCore().QuoteWord(column))).Value()
|
2021-05-02 12:17:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return value.Float64(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sum does "SELECT SUM(x) FROM ..." statement for the model.
|
|
|
|
func (m *Model) Sum(column string) (float64, error) {
|
|
|
|
if len(column) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2021-05-21 13:25:53 +08:00
|
|
|
value, err := m.Fields(fmt.Sprintf(`SUM(%s)`, m.db.GetCore().QuoteWord(column))).Value()
|
2021-05-02 12:17:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return value.Float64(), err
|
|
|
|
}
|
|
|
|
|
2020-03-13 17:21:30 +08:00
|
|
|
// FindOne retrieves and returns a single Record by Model.WherePri and Model.One.
|
|
|
|
// Also see Model.WherePri and Model.One.
|
|
|
|
func (m *Model) FindOne(where ...interface{}) (Record, error) {
|
|
|
|
if len(where) > 0 {
|
|
|
|
return m.WherePri(where[0], where[1:]...).One()
|
|
|
|
}
|
|
|
|
return m.One()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindAll retrieves and returns Result by by Model.WherePri and Model.All.
|
|
|
|
// Also see Model.WherePri and Model.All.
|
|
|
|
func (m *Model) FindAll(where ...interface{}) (Result, error) {
|
|
|
|
if len(where) > 0 {
|
|
|
|
return m.WherePri(where[0], where[1:]...).All()
|
|
|
|
}
|
|
|
|
return m.All()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindValue retrieves and returns single field value by Model.WherePri and Model.Value.
|
|
|
|
// Also see Model.WherePri and Model.Value.
|
|
|
|
func (m *Model) FindValue(fieldsAndWhere ...interface{}) (Value, error) {
|
|
|
|
if len(fieldsAndWhere) >= 2 {
|
|
|
|
return m.WherePri(fieldsAndWhere[1], fieldsAndWhere[2:]...).Fields(gconv.String(fieldsAndWhere[0])).Value()
|
|
|
|
}
|
|
|
|
if len(fieldsAndWhere) == 1 {
|
|
|
|
return m.Fields(gconv.String(fieldsAndWhere[0])).Value()
|
|
|
|
}
|
|
|
|
return m.Value()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindArray queries and returns data values as slice from database.
|
2021-01-26 01:19:55 +08:00
|
|
|
// Note that if there are multiple columns in the result, it returns just one column values randomly.
|
2020-03-13 17:21:30 +08:00
|
|
|
// Also see Model.WherePri and Model.Value.
|
|
|
|
func (m *Model) FindArray(fieldsAndWhere ...interface{}) ([]Value, error) {
|
|
|
|
if len(fieldsAndWhere) >= 2 {
|
|
|
|
return m.WherePri(fieldsAndWhere[1], fieldsAndWhere[2:]...).Fields(gconv.String(fieldsAndWhere[0])).Array()
|
|
|
|
}
|
|
|
|
if len(fieldsAndWhere) == 1 {
|
|
|
|
return m.Fields(gconv.String(fieldsAndWhere[0])).Array()
|
|
|
|
}
|
|
|
|
return m.Array()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindCount retrieves and returns the record number by Model.WherePri and Model.Count.
|
|
|
|
// Also see Model.WherePri and Model.Count.
|
|
|
|
func (m *Model) FindCount(where ...interface{}) (int, error) {
|
|
|
|
if len(where) > 0 {
|
|
|
|
return m.WherePri(where[0], where[1:]...).Count()
|
|
|
|
}
|
|
|
|
return m.Count()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindScan retrieves and returns the record/records by Model.WherePri and Model.Scan.
|
|
|
|
// Also see Model.WherePri and Model.Scan.
|
|
|
|
func (m *Model) FindScan(pointer interface{}, where ...interface{}) error {
|
|
|
|
if len(where) > 0 {
|
|
|
|
return m.WherePri(where[0], where[1:]...).Scan(pointer)
|
|
|
|
}
|
|
|
|
return m.Scan(pointer)
|
|
|
|
}
|
2020-04-26 21:31:55 +08:00
|
|
|
|
2021-06-06 23:06:39 +08:00
|
|
|
// Union does "(SELECT xxx FROM xxx) UNION (SELECT xxx FROM xxx) ..." statement for the model.
|
|
|
|
func (m *Model) Union(unions ...*Model) *Model {
|
|
|
|
return m.db.Union(unions...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnionAll does "(SELECT xxx FROM xxx) UNION ALL (SELECT xxx FROM xxx) ..." statement for the model.
|
|
|
|
func (m *Model) UnionAll(unions ...*Model) *Model {
|
|
|
|
return m.db.UnionAll(unions...)
|
|
|
|
}
|
|
|
|
|
2020-07-15 09:15:03 +08:00
|
|
|
// doGetAllBySql does the select statement on the database.
|
|
|
|
func (m *Model) doGetAllBySql(sql string, args ...interface{}) (result Result, err error) {
|
2020-04-26 21:31:55 +08:00
|
|
|
cacheKey := ""
|
2021-05-21 13:25:53 +08:00
|
|
|
cacheObj := m.db.GetCache().Ctx(m.GetCtx())
|
2020-04-26 21:31:55 +08:00
|
|
|
// Retrieve from cache.
|
|
|
|
if m.cacheEnabled && m.tx == nil {
|
|
|
|
cacheKey = m.cacheName
|
|
|
|
if len(cacheKey) == 0 {
|
2020-09-27 00:15:11 +08:00
|
|
|
cacheKey = sql + ", @PARAMS:" + gconv.String(args)
|
2020-04-26 21:31:55 +08:00
|
|
|
}
|
2020-10-09 20:59:49 +08:00
|
|
|
if v, _ := cacheObj.GetVar(cacheKey); !v.IsNil() {
|
2020-09-27 00:15:11 +08:00
|
|
|
if result, ok := v.Val().(Result); ok {
|
|
|
|
// In-memory cache.
|
|
|
|
return result, nil
|
|
|
|
} else {
|
|
|
|
// Other cache, it needs conversion.
|
|
|
|
var result Result
|
2021-05-15 22:38:07 +08:00
|
|
|
if err = json.UnmarshalUseNumber(v.Bytes(), &result); err != nil {
|
2020-09-27 00:15:11 +08:00
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
2020-04-26 21:31:55 +08:00
|
|
|
}
|
|
|
|
}
|
2021-06-04 09:54:19 +08:00
|
|
|
result, err = m.db.DoGetAll(
|
2021-05-27 22:18:16 +08:00
|
|
|
m.GetCtx(), m.getLink(false), sql, m.mergeArguments(args)...,
|
|
|
|
)
|
2020-04-26 21:31:55 +08:00
|
|
|
// Cache the result.
|
|
|
|
if cacheKey != "" && err == nil {
|
|
|
|
if m.cacheDuration < 0 {
|
2021-01-26 14:33:35 +08:00
|
|
|
if _, err := cacheObj.Remove(cacheKey); err != nil {
|
2021-06-26 16:23:54 +08:00
|
|
|
intlog.Error(m.GetCtx(), err)
|
2021-01-26 14:33:35 +08:00
|
|
|
}
|
2020-04-26 21:31:55 +08:00
|
|
|
} else {
|
2021-07-21 19:24:16 +08:00
|
|
|
// In case of Cache Penetration.
|
|
|
|
if result == nil {
|
|
|
|
result = Result{}
|
|
|
|
}
|
2021-01-26 14:33:35 +08:00
|
|
|
if err := cacheObj.Set(cacheKey, result, m.cacheDuration); err != nil {
|
2021-06-26 16:23:54 +08:00
|
|
|
intlog.Error(m.GetCtx(), err)
|
2021-01-26 14:33:35 +08:00
|
|
|
}
|
2020-04-26 21:31:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
2021-05-27 22:18:16 +08:00
|
|
|
|
2021-06-06 23:06:39 +08:00
|
|
|
func (m *Model) getFormattedSqlAndArgs(queryType int, limit1 bool) (sqlWithHolder string, holderArgs []interface{}) {
|
2021-05-27 22:18:16 +08:00
|
|
|
switch queryType {
|
|
|
|
case queryTypeCount:
|
|
|
|
countFields := "COUNT(1)"
|
|
|
|
if m.fields != "" && m.fields != "*" {
|
|
|
|
// DO NOT quote the m.fields here, in case of fields like:
|
|
|
|
// DISTINCT t.user_id uid
|
|
|
|
countFields = fmt.Sprintf(`COUNT(%s%s)`, m.distinct, m.fields)
|
|
|
|
}
|
2021-06-21 19:21:38 +08:00
|
|
|
// Raw SQL Model.
|
|
|
|
if m.rawSql != "" {
|
|
|
|
sqlWithHolder = fmt.Sprintf("SELECT %s FROM (%s) AS T", countFields, m.rawSql)
|
|
|
|
return sqlWithHolder, nil
|
|
|
|
}
|
2021-05-27 22:18:16 +08:00
|
|
|
conditionWhere, conditionExtra, conditionArgs := m.formatCondition(false, true)
|
|
|
|
sqlWithHolder = fmt.Sprintf("SELECT %s FROM %s%s", countFields, m.tables, conditionWhere+conditionExtra)
|
|
|
|
if len(m.groupBy) > 0 {
|
|
|
|
sqlWithHolder = fmt.Sprintf("SELECT COUNT(1) FROM (%s) count_alias", sqlWithHolder)
|
|
|
|
}
|
|
|
|
return sqlWithHolder, conditionArgs
|
|
|
|
|
|
|
|
default:
|
|
|
|
conditionWhere, conditionExtra, conditionArgs := m.formatCondition(limit1, false)
|
2021-06-06 23:06:39 +08:00
|
|
|
// Raw SQL Model, especially for UNION/UNION ALL featured SQL.
|
|
|
|
if m.rawSql != "" {
|
|
|
|
sqlWithHolder = fmt.Sprintf(
|
|
|
|
"%s%s",
|
|
|
|
m.rawSql,
|
|
|
|
conditionWhere+conditionExtra,
|
|
|
|
)
|
|
|
|
return sqlWithHolder, conditionArgs
|
|
|
|
}
|
2021-05-27 22:18:16 +08:00
|
|
|
// DO NOT quote the m.fields where, in case of fields like:
|
|
|
|
// DISTINCT t.user_id uid
|
|
|
|
sqlWithHolder = fmt.Sprintf(
|
|
|
|
"SELECT %s%s FROM %s%s",
|
|
|
|
m.distinct,
|
|
|
|
m.getFieldsFiltered(),
|
|
|
|
m.tables,
|
|
|
|
conditionWhere+conditionExtra,
|
|
|
|
)
|
|
|
|
return sqlWithHolder, conditionArgs
|
|
|
|
}
|
|
|
|
}
|