Skip to content

Commit

Permalink
🎸 Optimize gorm pkg operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Cairry committed Dec 31, 2024
1 parent 2606c5d commit b057257
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 75 deletions.
1 change: 1 addition & 0 deletions internal/models/fault_center.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package models
127 changes: 52 additions & 75 deletions internal/repo/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,109 +18,86 @@ type InterGormDBCli interface {

func NewInterGormDBCli(db *gorm.DB) InterGormDBCli {
return &GormDBCli{
db,
db: db,
}
}

// Create 插入数据
func (g GormDBCli) Create(table, value interface{}) error {
return g.executeTransaction(func(tx *gorm.DB) error {
return tx.Model(table).Create(value).Error
}, "数据写入失败")
}

tx := g.db.Begin()
err := tx.Model(table).Create(value).Error
if err != nil {
tx.Rollback()
return fmt.Errorf("数据写入失败 -> %s", err)
}
err = tx.Commit().Error
if err != nil {
tx.Rollback()
return fmt.Errorf("事务提交失败 -> %s", err)
}

return nil

// Update 更新单条数据
func (g GormDBCli) Update(value Update) error {
return g.executeTransaction(func(tx *gorm.DB) error {
tx = tx.Model(value.Table)
for column, val := range value.Where {
tx = tx.Where(column, val)
}
return tx.Update(value.Update[0], value.Update[1:]).Error
}, "数据更新失败")
}

type Update struct {
Table interface{}
Where []interface{}
Update []string
// Updates 更新多条数据
func (g GormDBCli) Updates(value Updates) error {
return g.executeTransaction(func(tx *gorm.DB) error {
tx = tx.Model(value.Table)
for column, val := range value.Where {
tx = tx.Where(column, val)
}
return tx.Updates(value.Updates).Error
}, "数据更新失败")
}

func (g GormDBCli) Update(value Update) error {
// Delete 删除数据
func (g GormDBCli) Delete(value Delete) error {
return g.executeTransaction(func(tx *gorm.DB) error {
tx = tx.Model(value.Table)
for column, val := range value.Where {
tx = tx.Where(column, val)
}
return tx.Delete(value.Table).Error
}, "数据删除失败")
}

// executeTransaction 执行事务并处理错误
func (g GormDBCli) executeTransaction(operation func(tx *gorm.DB) error, errorMessage string) error {
tx := g.db.Begin()
tx = tx.Model(value.Table)
for column, val := range value.Where {
tx = tx.Where(column, val)
if tx.Error != nil {
return fmt.Errorf("事务启动失败, err: %s", tx.Error)
}
err := tx.Update(value.Update[0], value.Update[1:]).Error
if err != nil {

if err := operation(tx); err != nil {
tx.Rollback()
return fmt.Errorf("数据更新失败 -> %s", err)
return fmt.Errorf("%s -> %s", errorMessage, err)
}
err = tx.Commit().Error
if err != nil {

if err := tx.Commit().Error; err != nil {
tx.Rollback()
return fmt.Errorf("事务提交失败 -> %s", err)
return fmt.Errorf("事务提交失败, err: %s", err)
}

return nil
}

// Update 定义更新单条数据的结构
type Update struct {
Table interface{}
Where map[string]interface{}
Update []string
}

// Updates 定义更新多条数据的结构
type Updates struct {
Table interface{}
Where map[string]interface{}
Updates interface{}
}

func (g GormDBCli) Updates(value Updates) error {

tx := g.db.Begin()
tx = tx.Model(value.Table)
for column, val := range value.Where {
tx = tx.Where(column, val)
}
err := tx.Updates(value.Updates).Error
if err != nil {
tx.Rollback()
return fmt.Errorf("数据更新失败 -> %s", err)
}

err = tx.Commit().Error
if err != nil {
tx.Rollback()
return fmt.Errorf("事务提交失败 -> %s", err)
}

return nil

}

// Delete 定义删除数据的结构
type Delete struct {
Table interface{}
Where map[string]interface{}
}

func (g GormDBCli) Delete(value Delete) error {

tx := g.db.Begin()

tx = tx.Model(value.Table)
for column, val := range value.Where {
tx = tx.Where(column, val)
}
err := tx.Delete(value.Table).Error
if err != nil {
tx.Rollback()
return fmt.Errorf("数据删除失败 -> %s", err)
}
err = tx.Commit().Error
if err != nil {
tx.Rollback()
return fmt.Errorf("事务提交失败 -> %s", err)
}

return nil

}

0 comments on commit b057257

Please sign in to comment.