feat: add gormv2 and support sqlite
This commit is contained in:
+33
-91
@@ -2,32 +2,33 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jinzhu/gorm"
|
||||
_ "github.com/jinzhu/gorm/dialects/mysql"
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
"log"
|
||||
"message-nest/pkg/setting"
|
||||
"message-nest/pkg/util"
|
||||
"time"
|
||||
)
|
||||
|
||||
var db *gorm.DB
|
||||
|
||||
type IDModel struct {
|
||||
ID int `gorm:"type:int(11) AUTO_INCREMENT comment 'id';primary_key" json:"id"`
|
||||
ID uint `gorm:"autoIncrement;type:integer;primaryKey" json:"id"`
|
||||
|
||||
CreatedBy string `json:"created_by" gorm:"type:varchar(100) comment '创建人';default:'';"`
|
||||
ModifiedBy string `json:"modified_by" gorm:"type:varchar(100) comment '修改人';default:'';"`
|
||||
CreatedOn util.Time `json:"created_on" gorm:"type:timestamp comment '创建时间';default:current_timestamp;"`
|
||||
ModifiedOn util.Time `json:"modified_on" gorm:"type:timestamp comment '更新时间';"`
|
||||
CreatedBy string `json:"created_by" gorm:"type:varchar(100) ;default:'';"`
|
||||
ModifiedBy string `json:"modified_by" gorm:"type:varchar(100) ;default:'';"`
|
||||
CreatedAt util.Time `json:"created_on" gorm:"column:created_on;autoCreateTime "`
|
||||
UpdatedAt util.Time `json:"modified_on" gorm:"column:modified_on;autoUpdateTime ;"`
|
||||
}
|
||||
|
||||
type UUIDModel struct {
|
||||
ID string `gorm:"type:varchar(12) comment 'id';primary_key" json:"id"`
|
||||
ID string `gorm:"type:varchar(12) ;primaryKey" json:"id"`
|
||||
|
||||
CreatedBy string `json:"created_by" gorm:"type:varchar(100) comment '创建人';default:'';"`
|
||||
ModifiedBy string `json:"modified_by" gorm:"type:varchar(100) comment '修改人';default:'';"`
|
||||
CreatedOn util.Time `json:"created_on" gorm:"type:timestamp comment '创建时间';default:current_timestamp;"`
|
||||
ModifiedOn util.Time `json:"modified_on" gorm:"type:timestamp comment '更新时间';"`
|
||||
CreatedBy string `json:"created_by" gorm:"type:varchar(100) ;default:'';"`
|
||||
ModifiedBy string `json:"modified_by" gorm:"type:varchar(100) ;default:'';"`
|
||||
CreatedAt util.Time `json:"created_on" gorm:"column:created_on;autoCreateTime "`
|
||||
UpdatedAt util.Time `json:"modified_on" gorm:"column:modified_on;autoUpdateTime ;"`
|
||||
}
|
||||
|
||||
// Setup initializes the database instance
|
||||
@@ -39,92 +40,33 @@ func Setup() *gorm.DB {
|
||||
setting.DatabaseSetting.Host,
|
||||
setting.DatabaseSetting.Port,
|
||||
setting.DatabaseSetting.Name)
|
||||
db, err = gorm.Open(setting.DatabaseSetting.Type, connStr)
|
||||
|
||||
config := &gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
TablePrefix: setting.DatabaseSetting.TablePrefix,
|
||||
},
|
||||
}
|
||||
|
||||
switch setting.DatabaseSetting.Type {
|
||||
case "mysql":
|
||||
db, err = gorm.Open(mysql.Open(connStr), config)
|
||||
case "sqlite":
|
||||
db, err = gorm.Open(sqlite.Open("conf/database.db"), config)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("models.Setup err: %v", err)
|
||||
}
|
||||
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
|
||||
return setting.DatabaseSetting.TablePrefix + defaultTableName
|
||||
}
|
||||
|
||||
if setting.DatabaseSetting.SqlDebug == "enable" {
|
||||
db.LogMode(true)
|
||||
db = db.Debug()
|
||||
}
|
||||
db.SingularTable(true)
|
||||
db.Callback().Create().Replace("gorm:update_time_stamp", updateTimeStampForCreateCallback)
|
||||
db.Callback().Update().Replace("gorm:update_time_stamp", updateTimeStampForUpdateCallback)
|
||||
db.Callback().Delete().Replace("gorm:delete", deleteCallback)
|
||||
db.DB().SetMaxIdleConns(10)
|
||||
db.DB().SetMaxOpenConns(100)
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
// CloseDB closes database connection (unnecessary)
|
||||
func CloseDB() {
|
||||
defer db.Close()
|
||||
}
|
||||
|
||||
// updateTimeStampForCreateCallback will set `CreatedOn`, `ModifiedOn` when creating
|
||||
func updateTimeStampForCreateCallback(scope *gorm.Scope) {
|
||||
if !scope.HasError() {
|
||||
nowTime := time.Now()
|
||||
|
||||
if createTimeField, ok := scope.FieldByName("CreatedOn"); ok {
|
||||
if createTimeField.IsBlank {
|
||||
createTimeField.Set(nowTime)
|
||||
}
|
||||
}
|
||||
|
||||
if modifyTimeField, ok := scope.FieldByName("ModifiedOn"); ok {
|
||||
if modifyTimeField.IsBlank {
|
||||
modifyTimeField.Set(nowTime)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// updateTimeStampForUpdateCallback will set `ModifiedOn` when updating
|
||||
func updateTimeStampForUpdateCallback(scope *gorm.Scope) {
|
||||
if _, ok := scope.Get("gorm:update_column"); !ok {
|
||||
scope.SetColumn("ModifiedOn", time.Now())
|
||||
}
|
||||
}
|
||||
|
||||
// deleteCallback will set `DeletedOn` where deleting
|
||||
func deleteCallback(scope *gorm.Scope) {
|
||||
if !scope.HasError() {
|
||||
var extraOption string
|
||||
if str, ok := scope.Get("gorm:delete_option"); ok {
|
||||
extraOption = fmt.Sprint(str)
|
||||
}
|
||||
|
||||
deletedOnField, hasDeletedOnField := scope.FieldByName("DeletedOn")
|
||||
|
||||
if !scope.Search.Unscoped && hasDeletedOnField {
|
||||
scope.Raw(fmt.Sprintf(
|
||||
"UPDATE %v SET %v=%v%v%v",
|
||||
scope.QuotedTableName(),
|
||||
scope.Quote(deletedOnField.DBName),
|
||||
scope.AddToVars(time.Now().Unix()),
|
||||
addExtraSpaceIfExist(scope.CombinedConditionSql()),
|
||||
addExtraSpaceIfExist(extraOption),
|
||||
)).Exec()
|
||||
} else {
|
||||
scope.Raw(fmt.Sprintf(
|
||||
"DELETE FROM %v%v%v",
|
||||
scope.QuotedTableName(),
|
||||
addExtraSpaceIfExist(scope.CombinedConditionSql()),
|
||||
addExtraSpaceIfExist(extraOption),
|
||||
)).Exec()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// addExtraSpaceIfExist adds a separator
|
||||
func addExtraSpaceIfExist(str string) string {
|
||||
if str != "" {
|
||||
return " " + str
|
||||
}
|
||||
return ""
|
||||
func GetSchema(table any) string {
|
||||
stmt := &gorm.Statement{DB: db}
|
||||
stmt.Parse(table)
|
||||
return stmt.Schema.Table
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user