engine_cond.go 6.0 KB

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