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 (
|
2020-04-27 21:18:42 +08:00
|
|
|
"fmt"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/container/gset"
|
|
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2020-03-13 17:21:30 +08:00
|
|
|
)
|
|
|
|
|
2021-08-12 19:42:44 +08:00
|
|
|
// Fields appends `fieldNamesOrMapStruct` to the operation fields of the model, multiple fields joined using char ','.
|
2021-02-08 17:57:21 +08:00
|
|
|
// The parameter `fieldNamesOrMapStruct` can be type of string/map/*map/struct/*struct.
|
2020-11-06 20:52:16 +08:00
|
|
|
func (m *Model) Fields(fieldNamesOrMapStruct ...interface{}) *Model {
|
|
|
|
length := len(fieldNamesOrMapStruct)
|
|
|
|
if length == 0 {
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
switch {
|
2020-12-04 14:22:50 +08:00
|
|
|
// String slice.
|
2020-11-06 20:52:16 +08:00
|
|
|
case length >= 2:
|
2021-08-12 19:42:44 +08:00
|
|
|
return m.appendFieldsByStr(gstr.Join(
|
|
|
|
m.mappingAndFilterToTableFields(gconv.Strings(fieldNamesOrMapStruct), true),
|
|
|
|
",",
|
|
|
|
))
|
|
|
|
// It needs type asserting.
|
2020-11-06 20:52:16 +08:00
|
|
|
case length == 1:
|
2021-10-06 11:10:35 +08:00
|
|
|
structOrMap := fieldNamesOrMapStruct[0]
|
|
|
|
switch r := structOrMap.(type) {
|
2020-11-06 20:52:16 +08:00
|
|
|
case string:
|
2021-08-12 19:42:44 +08:00
|
|
|
return m.appendFieldsByStr(gstr.Join(
|
|
|
|
m.mappingAndFilterToTableFields([]string{r}, false), ",",
|
|
|
|
))
|
2021-10-06 11:10:35 +08:00
|
|
|
|
2020-11-18 00:32:09 +08:00
|
|
|
case []string:
|
2021-08-12 19:42:44 +08:00
|
|
|
return m.appendFieldsByStr(gstr.Join(
|
|
|
|
m.mappingAndFilterToTableFields(r, true), ",",
|
|
|
|
))
|
2021-10-06 11:10:35 +08:00
|
|
|
|
2020-11-06 20:52:16 +08:00
|
|
|
default:
|
2021-08-12 19:42:44 +08:00
|
|
|
return m.appendFieldsByStr(gstr.Join(
|
2021-10-06 11:10:35 +08:00
|
|
|
m.mappingAndFilterToTableFields(getFieldsFromStructOrMap(structOrMap), true), ",",
|
2021-08-12 19:42:44 +08:00
|
|
|
))
|
2020-11-06 20:52:16 +08:00
|
|
|
}
|
2020-10-10 14:00:10 +08:00
|
|
|
}
|
|
|
|
return m
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
|
|
|
|
2021-08-12 19:42:44 +08:00
|
|
|
// FieldsEx appends `fieldNamesOrMapStruct` to the excluded operation fields of the model,
|
|
|
|
// multiple fields joined using char ','.
|
2020-06-04 17:29:16 +08:00
|
|
|
// Note that this function supports only single table operations.
|
2021-02-08 17:57:21 +08:00
|
|
|
// The parameter `fieldNamesOrMapStruct` can be type of string/map/*map/struct/*struct.
|
2020-11-06 20:52:16 +08:00
|
|
|
func (m *Model) FieldsEx(fieldNamesOrMapStruct ...interface{}) *Model {
|
|
|
|
length := len(fieldNamesOrMapStruct)
|
|
|
|
if length == 0 {
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
model := m.getModel()
|
|
|
|
switch {
|
|
|
|
case length >= 2:
|
2021-03-23 14:21:54 +08:00
|
|
|
model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(gconv.Strings(fieldNamesOrMapStruct), true), ",")
|
2020-11-06 20:52:16 +08:00
|
|
|
return model
|
|
|
|
case length == 1:
|
|
|
|
switch r := fieldNamesOrMapStruct[0].(type) {
|
|
|
|
case string:
|
2021-03-23 14:21:54 +08:00
|
|
|
model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields([]string{r}, false), ",")
|
2020-12-04 14:22:50 +08:00
|
|
|
case []string:
|
2021-03-23 14:21:54 +08:00
|
|
|
model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(r, true), ",")
|
2020-11-06 20:52:16 +08:00
|
|
|
default:
|
2021-09-28 22:02:08 +08:00
|
|
|
model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(getFieldsFromStructOrMap(r), true), ",")
|
2020-11-06 20:52:16 +08:00
|
|
|
}
|
2020-10-10 14:00:10 +08:00
|
|
|
return model
|
|
|
|
}
|
|
|
|
return m
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
|
|
|
|
2021-08-12 19:42:44 +08:00
|
|
|
// FieldCount formats and appends commonly used field `COUNT(column)` to the select fields of model.
|
|
|
|
func (m *Model) FieldCount(column string, as ...string) *Model {
|
|
|
|
asStr := ""
|
|
|
|
if len(as) > 0 && as[0] != "" {
|
|
|
|
asStr = fmt.Sprintf(` AS %s`, m.db.GetCore().QuoteWord(as[0]))
|
|
|
|
}
|
|
|
|
return m.appendFieldsByStr(fmt.Sprintf(`COUNT(%s)%s`, m.db.GetCore().QuoteWord(column), asStr))
|
|
|
|
}
|
|
|
|
|
|
|
|
// FieldSum formats and appends commonly used field `SUM(column)` to the select fields of model.
|
|
|
|
func (m *Model) FieldSum(column string, as ...string) *Model {
|
|
|
|
asStr := ""
|
|
|
|
if len(as) > 0 && as[0] != "" {
|
|
|
|
asStr = fmt.Sprintf(` AS %s`, m.db.GetCore().QuoteWord(as[0]))
|
|
|
|
}
|
|
|
|
return m.appendFieldsByStr(fmt.Sprintf(`SUM(%s)%s`, m.db.GetCore().QuoteWord(column), asStr))
|
|
|
|
}
|
|
|
|
|
|
|
|
// FieldMin formats and appends commonly used field `MIN(column)` to the select fields of model.
|
|
|
|
func (m *Model) FieldMin(column string, as ...string) *Model {
|
|
|
|
asStr := ""
|
|
|
|
if len(as) > 0 && as[0] != "" {
|
|
|
|
asStr = fmt.Sprintf(` AS %s`, m.db.GetCore().QuoteWord(as[0]))
|
|
|
|
}
|
|
|
|
return m.appendFieldsByStr(fmt.Sprintf(`MIN(%s)%s`, m.db.GetCore().QuoteWord(column), asStr))
|
|
|
|
}
|
|
|
|
|
|
|
|
// FieldMax formats and appends commonly used field `MAX(column)` to the select fields of model.
|
|
|
|
func (m *Model) FieldMax(column string, as ...string) *Model {
|
|
|
|
asStr := ""
|
|
|
|
if len(as) > 0 && as[0] != "" {
|
|
|
|
asStr = fmt.Sprintf(` AS %s`, m.db.GetCore().QuoteWord(as[0]))
|
|
|
|
}
|
|
|
|
return m.appendFieldsByStr(fmt.Sprintf(`MAX(%s)%s`, m.db.GetCore().QuoteWord(column), asStr))
|
|
|
|
}
|
|
|
|
|
|
|
|
// FieldAvg formats and appends commonly used field `AVG(column)` to the select fields of model.
|
|
|
|
func (m *Model) FieldAvg(column string, as ...string) *Model {
|
|
|
|
asStr := ""
|
|
|
|
if len(as) > 0 && as[0] != "" {
|
|
|
|
asStr = fmt.Sprintf(` AS %s`, m.db.GetCore().QuoteWord(as[0]))
|
|
|
|
}
|
|
|
|
return m.appendFieldsByStr(fmt.Sprintf(`AVG(%s)%s`, m.db.GetCore().QuoteWord(column), asStr))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) appendFieldsByStr(fields string) *Model {
|
|
|
|
if fields != "" {
|
|
|
|
model := m.getModel()
|
|
|
|
if model.fields == defaultFields {
|
|
|
|
model.fields = ""
|
|
|
|
}
|
|
|
|
if model.fields != "" {
|
|
|
|
model.fields += ","
|
|
|
|
}
|
|
|
|
model.fields += fields
|
|
|
|
return model
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) appendFieldsExByStr(fieldsEx string) *Model {
|
|
|
|
if fieldsEx != "" {
|
|
|
|
model := m.getModel()
|
|
|
|
if model.fieldsEx != "" {
|
|
|
|
model.fieldsEx += ","
|
|
|
|
}
|
|
|
|
model.fieldsEx += fieldsEx
|
|
|
|
return model
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2021-05-02 12:17:06 +08:00
|
|
|
// GetFieldsStr retrieves and returns all fields from the table, joined with char ','.
|
2021-07-08 15:42:51 +08:00
|
|
|
// The optional parameter `prefix` specifies the prefix for each field, eg: GetFieldsStr("u.").
|
2020-09-02 20:37:02 +08:00
|
|
|
func (m *Model) GetFieldsStr(prefix ...string) string {
|
2020-03-13 17:21:30 +08:00
|
|
|
prefixStr := ""
|
|
|
|
if len(prefix) > 0 {
|
|
|
|
prefixStr = prefix[0]
|
|
|
|
}
|
2021-07-06 13:14:33 +08:00
|
|
|
tableFields, err := m.TableFields(m.tablesInit)
|
2020-04-27 21:18:42 +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 len(newFields) > 0 {
|
|
|
|
newFields += ","
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
2020-04-27 21:18:42 +08:00
|
|
|
newFields += prefixStr + k
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
2021-05-21 13:25:53 +08:00
|
|
|
newFields = m.db.GetCore().QuoteString(newFields)
|
2020-04-27 21:18:42 +08:00
|
|
|
return newFields
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
|
|
|
|
2021-05-02 12:17:06 +08:00
|
|
|
// GetFieldsExStr retrieves and returns fields which are not in parameter `fields` from the table,
|
2020-03-13 17:21:30 +08:00
|
|
|
// joined with char ','.
|
2021-02-08 17:57:21 +08:00
|
|
|
// The parameter `fields` specifies the fields that are excluded.
|
|
|
|
// The optional parameter `prefix` specifies the prefix for each field, eg: FieldsExStr("id", "u.").
|
2020-09-02 20:37:02 +08:00
|
|
|
func (m *Model) GetFieldsExStr(fields string, prefix ...string) string {
|
2020-03-13 17:21:30 +08:00
|
|
|
prefixStr := ""
|
|
|
|
if len(prefix) > 0 {
|
|
|
|
prefixStr = prefix[0]
|
|
|
|
}
|
2021-07-06 20:59:09 +08:00
|
|
|
tableFields, err := m.TableFields(m.tablesInit)
|
2020-04-27 21:18:42 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if len(tableFields) == 0 {
|
|
|
|
panic(fmt.Sprintf(`empty table fields for table "%s"`, m.tables))
|
|
|
|
}
|
|
|
|
fieldsExSet := gset.NewStrSetFrom(gstr.SplitAndTrim(fields, ","))
|
|
|
|
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 += ","
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
2020-04-27 21:18:42 +08:00
|
|
|
newFields += prefixStr + k
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
2021-05-21 13:25:53 +08:00
|
|
|
newFields = m.db.GetCore().QuoteString(newFields)
|
2020-04-27 21:18:42 +08:00
|
|
|
return newFields
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
2020-07-17 14:28:50 +08:00
|
|
|
|
|
|
|
// HasField determine whether the field exists in the table.
|
2020-07-21 12:37:04 +08:00
|
|
|
func (m *Model) HasField(field string) (bool, error) {
|
2021-07-06 13:14:33 +08:00
|
|
|
tableFields, err := m.TableFields(m.tablesInit)
|
2020-07-17 14:28:50 +08:00
|
|
|
if err != nil {
|
2020-07-21 12:37:04 +08:00
|
|
|
return false, err
|
2020-07-17 14:28:50 +08:00
|
|
|
}
|
|
|
|
if len(tableFields) == 0 {
|
2021-09-13 16:43:09 +08:00
|
|
|
return false, gerror.NewCodef(gcode.CodeNotFound, `empty table fields for table "%s"`, m.tables)
|
2020-07-17 14:28:50 +08:00
|
|
|
}
|
|
|
|
fieldsArray := make([]string, len(tableFields))
|
|
|
|
for k, v := range tableFields {
|
|
|
|
fieldsArray[v.Index] = k
|
|
|
|
}
|
|
|
|
for _, f := range fieldsArray {
|
|
|
|
if f == field {
|
2020-07-21 12:37:04 +08:00
|
|
|
return true, nil
|
2020-07-17 14:28:50 +08:00
|
|
|
}
|
|
|
|
}
|
2020-07-21 12:37:04 +08:00
|
|
|
return false, nil
|
2020-07-21 13:28:25 +08:00
|
|
|
}
|