gdb_model_fields.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gdb
  7. import (
  8. "fmt"
  9. "github.com/gogf/gf/container/gset"
  10. "github.com/gogf/gf/text/gstr"
  11. "github.com/gogf/gf/util/gconv"
  12. "github.com/gogf/gf/util/gutil"
  13. )
  14. // Filter marks filtering the fields which does not exist in the fields of the operated table.
  15. // Note that this function supports only single table operations.
  16. func (m *Model) Filter() *Model {
  17. if gstr.Contains(m.tables, " ") {
  18. panic("function Filter supports only single table operations")
  19. }
  20. model := m.getModel()
  21. model.filter = true
  22. return model
  23. }
  24. // Fields sets the operation fields of the model, multiple fields joined using char ','.
  25. // The parameter <fieldNamesOrMapStruct> can be type of string/map/*map/struct/*struct.
  26. func (m *Model) Fields(fieldNamesOrMapStruct ...interface{}) *Model {
  27. length := len(fieldNamesOrMapStruct)
  28. if length == 0 {
  29. return m
  30. }
  31. switch {
  32. // String slice.
  33. case length >= 2:
  34. model := m.getModel()
  35. model.fields = gstr.Join(m.mappingAndFilterToTableFields(gconv.Strings(fieldNamesOrMapStruct)), ",")
  36. return model
  37. // It need type asserting.
  38. case length == 1:
  39. model := m.getModel()
  40. switch r := fieldNamesOrMapStruct[0].(type) {
  41. case string:
  42. model.fields = gstr.Join(m.mappingAndFilterToTableFields([]string{r}), ",")
  43. case []string:
  44. model.fields = gstr.Join(m.mappingAndFilterToTableFields(r), ",")
  45. default:
  46. model.fields = gstr.Join(m.mappingAndFilterToTableFields(gutil.Keys(r)), ",")
  47. }
  48. return model
  49. }
  50. return m
  51. }
  52. // FieldsEx sets the excluded operation fields of the model, multiple fields joined using char ','.
  53. // Note that this function supports only single table operations.
  54. // The parameter <fieldNamesOrMapStruct> can be type of string/map/*map/struct/*struct.
  55. func (m *Model) FieldsEx(fieldNamesOrMapStruct ...interface{}) *Model {
  56. length := len(fieldNamesOrMapStruct)
  57. if length == 0 {
  58. return m
  59. }
  60. model := m.getModel()
  61. switch {
  62. case length >= 2:
  63. model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(gconv.Strings(fieldNamesOrMapStruct)), ",")
  64. return model
  65. case length == 1:
  66. switch r := fieldNamesOrMapStruct[0].(type) {
  67. case string:
  68. model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields([]string{r}), ",")
  69. case []string:
  70. model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(r), ",")
  71. default:
  72. model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(gutil.Keys(r)), ",")
  73. }
  74. return model
  75. }
  76. return m
  77. }
  78. // Deprecated, use GetFieldsStr instead.
  79. // This function name confuses the user that it was a chaining function.
  80. func (m *Model) FieldsStr(prefix ...string) string {
  81. return m.GetFieldsStr(prefix...)
  82. }
  83. // FieldsStr retrieves and returns all fields from the table, joined with char ','.
  84. // The optional parameter <prefix> specifies the prefix for each field, eg: FieldsStr("u.").
  85. func (m *Model) GetFieldsStr(prefix ...string) string {
  86. prefixStr := ""
  87. if len(prefix) > 0 {
  88. prefixStr = prefix[0]
  89. }
  90. tableFields, err := m.db.TableFields(m.tables)
  91. if err != nil {
  92. panic(err)
  93. }
  94. if len(tableFields) == 0 {
  95. panic(fmt.Sprintf(`empty table fields for table "%s"`, m.tables))
  96. }
  97. fieldsArray := make([]string, len(tableFields))
  98. for k, v := range tableFields {
  99. fieldsArray[v.Index] = k
  100. }
  101. newFields := ""
  102. for _, k := range fieldsArray {
  103. if len(newFields) > 0 {
  104. newFields += ","
  105. }
  106. newFields += prefixStr + k
  107. }
  108. newFields = m.db.QuoteString(newFields)
  109. return newFields
  110. }
  111. // Deprecated, use GetFieldsExStr instead.
  112. // This function name confuses the user that it was a chaining function.
  113. func (m *Model) FieldsExStr(fields string, prefix ...string) string {
  114. return m.GetFieldsExStr(fields, prefix...)
  115. }
  116. // FieldsExStr retrieves and returns fields which are not in parameter <fields> from the table,
  117. // joined with char ','.
  118. // The parameter <fields> specifies the fields that are excluded.
  119. // The optional parameter <prefix> specifies the prefix for each field, eg: FieldsExStr("id", "u.").
  120. func (m *Model) GetFieldsExStr(fields string, prefix ...string) string {
  121. prefixStr := ""
  122. if len(prefix) > 0 {
  123. prefixStr = prefix[0]
  124. }
  125. tableFields, err := m.db.TableFields(m.tables)
  126. if err != nil {
  127. panic(err)
  128. }
  129. if len(tableFields) == 0 {
  130. panic(fmt.Sprintf(`empty table fields for table "%s"`, m.tables))
  131. }
  132. fieldsExSet := gset.NewStrSetFrom(gstr.SplitAndTrim(fields, ","))
  133. fieldsArray := make([]string, len(tableFields))
  134. for k, v := range tableFields {
  135. fieldsArray[v.Index] = k
  136. }
  137. newFields := ""
  138. for _, k := range fieldsArray {
  139. if fieldsExSet.Contains(k) {
  140. continue
  141. }
  142. if len(newFields) > 0 {
  143. newFields += ","
  144. }
  145. newFields += prefixStr + k
  146. }
  147. newFields = m.db.QuoteString(newFields)
  148. return newFields
  149. }
  150. // HasField determine whether the field exists in the table.
  151. func (m *Model) HasField(field string) (bool, error) {
  152. tableFields, err := m.db.TableFields(m.tables)
  153. if err != nil {
  154. return false, err
  155. }
  156. if len(tableFields) == 0 {
  157. return false, fmt.Errorf(`empty table fields for table "%s"`, m.tables)
  158. }
  159. fieldsArray := make([]string, len(tableFields))
  160. for k, v := range tableFields {
  161. fieldsArray[v.Index] = k
  162. }
  163. for _, f := range fieldsArray {
  164. if f == field {
  165. return true, nil
  166. }
  167. }
  168. return false, nil
  169. }