github.com/dolotech/hongbao@v0.0.0-20191130105438-fd59d7a5dda5/src/utils/db/db.go (about) 1 package db 2 3 import ( 4 "fmt" 5 "github.com/golang/glog" 6 "github.com/jinzhu/gorm" 7 _ "github.com/jinzhu/gorm/dialects/mysql" 8 "sync" 9 "time" 10 ) 11 12 func Get() *gorm.DB { 13 if db == nil { 14 glog.Fatalln("数据库未连接") 15 } 16 return db 17 } 18 19 type MysqlConnectiPool struct { 20 } 21 22 var instance *MysqlConnectiPool 23 var once sync.Once 24 25 var db *gorm.DB 26 var err_db error 27 28 func InitMysql(user, password, host, dbname string) { 29 once.Do(func() { 30 instance = &MysqlConnectiPool{} 31 b := instance.initPool(user, password, host, dbname) 32 if b { 33 glog.Info("数据库连接成功") 34 } else { 35 glog.Info("数据库连接失败") 36 } 37 }) 38 } 39 40 /* 41 * @fuc 初始化数据库连接(可在mail()适当位置调用) 42 */ 43 func (m *MysqlConnectiPool) initPool(user, password, host, dbname string) (issucc bool) { 44 db, err_db = gorm.Open("mysql", fmt.Sprintf("%v:%v@tcp(%v)/%v?charset=utf8&parseTime=True&loc=Local", user, password, host, dbname)) 45 if err_db != nil { 46 glog.Error(err_db) 47 return false 48 } 49 db.DB().SetMaxIdleConns(10) 50 db.DB().SetMaxOpenConns(50) 51 db.DB().SetConnMaxLifetime(time.Hour) 52 //db.Callback().Create().Replace("gorm:create_time_stamp", updateTimeStampForCreateCallback) 53 //db.Callback().Update().Replace("gorm:update_time_stamp", updateTimeStampForUpdateCallback) 54 //db.Callback().Delete().Replace("gorm:delete", deleteCallback) 55 return true 56 } 57 func (m *MysqlConnectiPool) GetMysqlDB() (db_con *gorm.DB) { 58 return db 59 } 60 61 // // 注册新建钩子在持久化之前 62 func updateTimeStampForCreateCallback(scope *gorm.Scope) { 63 glog.Error("create_time") 64 if !scope.HasError() { 65 nowTime := time.Now().Unix() 66 if createTimeField, ok := scope.FieldByName("create_time"); ok { 67 glog.Error("create_time") 68 if createTimeField.IsBlank { 69 createTimeField.Set(nowTime) 70 } 71 } 72 73 if modifyTimeField, ok := scope.FieldByName("update_time"); ok { 74 glog.Error("update_time") 75 if modifyTimeField.IsBlank { 76 modifyTimeField.Set(nowTime) 77 } 78 } 79 } 80 } 81 82 // 注册更新钩子在持久化之前 83 func updateTimeStampForUpdateCallback(scope *gorm.Scope) { 84 if _, ok := scope.Get("gorm:update_time"); !ok { 85 86 glog.Error("update_time") 87 scope.SetColumn("update_time", time.Now().Unix()) 88 } 89 } 90 91 // 注册删除钩子在删除之前 92 func deleteCallback(scope *gorm.Scope) { 93 if !scope.HasError() { 94 var extraOption string 95 if str, ok := scope.Get("gorm:delete_option"); ok { 96 extraOption = fmt.Sprint(str) 97 } 98 99 deletedOnField, hasDeletedOnField := scope.FieldByName("DeletedTime") 100 101 if !scope.Search.Unscoped && hasDeletedOnField { 102 scope.Raw(fmt.Sprintf( 103 "UPDATE %v SET %v=%v%v%v", 104 scope.QuotedTableName(), 105 scope.Quote(deletedOnField.DBName), 106 scope.AddToVars(time.Now().Unix()), 107 addExtraSpaceIfExist(scope.CombinedConditionSql()), 108 addExtraSpaceIfExist(extraOption), 109 )).Exec() 110 } else { 111 scope.Raw(fmt.Sprintf( 112 "DELETE FROM %v%v%v", 113 scope.QuotedTableName(), 114 addExtraSpaceIfExist(scope.CombinedConditionSql()), 115 addExtraSpaceIfExist(extraOption), 116 )).Exec() 117 } 118 } 119 } 120 func addExtraSpaceIfExist(str string) string { 121 if str != "" { 122 return " " + str 123 } 124 return "" 125 }