db_utils.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package orm
  15. import (
  16. "fmt"
  17. "reflect"
  18. "time"
  19. )
  20. // get table alias.
  21. func getDbAlias(name string) *alias {
  22. if al, ok := dataBaseCache.get(name); ok {
  23. return al
  24. }
  25. panic(fmt.Errorf("unknown DataBase alias name %s", name))
  26. }
  27. // get pk column info.
  28. func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interface{}, exist bool) {
  29. fi := mi.fields.pk
  30. v := ind.FieldByIndex(fi.fieldIndex)
  31. if fi.fieldType&IsPositiveIntegerField > 0 {
  32. vu := v.Uint()
  33. exist = vu > 0
  34. value = vu
  35. } else if fi.fieldType&IsIntegerField > 0 {
  36. vu := v.Int()
  37. exist = true
  38. value = vu
  39. } else if fi.fieldType&IsRelField > 0 {
  40. _, value, exist = getExistPk(fi.relModelInfo, reflect.Indirect(v))
  41. } else {
  42. vu := v.String()
  43. exist = vu != ""
  44. value = vu
  45. }
  46. column = fi.column
  47. return
  48. }
  49. // get fields description as flatted string.
  50. func getFlatParams(fi *fieldInfo, args []interface{}, tz *time.Location) (params []interface{}) {
  51. outFor:
  52. for _, arg := range args {
  53. val := reflect.ValueOf(arg)
  54. if arg == nil {
  55. params = append(params, arg)
  56. continue
  57. }
  58. kind := val.Kind()
  59. if kind == reflect.Ptr {
  60. val = val.Elem()
  61. kind = val.Kind()
  62. arg = val.Interface()
  63. }
  64. switch kind {
  65. case reflect.String:
  66. v := val.String()
  67. if fi != nil {
  68. if fi.fieldType == TypeTimeField || fi.fieldType == TypeDateField || fi.fieldType == TypeDateTimeField {
  69. var t time.Time
  70. var err error
  71. if len(v) >= 19 {
  72. s := v[:19]
  73. t, err = time.ParseInLocation(formatDateTime, s, DefaultTimeLoc)
  74. } else if len(v) >= 10 {
  75. s := v
  76. if len(v) > 10 {
  77. s = v[:10]
  78. }
  79. t, err = time.ParseInLocation(formatDate, s, tz)
  80. } else {
  81. s := v
  82. if len(s) > 8 {
  83. s = v[:8]
  84. }
  85. t, err = time.ParseInLocation(formatTime, s, tz)
  86. }
  87. if err == nil {
  88. if fi.fieldType == TypeDateField {
  89. v = t.In(tz).Format(formatDate)
  90. } else if fi.fieldType == TypeDateTimeField {
  91. v = t.In(tz).Format(formatDateTime)
  92. } else {
  93. v = t.In(tz).Format(formatTime)
  94. }
  95. }
  96. }
  97. }
  98. arg = v
  99. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  100. arg = val.Int()
  101. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  102. arg = val.Uint()
  103. case reflect.Float32:
  104. arg, _ = StrTo(ToStr(arg)).Float64()
  105. case reflect.Float64:
  106. arg = val.Float()
  107. case reflect.Bool:
  108. arg = val.Bool()
  109. case reflect.Slice, reflect.Array:
  110. if _, ok := arg.([]byte); ok {
  111. continue outFor
  112. }
  113. var args []interface{}
  114. for i := 0; i < val.Len(); i++ {
  115. v := val.Index(i)
  116. var vu interface{}
  117. if v.CanInterface() {
  118. vu = v.Interface()
  119. }
  120. if vu == nil {
  121. continue
  122. }
  123. args = append(args, vu)
  124. }
  125. if len(args) > 0 {
  126. p := getFlatParams(fi, args, tz)
  127. params = append(params, p...)
  128. }
  129. continue outFor
  130. case reflect.Struct:
  131. if v, ok := arg.(time.Time); ok {
  132. if fi != nil && fi.fieldType == TypeDateField {
  133. arg = v.In(tz).Format(formatDate)
  134. } else if fi != nil && fi.fieldType == TypeDateTimeField {
  135. arg = v.In(tz).Format(formatDateTime)
  136. } else if fi != nil && fi.fieldType == TypeTimeField {
  137. arg = v.In(tz).Format(formatTime)
  138. } else {
  139. arg = v.In(tz).Format(formatDateTime)
  140. }
  141. } else {
  142. typ := val.Type()
  143. name := getFullName(typ)
  144. var value interface{}
  145. if mmi, ok := modelCache.getByFullName(name); ok {
  146. if _, vu, exist := getExistPk(mmi, val); exist {
  147. value = vu
  148. }
  149. }
  150. arg = value
  151. if arg == nil {
  152. panic(fmt.Errorf("need a valid args value, unknown table or value `%s`", name))
  153. }
  154. }
  155. }
  156. params = append(params, arg)
  157. }
  158. return
  159. }