models.go 754 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package models
  2. import (
  3. "fmt"
  4. "git.wanbits.cc/sin/flytalk/config"
  5. "gorm.io/driver/mysql"
  6. "gorm.io/gorm"
  7. "time"
  8. )
  9. var DB *gorm.DB
  10. type Model struct {
  11. ID uint `gorm:"primary_key" json:"id"`
  12. CreatedAt time.Time `json:"created_at"`
  13. UpdatedAt time.Time `json:"updated_at"`
  14. DeletedAt *time.Time `sql:"index" json:"deleted_at"`
  15. }
  16. func InitDb() {
  17. dbc := config.C.Database
  18. dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
  19. dbc.Username, dbc.Password, dbc.Server, dbc.Port, dbc.Db)
  20. var err error
  21. DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
  22. if err != nil {
  23. panic("connect to database failed")
  24. }
  25. }
  26. func Execute(sql string) {
  27. if len(sql) > 0 {
  28. DB.Exec(sql)
  29. }
  30. }
  31. func CloseDB() {
  32. }