engine_cond.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright 2017 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "database/sql/driver"
  7. "encoding/json"
  8. "fmt"
  9. "reflect"
  10. "strings"
  11. "time"
  12. "github.com/go-xorm/builder"
  13. "github.com/go-xorm/core"
  14. )
  15. func (engine *Engine) buildConds(table *core.Table, bean interface{},
  16. includeVersion bool, includeUpdated bool, includeNil bool,
  17. includeAutoIncr bool, allUseBool bool, useAllCols bool, unscoped bool,
  18. mustColumnMap map[string]bool, tableName, aliasName string, addedTableName bool) (builder.Cond, error) {
  19. var conds []builder.Cond
  20. for _, col := range table.Columns() {
  21. if !includeVersion && col.IsVersion {
  22. continue
  23. }
  24. if !includeUpdated && col.IsUpdated {
  25. continue
  26. }
  27. if !includeAutoIncr && col.IsAutoIncrement {
  28. continue
  29. }
  30. if engine.dialect.DBType() == core.MSSQL && (col.SQLType.Name == core.Text || col.SQLType.IsBlob() || col.SQLType.Name == core.TimeStampz) {
  31. continue
  32. }
  33. if col.SQLType.IsJson() {
  34. continue
  35. }
  36. var colName string
  37. if addedTableName {
  38. var nm = tableName
  39. if len(aliasName) > 0 {
  40. nm = aliasName
  41. }
  42. colName = engine.Quote(nm) + "." + engine.Quote(col.Name)
  43. } else {
  44. colName = engine.Quote(col.Name)
  45. }
  46. fieldValuePtr, err := col.ValueOf(bean)
  47. if err != nil {
  48. if !strings.Contains(err.Error(), "is not valid") {
  49. engine.logger.Warn(err)
  50. }
  51. continue
  52. }
  53. if col.IsDeleted && !unscoped { // tag "deleted" is enabled
  54. conds = append(conds, engine.CondDeleted(colName))
  55. }
  56. fieldValue := *fieldValuePtr
  57. if fieldValue.Interface() == nil {
  58. continue
  59. }
  60. fieldType := reflect.TypeOf(fieldValue.Interface())
  61. requiredField := useAllCols
  62. if b, ok := getFlagForColumn(mustColumnMap, col); ok {
  63. if b {
  64. requiredField = true
  65. } else {
  66. continue
  67. }
  68. }
  69. if fieldType.Kind() == reflect.Ptr {
  70. if fieldValue.IsNil() {
  71. if includeNil {
  72. conds = append(conds, builder.Eq{colName: nil})
  73. }
  74. continue
  75. } else if !fieldValue.IsValid() {
  76. continue
  77. } else {
  78. // dereference ptr type to instance type
  79. fieldValue = fieldValue.Elem()
  80. fieldType = reflect.TypeOf(fieldValue.Interface())
  81. requiredField = true
  82. }
  83. }
  84. var val interface{}
  85. switch fieldType.Kind() {
  86. case reflect.Bool:
  87. if allUseBool || requiredField {
  88. val = fieldValue.Interface()
  89. } else {
  90. // if a bool in a struct, it will not be as a condition because it default is false,
  91. // please use Where() instead
  92. continue
  93. }
  94. case reflect.String:
  95. if !requiredField && fieldValue.String() == "" {
  96. continue
  97. }
  98. // for MyString, should convert to string or panic
  99. if fieldType.String() != reflect.String.String() {
  100. val = fieldValue.String()
  101. } else {
  102. val = fieldValue.Interface()
  103. }
  104. case reflect.Int8, reflect.Int16, reflect.Int, reflect.Int32, reflect.Int64:
  105. if !requiredField && fieldValue.Int() == 0 {
  106. continue
  107. }
  108. val = fieldValue.Interface()
  109. case reflect.Float32, reflect.Float64:
  110. if !requiredField && fieldValue.Float() == 0.0 {
  111. continue
  112. }
  113. val = fieldValue.Interface()
  114. case reflect.Uint8, reflect.Uint16, reflect.Uint, reflect.Uint32, reflect.Uint64:
  115. if !requiredField && fieldValue.Uint() == 0 {
  116. continue
  117. }
  118. t := int64(fieldValue.Uint())
  119. val = reflect.ValueOf(&t).Interface()
  120. case reflect.Struct:
  121. if fieldType.ConvertibleTo(core.TimeType) {
  122. t := fieldValue.Convert(core.TimeType).Interface().(time.Time)
  123. if !requiredField && (t.IsZero() || !fieldValue.IsValid()) {
  124. continue
  125. }
  126. val = engine.formatColTime(col, t)
  127. } else if _, ok := reflect.New(fieldType).Interface().(core.Conversion); ok {
  128. continue
  129. } else if valNul, ok := fieldValue.Interface().(driver.Valuer); ok {
  130. val, _ = valNul.Value()
  131. if val == nil {
  132. continue
  133. }
  134. } else {
  135. if col.SQLType.IsJson() {
  136. if col.SQLType.IsText() {
  137. bytes, err := json.Marshal(fieldValue.Interface())
  138. if err != nil {
  139. engine.logger.Error(err)
  140. continue
  141. }
  142. val = string(bytes)
  143. } else if col.SQLType.IsBlob() {
  144. var bytes []byte
  145. var err error
  146. bytes, err = json.Marshal(fieldValue.Interface())
  147. if err != nil {
  148. engine.logger.Error(err)
  149. continue
  150. }
  151. val = bytes
  152. }
  153. } else {
  154. engine.autoMapType(fieldValue)
  155. if table, ok := engine.Tables[fieldValue.Type()]; ok {
  156. if len(table.PrimaryKeys) == 1 {
  157. pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName)
  158. // fix non-int pk issues
  159. //if pkField.Int() != 0 {
  160. if pkField.IsValid() && !isZero(pkField.Interface()) {
  161. val = pkField.Interface()
  162. } else {
  163. continue
  164. }
  165. } else {
  166. //TODO: how to handler?
  167. return nil, fmt.Errorf("not supported %v as %v", fieldValue.Interface(), table.PrimaryKeys)
  168. }
  169. } else {
  170. val = fieldValue.Interface()
  171. }
  172. }
  173. }
  174. case reflect.Array:
  175. continue
  176. case reflect.Slice, reflect.Map:
  177. if fieldValue == reflect.Zero(fieldType) {
  178. continue
  179. }
  180. if fieldValue.IsNil() || !fieldValue.IsValid() || fieldValue.Len() == 0 {
  181. continue
  182. }
  183. if col.SQLType.IsText() {
  184. bytes, err := json.Marshal(fieldValue.Interface())
  185. if err != nil {
  186. engine.logger.Error(err)
  187. continue
  188. }
  189. val = string(bytes)
  190. } else if col.SQLType.IsBlob() {
  191. var bytes []byte
  192. var err error
  193. if (fieldType.Kind() == reflect.Array || fieldType.Kind() == reflect.Slice) &&
  194. fieldType.Elem().Kind() == reflect.Uint8 {
  195. if fieldValue.Len() > 0 {
  196. val = fieldValue.Bytes()
  197. } else {
  198. continue
  199. }
  200. } else {
  201. bytes, err = json.Marshal(fieldValue.Interface())
  202. if err != nil {
  203. engine.logger.Error(err)
  204. continue
  205. }
  206. val = bytes
  207. }
  208. } else {
  209. continue
  210. }
  211. default:
  212. val = fieldValue.Interface()
  213. }
  214. conds = append(conds, builder.Eq{colName: val})
  215. }
  216. return builder.And(conds...), nil
  217. }