数据库操作
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.

153 lines
3.4 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package dbquery
  2. import (
  3. "database/sql"
  4. "log"
  5. "errors"
  6. "strings"
  7. "time"
  8. "git.tetele.net/tgo/helper"
  9. _ "github.com/go-sql-driver/mysql"
  10. )
  11. var DB *sql.DB
  12. var SLAVER_DB *sql.DB
  13. // db类型,默认空,如TencentDB(腾讯),
  14. var DB_PROVIDER string
  15. func Connect(DBHOST, DBUSER, DBPWD, DBNAME, DBPORT string, conns ...int) error {
  16. log.Println("database connectting...")
  17. var dbConnErr error
  18. if DBHOST != "" && DBUSER != "" && DBPWD != "" && DBPORT != "" { //&& DBNAME != ""
  19. for i := 0; i < 10; i++ {
  20. DB, dbConnErr = sql.Open("mysql", DBUSER+":"+DBPWD+"@tcp("+DBHOST+":"+DBPORT+")/"+DBNAME+"?charset=utf8mb4")
  21. if dbConnErr != nil {
  22. log.Println("ERROR", "can not connect to Database, ", dbConnErr)
  23. time.Sleep(time.Second * 5)
  24. } else {
  25. if len(conns) > 0 {
  26. DB.SetMaxOpenConns(conns[0]) //用于设置最大打开的连接数,默认值为0表示不限制
  27. } else {
  28. DB.SetMaxOpenConns(200) //默认值为0表示不限制
  29. }
  30. if len(conns) > 1 {
  31. DB.SetMaxIdleConns(conns[1]) //用于设置闲置的连接数
  32. } else {
  33. DB.SetMaxIdleConns(50)
  34. }
  35. DB.Ping()
  36. log.Println("database connected")
  37. DB.SetConnMaxLifetime(time.Minute * 2)
  38. break
  39. }
  40. }
  41. } else {
  42. return errors.New("db connection params errors")
  43. }
  44. return dbConnErr
  45. }
  46. func CloseConn() error {
  47. return DB.Close()
  48. }
  49. func ConnectSlaver(DBHOST, DBUSER_SLAVER, DBPWD_SLAVER, DBNAME, DBPORT string, conns ...int) error {
  50. log.Println("database connectting with slaver...")
  51. var dbConnErr error
  52. if DBHOST != "" && DBUSER_SLAVER != "" && DBPWD_SLAVER != "" && DBPORT != "" { //&& DBNAME != ""
  53. for i := 0; i < 10; i++ {
  54. SLAVER_DB, dbConnErr = sql.Open("mysql", DBUSER_SLAVER+":"+DBPWD_SLAVER+"@tcp("+DBHOST+":"+DBPORT+")/"+DBNAME+"?charset=utf8mb4")
  55. if dbConnErr != nil {
  56. log.Println("ERROR", "can not connect to Database, ", dbConnErr)
  57. time.Sleep(time.Second * 5)
  58. } else {
  59. if len(conns) > 0 {
  60. SLAVER_DB.SetMaxOpenConns(conns[0]) //用于设置最大打开的连接数,默认值为0表示不限制
  61. } else {
  62. SLAVER_DB.SetMaxOpenConns(200) //默认值为0表示不限制
  63. }
  64. if len(conns) > 1 {
  65. SLAVER_DB.SetMaxIdleConns(conns[1]) //用于设置闲置的连接数
  66. } else {
  67. SLAVER_DB.SetMaxIdleConns(50)
  68. }
  69. SLAVER_DB.Ping()
  70. log.Println("database connected")
  71. SLAVER_DB.SetConnMaxLifetime(time.Minute * 2)
  72. break
  73. }
  74. }
  75. } else {
  76. return errors.New("db connection params errors")
  77. }
  78. return dbConnErr
  79. }
  80. func CloseSlaverConn() error {
  81. return SLAVER_DB.Close()
  82. }
  83. /**
  84. * 检测表名
  85. */
  86. func getTableName(dbName, table string, dbtype ...string) string {
  87. var db_type string = "mysql"
  88. if len(dbtype) > 0 {
  89. if dbtype[0] != "" {
  90. db_type = dbtype[0]
  91. }
  92. }
  93. var ret string
  94. switch db_type {
  95. case "mysql":
  96. if strings.Contains(table, ".") {
  97. ret = table
  98. }
  99. if dbName != "" {
  100. if strings.Contains(table, ",") {
  101. arr := strings.Split(table, ",")
  102. arrStrs := make([]string, 0, len(arr))
  103. for _, v := range arr {
  104. arrStrs = append(arrStrs, helper.StringJoin(dbName, ".", v))
  105. }
  106. ret = strings.Join(arrStrs, ",")
  107. } else {
  108. ret = helper.StringJoin(dbName, ".", table)
  109. }
  110. } else {
  111. ret = table
  112. }
  113. case "mssql":
  114. ret = helper.StringJoin(dbName, ".", table)
  115. }
  116. return ret
  117. }
  118. func GetDbTableName(dbName, table string) string {
  119. return getTableName(dbName, table)
  120. }
  121. func judg() []string {
  122. return []string{"=", ">", "<", "!=", "<=", ">="}
  123. }