rows.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. // ErrorRow return an error row
  208. func ErrorRow(err error) *Row {
  209. return &Row{
  210. err: err,
  211. }
  212. }
  213. // NewRow from rows
  214. func NewRow(rows *Rows, err error) *Row {
  215. return &Row{rows, err}
  216. }
  217. func (row *Row) Columns() ([]string, error) {
  218. if row.err != nil {
  219. return nil, row.err
  220. }
  221. return row.rows.Columns()
  222. }
  223. func (row *Row) Scan(dest ...interface{}) error {
  224. if row.err != nil {
  225. return row.err
  226. }
  227. defer row.rows.Close()
  228. for _, dp := range dest {
  229. if _, ok := dp.(*sql.RawBytes); ok {
  230. return errors.New("sql: RawBytes isn't allowed on Row.Scan")
  231. }
  232. }
  233. if !row.rows.Next() {
  234. if err := row.rows.Err(); err != nil {
  235. return err
  236. }
  237. return sql.ErrNoRows
  238. }
  239. err := row.rows.Scan(dest...)
  240. if err != nil {
  241. return err
  242. }
  243. // Make sure the query can be processed to completion with no errors.
  244. return row.rows.Close()
  245. }
  246. func (row *Row) ScanStructByName(dest interface{}) error {
  247. if row.err != nil {
  248. return row.err
  249. }
  250. defer row.rows.Close()
  251. if !row.rows.Next() {
  252. if err := row.rows.Err(); err != nil {
  253. return err
  254. }
  255. return sql.ErrNoRows
  256. }
  257. err := row.rows.ScanStructByName(dest)
  258. if err != nil {
  259. return err
  260. }
  261. // Make sure the query can be processed to completion with no errors.
  262. return row.rows.Close()
  263. }
  264. func (row *Row) ScanStructByIndex(dest interface{}) error {
  265. if row.err != nil {
  266. return row.err
  267. }
  268. defer row.rows.Close()
  269. if !row.rows.Next() {
  270. if err := row.rows.Err(); err != nil {
  271. return err
  272. }
  273. return sql.ErrNoRows
  274. }
  275. err := row.rows.ScanStructByIndex(dest)
  276. if err != nil {
  277. return err
  278. }
  279. // Make sure the query can be processed to completion with no errors.
  280. return row.rows.Close()
  281. }
  282. // scan data to a slice's pointer, slice's length should equal to columns' number
  283. func (row *Row) ScanSlice(dest interface{}) error {
  284. if row.err != nil {
  285. return row.err
  286. }
  287. defer row.rows.Close()
  288. if !row.rows.Next() {
  289. if err := row.rows.Err(); err != nil {
  290. return err
  291. }
  292. return sql.ErrNoRows
  293. }
  294. err := row.rows.ScanSlice(dest)
  295. if err != nil {
  296. return err
  297. }
  298. // Make sure the query can be processed to completion with no errors.
  299. return row.rows.Close()
  300. }
  301. // scan data to a map's pointer
  302. func (row *Row) ScanMap(dest interface{}) error {
  303. if row.err != nil {
  304. return row.err
  305. }
  306. defer row.rows.Close()
  307. if !row.rows.Next() {
  308. if err := row.rows.Err(); err != nil {
  309. return err
  310. }
  311. return sql.ErrNoRows
  312. }
  313. err := row.rows.ScanMap(dest)
  314. if err != nil {
  315. return err
  316. }
  317. // Make sure the query can be processed to completion with no errors.
  318. return row.rows.Close()
  319. }
  320. func (row *Row) ToMapString() (map[string]string, error) {
  321. cols, err := row.Columns()
  322. if err != nil {
  323. return nil, err
  324. }
  325. var record = make(map[string]string, len(cols))
  326. err = row.ScanMap(&record)
  327. if err != nil {
  328. return nil, err
  329. }
  330. return record, nil
  331. }