orm.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // copy from core/stores/sqlx/orm.go
  2. package mocksql
  3. import (
  4. "errors"
  5. "reflect"
  6. "strings"
  7. "github.com/tal-tech/go-zero/core/mapping"
  8. )
  9. const tagName = "db"
  10. var (
  11. ErrNotMatchDestination = errors.New("not matching destination to scan")
  12. ErrNotReadableValue = errors.New("value not addressable or interfaceable")
  13. ErrNotSettable = errors.New("passed in variable is not settable")
  14. ErrUnsupportedValueType = errors.New("unsupported unmarshal type")
  15. )
  16. type rowsScanner interface {
  17. Columns() ([]string, error)
  18. Err() error
  19. Next() bool
  20. Scan(v ...interface{}) error
  21. }
  22. func getTaggedFieldValueMap(v reflect.Value) (map[string]interface{}, error) {
  23. rt := mapping.Deref(v.Type())
  24. size := rt.NumField()
  25. result := make(map[string]interface{}, size)
  26. for i := 0; i < size; i++ {
  27. key := parseTagName(rt.Field(i))
  28. if len(key) == 0 {
  29. return nil, nil
  30. }
  31. valueField := reflect.Indirect(v).Field(i)
  32. switch valueField.Kind() {
  33. case reflect.Ptr:
  34. if !valueField.CanInterface() {
  35. return nil, ErrNotReadableValue
  36. }
  37. if valueField.IsNil() {
  38. baseValueType := mapping.Deref(valueField.Type())
  39. valueField.Set(reflect.New(baseValueType))
  40. }
  41. result[key] = valueField.Interface()
  42. default:
  43. if !valueField.CanAddr() || !valueField.Addr().CanInterface() {
  44. return nil, ErrNotReadableValue
  45. }
  46. result[key] = valueField.Addr().Interface()
  47. }
  48. }
  49. return result, nil
  50. }
  51. func mapStructFieldsIntoSlice(v reflect.Value, columns []string, strict bool) ([]interface{}, error) {
  52. fields := unwrapFields(v)
  53. if strict && len(columns) < len(fields) {
  54. return nil, ErrNotMatchDestination
  55. }
  56. taggedMap, err := getTaggedFieldValueMap(v)
  57. if err != nil {
  58. return nil, err
  59. }
  60. values := make([]interface{}, len(columns))
  61. if len(taggedMap) == 0 {
  62. for i := 0; i < len(values); i++ {
  63. valueField := fields[i]
  64. switch valueField.Kind() {
  65. case reflect.Ptr:
  66. if !valueField.CanInterface() {
  67. return nil, ErrNotReadableValue
  68. }
  69. if valueField.IsNil() {
  70. baseValueType := mapping.Deref(valueField.Type())
  71. valueField.Set(reflect.New(baseValueType))
  72. }
  73. values[i] = valueField.Interface()
  74. default:
  75. if !valueField.CanAddr() || !valueField.Addr().CanInterface() {
  76. return nil, ErrNotReadableValue
  77. }
  78. values[i] = valueField.Addr().Interface()
  79. }
  80. }
  81. } else {
  82. for i, column := range columns {
  83. if tagged, ok := taggedMap[column]; ok {
  84. values[i] = tagged
  85. } else {
  86. var anonymous interface{}
  87. values[i] = &anonymous
  88. }
  89. }
  90. }
  91. return values, nil
  92. }
  93. func parseTagName(field reflect.StructField) string {
  94. key := field.Tag.Get(tagName)
  95. if len(key) == 0 {
  96. return ""
  97. }
  98. options := strings.Split(key, ",")
  99. return options[0]
  100. }
  101. func unmarshalRow(v interface{}, scanner rowsScanner, strict bool) error {
  102. if !scanner.Next() {
  103. if err := scanner.Err(); err != nil {
  104. return err
  105. }
  106. return ErrNotFound
  107. }
  108. rv := reflect.ValueOf(v)
  109. if err := mapping.ValidatePtr(&rv); err != nil {
  110. return err
  111. }
  112. rte := reflect.TypeOf(v).Elem()
  113. rve := rv.Elem()
  114. switch rte.Kind() {
  115. case reflect.Bool,
  116. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  117. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
  118. reflect.Float32, reflect.Float64,
  119. reflect.String:
  120. if rve.CanSet() {
  121. return scanner.Scan(v)
  122. }
  123. return ErrNotSettable
  124. case reflect.Struct:
  125. columns, err := scanner.Columns()
  126. if err != nil {
  127. return err
  128. }
  129. values, err := mapStructFieldsIntoSlice(rve, columns, strict)
  130. if err != nil {
  131. return err
  132. }
  133. return scanner.Scan(values...)
  134. default:
  135. return ErrUnsupportedValueType
  136. }
  137. }
  138. func unmarshalRows(v interface{}, scanner rowsScanner, strict bool) error {
  139. rv := reflect.ValueOf(v)
  140. if err := mapping.ValidatePtr(&rv); err != nil {
  141. return err
  142. }
  143. rt := reflect.TypeOf(v)
  144. rte := rt.Elem()
  145. rve := rv.Elem()
  146. switch rte.Kind() {
  147. case reflect.Slice:
  148. if rve.CanSet() {
  149. ptr := rte.Elem().Kind() == reflect.Ptr
  150. appendFn := func(item reflect.Value) {
  151. if ptr {
  152. rve.Set(reflect.Append(rve, item))
  153. } else {
  154. rve.Set(reflect.Append(rve, reflect.Indirect(item)))
  155. }
  156. }
  157. fillFn := func(value interface{}) error {
  158. if rve.CanSet() {
  159. if err := scanner.Scan(value); err != nil {
  160. return err
  161. }
  162. appendFn(reflect.ValueOf(value))
  163. return nil
  164. }
  165. return ErrNotSettable
  166. }
  167. base := mapping.Deref(rte.Elem())
  168. switch base.Kind() {
  169. case reflect.Bool,
  170. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  171. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
  172. reflect.Float32, reflect.Float64,
  173. reflect.String:
  174. for scanner.Next() {
  175. value := reflect.New(base)
  176. if err := fillFn(value.Interface()); err != nil {
  177. return err
  178. }
  179. }
  180. case reflect.Struct:
  181. columns, err := scanner.Columns()
  182. if err != nil {
  183. return err
  184. }
  185. for scanner.Next() {
  186. value := reflect.New(base)
  187. values, err := mapStructFieldsIntoSlice(value, columns, strict)
  188. if err != nil {
  189. return err
  190. }
  191. if err := scanner.Scan(values...); err != nil {
  192. return err
  193. }
  194. appendFn(value)
  195. }
  196. default:
  197. return ErrUnsupportedValueType
  198. }
  199. return nil
  200. }
  201. return ErrNotSettable
  202. default:
  203. return ErrUnsupportedValueType
  204. }
  205. }
  206. func unwrapFields(v reflect.Value) []reflect.Value {
  207. var fields []reflect.Value
  208. indirect := reflect.Indirect(v)
  209. for i := 0; i < indirect.NumField(); i++ {
  210. child := indirect.Field(i)
  211. if child.Kind() == reflect.Ptr && child.IsNil() {
  212. baseValueType := mapping.Deref(child.Type())
  213. child.Set(reflect.New(baseValueType))
  214. }
  215. child = reflect.Indirect(child)
  216. childType := indirect.Type().Field(i)
  217. if child.Kind() == reflect.Struct && childType.Anonymous {
  218. fields = append(fields, unwrapFields(child)...)
  219. } else {
  220. fields = append(fields, child)
  221. }
  222. }
  223. return fields
  224. }