myql操作
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.

167 lines
3.1 KiB

  1. package tencentdb
  2. import (
  3. "fmt"
  4. "log"
  5. "strconv"
  6. "strings"
  7. )
  8. /*
  9. * 连接多个字符串
  10. * 2019/05/05
  11. */
  12. func StringJoin(s ...string) string {
  13. var build strings.Builder
  14. if len(s) > 0 {
  15. for _, v := range s {
  16. build.WriteString(v)
  17. }
  18. }
  19. return build.String()
  20. }
  21. /**
  22. * 字符串转大驼峰 ios_bbbbbbbb -> IosBbbbbbbbb
  23. */
  24. func StrFirstToUpper(str string) string {
  25. str = strings.ReplaceAll(str, "`", "")
  26. temp := strings.Split(str, "_")
  27. var upperStr string
  28. for y := 0; y < len(temp); y++ {
  29. vv := []rune(temp[y])
  30. for i := 0; i < len(vv); i++ {
  31. if i == 0 {
  32. vv[i] -= 32
  33. upperStr += string(vv[i])
  34. } else {
  35. upperStr += string(vv[i])
  36. }
  37. }
  38. }
  39. return upperStr
  40. }
  41. func ToString(v interface{}) string {
  42. var value string
  43. switch v.(type) {
  44. case string:
  45. value = v.(string)
  46. case int:
  47. value = strconv.Itoa(v.(int))
  48. case float64:
  49. value = strconv.FormatFloat(v.(float64), 'f', 2, 64)
  50. case float32:
  51. value = strconv.FormatFloat(float64(v.(float32)), 'f', 2, 64)
  52. case int64:
  53. value = strconv.FormatInt(v.(int64), 10)
  54. case []uint8:
  55. value = string(v.([]uint8))
  56. // case []byte:
  57. // value = string(v.([]byte))
  58. case interface{}:
  59. value = v.(string)
  60. case nil:
  61. value = ""
  62. default:
  63. log.Println("参数值类型错误", v, "not in string|int|float64|interface|int64")
  64. }
  65. return strings.Trim(value, " ")
  66. }
  67. /**
  68. * 是否存在在字符切片中
  69. */
  70. func IsInStringArray(arr []string, str string) bool {
  71. var isIn bool = false
  72. length := len(arr)
  73. if length < 1 {
  74. return false
  75. }
  76. for _, item := range arr {
  77. if item == str {
  78. isIn = true
  79. break
  80. }
  81. }
  82. return isIn
  83. }
  84. func ToInt(inter interface{}) int {
  85. var value int
  86. switch inter.(type) {
  87. case string:
  88. value, _ = strconv.Atoi(inter.(string))
  89. case int:
  90. value = inter.(int)
  91. case int64:
  92. value = int(inter.(int64))
  93. case float64:
  94. value, _ = strconv.Atoi(fmt.Sprintf("%1.0f", inter))
  95. case nil:
  96. value = 0
  97. case interface{}:
  98. value = inter.(int)
  99. default:
  100. log.Println("参数值类型错误", inter, "not in string|int|float64|interface|int64")
  101. }
  102. return value
  103. }
  104. func ToInt64(inter interface{}) int64 {
  105. var value int64
  106. switch inter.(type) {
  107. case string:
  108. value, _ = strconv.ParseInt(inter.(string), 10, 64)
  109. case int:
  110. value = int64(inter.(int))
  111. case int64:
  112. value = inter.(int64)
  113. case float64:
  114. value_int, _ := strconv.Atoi(fmt.Sprintf("%1.0f", inter))
  115. value = int64(value_int)
  116. case nil:
  117. value = 0
  118. case interface{}:
  119. if _, ok := inter.(int64); !ok {
  120. value = inter.(int64)
  121. }
  122. default:
  123. log.Println("参数值类型错误", inter, "not in string|int|float64|interface|int64")
  124. }
  125. return value
  126. }
  127. /**
  128. * 根据第几页计算从第几行开始
  129. * @param pageNum 第几页
  130. * @param pageSize 每页几行
  131. * @return from,offset 开始行数偏移量
  132. * 2020/05/15
  133. * gz
  134. */
  135. func GetPage(pageNum, pageSize interface{}) (string, string) {
  136. var from string
  137. var offset int = ToInt(pageSize)
  138. var pageNumInt, pageSizeInt int = ToInt(pageNum), ToInt(pageSize)
  139. if pageNumInt < 1 {
  140. pageNumInt = 1
  141. }
  142. if pageSizeInt < 1 {
  143. offset = 10
  144. pageSizeInt = 10
  145. }
  146. from = ToString((pageNumInt - 1) * pageSizeInt)
  147. return from, ToString(offset)
  148. }