You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-fly/models/models.go

44 lines
967 B

5 years ago
package models
import (
"fmt"
"github.com/jinzhu/gorm"
"github.com/taoshihan1991/imaptool/config"
4 years ago
"github.com/taoshihan1991/imaptool/tools"
4 years ago
"log"
"time"
5 years ago
)
5 years ago
var DB *gorm.DB
type Model struct {
ID uint `gorm:"primary_key" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `sql:"index" json:"deleted_at"`
}
func init() {
mysql := config.CreateMysql()
4 years ago
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", mysql.Username, mysql.Password, mysql.Server, mysql.Port, mysql.Database)
var err error
DB, err = gorm.Open("mysql", dsn)
if err != nil {
4 years ago
log.Println(err)
panic("数据库连接失败!")
}
DB.SingularTable(true)
DB.LogMode(true)
4 years ago
DB.SetLogger(tools.Logger())
DB.DB().SetMaxIdleConns(10)
DB.DB().SetMaxOpenConns(100)
InitConfig()
}
func Execute(sql string) error {
return DB.Exec(sql).Error
}
func CloseDB() {
5 years ago
defer DB.Close()
}