fix delete and existing for more than one id
This commit is contained in:
51
dbHandler.go
51
dbHandler.go
@@ -234,17 +234,24 @@ func (dH *DBHandler) UpdateValuesByKey(model any, key string, value any) error {
|
||||
//
|
||||
// Returns:
|
||||
// - error: Any deletion error.
|
||||
func (dH *DBHandler) DeleteById(model any, id ...uint) error {
|
||||
if id[0] == 0 {
|
||||
func (dH *DBHandler) DeleteById(model any, ids ...uint) error {
|
||||
if len(ids) == 0 {
|
||||
return fmt.Errorf("no IDs provided")
|
||||
}
|
||||
|
||||
if ids[0] == 0 {
|
||||
dH.logger.Debug("deleteById", "delete all")
|
||||
return dH.db.Where("1 = 1").Delete(model).Error
|
||||
}
|
||||
|
||||
dH.logger.Debug("deleteById", "delete ids"+fmt.Sprint(id))
|
||||
if err := dH.Exists(model, "id", id, false); err != nil {
|
||||
return err
|
||||
dH.logger.Debug("deleteById", "delete ids "+fmt.Sprint(ids))
|
||||
|
||||
// Always use Model() to avoid GORM’s “limit 1” safeguard
|
||||
if err := dH.db.Model(model).Where("id IN ?", ids).Delete(nil).Error; err != nil {
|
||||
return fmt.Errorf("delete failed: %w", err)
|
||||
}
|
||||
return dH.db.Delete(model, id).Error
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// deleteByKey
|
||||
@@ -294,20 +301,34 @@ func (dH *DBHandler) DeleteByKey(model any, key string, value any, LikeSearch bo
|
||||
//
|
||||
// Returns:
|
||||
// - error: “no record found” or DB error.
|
||||
func (dH *DBHandler) Exists(model any, key string, value any, LikeSearch bool) (err error) {
|
||||
if LikeSearch {
|
||||
value = strings.ReplaceAll(fmt.Sprint(value), "*", "%")
|
||||
key = key + " LIKE ?"
|
||||
func (dH *DBHandler) Exists(model any, key string, value any, likeSearch bool) (err error) {
|
||||
var query string
|
||||
|
||||
switch v := value.(type) {
|
||||
case []string, []int, []uint, []int64, []uint64, []float64:
|
||||
// Handle slice case — use IN clause
|
||||
query = fmt.Sprintf("%s IN ?", key)
|
||||
case string:
|
||||
if likeSearch {
|
||||
v = strings.ReplaceAll(v, "*", "%")
|
||||
query = fmt.Sprintf("%s LIKE ?", key)
|
||||
value = v
|
||||
} else {
|
||||
key = key + " = ?"
|
||||
query = fmt.Sprintf("%s = ?", key)
|
||||
}
|
||||
default:
|
||||
// Handle any other single value (numbers, bools, etc.)
|
||||
query = fmt.Sprintf("%s = ?", key)
|
||||
}
|
||||
|
||||
dH.logger.Debug("exists", "check if exists key "+key+" value "+fmt.Sprint(value))
|
||||
err = dH.db.Where(key, value).First(model).Error
|
||||
dH.logger.Debug("exists", "check if exists key "+query+" value "+fmt.Sprint(value))
|
||||
|
||||
err = dH.db.Where(query, value).First(model).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fmt.Errorf("no record found for %s %v", key[:len(key)-1], value)
|
||||
return fmt.Errorf("no record found for %s %v", key, value)
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("query failed: %w", err)
|
||||
}
|
||||
return
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user