github.com/kotovmak/go-admin@v1.1.1/modules/db/mysql.go (about)

     1  // Copyright 2019 GoAdmin Core Team. All rights reserved.
     2  // Use of this source code is governed by a Apache-2.0 style
     3  // license that can be found in the LICENSE file.
     4  
     5  package db
     6  
     7  import (
     8  	"database/sql"
     9  
    10  	"github.com/kotovmak/go-admin/modules/config"
    11  )
    12  
    13  // SQLTx is an in-progress database transaction.
    14  type SQLTx struct {
    15  	Tx *sql.Tx
    16  }
    17  
    18  // Mysql is a Connection of mysql.
    19  type Mysql struct {
    20  	Base
    21  }
    22  
    23  // GetMysqlDB return the global mysql connection.
    24  func GetMysqlDB() *Mysql {
    25  	return &Mysql{
    26  		Base: Base{
    27  			DbList: make(map[string]*sql.DB),
    28  		},
    29  	}
    30  }
    31  
    32  // Name implements the method Connection.Name.
    33  func (db *Mysql) Name() string {
    34  	return "mysql"
    35  }
    36  
    37  // GetDelimiter implements the method Connection.GetDelimiter.
    38  func (db *Mysql) GetDelimiter() string {
    39  	return "`"
    40  }
    41  
    42  // GetDelimiter2 implements the method Connection.GetDelimiter2.
    43  func (db *Mysql) GetDelimiter2() string {
    44  	return "`"
    45  }
    46  
    47  // GetDelimiters implements the method Connection.GetDelimiters.
    48  func (db *Mysql) GetDelimiters() []string {
    49  	return []string{"`", "`"}
    50  }
    51  
    52  // InitDB implements the method Connection.InitDB.
    53  func (db *Mysql) InitDB(cfgs map[string]config.Database) Connection {
    54  	db.Configs = cfgs
    55  	db.Once.Do(func() {
    56  		for conn, cfg := range cfgs {
    57  
    58  			sqlDB, err := sql.Open("mysql", cfg.GetDSN())
    59  
    60  			if err != nil {
    61  				if sqlDB != nil {
    62  					_ = sqlDB.Close()
    63  				}
    64  				panic(err)
    65  			}
    66  
    67  			// Largest set up the database connection reduce time wait
    68  			sqlDB.SetMaxIdleConns(cfg.MaxIdleConns)
    69  			sqlDB.SetMaxOpenConns(cfg.MaxOpenConns)
    70  			sqlDB.SetConnMaxLifetime(cfg.ConnMaxLifetime)
    71  			sqlDB.SetConnMaxIdleTime(cfg.ConnMaxIdleTime)
    72  
    73  			db.DbList[conn] = sqlDB
    74  
    75  			if err := sqlDB.Ping(); err != nil {
    76  				panic(err)
    77  			}
    78  		}
    79  	})
    80  	return db
    81  }
    82  
    83  // QueryWithConnection implements the method Connection.QueryWithConnection.
    84  func (db *Mysql) QueryWithConnection(con string, query string, args ...interface{}) ([]map[string]interface{}, error) {
    85  	return CommonQuery(db.DbList[con], query, args...)
    86  }
    87  
    88  // ExecWithConnection implements the method Connection.ExecWithConnection.
    89  func (db *Mysql) ExecWithConnection(con string, query string, args ...interface{}) (sql.Result, error) {
    90  	return CommonExec(db.DbList[con], query, args...)
    91  }
    92  
    93  // Query implements the method Connection.Query.
    94  func (db *Mysql) Query(query string, args ...interface{}) ([]map[string]interface{}, error) {
    95  	return CommonQuery(db.DbList["default"], query, args...)
    96  }
    97  
    98  // Exec implements the method Connection.Exec.
    99  func (db *Mysql) Exec(query string, args ...interface{}) (sql.Result, error) {
   100  	return CommonExec(db.DbList["default"], query, args...)
   101  }
   102  
   103  // QueryWithTx is query method within the transaction.
   104  func (db *Mysql) QueryWithTx(tx *sql.Tx, query string, args ...interface{}) ([]map[string]interface{}, error) {
   105  	return CommonQueryWithTx(tx, query, args...)
   106  }
   107  
   108  // ExecWithTx is exec method within the transaction.
   109  func (db *Mysql) ExecWithTx(tx *sql.Tx, query string, args ...interface{}) (sql.Result, error) {
   110  	return CommonExecWithTx(tx, query, args...)
   111  }
   112  
   113  func (db *Mysql) QueryWith(tx *sql.Tx, conn, query string, args ...interface{}) ([]map[string]interface{}, error) {
   114  	if tx != nil {
   115  		return db.QueryWithTx(tx, query, args...)
   116  	}
   117  	return db.QueryWithConnection(conn, query, args...)
   118  }
   119  
   120  func (db *Mysql) ExecWith(tx *sql.Tx, conn, query string, args ...interface{}) (sql.Result, error) {
   121  	if tx != nil {
   122  		return db.ExecWithTx(tx, query, args...)
   123  	}
   124  	return db.ExecWithConnection(conn, query, args...)
   125  }
   126  
   127  // BeginTxWithReadUncommitted starts a transaction with level LevelReadUncommitted.
   128  func (db *Mysql) BeginTxWithReadUncommitted() *sql.Tx {
   129  	return CommonBeginTxWithLevel(db.DbList["default"], sql.LevelReadUncommitted)
   130  }
   131  
   132  // BeginTxWithReadCommitted starts a transaction with level LevelReadCommitted.
   133  func (db *Mysql) BeginTxWithReadCommitted() *sql.Tx {
   134  	return CommonBeginTxWithLevel(db.DbList["default"], sql.LevelReadCommitted)
   135  }
   136  
   137  // BeginTxWithRepeatableRead starts a transaction with level LevelRepeatableRead.
   138  func (db *Mysql) BeginTxWithRepeatableRead() *sql.Tx {
   139  	return CommonBeginTxWithLevel(db.DbList["default"], sql.LevelRepeatableRead)
   140  }
   141  
   142  // BeginTx starts a transaction with level LevelDefault.
   143  func (db *Mysql) BeginTx() *sql.Tx {
   144  	return CommonBeginTxWithLevel(db.DbList["default"], sql.LevelDefault)
   145  }
   146  
   147  // BeginTxWithLevel starts a transaction with given transaction isolation level.
   148  func (db *Mysql) BeginTxWithLevel(level sql.IsolationLevel) *sql.Tx {
   149  	return CommonBeginTxWithLevel(db.DbList["default"], level)
   150  }
   151  
   152  // BeginTxWithReadUncommittedAndConnection starts a transaction with level LevelReadUncommitted and connection.
   153  func (db *Mysql) BeginTxWithReadUncommittedAndConnection(conn string) *sql.Tx {
   154  	return CommonBeginTxWithLevel(db.DbList[conn], sql.LevelReadUncommitted)
   155  }
   156  
   157  // BeginTxWithReadCommittedAndConnection starts a transaction with level LevelReadCommitted and connection.
   158  func (db *Mysql) BeginTxWithReadCommittedAndConnection(conn string) *sql.Tx {
   159  	return CommonBeginTxWithLevel(db.DbList[conn], sql.LevelReadCommitted)
   160  }
   161  
   162  // BeginTxWithRepeatableReadAndConnection starts a transaction with level LevelRepeatableRead and connection.
   163  func (db *Mysql) BeginTxWithRepeatableReadAndConnection(conn string) *sql.Tx {
   164  	return CommonBeginTxWithLevel(db.DbList[conn], sql.LevelRepeatableRead)
   165  }
   166  
   167  // BeginTxAndConnection starts a transaction with level LevelDefault and connection.
   168  func (db *Mysql) BeginTxAndConnection(conn string) *sql.Tx {
   169  	return CommonBeginTxWithLevel(db.DbList[conn], sql.LevelDefault)
   170  }
   171  
   172  // BeginTxWithLevelAndConnection starts a transaction with given transaction isolation level and connection.
   173  func (db *Mysql) BeginTxWithLevelAndConnection(conn string, level sql.IsolationLevel) *sql.Tx {
   174  	return CommonBeginTxWithLevel(db.DbList[conn], level)
   175  }