helpers_plus.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package xorm
  2. import (
  3. "fmt"
  4. "reflect"
  5. "time"
  6. "github.com/xormplus/core"
  7. )
  8. func reflect2objectWithDateFormat(rawValue *reflect.Value, dateFormat string) (value interface{}, err error) {
  9. aa := reflect.TypeOf((*rawValue).Interface())
  10. vv := reflect.ValueOf((*rawValue).Interface())
  11. switch aa.Kind() {
  12. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  13. value = vv.Int()
  14. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  15. value = vv.Uint()
  16. case reflect.Float32, reflect.Float64:
  17. value = vv.Float()
  18. case reflect.String:
  19. value = vv.String()
  20. case reflect.Array, reflect.Slice:
  21. switch aa.Elem().Kind() {
  22. case reflect.Uint8:
  23. data := rawValue.Interface().([]byte)
  24. value = string(data)
  25. default:
  26. err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
  27. }
  28. // time type
  29. case reflect.Struct:
  30. if aa.ConvertibleTo(core.TimeType) {
  31. value = vv.Convert(core.TimeType).Interface().(time.Time).Format(dateFormat)
  32. } else {
  33. err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
  34. }
  35. case reflect.Bool:
  36. value = vv.Bool()
  37. case reflect.Complex128, reflect.Complex64:
  38. value = vv.Complex()
  39. /* TODO: unsupported types below
  40. case reflect.Map:
  41. case reflect.Ptr:
  42. case reflect.Uintptr:
  43. case reflect.UnsafePointer:
  44. case reflect.Chan, reflect.Func, reflect.Interface:
  45. */
  46. default:
  47. err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
  48. }
  49. return
  50. }
  51. func value2ObjectWithDateFormat(rawValue *reflect.Value, dateFormat string) (data interface{}, err error) {
  52. data, err = reflect2objectWithDateFormat(rawValue, dateFormat)
  53. if err != nil {
  54. return
  55. }
  56. return
  57. }
  58. func rows2mapObjectsWithDateFormat(rows *core.Rows, dateFormat string) (resultsSlice []map[string]interface{}, err error) {
  59. fields, err := rows.Columns()
  60. if err != nil {
  61. return nil, err
  62. }
  63. for rows.Next() {
  64. result, err := rows2mapObjectWithDateFormat(rows, dateFormat, fields)
  65. if err != nil {
  66. return nil, err
  67. }
  68. resultsSlice = append(resultsSlice, result)
  69. }
  70. return resultsSlice, nil
  71. }
  72. func rows2mapObjectWithDateFormat(rows *core.Rows, dateFormat string, fields []string) (resultsMap map[string]interface{}, err error) {
  73. result := make(map[string]interface{})
  74. scanResultContainers := make([]interface{}, len(fields))
  75. for i := 0; i < len(fields); i++ {
  76. var scanResultContainer interface{}
  77. scanResultContainers[i] = &scanResultContainer
  78. }
  79. if err := rows.Scan(scanResultContainers...); err != nil {
  80. return nil, err
  81. }
  82. for ii, key := range fields {
  83. rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
  84. //if row is null then ignore
  85. if rawValue.Interface() == nil {
  86. continue
  87. }
  88. if data, err := value2ObjectWithDateFormat(&rawValue, dateFormat); err == nil {
  89. result[key] = data
  90. } else {
  91. return nil, err // !nashtsai! REVIEW, should return err or just error log?
  92. }
  93. }
  94. return result, nil
  95. }
  96. func txQueryByMap(tx *core.Tx, sqlStr string, params interface{}) (resultsSlice []map[string]interface{}, err error) {
  97. rows, err := tx.QueryMap(sqlStr, params)
  98. if err != nil {
  99. return nil, err
  100. }
  101. defer rows.Close()
  102. return rows2mapObjects(rows)
  103. }
  104. func txQuery3WithDateFormat(tx *core.Tx, dateFormat string, sqlStr string, params ...interface{}) (resultsSlice []map[string]interface{}, err error) {
  105. rows, err := tx.Query(sqlStr, params...)
  106. if err != nil {
  107. return nil, err
  108. }
  109. defer rows.Close()
  110. return rows2mapObjectsWithDateFormat(rows, dateFormat)
  111. }
  112. func queryByMap(db *core.DB, sqlStr string, params interface{}) (resultsSlice []map[string]interface{}, err error) {
  113. s, err := db.Prepare(sqlStr)
  114. if err != nil {
  115. return nil, err
  116. }
  117. defer s.Close()
  118. rows, err := s.QueryMap(params)
  119. if err != nil {
  120. return nil, err
  121. }
  122. defer rows.Close()
  123. return rows2mapObjects(rows)
  124. }
  125. func query3WithDateFormat(db *core.DB, dateFormat string, sqlStr string, params ...interface{}) (resultsSlice []map[string]interface{}, err error) {
  126. s, err := db.Prepare(sqlStr)
  127. if err != nil {
  128. return nil, err
  129. }
  130. defer s.Close()
  131. rows, err := s.Query(params...)
  132. if err != nil {
  133. return nil, err
  134. }
  135. defer rows.Close()
  136. return rows2mapObjectsWithDateFormat(rows, dateFormat)
  137. }
  138. func reflect2object(rawValue *reflect.Value) (value interface{}, err error) {
  139. aa := reflect.TypeOf((*rawValue).Interface())
  140. vv := reflect.ValueOf((*rawValue).Interface())
  141. switch aa.Kind() {
  142. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  143. value = vv.Int()
  144. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  145. value = vv.Uint()
  146. case reflect.Float32, reflect.Float64:
  147. value = vv.Float()
  148. case reflect.String:
  149. value = vv.String()
  150. case reflect.Array, reflect.Slice:
  151. switch aa.Elem().Kind() {
  152. case reflect.Uint8:
  153. data := rawValue.Interface().([]byte)
  154. value = string(data)
  155. default:
  156. err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
  157. }
  158. // time type
  159. case reflect.Struct:
  160. if aa.ConvertibleTo(core.TimeType) {
  161. value = vv.Convert(core.TimeType).Interface().(time.Time)
  162. } else {
  163. err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
  164. }
  165. case reflect.Bool:
  166. value = vv.Bool()
  167. case reflect.Complex128, reflect.Complex64:
  168. value = vv.Complex()
  169. /* TODO: unsupported types below
  170. case reflect.Map:
  171. case reflect.Ptr:
  172. case reflect.Uintptr:
  173. case reflect.UnsafePointer:
  174. case reflect.Chan, reflect.Func, reflect.Interface:
  175. */
  176. default:
  177. err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
  178. }
  179. return
  180. }
  181. func value2Object(rawValue *reflect.Value) (data interface{}, err error) {
  182. data, err = reflect2object(rawValue)
  183. if err != nil {
  184. return
  185. }
  186. return
  187. }
  188. func rows2mapObjects(rows *core.Rows) (resultsSlice []map[string]interface{}, err error) {
  189. fields, err := rows.Columns()
  190. if err != nil {
  191. return nil, err
  192. }
  193. for rows.Next() {
  194. result, err := rows2mapObject(rows, fields)
  195. if err != nil {
  196. return nil, err
  197. }
  198. resultsSlice = append(resultsSlice, result)
  199. }
  200. return resultsSlice, nil
  201. }
  202. func rows2mapObject(rows *core.Rows, fields []string) (resultsMap map[string]interface{}, err error) {
  203. result := make(map[string]interface{})
  204. scanResultContainers := make([]interface{}, len(fields))
  205. for i := 0; i < len(fields); i++ {
  206. var scanResultContainer interface{}
  207. scanResultContainers[i] = &scanResultContainer
  208. }
  209. if err := rows.Scan(scanResultContainers...); err != nil {
  210. return nil, err
  211. }
  212. for ii, key := range fields {
  213. rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
  214. //if row is null then ignore
  215. if rawValue.Interface() == nil {
  216. continue
  217. }
  218. if data, err := value2Object(&rawValue); err == nil {
  219. result[key] = data
  220. } else {
  221. return nil, err // !nashtsai! REVIEW, should return err or just error log?
  222. }
  223. }
  224. return result, nil
  225. }
  226. func txQuery3(tx *core.Tx, sqlStr string, params ...interface{}) (resultsSlice []map[string]interface{}, err error) {
  227. rows, err := tx.Query(sqlStr, params...)
  228. if err != nil {
  229. return nil, err
  230. }
  231. defer rows.Close()
  232. return rows2mapObjects(rows)
  233. }
  234. func query3(db *core.DB, sqlStr string, params ...interface{}) (resultsSlice []map[string]interface{}, err error) {
  235. s, err := db.Prepare(sqlStr)
  236. if err != nil {
  237. return nil, err
  238. }
  239. defer s.Close()
  240. rows, err := s.Query(params...)
  241. if err != nil {
  242. return nil, err
  243. }
  244. defer rows.Close()
  245. return rows2mapObjects(rows)
  246. }