123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- /**
- #*****************************************************************************
- # @file database.go
- # @author MakerYang(https://www.makeryang.com)
- # @statement 免费课程配套开源项目,任何形式收费均为盗版
- #*****************************************************************************
- */
- package Database
- import (
- "Game/framework/config"
- "fmt"
- "github.com/gookit/color"
- "github.com/jinzhu/gorm"
- _ "github.com/jinzhu/gorm/dialects/mysql"
- "log"
- "time"
- )
- // Get 定义数据库模块的调用指针
- var Get *gorm.DB
- // DefaultField 声明数据库默认字段
- type DefaultField struct {
- CreateAt int `gorm:"Column:create_at" json:"create_at"`
- UpdateAt int `gorm:"Column:update_at" json:"update_at"`
- DeleteAt int `gorm:"Column:delete_at" json:"delete_at"`
- }
- // Init 初始化数据库连接
- func Init() {
- var err error
- Get, err = gorm.Open(Config.Get.Database.Type, fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", Config.Get.Database.User, Config.Get.Database.Password, Config.Get.Database.Host, Config.Get.Database.Name))
- if err != nil {
- log.Println("[database]", color.Red.Text(err.Error()))
- }
- if Config.Get.Service.Mode == "release" {
- Get.LogMode(false)
- } else {
- Get.LogMode(true)
- }
- gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
- return defaultTableName
- }
- Get.SingularTable(true)
- Get.Callback().Create().Replace("gorm:update_time_stamp", func(scope *gorm.Scope) {
- if !scope.HasError() {
- nowTime := time.Now().Unix()
- if createTimeField, ok := scope.FieldByName("CreateAt"); ok {
- if createTimeField.IsBlank {
- err := createTimeField.Set(nowTime)
- if err != nil {
- }
- }
- }
- if modifyTimeField, ok := scope.FieldByName("UpdateAt"); ok {
- if modifyTimeField.IsBlank {
- err := modifyTimeField.Set(nowTime)
- if err != nil {
- }
- }
- }
- }
- })
- Get.DB().SetMaxIdleConns(1000)
- Get.DB().SetMaxOpenConns(10000)
- Get.DB().SetConnMaxLifetime(time.Second * 45)
- }
|