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

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