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.

599 lines
14 KiB

  1. // Copyright 2015 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. "errors"
  7. "fmt"
  8. "reflect"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/go-xorm/core"
  14. )
  15. // str2PK convert string value to primary key value according to tp
  16. func str2PK(s string, tp reflect.Type) (interface{}, error) {
  17. var err error
  18. var result interface{}
  19. switch tp.Kind() {
  20. case reflect.Int:
  21. result, err = strconv.Atoi(s)
  22. if err != nil {
  23. return nil, errors.New("convert " + s + " as int: " + err.Error())
  24. }
  25. case reflect.Int8:
  26. x, err := strconv.Atoi(s)
  27. if err != nil {
  28. return nil, errors.New("convert " + s + " as int16: " + err.Error())
  29. }
  30. result = int8(x)
  31. case reflect.Int16:
  32. x, err := strconv.Atoi(s)
  33. if err != nil {
  34. return nil, errors.New("convert " + s + " as int16: " + err.Error())
  35. }
  36. result = int16(x)
  37. case reflect.Int32:
  38. x, err := strconv.Atoi(s)
  39. if err != nil {
  40. return nil, errors.New("convert " + s + " as int32: " + err.Error())
  41. }
  42. result = int32(x)
  43. case reflect.Int64:
  44. result, err = strconv.ParseInt(s, 10, 64)
  45. if err != nil {
  46. return nil, errors.New("convert " + s + " as int64: " + err.Error())
  47. }
  48. case reflect.Uint:
  49. x, err := strconv.ParseUint(s, 10, 64)
  50. if err != nil {
  51. return nil, errors.New("convert " + s + " as uint: " + err.Error())
  52. }
  53. result = uint(x)
  54. case reflect.Uint8:
  55. x, err := strconv.ParseUint(s, 10, 64)
  56. if err != nil {
  57. return nil, errors.New("convert " + s + " as uint8: " + err.Error())
  58. }
  59. result = uint8(x)
  60. case reflect.Uint16:
  61. x, err := strconv.ParseUint(s, 10, 64)
  62. if err != nil {
  63. return nil, errors.New("convert " + s + " as uint16: " + err.Error())
  64. }
  65. result = uint16(x)
  66. case reflect.Uint32:
  67. x, err := strconv.ParseUint(s, 10, 64)
  68. if err != nil {
  69. return nil, errors.New("convert " + s + " as uint32: " + err.Error())
  70. }
  71. result = uint32(x)
  72. case reflect.Uint64:
  73. result, err = strconv.ParseUint(s, 10, 64)
  74. if err != nil {
  75. return nil, errors.New("convert " + s + " as uint64: " + err.Error())
  76. }
  77. case reflect.String:
  78. result = s
  79. default:
  80. panic("unsupported convert type")
  81. }
  82. result = reflect.ValueOf(result).Convert(tp).Interface()
  83. return result, nil
  84. }
  85. func splitTag(tag string) (tags []string) {
  86. tag = strings.TrimSpace(tag)
  87. var hasQuote = false
  88. var lastIdx = 0
  89. for i, t := range tag {
  90. if t == '\'' {
  91. hasQuote = !hasQuote
  92. } else if t == ' ' {
  93. if lastIdx < i && !hasQuote {
  94. tags = append(tags, strings.TrimSpace(tag[lastIdx:i]))
  95. lastIdx = i + 1
  96. }
  97. }
  98. }
  99. if lastIdx < len(tag) {
  100. tags = append(tags, strings.TrimSpace(tag[lastIdx:len(tag)]))
  101. }
  102. return
  103. }
  104. type zeroable interface {
  105. IsZero() bool
  106. }
  107. func isZero(k interface{}) bool {
  108. switch k.(type) {
  109. case int:
  110. return k.(int) == 0
  111. case int8:
  112. return k.(int8) == 0
  113. case int16:
  114. return k.(int16) == 0
  115. case int32:
  116. return k.(int32) == 0
  117. case int64:
  118. return k.(int64) == 0
  119. case uint:
  120. return k.(uint) == 0
  121. case uint8:
  122. return k.(uint8) == 0
  123. case uint16:
  124. return k.(uint16) == 0
  125. case uint32:
  126. return k.(uint32) == 0
  127. case uint64:
  128. return k.(uint64) == 0
  129. case float32:
  130. return k.(float32) == 0
  131. case float64:
  132. return k.(float64) == 0
  133. case bool:
  134. return k.(bool) == false
  135. case string:
  136. return k.(string) == ""
  137. case zeroable:
  138. return k.(zeroable).IsZero()
  139. }
  140. return false
  141. }
  142. func isStructZero(v reflect.Value) bool {
  143. if !v.IsValid() {
  144. return true
  145. }
  146. for i := 0; i < v.NumField(); i++ {
  147. field := v.Field(i)
  148. switch field.Kind() {
  149. case reflect.Ptr:
  150. field = field.Elem()
  151. fallthrough
  152. case reflect.Struct:
  153. if !isStructZero(field) {
  154. return false
  155. }
  156. default:
  157. if field.CanInterface() && !isZero(field.Interface()) {
  158. return false
  159. }
  160. }
  161. }
  162. return true
  163. }
  164. func int64ToIntValue(id int64, tp reflect.Type) reflect.Value {
  165. var v interface{}
  166. switch tp.Kind() {
  167. case reflect.Int16:
  168. v = int16(id)
  169. case reflect.Int32:
  170. v = int32(id)
  171. case reflect.Int:
  172. v = int(id)
  173. case reflect.Int64:
  174. v = id
  175. case reflect.Uint16:
  176. v = uint16(id)
  177. case reflect.Uint32:
  178. v = uint32(id)
  179. case reflect.Uint64:
  180. v = uint64(id)
  181. case reflect.Uint:
  182. v = uint(id)
  183. }
  184. return reflect.ValueOf(v).Convert(tp)
  185. }
  186. func int64ToInt(id int64, tp reflect.Type) interface{} {
  187. return int64ToIntValue(id, tp).Interface()
  188. }
  189. func isPKZero(pk core.PK) bool {
  190. for _, k := range pk {
  191. if isZero(k) {
  192. return true
  193. }
  194. }
  195. return false
  196. }
  197. func indexNoCase(s, sep string) int {
  198. return strings.Index(strings.ToLower(s), strings.ToLower(sep))
  199. }
  200. func splitNoCase(s, sep string) []string {
  201. idx := indexNoCase(s, sep)
  202. if idx < 0 {
  203. return []string{s}
  204. }
  205. return strings.Split(s, s[idx:idx+len(sep)])
  206. }
  207. func splitNNoCase(s, sep string, n int) []string {
  208. idx := indexNoCase(s, sep)
  209. if idx < 0 {
  210. return []string{s}
  211. }
  212. return strings.SplitN(s, s[idx:idx+len(sep)], n)
  213. }
  214. func makeArray(elem string, count int) []string {
  215. res := make([]string, count)
  216. for i := 0; i < count; i++ {
  217. res[i] = elem
  218. }
  219. return res
  220. }
  221. func rValue(bean interface{}) reflect.Value {
  222. return reflect.Indirect(reflect.ValueOf(bean))
  223. }
  224. func rType(bean interface{}) reflect.Type {
  225. sliceValue := reflect.Indirect(reflect.ValueOf(bean))
  226. //return reflect.TypeOf(sliceValue.Interface())
  227. return sliceValue.Type()
  228. }
  229. func structName(v reflect.Type) string {
  230. for v.Kind() == reflect.Ptr {
  231. v = v.Elem()
  232. }
  233. return v.Name()
  234. }
  235. func col2NewCols(columns ...string) []string {
  236. newColumns := make([]string, 0, len(columns))
  237. for _, col := range columns {
  238. col = strings.Replace(col, "`", "", -1)
  239. col = strings.Replace(col, `"`, "", -1)
  240. ccols := strings.Split(col, ",")
  241. for _, c := range ccols {
  242. newColumns = append(newColumns, strings.TrimSpace(c))
  243. }
  244. }
  245. return newColumns
  246. }
  247. func sliceEq(left, right []string) bool {
  248. if len(left) != len(right) {
  249. return false
  250. }
  251. sort.Sort(sort.StringSlice(left))
  252. sort.Sort(sort.StringSlice(right))
  253. for i := 0; i < len(left); i++ {
  254. if left[i] != right[i] {
  255. return false
  256. }
  257. }
  258. return true
  259. }
  260. func reflect2value(rawValue *reflect.Value) (str string, err error) {
  261. aa := reflect.TypeOf((*rawValue).Interface())
  262. vv := reflect.ValueOf((*rawValue).Interface())
  263. switch aa.Kind() {
  264. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  265. str = strconv.FormatInt(vv.Int(), 10)
  266. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  267. str = strconv.FormatUint(vv.Uint(), 10)
  268. case reflect.Float32, reflect.Float64:
  269. str = strconv.FormatFloat(vv.Float(), 'f', -1, 64)
  270. case reflect.String:
  271. str = vv.String()
  272. case reflect.Array, reflect.Slice:
  273. switch aa.Elem().Kind() {
  274. case reflect.Uint8:
  275. data := rawValue.Interface().([]byte)
  276. str = string(data)
  277. default:
  278. err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
  279. }
  280. // time type
  281. case reflect.Struct:
  282. if aa.ConvertibleTo(core.TimeType) {
  283. str = vv.Convert(core.TimeType).Interface().(time.Time).Format(time.RFC3339Nano)
  284. } else {
  285. err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
  286. }
  287. case reflect.Bool:
  288. str = strconv.FormatBool(vv.Bool())
  289. case reflect.Complex128, reflect.Complex64:
  290. str = fmt.Sprintf("%v", vv.Complex())
  291. /* TODO: unsupported types below
  292. case reflect.Map:
  293. case reflect.Ptr:
  294. case reflect.Uintptr:
  295. case reflect.UnsafePointer:
  296. case reflect.Chan, reflect.Func, reflect.Interface:
  297. */
  298. default:
  299. err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
  300. }
  301. return
  302. }
  303. func value2Bytes(rawValue *reflect.Value) (data []byte, err error) {
  304. var str string
  305. str, err = reflect2value(rawValue)
  306. if err != nil {
  307. return
  308. }
  309. data = []byte(str)
  310. return
  311. }
  312. func value2String(rawValue *reflect.Value) (data string, err error) {
  313. data, err = reflect2value(rawValue)
  314. if err != nil {
  315. return
  316. }
  317. return
  318. }
  319. func rows2Strings(rows *core.Rows) (resultsSlice []map[string]string, err error) {
  320. fields, err := rows.Columns()
  321. if err != nil {
  322. return nil, err
  323. }
  324. for rows.Next() {
  325. result, err := row2mapStr(rows, fields)
  326. if err != nil {
  327. return nil, err
  328. }
  329. resultsSlice = append(resultsSlice, result)
  330. }
  331. return resultsSlice, nil
  332. }
  333. func rows2maps(rows *core.Rows) (resultsSlice []map[string][]byte, err error) {
  334. fields, err := rows.Columns()
  335. if err != nil {
  336. return nil, err
  337. }
  338. for rows.Next() {
  339. result, err := row2map(rows, fields)
  340. if err != nil {
  341. return nil, err
  342. }
  343. resultsSlice = append(resultsSlice, result)
  344. }
  345. return resultsSlice, nil
  346. }
  347. func row2map(rows *core.Rows, fields []string) (resultsMap map[string][]byte, err error) {
  348. result := make(map[string][]byte)
  349. scanResultContainers := make([]interface{}, len(fields))
  350. for i := 0; i < len(fields); i++ {
  351. var scanResultContainer interface{}
  352. scanResultContainers[i] = &scanResultContainer
  353. }
  354. if err := rows.Scan(scanResultContainers...); err != nil {
  355. return nil, err
  356. }
  357. for ii, key := range fields {
  358. rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
  359. //if row is null then ignore
  360. if rawValue.Interface() == nil {
  361. //fmt.Println("ignore ...", key, rawValue)
  362. continue
  363. }
  364. if data, err := value2Bytes(&rawValue); err == nil {
  365. result[key] = data
  366. } else {
  367. return nil, err // !nashtsai! REVIEW, should return err or just error log?
  368. }
  369. }
  370. return result, nil
  371. }
  372. func row2mapStr(rows *core.Rows, fields []string) (resultsMap map[string]string, err error) {
  373. result := make(map[string]string)
  374. scanResultContainers := make([]interface{}, len(fields))
  375. for i := 0; i < len(fields); i++ {
  376. var scanResultContainer interface{}
  377. scanResultContainers[i] = &scanResultContainer
  378. }
  379. if err := rows.Scan(scanResultContainers...); err != nil {
  380. return nil, err
  381. }
  382. for ii, key := range fields {
  383. rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
  384. //if row is null then ignore
  385. if rawValue.Interface() == nil {
  386. //fmt.Println("ignore ...", key, rawValue)
  387. continue
  388. }
  389. if data, err := value2String(&rawValue); err == nil {
  390. result[key] = data
  391. } else {
  392. return nil, err // !nashtsai! REVIEW, should return err or just error log?
  393. }
  394. }
  395. return result, nil
  396. }
  397. func txQuery2(tx *core.Tx, sqlStr string, params ...interface{}) (resultsSlice []map[string]string, err error) {
  398. rows, err := tx.Query(sqlStr, params...)
  399. if err != nil {
  400. return nil, err
  401. }
  402. defer rows.Close()
  403. return rows2Strings(rows)
  404. }
  405. func query2(db *core.DB, sqlStr string, params ...interface{}) (resultsSlice []map[string]string, err error) {
  406. s, err := db.Prepare(sqlStr)
  407. if err != nil {
  408. return nil, err
  409. }
  410. defer s.Close()
  411. rows, err := s.Query(params...)
  412. if err != nil {
  413. return nil, err
  414. }
  415. defer rows.Close()
  416. return rows2Strings(rows)
  417. }
  418. func setColumnInt(bean interface{}, col *core.Column, t int64) {
  419. v, err := col.ValueOf(bean)
  420. if err != nil {
  421. return
  422. }
  423. if v.CanSet() {
  424. switch v.Type().Kind() {
  425. case reflect.Int, reflect.Int64, reflect.Int32:
  426. v.SetInt(t)
  427. case reflect.Uint, reflect.Uint64, reflect.Uint32:
  428. v.SetUint(uint64(t))
  429. }
  430. }
  431. }
  432. func setColumnTime(bean interface{}, col *core.Column, t time.Time) {
  433. v, err := col.ValueOf(bean)
  434. if err != nil {
  435. return
  436. }
  437. if v.CanSet() {
  438. switch v.Type().Kind() {
  439. case reflect.Struct:
  440. v.Set(reflect.ValueOf(t).Convert(v.Type()))
  441. case reflect.Int, reflect.Int64, reflect.Int32:
  442. v.SetInt(t.Unix())
  443. case reflect.Uint, reflect.Uint64, reflect.Uint32:
  444. v.SetUint(uint64(t.Unix()))
  445. }
  446. }
  447. }
  448. func genCols(table *core.Table, session *Session, bean interface{}, useCol bool, includeQuote bool) ([]string, []interface{}, error) {
  449. colNames := make([]string, 0, len(table.ColumnsSeq()))
  450. args := make([]interface{}, 0, len(table.ColumnsSeq()))
  451. for _, col := range table.Columns() {
  452. if useCol && !col.IsVersion && !col.IsCreated && !col.IsUpdated {
  453. if _, ok := getFlagForColumn(session.Statement.columnMap, col); !ok {
  454. continue
  455. }
  456. }
  457. if col.MapType == core.ONLYFROMDB {
  458. continue
  459. }
  460. fieldValuePtr, err := col.ValueOf(bean)
  461. if err != nil {
  462. return nil, nil, err
  463. }
  464. fieldValue := *fieldValuePtr
  465. if col.IsAutoIncrement {
  466. switch fieldValue.Type().Kind() {
  467. case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
  468. if fieldValue.Int() == 0 {
  469. continue
  470. }
  471. case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint, reflect.Uint64:
  472. if fieldValue.Uint() == 0 {
  473. continue
  474. }
  475. case reflect.String:
  476. if len(fieldValue.String()) == 0 {
  477. continue
  478. }
  479. }
  480. }
  481. if col.IsDeleted {
  482. continue
  483. }
  484. if session.Statement.ColumnStr != "" {
  485. if _, ok := getFlagForColumn(session.Statement.columnMap, col); !ok {
  486. continue
  487. }
  488. }
  489. if session.Statement.OmitStr != "" {
  490. if _, ok := getFlagForColumn(session.Statement.columnMap, col); ok {
  491. continue
  492. }
  493. }
  494. // !evalphobia! set fieldValue as nil when column is nullable and zero-value
  495. if _, ok := getFlagForColumn(session.Statement.nullableMap, col); ok {
  496. if col.Nullable && isZero(fieldValue.Interface()) {
  497. var nilValue *int
  498. fieldValue = reflect.ValueOf(nilValue)
  499. }
  500. }
  501. if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime /*&& isZero(fieldValue.Interface())*/ {
  502. // if time is non-empty, then set to auto time
  503. val, t := session.Engine.NowTime2(col.SQLType.Name)
  504. args = append(args, val)
  505. var colName = col.Name
  506. session.afterClosures = append(session.afterClosures, func(bean interface{}) {
  507. col := table.GetColumn(colName)
  508. setColumnTime(bean, col, t)
  509. })
  510. } else if col.IsVersion && session.Statement.checkVersion {
  511. args = append(args, 1)
  512. } else {
  513. arg, err := session.value2Interface(col, fieldValue)
  514. if err != nil {
  515. return colNames, args, err
  516. }
  517. args = append(args, arg)
  518. }
  519. if includeQuote {
  520. colNames = append(colNames, session.Engine.Quote(col.Name)+" = ?")
  521. } else {
  522. colNames = append(colNames, col.Name)
  523. }
  524. }
  525. return colNames, args, nil
  526. }
  527. func indexName(tableName, idxName string) string {
  528. return fmt.Sprintf("IDX_%v_%v", tableName, idxName)
  529. }
  530. func getFlagForColumn(m map[string]bool, col *core.Column) (val bool, has bool) {
  531. if len(m) == 0 {
  532. return false, false
  533. }
  534. n := len(col.Name)
  535. for mk := range m {
  536. if len(mk) != n {
  537. continue
  538. }
  539. if strings.EqualFold(mk, col.Name) {
  540. return m[mk], true
  541. }
  542. }
  543. return false, false
  544. }