rows.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. package core
  2. import (
  3. "database/sql"
  4. "errors"
  5. "reflect"
  6. "sync"
  7. )
  8. type Rows struct {
  9. *sql.Rows
  10. Mapper IMapper
  11. }
  12. func (rs *Rows) ToMapString() ([]map[string]string, error) {
  13. cols, err := rs.Columns()
  14. if err != nil {
  15. return nil, err
  16. }
  17. var results = make([]map[string]string, 0, 10)
  18. for rs.Next() {
  19. var record = make(map[string]string, len(cols))
  20. err = rs.ScanMap(&record)
  21. if err != nil {
  22. return nil, err
  23. }
  24. results = append(results, record)
  25. }
  26. return results, nil
  27. }
  28. // scan data to a struct's pointer according field index
  29. func (rs *Rows) ScanStructByIndex(dest ...interface{}) error {
  30. if len(dest) == 0 {
  31. return errors.New("at least one struct")
  32. }
  33. vvvs := make([]reflect.Value, len(dest))
  34. for i, s := range dest {
  35. vv := reflect.ValueOf(s)
  36. if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct {
  37. return errors.New("dest should be a struct's pointer")
  38. }
  39. vvvs[i] = vv.Elem()
  40. }
  41. cols, err := rs.Columns()
  42. if err != nil {
  43. return err
  44. }
  45. newDest := make([]interface{}, len(cols))
  46. var i = 0
  47. for _, vvv := range vvvs {
  48. for j := 0; j < vvv.NumField(); j++ {
  49. newDest[i] = vvv.Field(j).Addr().Interface()
  50. i = i + 1
  51. }
  52. }
  53. return rs.Rows.Scan(newDest...)
  54. }
  55. var (
  56. fieldCache = make(map[reflect.Type]map[string]int)
  57. fieldCacheMutex sync.RWMutex
  58. )
  59. func fieldByName(v reflect.Value, name string) reflect.Value {
  60. t := v.Type()
  61. fieldCacheMutex.RLock()
  62. cache, ok := fieldCache[t]
  63. fieldCacheMutex.RUnlock()
  64. if !ok {
  65. cache = make(map[string]int)
  66. for i := 0; i < v.NumField(); i++ {
  67. cache[t.Field(i).Name] = i
  68. }
  69. fieldCacheMutex.Lock()
  70. fieldCache[t] = cache
  71. fieldCacheMutex.Unlock()
  72. }
  73. if i, ok := cache[name]; ok {
  74. return v.Field(i)
  75. }
  76. return reflect.Zero(t)
  77. }
  78. // scan data to a struct's pointer according field name
  79. func (rs *Rows) ScanStructByName(dest interface{}) error {
  80. vv := reflect.ValueOf(dest)
  81. if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct {
  82. return errors.New("dest should be a struct's pointer")
  83. }
  84. cols, err := rs.Columns()
  85. if err != nil {
  86. return err
  87. }
  88. newDest := make([]interface{}, len(cols))
  89. var v EmptyScanner
  90. for j, name := range cols {
  91. f := fieldByName(vv.Elem(), rs.Mapper.Table2Obj(name))
  92. if f.IsValid() {
  93. newDest[j] = f.Addr().Interface()
  94. } else {
  95. newDest[j] = &v
  96. }
  97. }
  98. return rs.Rows.Scan(newDest...)
  99. }
  100. type cacheStruct struct {
  101. value reflect.Value
  102. idx int
  103. }
  104. var (
  105. reflectCache = make(map[reflect.Type]*cacheStruct)
  106. reflectCacheMutex sync.RWMutex
  107. )
  108. func ReflectNew(typ reflect.Type) reflect.Value {
  109. reflectCacheMutex.RLock()
  110. cs, ok := reflectCache[typ]
  111. reflectCacheMutex.RUnlock()
  112. const newSize = 200
  113. if !ok || cs.idx+1 > newSize-1 {
  114. cs = &cacheStruct{reflect.MakeSlice(reflect.SliceOf(typ), newSize, newSize), 0}
  115. reflectCacheMutex.Lock()
  116. reflectCache[typ] = cs
  117. reflectCacheMutex.Unlock()
  118. } else {
  119. reflectCacheMutex.Lock()
  120. cs.idx = cs.idx + 1
  121. reflectCacheMutex.Unlock()
  122. }
  123. return cs.value.Index(cs.idx).Addr()
  124. }
  125. // scan data to a slice's pointer, slice's length should equal to columns' number
  126. func (rs *Rows) ScanSlice(dest interface{}) error {
  127. vv := reflect.ValueOf(dest)
  128. if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Slice {
  129. return errors.New("dest should be a slice's pointer")
  130. }
  131. vvv := vv.Elem()
  132. cols, err := rs.Columns()
  133. if err != nil {
  134. return err
  135. }
  136. newDest := make([]interface{}, len(cols))
  137. for j := 0; j < len(cols); j++ {
  138. if j >= vvv.Len() {
  139. newDest[j] = reflect.New(vvv.Type().Elem()).Interface()
  140. } else {
  141. newDest[j] = vvv.Index(j).Addr().Interface()
  142. }
  143. }
  144. err = rs.Rows.Scan(newDest...)
  145. if err != nil {
  146. return err
  147. }
  148. srcLen := vvv.Len()
  149. for i := srcLen; i < len(cols); i++ {
  150. vvv = reflect.Append(vvv, reflect.ValueOf(newDest[i]).Elem())
  151. }
  152. return nil
  153. }
  154. // scan data to a map's pointer
  155. func (rs *Rows) ScanMap(dest interface{}) error {
  156. vv := reflect.ValueOf(dest)
  157. if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
  158. return errors.New("dest should be a map's pointer")
  159. }
  160. cols, err := rs.Columns()
  161. if err != nil {
  162. return err
  163. }
  164. newDest := make([]interface{}, len(cols))
  165. vvv := vv.Elem()
  166. for i, _ := range cols {
  167. newDest[i] = ReflectNew(vvv.Type().Elem()).Interface()
  168. //v := reflect.New(vvv.Type().Elem())
  169. //newDest[i] = v.Interface()
  170. }
  171. err = rs.Rows.Scan(newDest...)
  172. if err != nil {
  173. return err
  174. }
  175. for i, name := range cols {
  176. vname := reflect.ValueOf(name)
  177. vvv.SetMapIndex(vname, reflect.ValueOf(newDest[i]).Elem())
  178. }
  179. return nil
  180. }
  181. /*func (rs *Rows) ScanMap(dest interface{}) error {
  182. vv := reflect.ValueOf(dest)
  183. if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
  184. return errors.New("dest should be a map's pointer")
  185. }
  186. cols, err := rs.Columns()
  187. if err != nil {
  188. return err
  189. }
  190. newDest := make([]interface{}, len(cols))
  191. err = rs.ScanSlice(newDest)
  192. if err != nil {
  193. return err
  194. }
  195. vvv := vv.Elem()
  196. for i, name := range cols {
  197. vname := reflect.ValueOf(name)
  198. vvv.SetMapIndex(vname, reflect.ValueOf(newDest[i]).Elem())
  199. }
  200. return nil
  201. }*/
  202. type Row struct {
  203. rows *Rows
  204. // One of these two will be non-nil:
  205. err error // deferred error for easy chaining
  206. }
  207. func (row *Row) Columns() ([]string, error) {
  208. if row.err != nil {
  209. return nil, row.err
  210. }
  211. return row.rows.Columns()
  212. }
  213. func (row *Row) Scan(dest ...interface{}) error {
  214. if row.err != nil {
  215. return row.err
  216. }
  217. defer row.rows.Close()
  218. for _, dp := range dest {
  219. if _, ok := dp.(*sql.RawBytes); ok {
  220. return errors.New("sql: RawBytes isn't allowed on Row.Scan")
  221. }
  222. }
  223. if !row.rows.Next() {
  224. if err := row.rows.Err(); err != nil {
  225. return err
  226. }
  227. return sql.ErrNoRows
  228. }
  229. err := row.rows.Scan(dest...)
  230. if err != nil {
  231. return err
  232. }
  233. // Make sure the query can be processed to completion with no errors.
  234. return row.rows.Close()
  235. }
  236. func (row *Row) ScanStructByName(dest interface{}) error {
  237. if row.err != nil {
  238. return row.err
  239. }
  240. defer row.rows.Close()
  241. if !row.rows.Next() {
  242. if err := row.rows.Err(); err != nil {
  243. return err
  244. }
  245. return sql.ErrNoRows
  246. }
  247. err := row.rows.ScanStructByName(dest)
  248. if err != nil {
  249. return err
  250. }
  251. // Make sure the query can be processed to completion with no errors.
  252. return row.rows.Close()
  253. }
  254. func (row *Row) ScanStructByIndex(dest interface{}) error {
  255. if row.err != nil {
  256. return row.err
  257. }
  258. defer row.rows.Close()
  259. if !row.rows.Next() {
  260. if err := row.rows.Err(); err != nil {
  261. return err
  262. }
  263. return sql.ErrNoRows
  264. }
  265. err := row.rows.ScanStructByIndex(dest)
  266. if err != nil {
  267. return err
  268. }
  269. // Make sure the query can be processed to completion with no errors.
  270. return row.rows.Close()
  271. }
  272. // scan data to a slice's pointer, slice's length should equal to columns' number
  273. func (row *Row) ScanSlice(dest interface{}) error {
  274. if row.err != nil {
  275. return row.err
  276. }
  277. defer row.rows.Close()
  278. if !row.rows.Next() {
  279. if err := row.rows.Err(); err != nil {
  280. return err
  281. }
  282. return sql.ErrNoRows
  283. }
  284. err := row.rows.ScanSlice(dest)
  285. if err != nil {
  286. return err
  287. }
  288. // Make sure the query can be processed to completion with no errors.
  289. return row.rows.Close()
  290. }
  291. // scan data to a map's pointer
  292. func (row *Row) ScanMap(dest interface{}) error {
  293. if row.err != nil {
  294. return row.err
  295. }
  296. defer row.rows.Close()
  297. if !row.rows.Next() {
  298. if err := row.rows.Err(); err != nil {
  299. return err
  300. }
  301. return sql.ErrNoRows
  302. }
  303. err := row.rows.ScanMap(dest)
  304. if err != nil {
  305. return err
  306. }
  307. // Make sure the query can be processed to completion with no errors.
  308. return row.rows.Close()
  309. }
  310. func (row *Row) ToMapString() (map[string]string, error) {
  311. cols, err := row.Columns()
  312. if err != nil {
  313. return nil, err
  314. }
  315. var record = make(map[string]string, len(cols))
  316. err = row.ScanMap(&record)
  317. if err != nil {
  318. return nil, err
  319. }
  320. return record, nil
  321. }