db_utils.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package utils
  2. import (
  3. "errors"
  4. "reflect"
  5. "strings"
  6. "xorm.io/xorm"
  7. )
  8. // 分页插入数据库
  9. func Insert(db *xorm.Engine, beans ...interface{}) error {
  10. return InsertWithTableNameAndSize(db, "", 500, beans...)
  11. }
  12. func InsertWithTableName(db *xorm.Engine, tableName string, beans ...interface{}) error {
  13. return InsertWithTableNameAndSize(db, tableName, 500, beans...)
  14. }
  15. func InsertWithSize(db *xorm.Engine, size int, beans ...interface{}) error {
  16. return InsertWithTableNameAndSize(db, "", size, beans...)
  17. }
  18. func InsertWithTableNameAndSize(db *xorm.Engine, tableName string, size int, beans ...interface{}) error {
  19. if nil == db {
  20. return errors.New(`db can not be nil`)
  21. }
  22. if nil == beans || len(beans) == 0 {
  23. return nil
  24. }
  25. trans := db.NewSession()
  26. defer trans.Close()
  27. _ = trans.Begin()
  28. err := TransInsertWithTableNameAndSize(trans, tableName, size, beans...)
  29. if nil != err {
  30. _ = trans.Rollback()
  31. return err
  32. }
  33. return trans.Commit()
  34. }
  35. func TransInsert(trans *xorm.Session, beans ...interface{}) error {
  36. return TransInsertWithTableNameAndSize(trans, "", 500, beans...)
  37. }
  38. func TransInsertWithTableName(trans *xorm.Session, tableName string, beans ...interface{}) error {
  39. return TransInsertWithTableNameAndSize(trans, tableName, 500, beans...)
  40. }
  41. func TransInsertWithSize(trans *xorm.Session, size int, beans ...interface{}) error {
  42. return TransInsertWithTableNameAndSize(trans, "", size, beans...)
  43. }
  44. func TransInsertWithTableNameAndSize(trans *xorm.Session, tableName string, size int, beans ...interface{}) error {
  45. if nil == trans {
  46. return errors.New(`trans can not be nil`)
  47. }
  48. if nil == beans || len(beans) == 0 {
  49. return nil
  50. }
  51. tableName = strings.TrimSpace(tableName)
  52. if size <= 0 {
  53. size = 500
  54. }
  55. var objects []interface{}
  56. for _, bean := range beans {
  57. sliceValue := reflect.Indirect(reflect.ValueOf(bean))
  58. if sliceValue.Kind() == reflect.Slice {
  59. sLen := sliceValue.Len()
  60. if sLen == 0 {
  61. continue
  62. }
  63. if sLen <= size {
  64. objects = append(objects, bean)
  65. continue
  66. }
  67. idx := 0
  68. arrMap := make(map[int][]interface{})
  69. for i := 0; i < sliceValue.Len(); i++ {
  70. if i%size == 0 {
  71. idx++
  72. }
  73. v := sliceValue.Index(i)
  74. if v.Kind() == reflect.Ptr {
  75. v = v.Elem()
  76. }
  77. if v.IsZero() {
  78. continue
  79. }
  80. elemValue := v.Interface()
  81. if nil == arrMap[idx] {
  82. arrMap[idx] = []interface{}{elemValue}
  83. } else {
  84. arrMap[idx] = append(arrMap[idx], elemValue)
  85. }
  86. }
  87. for i := 1; i <= idx; i++ {
  88. arr := arrMap[i]
  89. objects = append(objects, &arr)
  90. }
  91. } else {
  92. objects = append(objects, bean)
  93. }
  94. }
  95. var err error
  96. if tableName != "" {
  97. for i := 0; i < len(objects); i++ {
  98. _, err = trans.Table(tableName).Insert(objects[i])
  99. if nil != err {
  100. return err
  101. }
  102. }
  103. } else {
  104. _, err = trans.Insert(objects...)
  105. if nil != err {
  106. return err
  107. }
  108. }
  109. return nil
  110. }