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.

187 lines
4.5 KiB

  1. package tencentdb
  2. import (
  3. "database/sql"
  4. "log"
  5. "errors"
  6. "strings"
  7. "time"
  8. _ "github.com/go-sql-driver/mysql"
  9. )
  10. var DB *sql.DB
  11. var dbhost_slaver, dbname_slaver, dbport_slaver, dbuser_slaver, dbpwd_slaver string //从库信息
  12. var use_slaver bool = false
  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) {
  48. dbhost_slaver = DBHOST
  49. dbuser_slaver = DBUSER_SLAVER
  50. dbpwd_slaver = DBPWD_SLAVER
  51. dbname_slaver = DBNAME
  52. dbport_slaver = DBPORT
  53. use_slaver = true
  54. // log.Println("database connectting with slaver...")
  55. // var dbConnErr error
  56. // if DBHOST != "" && DBUSER_SLAVER != "" && DBPWD_SLAVER != "" && DBPORT != "" { //&& DBNAME != ""
  57. // for i := 0; i < 10; i++ {
  58. // SLAVER_DB, dbConnErr = sql.Open("mysql", DBUSER_SLAVER+":"+DBPWD_SLAVER+"@tcp("+DBHOST+":"+DBPORT+")/"+DBNAME+"?charset=utf8mb4")
  59. // if dbConnErr != nil {
  60. // log.Println("ERROR", "can not connect to Database, ", dbConnErr)
  61. // time.Sleep(time.Second * 5)
  62. // } else {
  63. // if len(conns) > 0 {
  64. // SLAVER_DB.SetMaxOpenConns(conns[0]) //用于设置最大打开的连接数,默认值为0表示不限制
  65. // } else {
  66. // SLAVER_DB.SetMaxOpenConns(200) //默认值为0表示不限制
  67. // }
  68. // if len(conns) > 1 {
  69. // SLAVER_DB.SetMaxIdleConns(conns[1]) //用于设置闲置的连接数
  70. // } else {
  71. // SLAVER_DB.SetMaxIdleConns(50)
  72. // }
  73. // SLAVER_DB.Ping()
  74. // log.Println("database connected")
  75. // SLAVER_DB.SetConnMaxLifetime(time.Minute * 2)
  76. // break
  77. // }
  78. // }
  79. // } else {
  80. // return errors.New("db connection params errors")
  81. // }
  82. // return dbConnErr
  83. }
  84. func connectDb(dbhost, dbuser, dbpwd, dbname, dbport string, conns ...int) (*sql.DB, error) {
  85. log.Println("database connectting...")
  86. var dbconn *sql.DB
  87. var dbConnErr error
  88. if dbhost != "" && dbuser != "" && dbpwd != "" && dbport != "" {
  89. for i := 0; i < 10; i++ {
  90. dbconn, dbConnErr = sql.Open("mysql", dbuser+":"+dbpwd+"@tcp("+dbhost+":"+dbport+")/"+dbname+"?charset=utf8mb4")
  91. if dbConnErr != nil {
  92. log.Println("ERROR", "can not connect to Database, ", dbConnErr)
  93. time.Sleep(time.Second * 5)
  94. } else {
  95. if len(conns) > 0 {
  96. dbconn.SetMaxOpenConns(conns[0]) //用于设置最大打开的连接数,默认值为0表示不限制
  97. } else {
  98. dbconn.SetMaxOpenConns(200) //默认值为0表示不限制
  99. }
  100. if len(conns) > 1 {
  101. dbconn.SetMaxIdleConns(conns[1]) //用于设置闲置的连接数
  102. } else {
  103. dbconn.SetMaxIdleConns(50)
  104. }
  105. // dbconn.Ping()
  106. // log.Println(dbconn.Stats())
  107. log.Println("database connected")
  108. dbconn.SetConnMaxLifetime(time.Minute * 1)
  109. break
  110. }
  111. }
  112. } else {
  113. return nil, errors.New("db connection params errors")
  114. }
  115. return dbconn, dbConnErr
  116. }
  117. // func CloseSlaverConn() error {
  118. // return SLAVER_DB.Close()
  119. // }
  120. /**
  121. * 检测表名
  122. */
  123. func getTableName(dbName, table string, dbtype ...string) string {
  124. var db_type string = "mysql"
  125. if len(dbtype) > 0 {
  126. if dbtype[0] != "" {
  127. db_type = dbtype[0]
  128. }
  129. }
  130. var ret string
  131. switch db_type {
  132. case "mysql":
  133. if strings.Contains(table, ".") {
  134. ret = table
  135. }
  136. if dbName != "" {
  137. ret = StringJoin(dbName, ".", table)
  138. } else {
  139. ret = table
  140. }
  141. case "mssql":
  142. ret = StringJoin(dbName, ".", table)
  143. }
  144. return ret
  145. }
  146. func GetDbTableName(dbName, table string) string {
  147. return getTableName(dbName, table)
  148. }
  149. func judg() []string {
  150. return []string{"=", ">", "<", "!=", "<=", ">="}
  151. }