orm.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. } else {
  97. options := strings.Split(key, ",")
  98. return options[0]
  99. }
  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. } else {
  123. return ErrNotSettable
  124. }
  125. case reflect.Struct:
  126. columns, err := scanner.Columns()
  127. if err != nil {
  128. return err
  129. }
  130. if values, err := mapStructFieldsIntoSlice(rve, columns, strict); err != nil {
  131. return err
  132. } else {
  133. return scanner.Scan(values...)
  134. }
  135. default:
  136. return ErrUnsupportedValueType
  137. }
  138. }
  139. func unmarshalRows(v interface{}, scanner rowsScanner, strict bool) error {
  140. rv := reflect.ValueOf(v)
  141. if err := mapping.ValidatePtr(&rv); err != nil {
  142. return err
  143. }
  144. rt := reflect.TypeOf(v)
  145. rte := rt.Elem()
  146. rve := rv.Elem()
  147. switch rte.Kind() {
  148. case reflect.Slice:
  149. if rve.CanSet() {
  150. ptr := rte.Elem().Kind() == reflect.Ptr
  151. appendFn := func(item reflect.Value) {
  152. if ptr {
  153. rve.Set(reflect.Append(rve, item))
  154. } else {
  155. rve.Set(reflect.Append(rve, reflect.Indirect(item)))
  156. }
  157. }
  158. fillFn := func(value interface{}) error {
  159. if rve.CanSet() {
  160. if err := scanner.Scan(value); err != nil {
  161. return err
  162. } else {
  163. appendFn(reflect.ValueOf(value))
  164. return nil
  165. }
  166. }
  167. return ErrNotSettable
  168. }
  169. base := mapping.Deref(rte.Elem())
  170. switch base.Kind() {
  171. case reflect.Bool,
  172. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  173. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
  174. reflect.Float32, reflect.Float64,
  175. reflect.String:
  176. for scanner.Next() {
  177. value := reflect.New(base)
  178. if err := fillFn(value.Interface()); err != nil {
  179. return err
  180. }
  181. }
  182. case reflect.Struct:
  183. columns, err := scanner.Columns()
  184. if err != nil {
  185. return err
  186. }
  187. for scanner.Next() {
  188. value := reflect.New(base)
  189. if values, err := mapStructFieldsIntoSlice(value, columns, strict); err != nil {
  190. return err
  191. } else {
  192. if err := scanner.Scan(values...); err != nil {
  193. return err
  194. } else {
  195. appendFn(value)
  196. }
  197. }
  198. }
  199. default:
  200. return ErrUnsupportedValueType
  201. }
  202. return nil
  203. } else {
  204. return ErrNotSettable
  205. }
  206. default:
  207. return ErrUnsupportedValueType
  208. }
  209. }
  210. func unwrapFields(v reflect.Value) []reflect.Value {
  211. var fields []reflect.Value
  212. indirect := reflect.Indirect(v)
  213. for i := 0; i < indirect.NumField(); i++ {
  214. child := indirect.Field(i)
  215. if child.Kind() == reflect.Ptr && child.IsNil() {
  216. baseValueType := mapping.Deref(child.Type())
  217. child.Set(reflect.New(baseValueType))
  218. }
  219. child = reflect.Indirect(child)
  220. childType := indirect.Type().Field(i)
  221. if child.Kind() == reflect.Struct && childType.Anonymous {
  222. fields = append(fields, unwrapFields(child)...)
  223. } else {
  224. fields = append(fields, child)
  225. }
  226. }
  227. return fields
  228. }