orm.go 6.1 KB

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