2024-04-09 08:28:18 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2024-04-19 04:47:00 +00:00
|
|
|
clone "github.com/huandu/go-clone/generic"
|
2024-04-09 08:28:18 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BaseDAO[T any] struct{}
|
|
|
|
|
|
|
|
func (b BaseDAO[T]) Migrate(db *gorm.DB, e T) error {
|
|
|
|
return db.Set("gorm:table_options", "CHARSET=utf8mb4").AutoMigrate(e)
|
|
|
|
}
|
|
|
|
|
2024-04-11 02:51:50 +00:00
|
|
|
func (BaseDAO[T]) GetAll(db *gorm.DB, e T, cond ...T) ([]T, error) {
|
2024-04-09 08:28:18 +00:00
|
|
|
var r []T
|
2024-04-19 01:44:37 +00:00
|
|
|
tx := db.Model(e)
|
2024-04-11 02:51:50 +00:00
|
|
|
for _, c := range cond {
|
|
|
|
tx = tx.Where(c)
|
|
|
|
}
|
|
|
|
|
2024-04-19 01:44:37 +00:00
|
|
|
rows, err := tx.Rows()
|
|
|
|
if err != nil {
|
2024-04-09 08:28:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-19 01:44:37 +00:00
|
|
|
defer rows.Close()
|
2024-04-09 08:28:18 +00:00
|
|
|
|
2024-04-19 01:44:37 +00:00
|
|
|
for rows.Next() {
|
|
|
|
if err := db.ScanRows(rows, e); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-19 04:47:00 +00:00
|
|
|
i := clone.Clone(e)
|
2024-04-11 02:51:50 +00:00
|
|
|
|
2024-04-19 01:44:37 +00:00
|
|
|
r = append(r, i)
|
2024-04-09 08:28:18 +00:00
|
|
|
}
|
2024-04-19 01:44:37 +00:00
|
|
|
|
2024-04-09 08:28:18 +00:00
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2024-04-19 01:44:37 +00:00
|
|
|
func (BaseDAO[T]) GetOne(db *gorm.DB, e T, cond ...T) (T, error) {
|
2024-04-11 02:51:50 +00:00
|
|
|
tx := db
|
|
|
|
for _, c := range cond {
|
|
|
|
tx = tx.Where(c)
|
|
|
|
}
|
|
|
|
|
2024-04-19 01:44:37 +00:00
|
|
|
if err := tx.First(e).Error; err != nil {
|
|
|
|
return e, err
|
2024-04-09 08:28:18 +00:00
|
|
|
}
|
2024-04-19 01:44:37 +00:00
|
|
|
return e, nil
|
2024-04-09 08:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (BaseDAO[T]) Create(db *gorm.DB, e T) (T, error) {
|
2024-04-11 02:51:50 +00:00
|
|
|
if err := db.Create(e).Error; err != nil {
|
2024-04-09 08:28:18 +00:00
|
|
|
return e, err
|
|
|
|
}
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (BaseDAO[T]) FirstOrCreate(db *gorm.DB, e T) (T, error) {
|
|
|
|
if err := db.FirstOrCreate(&e).Error; err != nil {
|
|
|
|
return e, err
|
|
|
|
}
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2024-04-11 02:51:50 +00:00
|
|
|
func (BaseDAO[T]) Update(db *gorm.DB, e T, cond ...T) (T, error) {
|
|
|
|
tx := db.Model(e)
|
|
|
|
for _, c := range cond {
|
|
|
|
tx = tx.Where(c)
|
|
|
|
}
|
|
|
|
|
2024-04-11 03:41:33 +00:00
|
|
|
result := tx.Updates(e)
|
|
|
|
|
|
|
|
if err := result.Error; err != nil {
|
2024-04-09 08:28:18 +00:00
|
|
|
return e, err
|
|
|
|
}
|
2024-04-11 03:41:33 +00:00
|
|
|
if result.RowsAffected == 0 {
|
|
|
|
return e, gorm.ErrRecordNotFound
|
|
|
|
}
|
2024-04-09 08:28:18 +00:00
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2024-04-11 02:51:50 +00:00
|
|
|
func (b BaseDAO[T]) UpdateOrCreate(db *gorm.DB, e T, cond ...T) (T, error) {
|
2024-04-11 03:41:33 +00:00
|
|
|
_, err := b.Update(db, e, cond...)
|
2024-04-09 08:28:18 +00:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
2024-04-11 03:41:33 +00:00
|
|
|
_, err = b.Create(db, e)
|
2024-04-09 08:28:18 +00:00
|
|
|
}
|
|
|
|
return e, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (BaseDAO[T]) Delete(db *gorm.DB, e T) error {
|
|
|
|
if err := db.Delete(e).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type IBaseDAO interface {
|
|
|
|
Migrate()
|
|
|
|
|
|
|
|
GetAll()
|
|
|
|
GetOne()
|
|
|
|
GetSome()
|
|
|
|
|
|
|
|
Create()
|
|
|
|
FirstOrCreate()
|
|
|
|
|
|
|
|
Update()
|
|
|
|
UpdateOrCreate()
|
|
|
|
|
|
|
|
Delete()
|
|
|
|
}
|