|
|
- package tencentdb
-
- import (
- "database/sql"
- "log"
-
- "errors"
- "strings"
- "time"
-
- _ "github.com/go-sql-driver/mysql"
- )
-
- var DB *sql.DB
-
- var dbhost_slaver, dbname_slaver, dbport_slaver, dbuser_slaver, dbpwd_slaver string //从库信息
-
- var use_slaver bool = false
-
- func Connect(DBHOST, DBUSER, DBPWD, DBNAME, DBPORT string, conns ...int) error {
-
- log.Println("database connectting...")
- var dbConnErr error
- if DBHOST != "" && DBUSER != "" && DBPWD != "" && DBPORT != "" { //&& DBNAME != ""
-
- for i := 0; i < 10; i++ {
- DB, dbConnErr = sql.Open("mysql", DBUSER+":"+DBPWD+"@tcp("+DBHOST+":"+DBPORT+")/"+DBNAME+"?charset=utf8mb4")
- if dbConnErr != nil {
- log.Println("ERROR", "can not connect to Database, ", dbConnErr)
- time.Sleep(time.Second * 5)
- } else {
- if len(conns) > 0 {
- DB.SetMaxOpenConns(conns[0]) //用于设置最大打开的连接数,默认值为0表示不限制
- } else {
- DB.SetMaxOpenConns(200) //默认值为0表示不限制
- }
- if len(conns) > 1 {
- DB.SetMaxIdleConns(conns[1]) //用于设置闲置的连接数
- } else {
- DB.SetMaxIdleConns(50)
- }
-
- DB.Ping()
-
- log.Println("database connected")
- DB.SetConnMaxLifetime(time.Minute * 2)
- break
- }
- }
- } else {
- return errors.New("db connection params errors")
- }
- return dbConnErr
- }
-
- func CloseConn() error {
- return DB.Close()
- }
-
- func ConnectSlaver(DBHOST, DBUSER_SLAVER, DBPWD_SLAVER, DBNAME, DBPORT string, conns ...int) {
-
- dbhost_slaver = DBHOST
- dbuser_slaver = DBUSER_SLAVER
- dbpwd_slaver = DBPWD_SLAVER
- dbname_slaver = DBNAME
- dbport_slaver = DBPORT
-
- use_slaver = true
-
- // log.Println("database connectting with slaver...")
- // var dbConnErr error
- // if DBHOST != "" && DBUSER_SLAVER != "" && DBPWD_SLAVER != "" && DBPORT != "" { //&& DBNAME != ""
-
- // for i := 0; i < 10; i++ {
- // SLAVER_DB, dbConnErr = sql.Open("mysql", DBUSER_SLAVER+":"+DBPWD_SLAVER+"@tcp("+DBHOST+":"+DBPORT+")/"+DBNAME+"?charset=utf8mb4")
- // if dbConnErr != nil {
- // log.Println("ERROR", "can not connect to Database, ", dbConnErr)
- // time.Sleep(time.Second * 5)
- // } else {
- // if len(conns) > 0 {
- // SLAVER_DB.SetMaxOpenConns(conns[0]) //用于设置最大打开的连接数,默认值为0表示不限制
- // } else {
- // SLAVER_DB.SetMaxOpenConns(200) //默认值为0表示不限制
- // }
- // if len(conns) > 1 {
- // SLAVER_DB.SetMaxIdleConns(conns[1]) //用于设置闲置的连接数
- // } else {
- // SLAVER_DB.SetMaxIdleConns(50)
- // }
-
- // SLAVER_DB.Ping()
-
- // log.Println("database connected")
- // SLAVER_DB.SetConnMaxLifetime(time.Minute * 2)
- // break
- // }
- // }
- // } else {
- // return errors.New("db connection params errors")
- // }
- // return dbConnErr
- }
-
- func connectDb(dbhost, dbuser, dbpwd, dbname, dbport string, conns ...int) (*sql.DB, error) {
-
- log.Println("database connectting...")
- var dbconn *sql.DB
- var dbConnErr error
- if dbhost != "" && dbuser != "" && dbpwd != "" && dbport != "" {
-
- for i := 0; i < 10; i++ {
- dbconn, dbConnErr = sql.Open("mysql", dbuser+":"+dbpwd+"@tcp("+dbhost+":"+dbport+")/"+dbname+"?charset=utf8mb4")
- if dbConnErr != nil {
- log.Println("ERROR", "can not connect to Database, ", dbConnErr)
- time.Sleep(time.Second * 5)
- } else {
- if len(conns) > 0 {
- dbconn.SetMaxOpenConns(conns[0]) //用于设置最大打开的连接数,默认值为0表示不限制
- } else {
- dbconn.SetMaxOpenConns(200) //默认值为0表示不限制
- }
- if len(conns) > 1 {
- dbconn.SetMaxIdleConns(conns[1]) //用于设置闲置的连接数
- } else {
- dbconn.SetMaxIdleConns(50)
- }
-
- // dbconn.Ping()
- // log.Println(dbconn.Stats())
-
- log.Println("database connected")
- dbconn.SetConnMaxLifetime(time.Minute * 1)
- break
- }
- }
- } else {
- return nil, errors.New("db connection params errors")
- }
- return dbconn, dbConnErr
- }
-
- // func CloseSlaverConn() error {
- // return SLAVER_DB.Close()
- // }
-
- /**
- * 检测表名
- */
- func getTableName(dbName, table string, dbtype ...string) string {
-
- var db_type string = "mysql"
-
- if len(dbtype) > 0 {
- if dbtype[0] != "" {
- db_type = dbtype[0]
- }
- }
-
- var ret string
-
- switch db_type {
-
- case "mysql":
-
- if strings.Contains(table, ".") {
- ret = table
- }
- if dbName != "" {
- ret = StringJoin(dbName, ".", table)
- } else {
- ret = table
- }
-
- case "mssql":
- ret = StringJoin(dbName, ".", table)
-
- }
- return ret
- }
-
- func GetDbTableName(dbName, table string) string {
- return getTableName(dbName, table)
- }
-
- func judg() []string {
- return []string{"=", ">", "<", "!=", "<=", ">="}
- }
|