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