gdb_model_fields.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright GoFrame Author(https://goframe.org). 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. // Fields appends `fieldNamesOrMapStruct` to the operation fields of the model, multiple fields joined using char ','.
  15. // The parameter `fieldNamesOrMapStruct` can be type of string/map/*map/struct/*struct.
  16. func (m *Model) Fields(fieldNamesOrMapStruct ...interface{}) *Model {
  17. length := len(fieldNamesOrMapStruct)
  18. if length == 0 {
  19. return m
  20. }
  21. switch {
  22. // String slice.
  23. case length >= 2:
  24. return m.appendFieldsByStr(gstr.Join(
  25. m.mappingAndFilterToTableFields(gconv.Strings(fieldNamesOrMapStruct), true),
  26. ",",
  27. ))
  28. // It needs type asserting.
  29. case length == 1:
  30. switch r := fieldNamesOrMapStruct[0].(type) {
  31. case string:
  32. return m.appendFieldsByStr(gstr.Join(
  33. m.mappingAndFilterToTableFields([]string{r}, false), ",",
  34. ))
  35. case []string:
  36. return m.appendFieldsByStr(gstr.Join(
  37. m.mappingAndFilterToTableFields(r, true), ",",
  38. ))
  39. default:
  40. return m.appendFieldsByStr(gstr.Join(
  41. m.mappingAndFilterToTableFields(gutil.Keys(r), true), ",",
  42. ))
  43. }
  44. }
  45. return m
  46. }
  47. // FieldsEx appends `fieldNamesOrMapStruct` to the excluded operation fields of the model,
  48. // multiple fields joined using char ','.
  49. // Note that this function supports only single table operations.
  50. // The parameter `fieldNamesOrMapStruct` can be type of string/map/*map/struct/*struct.
  51. func (m *Model) FieldsEx(fieldNamesOrMapStruct ...interface{}) *Model {
  52. length := len(fieldNamesOrMapStruct)
  53. if length == 0 {
  54. return m
  55. }
  56. model := m.getModel()
  57. switch {
  58. case length >= 2:
  59. model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(gconv.Strings(fieldNamesOrMapStruct), true), ",")
  60. return model
  61. case length == 1:
  62. switch r := fieldNamesOrMapStruct[0].(type) {
  63. case string:
  64. model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields([]string{r}, false), ",")
  65. case []string:
  66. model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(r, true), ",")
  67. default:
  68. model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(gutil.Keys(r), true), ",")
  69. }
  70. return model
  71. }
  72. return m
  73. }
  74. // FieldCount formats and appends commonly used field `COUNT(column)` to the select fields of model.
  75. func (m *Model) FieldCount(column string, as ...string) *Model {
  76. asStr := ""
  77. if len(as) > 0 && as[0] != "" {
  78. asStr = fmt.Sprintf(` AS %s`, m.db.GetCore().QuoteWord(as[0]))
  79. }
  80. return m.appendFieldsByStr(fmt.Sprintf(`COUNT(%s)%s`, m.db.GetCore().QuoteWord(column), asStr))
  81. }
  82. // FieldSum formats and appends commonly used field `SUM(column)` to the select fields of model.
  83. func (m *Model) FieldSum(column string, as ...string) *Model {
  84. asStr := ""
  85. if len(as) > 0 && as[0] != "" {
  86. asStr = fmt.Sprintf(` AS %s`, m.db.GetCore().QuoteWord(as[0]))
  87. }
  88. return m.appendFieldsByStr(fmt.Sprintf(`SUM(%s)%s`, m.db.GetCore().QuoteWord(column), asStr))
  89. }
  90. // FieldMin formats and appends commonly used field `MIN(column)` to the select fields of model.
  91. func (m *Model) FieldMin(column string, as ...string) *Model {
  92. asStr := ""
  93. if len(as) > 0 && as[0] != "" {
  94. asStr = fmt.Sprintf(` AS %s`, m.db.GetCore().QuoteWord(as[0]))
  95. }
  96. return m.appendFieldsByStr(fmt.Sprintf(`MIN(%s)%s`, m.db.GetCore().QuoteWord(column), asStr))
  97. }
  98. // FieldMax formats and appends commonly used field `MAX(column)` to the select fields of model.
  99. func (m *Model) FieldMax(column string, as ...string) *Model {
  100. asStr := ""
  101. if len(as) > 0 && as[0] != "" {
  102. asStr = fmt.Sprintf(` AS %s`, m.db.GetCore().QuoteWord(as[0]))
  103. }
  104. return m.appendFieldsByStr(fmt.Sprintf(`MAX(%s)%s`, m.db.GetCore().QuoteWord(column), asStr))
  105. }
  106. // FieldAvg formats and appends commonly used field `AVG(column)` to the select fields of model.
  107. func (m *Model) FieldAvg(column string, as ...string) *Model {
  108. asStr := ""
  109. if len(as) > 0 && as[0] != "" {
  110. asStr = fmt.Sprintf(` AS %s`, m.db.GetCore().QuoteWord(as[0]))
  111. }
  112. return m.appendFieldsByStr(fmt.Sprintf(`AVG(%s)%s`, m.db.GetCore().QuoteWord(column), asStr))
  113. }
  114. func (m *Model) appendFieldsByStr(fields string) *Model {
  115. if fields != "" {
  116. model := m.getModel()
  117. if model.fields == defaultFields {
  118. model.fields = ""
  119. }
  120. if model.fields != "" {
  121. model.fields += ","
  122. }
  123. model.fields += fields
  124. return model
  125. }
  126. return m
  127. }
  128. func (m *Model) appendFieldsExByStr(fieldsEx string) *Model {
  129. if fieldsEx != "" {
  130. model := m.getModel()
  131. if model.fieldsEx != "" {
  132. model.fieldsEx += ","
  133. }
  134. model.fieldsEx += fieldsEx
  135. return model
  136. }
  137. return m
  138. }
  139. // Filter marks filtering the fields which does not exist in the fields of the operated table.
  140. // Note that this function supports only single table operations.
  141. // Deprecated, filter feature is automatically enabled from GoFrame v1.16.0, it is so no longer used.
  142. func (m *Model) Filter() *Model {
  143. if gstr.Contains(m.tables, " ") {
  144. panic("function Filter supports only single table operations")
  145. }
  146. model := m.getModel()
  147. model.filter = true
  148. return model
  149. }
  150. // FieldsStr retrieves and returns all fields from the table, joined with char ','.
  151. // The optional parameter `prefix` specifies the prefix for each field, eg: FieldsStr("u.").
  152. // Deprecated, use GetFieldsStr instead.
  153. func (m *Model) FieldsStr(prefix ...string) string {
  154. return m.GetFieldsStr(prefix...)
  155. }
  156. // GetFieldsStr retrieves and returns all fields from the table, joined with char ','.
  157. // The optional parameter `prefix` specifies the prefix for each field, eg: GetFieldsStr("u.").
  158. func (m *Model) GetFieldsStr(prefix ...string) string {
  159. prefixStr := ""
  160. if len(prefix) > 0 {
  161. prefixStr = prefix[0]
  162. }
  163. tableFields, err := m.TableFields(m.tablesInit)
  164. if err != nil {
  165. panic(err)
  166. }
  167. if len(tableFields) == 0 {
  168. panic(fmt.Sprintf(`empty table fields for table "%s"`, m.tables))
  169. }
  170. fieldsArray := make([]string, len(tableFields))
  171. for k, v := range tableFields {
  172. fieldsArray[v.Index] = k
  173. }
  174. newFields := ""
  175. for _, k := range fieldsArray {
  176. if len(newFields) > 0 {
  177. newFields += ","
  178. }
  179. newFields += prefixStr + k
  180. }
  181. newFields = m.db.GetCore().QuoteString(newFields)
  182. return newFields
  183. }
  184. // FieldsExStr retrieves and returns fields which are not in parameter `fields` from the table,
  185. // joined with char ','.
  186. // The parameter `fields` specifies the fields that are excluded.
  187. // The optional parameter `prefix` specifies the prefix for each field, eg: FieldsExStr("id", "u.").
  188. // Deprecated, use GetFieldsExStr instead.
  189. func (m *Model) FieldsExStr(fields string, prefix ...string) string {
  190. return m.GetFieldsExStr(fields, prefix...)
  191. }
  192. // GetFieldsExStr retrieves and returns fields which are not in parameter `fields` from the table,
  193. // joined with char ','.
  194. // The parameter `fields` specifies the fields that are excluded.
  195. // The optional parameter `prefix` specifies the prefix for each field, eg: FieldsExStr("id", "u.").
  196. func (m *Model) GetFieldsExStr(fields string, prefix ...string) string {
  197. prefixStr := ""
  198. if len(prefix) > 0 {
  199. prefixStr = prefix[0]
  200. }
  201. tableFields, err := m.TableFields(m.tablesInit)
  202. if err != nil {
  203. panic(err)
  204. }
  205. if len(tableFields) == 0 {
  206. panic(fmt.Sprintf(`empty table fields for table "%s"`, m.tables))
  207. }
  208. fieldsExSet := gset.NewStrSetFrom(gstr.SplitAndTrim(fields, ","))
  209. fieldsArray := make([]string, len(tableFields))
  210. for k, v := range tableFields {
  211. fieldsArray[v.Index] = k
  212. }
  213. newFields := ""
  214. for _, k := range fieldsArray {
  215. if fieldsExSet.Contains(k) {
  216. continue
  217. }
  218. if len(newFields) > 0 {
  219. newFields += ","
  220. }
  221. newFields += prefixStr + k
  222. }
  223. newFields = m.db.GetCore().QuoteString(newFields)
  224. return newFields
  225. }
  226. // HasField determine whether the field exists in the table.
  227. func (m *Model) HasField(field string) (bool, error) {
  228. tableFields, err := m.TableFields(m.tablesInit)
  229. if err != nil {
  230. return false, err
  231. }
  232. if len(tableFields) == 0 {
  233. return false, fmt.Errorf(`empty table fields for table "%s"`, m.tables)
  234. }
  235. fieldsArray := make([]string, len(tableFields))
  236. for k, v := range tableFields {
  237. fieldsArray[v.Index] = k
  238. }
  239. for _, f := range fieldsArray {
  240. if f == field {
  241. return true, nil
  242. }
  243. }
  244. return false, nil
  245. }