|
|
|
|
|
|
|
|
|
package db |
|
|
|
import ( |
|
"database/sql" |
|
|
|
"github.com/GoAdminGroup/go-admin/modules/config" |
|
) |
|
|
|
|
|
type SQLTx struct { |
|
Tx *sql.Tx |
|
} |
|
|
|
|
|
type Mysql struct { |
|
Base |
|
} |
|
|
|
|
|
func GetMysqlDB() *Mysql { |
|
return &Mysql{ |
|
Base: Base{ |
|
DbList: make(map[string]*sql.DB), |
|
}, |
|
} |
|
} |
|
|
|
|
|
func (db *Mysql) Name() string { |
|
return "mysql" |
|
} |
|
|
|
|
|
func (db *Mysql) GetDelimiter() string { |
|
return "`" |
|
} |
|
|
|
|
|
func (db *Mysql) GetDelimiter2() string { |
|
return "`" |
|
} |
|
|
|
|
|
func (db *Mysql) GetDelimiters() []string { |
|
return []string{"`", "`"} |
|
} |
|
|
|
|
|
func (db *Mysql) InitDB(cfgs map[string]config.Database) Connection { |
|
db.Configs = cfgs |
|
db.Once.Do(func() { |
|
for conn, cfg := range cfgs { |
|
|
|
sqlDB, err := sql.Open("mysql", cfg.GetDSN()) |
|
|
|
if err != nil { |
|
if sqlDB != nil { |
|
_ = sqlDB.Close() |
|
} |
|
panic(err) |
|
} |
|
|
|
|
|
sqlDB.SetMaxIdleConns(cfg.MaxIdleConns) |
|
sqlDB.SetMaxOpenConns(cfg.MaxOpenConns) |
|
sqlDB.SetConnMaxLifetime(cfg.ConnMaxLifetime) |
|
sqlDB.SetConnMaxIdleTime(cfg.ConnMaxIdleTime) |
|
|
|
db.DbList[conn] = sqlDB |
|
|
|
if err := sqlDB.Ping(); err != nil { |
|
panic(err) |
|
} |
|
} |
|
}) |
|
return db |
|
} |
|
|
|
|
|
func (db *Mysql) QueryWithConnection(con string, query string, args ...interface{}) ([]map[string]interface{}, error) { |
|
return CommonQuery(db.DbList[con], query, args...) |
|
} |
|
|
|
|
|
func (db *Mysql) ExecWithConnection(con string, query string, args ...interface{}) (sql.Result, error) { |
|
return CommonExec(db.DbList[con], query, args...) |
|
} |
|
|
|
|
|
func (db *Mysql) Query(query string, args ...interface{}) ([]map[string]interface{}, error) { |
|
return CommonQuery(db.DbList["default"], query, args...) |
|
} |
|
|
|
|
|
func (db *Mysql) Exec(query string, args ...interface{}) (sql.Result, error) { |
|
return CommonExec(db.DbList["default"], query, args...) |
|
} |
|
|
|
|
|
func (db *Mysql) QueryWithTx(tx *sql.Tx, query string, args ...interface{}) ([]map[string]interface{}, error) { |
|
return CommonQueryWithTx(tx, query, args...) |
|
} |
|
|
|
|
|
func (db *Mysql) ExecWithTx(tx *sql.Tx, query string, args ...interface{}) (sql.Result, error) { |
|
return CommonExecWithTx(tx, query, args...) |
|
} |
|
|
|
func (db *Mysql) QueryWith(tx *sql.Tx, conn, query string, args ...interface{}) ([]map[string]interface{}, error) { |
|
if tx != nil { |
|
return db.QueryWithTx(tx, query, args...) |
|
} |
|
return db.QueryWithConnection(conn, query, args...) |
|
} |
|
|
|
func (db *Mysql) ExecWith(tx *sql.Tx, conn, query string, args ...interface{}) (sql.Result, error) { |
|
if tx != nil { |
|
return db.ExecWithTx(tx, query, args...) |
|
} |
|
return db.ExecWithConnection(conn, query, args...) |
|
} |
|
|
|
|
|
func (db *Mysql) BeginTxWithReadUncommitted() *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList["default"], sql.LevelReadUncommitted) |
|
} |
|
|
|
|
|
func (db *Mysql) BeginTxWithReadCommitted() *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList["default"], sql.LevelReadCommitted) |
|
} |
|
|
|
|
|
func (db *Mysql) BeginTxWithRepeatableRead() *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList["default"], sql.LevelRepeatableRead) |
|
} |
|
|
|
|
|
func (db *Mysql) BeginTx() *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList["default"], sql.LevelDefault) |
|
} |
|
|
|
|
|
func (db *Mysql) BeginTxWithLevel(level sql.IsolationLevel) *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList["default"], level) |
|
} |
|
|
|
|
|
func (db *Mysql) BeginTxWithReadUncommittedAndConnection(conn string) *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList[conn], sql.LevelReadUncommitted) |
|
} |
|
|
|
|
|
func (db *Mysql) BeginTxWithReadCommittedAndConnection(conn string) *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList[conn], sql.LevelReadCommitted) |
|
} |
|
|
|
|
|
func (db *Mysql) BeginTxWithRepeatableReadAndConnection(conn string) *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList[conn], sql.LevelRepeatableRead) |
|
} |
|
|
|
|
|
func (db *Mysql) BeginTxAndConnection(conn string) *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList[conn], sql.LevelDefault) |
|
} |
|
|
|
|
|
func (db *Mysql) BeginTxWithLevelAndConnection(conn string, level sql.IsolationLevel) *sql.Tx { |
|
return CommonBeginTxWithLevel(db.DbList[conn], level) |
|
} |
|
|