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"
|
2020-03-13 17:21:30 +08:00
|
|
|
"github.com/gogf/gf/container/gset"
|
|
|
|
"github.com/gogf/gf/text/gstr"
|
2020-11-06 20:52:16 +08:00
|
|
|
"github.com/gogf/gf/util/gconv"
|
|
|
|
"github.com/gogf/gf/util/gutil"
|
2020-03-13 17:21:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Filter marks filtering the fields which does not exist in the fields of the operated table.
|
2020-06-04 17:29:16 +08:00
|
|
|
// Note that this function supports only single table operations.
|
2020-03-13 17:21:30 +08:00
|
|
|
func (m *Model) Filter() *Model {
|
|
|
|
if gstr.Contains(m.tables, " ") {
|
|
|
|
panic("function Filter supports only single table operations")
|
|
|
|
}
|
|
|
|
model := m.getModel()
|
|
|
|
model.filter = true
|
|
|
|
return model
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fields sets the operation fields of the model, multiple fields joined using char ','.
|
2020-11-06 20:52:16 +08:00
|
|
|
// The parameter <fieldNamesOrMapStruct> can be type of string/map/*map/struct/*struct.
|
|
|
|
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:
|
|
|
|
model := m.getModel()
|
2020-12-04 14:22:50 +08:00
|
|
|
model.fields = gstr.Join(m.mappingAndFilterToTableFields(gconv.Strings(fieldNamesOrMapStruct)), ",")
|
2020-11-06 20:52:16 +08:00
|
|
|
return model
|
2020-12-04 14:22:50 +08:00
|
|
|
// It need type asserting.
|
2020-11-06 20:52:16 +08:00
|
|
|
case length == 1:
|
2020-10-10 14:00:10 +08:00
|
|
|
model := m.getModel()
|
2020-11-06 20:52:16 +08:00
|
|
|
switch r := fieldNamesOrMapStruct[0].(type) {
|
|
|
|
case string:
|
2020-12-04 14:22:50 +08:00
|
|
|
model.fields = gstr.Join(m.mappingAndFilterToTableFields([]string{r}), ",")
|
2020-11-18 00:32:09 +08:00
|
|
|
case []string:
|
2020-12-04 14:22:50 +08:00
|
|
|
model.fields = gstr.Join(m.mappingAndFilterToTableFields(r), ",")
|
2020-11-06 20:52:16 +08:00
|
|
|
default:
|
2020-12-04 14:22:50 +08:00
|
|
|
model.fields = gstr.Join(m.mappingAndFilterToTableFields(gutil.Keys(r)), ",")
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// FieldsEx sets 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.
|
2020-11-06 20:52:16 +08:00
|
|
|
// The parameter <fieldNamesOrMapStruct> can be type of string/map/*map/struct/*struct.
|
|
|
|
func (m *Model) FieldsEx(fieldNamesOrMapStruct ...interface{}) *Model {
|
|
|
|
length := len(fieldNamesOrMapStruct)
|
|
|
|
if length == 0 {
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
model := m.getModel()
|
|
|
|
switch {
|
|
|
|
case length >= 2:
|
2020-12-04 14:22:50 +08:00
|
|
|
model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(gconv.Strings(fieldNamesOrMapStruct)), ",")
|
2020-11-06 20:52:16 +08:00
|
|
|
return model
|
|
|
|
case length == 1:
|
|
|
|
switch r := fieldNamesOrMapStruct[0].(type) {
|
|
|
|
case string:
|
2020-12-04 14:22:50 +08:00
|
|
|
model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields([]string{r}), ",")
|
|
|
|
case []string:
|
|
|
|
model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(r), ",")
|
2020-11-06 20:52:16 +08:00
|
|
|
default:
|
2020-12-04 14:22:50 +08:00
|
|
|
model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(gutil.Keys(r)), ",")
|
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
|
|
|
}
|
|
|
|
|
2020-09-02 20:37:02 +08:00
|
|
|
// Deprecated, use GetFieldsStr instead.
|
|
|
|
// This function name confuses the user that it was a chaining function.
|
|
|
|
func (m *Model) FieldsStr(prefix ...string) string {
|
|
|
|
return m.GetFieldsStr(prefix...)
|
|
|
|
}
|
|
|
|
|
2020-03-13 17:21:30 +08:00
|
|
|
// FieldsStr retrieves and returns all fields from the table, joined with char ','.
|
|
|
|
// The optional parameter <prefix> specifies the prefix for each field, eg: FieldsStr("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]
|
|
|
|
}
|
2020-04-27 21:18:42 +08:00
|
|
|
tableFields, err := m.db.TableFields(m.tables)
|
|
|
|
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
|
|
|
}
|
2020-04-27 21:18:42 +08:00
|
|
|
newFields = m.db.QuoteString(newFields)
|
|
|
|
return newFields
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
|
|
|
|
2020-09-02 20:37:02 +08:00
|
|
|
// Deprecated, use GetFieldsExStr instead.
|
|
|
|
// This function name confuses the user that it was a chaining function.
|
|
|
|
func (m *Model) FieldsExStr(fields string, prefix ...string) string {
|
|
|
|
return m.GetFieldsExStr(fields, prefix...)
|
|
|
|
}
|
|
|
|
|
2020-03-13 17:21:30 +08:00
|
|
|
// FieldsExStr retrieves and returns fields which are not in parameter <fields> from the table,
|
|
|
|
// joined with char ','.
|
|
|
|
// 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]
|
|
|
|
}
|
2020-04-27 21:18:42 +08:00
|
|
|
tableFields, err := m.db.TableFields(m.tables)
|
|
|
|
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
|
|
|
}
|
2020-04-27 21:18:42 +08:00
|
|
|
newFields = m.db.QuoteString(newFields)
|
|
|
|
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) {
|
2020-07-17 14:28:50 +08:00
|
|
|
tableFields, err := m.db.TableFields(m.tables)
|
|
|
|
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 {
|
2020-07-21 12:40:13 +08:00
|
|
|
return false, fmt.Errorf(`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
|
|
|
}
|