github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/sqldb/mysql/mysqldb.go (about) 1 // Copyright 2023 IAC. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package mysqldb 16 17 import ( 18 "database/sql" 19 "fmt" 20 "sync" 21 "time" 22 23 com "github.com/mdaxf/iac/com" 24 "github.com/mdaxf/iac/logger" 25 26 _ "github.com/denisenkom/go-mssqldb" 27 _ "github.com/go-sql-driver/mysql" 28 ) 29 30 type DBConn com.DBConn 31 32 var once sync.Once 33 34 func (db *DBConn) Connect() error { 35 // Function execution logging 36 iLog := logger.Log{ModuleName: logger.Framework, User: "System", ControllerName: "Database.Connect"} 37 startTime := time.Now() 38 defer func() { 39 elapsed := time.Since(startTime) 40 iLog.PerformanceWithDuration("database.Connect", elapsed) 41 }() 42 43 // Recover from any panics and log the error 44 defer func() { 45 if err := recover(); err != nil { 46 iLog.Error(fmt.Sprintf("Connect Database defer error: %s", err)) 47 // ctx.JSON(http.StatusBadRequest, gin.H{"error": err}) 48 } 49 }() 50 // Log the database connection details 51 iLog.Info(fmt.Sprintf("Connect Database: %s %s", db.DBType, db.DBConnection)) 52 53 // Establish the database connection if it hasn't been done before 54 once.Do(func() { 55 DB, err := sql.Open(db.DBType, db.DBConnection) 56 if err != nil { 57 iLog.Error(fmt.Sprintf("Connect Database Error: %s", err.Error())) 58 return 59 } 60 DB.SetMaxIdleConns(db.MaxIdleConns) 61 DB.SetMaxOpenConns(db.MaxOpenConns) 62 63 db.DB = DB 64 }) 65 66 return nil 67 } 68 69 func (db *DBConn) Disconnect() error { 70 // Function execution logging 71 iLog := logger.Log{ModuleName: logger.Framework, User: "System", ControllerName: "Database.Close"} 72 startTime := time.Now() 73 defer func() { 74 elapsed := time.Since(startTime) 75 iLog.PerformanceWithDuration("database.Close", elapsed) 76 }() 77 78 // Recover from any panics and log the error 79 defer func() { 80 if err := recover(); err != nil { 81 iLog.Error(fmt.Sprintf("Close Database defer error: %s", err)) 82 // ctx.JSON(http.StatusBadRequest, gin.H{"error": err}) 83 } 84 }() 85 86 // Close the database connection 87 err := db.DB.Close() 88 if err != nil { 89 iLog.Error(fmt.Sprintf("Close Database Error: %s", err.Error())) 90 return err 91 } 92 93 return nil 94 } 95 96 func (db *DBConn) ReConnect() error { 97 // Function execution logging 98 iLog := logger.Log{ModuleName: logger.Framework, User: "System", ControllerName: "Database.ReConnect"} 99 startTime := time.Now() 100 defer func() { 101 elapsed := time.Since(startTime) 102 iLog.PerformanceWithDuration("database.ReConnect", elapsed) 103 }() 104 105 // Recover from any panics and log the error 106 defer func() { 107 if err := recover(); err != nil { 108 iLog.Error(fmt.Sprintf("ReConnect Database defer error: %s", err)) 109 // ctx.JSON(http.StatusBadRequest, gin.H{"error": err}) 110 } 111 }() 112 113 // Reconnect to the database 114 err := db.Connect() 115 if err != nil { 116 iLog.Error(fmt.Sprintf("ReConnect Database Error: %s", err.Error())) 117 return err 118 } 119 120 return nil 121 } 122 123 func (db *DBConn) Ping() error { 124 // Function execution logging 125 iLog := logger.Log{ModuleName: logger.Framework, User: "System", ControllerName: "Database.Ping"} 126 startTime := time.Now() 127 defer func() { 128 elapsed := time.Since(startTime) 129 iLog.PerformanceWithDuration("database.Ping", elapsed) 130 }() 131 132 // Recover from any panics and log the error 133 defer func() { 134 if err := recover(); err != nil { 135 iLog.Error(fmt.Sprintf("Ping Database defer error: %s", err)) 136 // ctx.JSON(http.StatusBadRequest, gin.H{"error": err}) 137 } 138 }() 139 140 iLog.Debug(fmt.Sprintf("Database.Ping: %v", db)) 141 // Ping the database to check if it is still alive 142 err := db.DB.Ping() 143 if err != nil { 144 iLog.Error(fmt.Sprintf("Ping Database Error: %s", err.Error())) 145 return err 146 } 147 148 return nil 149 }