You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.go 1.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package db2
  2. import (
  3. "gorm.io/gorm"
  4. )
  5. const (
  6. maxPlaceholderCount = 65535
  7. )
  8. func BatchNamedExec[T any](ctx SQLContext, sql string, argCnt int, arr []T, callback func(result *gorm.DB) bool) error {
  9. if argCnt == 0 {
  10. result := ctx.Exec(sql, toInterfaceSlice(arr)...)
  11. if result.Error != nil {
  12. return result.Error
  13. }
  14. if callback != nil {
  15. callback(result)
  16. }
  17. return nil
  18. }
  19. batchSize := maxPlaceholderCount / argCnt
  20. for len(arr) > 0 {
  21. curBatchSize := min(batchSize, len(arr))
  22. result := ctx.Exec(sql, toInterfaceSlice(arr[:curBatchSize])...)
  23. if result.Error != nil {
  24. return result.Error
  25. }
  26. if callback != nil && !callback(result) {
  27. return nil
  28. }
  29. arr = arr[curBatchSize:]
  30. }
  31. return nil
  32. }
  33. // 将 []T 转换为 []interface{}
  34. func toInterfaceSlice[T any](arr []T) []interface{} {
  35. interfaceSlice := make([]interface{}, len(arr))
  36. for i, v := range arr {
  37. interfaceSlice[i] = v
  38. }
  39. return interfaceSlice
  40. }
  41. func min(a, b int) int {
  42. if a < b {
  43. return a
  44. }
  45. return b
  46. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。