orm.go 5.8 KB

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