Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle https://xorm.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

423 lines
9.7 KiB

  1. // Copyright 2017 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "database/sql/driver"
  7. "errors"
  8. "fmt"
  9. "reflect"
  10. "strconv"
  11. "time"
  12. )
  13. var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error
  14. func strconvErr(err error) error {
  15. if ne, ok := err.(*strconv.NumError); ok {
  16. return ne.Err
  17. }
  18. return err
  19. }
  20. func cloneBytes(b []byte) []byte {
  21. if b == nil {
  22. return nil
  23. }
  24. c := make([]byte, len(b))
  25. copy(c, b)
  26. return c
  27. }
  28. func asString(src interface{}) string {
  29. switch v := src.(type) {
  30. case string:
  31. return v
  32. case []byte:
  33. return string(v)
  34. }
  35. rv := reflect.ValueOf(src)
  36. switch rv.Kind() {
  37. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  38. return strconv.FormatInt(rv.Int(), 10)
  39. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  40. return strconv.FormatUint(rv.Uint(), 10)
  41. case reflect.Float64:
  42. return strconv.FormatFloat(rv.Float(), 'g', -1, 64)
  43. case reflect.Float32:
  44. return strconv.FormatFloat(rv.Float(), 'g', -1, 32)
  45. case reflect.Bool:
  46. return strconv.FormatBool(rv.Bool())
  47. }
  48. return fmt.Sprintf("%v", src)
  49. }
  50. func asBytes(buf []byte, rv reflect.Value) (b []byte, ok bool) {
  51. switch rv.Kind() {
  52. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  53. return strconv.AppendInt(buf, rv.Int(), 10), true
  54. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  55. return strconv.AppendUint(buf, rv.Uint(), 10), true
  56. case reflect.Float32:
  57. return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 32), true
  58. case reflect.Float64:
  59. return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 64), true
  60. case reflect.Bool:
  61. return strconv.AppendBool(buf, rv.Bool()), true
  62. case reflect.String:
  63. s := rv.String()
  64. return append(buf, s...), true
  65. }
  66. return
  67. }
  68. // convertAssign copies to dest the value in src, converting it if possible.
  69. // An error is returned if the copy would result in loss of information.
  70. // dest should be a pointer type.
  71. func convertAssign(dest, src interface{}) error {
  72. // Common cases, without reflect.
  73. switch s := src.(type) {
  74. case string:
  75. switch d := dest.(type) {
  76. case *string:
  77. if d == nil {
  78. return errNilPtr
  79. }
  80. *d = s
  81. return nil
  82. case *[]byte:
  83. if d == nil {
  84. return errNilPtr
  85. }
  86. *d = []byte(s)
  87. return nil
  88. }
  89. case []byte:
  90. switch d := dest.(type) {
  91. case *string:
  92. if d == nil {
  93. return errNilPtr
  94. }
  95. *d = string(s)
  96. return nil
  97. case *interface{}:
  98. if d == nil {
  99. return errNilPtr
  100. }
  101. *d = cloneBytes(s)
  102. return nil
  103. case *[]byte:
  104. if d == nil {
  105. return errNilPtr
  106. }
  107. *d = cloneBytes(s)
  108. return nil
  109. }
  110. case time.Time:
  111. switch d := dest.(type) {
  112. case *string:
  113. *d = s.Format(time.RFC3339Nano)
  114. return nil
  115. case *[]byte:
  116. if d == nil {
  117. return errNilPtr
  118. }
  119. *d = []byte(s.Format(time.RFC3339Nano))
  120. return nil
  121. }
  122. case nil:
  123. switch d := dest.(type) {
  124. case *interface{}:
  125. if d == nil {
  126. return errNilPtr
  127. }
  128. *d = nil
  129. return nil
  130. case *[]byte:
  131. if d == nil {
  132. return errNilPtr
  133. }
  134. *d = nil
  135. return nil
  136. }
  137. }
  138. var sv reflect.Value
  139. switch d := dest.(type) {
  140. case *string:
  141. sv = reflect.ValueOf(src)
  142. switch sv.Kind() {
  143. case reflect.Bool,
  144. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  145. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
  146. reflect.Float32, reflect.Float64:
  147. *d = asString(src)
  148. return nil
  149. }
  150. case *[]byte:
  151. sv = reflect.ValueOf(src)
  152. if b, ok := asBytes(nil, sv); ok {
  153. *d = b
  154. return nil
  155. }
  156. case *bool:
  157. bv, err := driver.Bool.ConvertValue(src)
  158. if err == nil {
  159. *d = bv.(bool)
  160. }
  161. return err
  162. case *interface{}:
  163. *d = src
  164. return nil
  165. }
  166. dpv := reflect.ValueOf(dest)
  167. if dpv.Kind() != reflect.Ptr {
  168. return errors.New("destination not a pointer")
  169. }
  170. if dpv.IsNil() {
  171. return errNilPtr
  172. }
  173. if !sv.IsValid() {
  174. sv = reflect.ValueOf(src)
  175. }
  176. dv := reflect.Indirect(dpv)
  177. if sv.IsValid() && sv.Type().AssignableTo(dv.Type()) {
  178. switch b := src.(type) {
  179. case []byte:
  180. dv.Set(reflect.ValueOf(cloneBytes(b)))
  181. default:
  182. dv.Set(sv)
  183. }
  184. return nil
  185. }
  186. if dv.Kind() == sv.Kind() && sv.Type().ConvertibleTo(dv.Type()) {
  187. dv.Set(sv.Convert(dv.Type()))
  188. return nil
  189. }
  190. switch dv.Kind() {
  191. case reflect.Ptr:
  192. if src == nil {
  193. dv.Set(reflect.Zero(dv.Type()))
  194. return nil
  195. }
  196. dv.Set(reflect.New(dv.Type().Elem()))
  197. return convertAssign(dv.Interface(), src)
  198. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  199. s := asString(src)
  200. i64, err := strconv.ParseInt(s, 10, dv.Type().Bits())
  201. if err != nil {
  202. err = strconvErr(err)
  203. return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
  204. }
  205. dv.SetInt(i64)
  206. return nil
  207. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  208. s := asString(src)
  209. u64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
  210. if err != nil {
  211. err = strconvErr(err)
  212. return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
  213. }
  214. dv.SetUint(u64)
  215. return nil
  216. case reflect.Float32, reflect.Float64:
  217. s := asString(src)
  218. f64, err := strconv.ParseFloat(s, dv.Type().Bits())
  219. if err != nil {
  220. err = strconvErr(err)
  221. return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
  222. }
  223. dv.SetFloat(f64)
  224. return nil
  225. case reflect.String:
  226. dv.SetString(asString(src))
  227. return nil
  228. }
  229. return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, dest)
  230. }
  231. func asKind(vv reflect.Value, tp reflect.Type) (interface{}, error) {
  232. switch tp.Kind() {
  233. case reflect.Int64:
  234. return vv.Int(), nil
  235. case reflect.Int:
  236. return int(vv.Int()), nil
  237. case reflect.Int32:
  238. return int32(vv.Int()), nil
  239. case reflect.Int16:
  240. return int16(vv.Int()), nil
  241. case reflect.Int8:
  242. return int8(vv.Int()), nil
  243. case reflect.Uint64:
  244. return vv.Uint(), nil
  245. case reflect.Uint:
  246. return uint(vv.Uint()), nil
  247. case reflect.Uint32:
  248. return uint32(vv.Uint()), nil
  249. case reflect.Uint16:
  250. return uint16(vv.Uint()), nil
  251. case reflect.Uint8:
  252. return uint8(vv.Uint()), nil
  253. case reflect.String:
  254. return vv.String(), nil
  255. case reflect.Slice:
  256. if tp.Elem().Kind() == reflect.Uint8 {
  257. v, err := strconv.ParseInt(string(vv.Interface().([]byte)), 10, 64)
  258. if err != nil {
  259. return nil, err
  260. }
  261. return v, nil
  262. }
  263. }
  264. return nil, fmt.Errorf("unsupported primary key type: %v, %v", tp, vv)
  265. }
  266. func asBool(bs []byte) (bool, error) {
  267. if len(bs) == 0 {
  268. return false, nil
  269. }
  270. if bs[0] == 0x00 {
  271. return false, nil
  272. } else if bs[0] == 0x01 {
  273. return true, nil
  274. }
  275. return strconv.ParseBool(string(bs))
  276. }
  277. // str2PK convert string value to primary key value according to tp
  278. func str2PKValue(s string, tp reflect.Type) (reflect.Value, error) {
  279. var err error
  280. var result interface{}
  281. var defReturn = reflect.Zero(tp)
  282. switch tp.Kind() {
  283. case reflect.Int:
  284. result, err = strconv.Atoi(s)
  285. if err != nil {
  286. return defReturn, fmt.Errorf("convert %s as int: %s", s, err.Error())
  287. }
  288. case reflect.Int8:
  289. x, err := strconv.Atoi(s)
  290. if err != nil {
  291. return defReturn, fmt.Errorf("convert %s as int8: %s", s, err.Error())
  292. }
  293. result = int8(x)
  294. case reflect.Int16:
  295. x, err := strconv.Atoi(s)
  296. if err != nil {
  297. return defReturn, fmt.Errorf("convert %s as int16: %s", s, err.Error())
  298. }
  299. result = int16(x)
  300. case reflect.Int32:
  301. x, err := strconv.Atoi(s)
  302. if err != nil {
  303. return defReturn, fmt.Errorf("convert %s as int32: %s", s, err.Error())
  304. }
  305. result = int32(x)
  306. case reflect.Int64:
  307. result, err = strconv.ParseInt(s, 10, 64)
  308. if err != nil {
  309. return defReturn, fmt.Errorf("convert %s as int64: %s", s, err.Error())
  310. }
  311. case reflect.Uint:
  312. x, err := strconv.ParseUint(s, 10, 64)
  313. if err != nil {
  314. return defReturn, fmt.Errorf("convert %s as uint: %s", s, err.Error())
  315. }
  316. result = uint(x)
  317. case reflect.Uint8:
  318. x, err := strconv.ParseUint(s, 10, 64)
  319. if err != nil {
  320. return defReturn, fmt.Errorf("convert %s as uint8: %s", s, err.Error())
  321. }
  322. result = uint8(x)
  323. case reflect.Uint16:
  324. x, err := strconv.ParseUint(s, 10, 64)
  325. if err != nil {
  326. return defReturn, fmt.Errorf("convert %s as uint16: %s", s, err.Error())
  327. }
  328. result = uint16(x)
  329. case reflect.Uint32:
  330. x, err := strconv.ParseUint(s, 10, 64)
  331. if err != nil {
  332. return defReturn, fmt.Errorf("convert %s as uint32: %s", s, err.Error())
  333. }
  334. result = uint32(x)
  335. case reflect.Uint64:
  336. result, err = strconv.ParseUint(s, 10, 64)
  337. if err != nil {
  338. return defReturn, fmt.Errorf("convert %s as uint64: %s", s, err.Error())
  339. }
  340. case reflect.String:
  341. result = s
  342. default:
  343. return defReturn, errors.New("unsupported convert type")
  344. }
  345. return reflect.ValueOf(result).Convert(tp), nil
  346. }
  347. func str2PK(s string, tp reflect.Type) (interface{}, error) {
  348. v, err := str2PKValue(s, tp)
  349. if err != nil {
  350. return nil, err
  351. }
  352. return v.Interface(), nil
  353. }
  354. func int64ToIntValue(id int64, tp reflect.Type) reflect.Value {
  355. var v interface{}
  356. kind := tp.Kind()
  357. if kind == reflect.Ptr {
  358. kind = tp.Elem().Kind()
  359. }
  360. switch kind {
  361. case reflect.Int16:
  362. temp := int16(id)
  363. v = &temp
  364. case reflect.Int32:
  365. temp := int32(id)
  366. v = &temp
  367. case reflect.Int:
  368. temp := int(id)
  369. v = &temp
  370. case reflect.Int64:
  371. temp := id
  372. v = &temp
  373. case reflect.Uint16:
  374. temp := uint16(id)
  375. v = &temp
  376. case reflect.Uint32:
  377. temp := uint32(id)
  378. v = &temp
  379. case reflect.Uint64:
  380. temp := uint64(id)
  381. v = &temp
  382. case reflect.Uint:
  383. temp := uint(id)
  384. v = &temp
  385. }
  386. if tp.Kind() == reflect.Ptr {
  387. return reflect.ValueOf(v).Convert(tp)
  388. }
  389. return reflect.ValueOf(v).Elem().Convert(tp)
  390. }
  391. func int64ToInt(id int64, tp reflect.Type) interface{} {
  392. return int64ToIntValue(id, tp).Interface()
  393. }