7 Commits

Author SHA1 Message Date
Adrian Zürcher
98094abf3e fix delete function 2025-11-07 08:17:48 +01:00
Adrian Zürcher
97a3ebf3a0 remove refelect 2025-11-07 08:04:01 +01:00
Adrian Zürcher
f59d3e9b1b fix existing function add reflect, slow but safe 2025-11-07 08:02:19 +01:00
Adrian Zürcher
14cb20f306 fix existing function add reflect, slow but safe 2025-11-07 08:01:50 +01:00
Adrian Zürcher
2b18549f02 fix exist function for array of values and error handling delete 2025-10-29 22:14:07 +01:00
Adrian Zürcher
d2a4a3d548 fix delete and existing for more than one id 2025-10-29 21:38:17 +01:00
Adrian Zürcher
e65fb8fdf5 fix wrong package name 2025-10-24 14:44:26 +02:00

View File

@@ -1,4 +1,4 @@
package AccessHandler
package dbHandler
import (
"errors"
@@ -218,33 +218,23 @@ func (dH *DBHandler) UpdateValuesByKey(model any, key string, value any) error {
return dH.db.Model(lookUpModel).Updates(model).Error
}
// deleteById
//
// Description:
//
// Deletes records by their ID(s).
//
// Behavior:
// - If the first ID == 0, all records in the table are deleted.
// - Otherwise, deletes the provided IDs.
//
// Parameters:
// - model: Model struct type representing the table.
// - id: Variadic list of IDs to delete.
//
// Returns:
// - error: Any deletion error.
func (dH *DBHandler) DeleteById(model any, id ...uint) error {
if id[0] == 0 {
dH.logger.Debug("deleteById", "delete all")
return dH.db.Where("1 = 1").Delete(model).Error
func (dH *DBHandler) DeleteById(model any, ids ...uint) error {
if len(ids) == 0 {
return fmt.Errorf("no IDs provided")
}
dH.logger.Debug("deleteById", "delete ids"+fmt.Sprint(id))
if err := dH.Exists(model, "id", id, false); err != nil {
return err
if len(ids) == 1 && ids[0] == 0 {
dH.logger.Debug("deleteById", "delete ALL records!")
return dH.db.Session(&gorm.Session{AllowGlobalUpdate: true}).
Delete(model).Error
}
return dH.db.Delete(model, id).Error
dH.logger.Debug("deleteById", "delete ids "+fmt.Sprint(ids))
if err := dH.db.Delete(model, ids).Error; err != nil {
return fmt.Errorf("delete failed: %w", err)
}
return nil
}
// deleteByKey
@@ -275,39 +265,40 @@ func (dH *DBHandler) DeleteByKey(model any, key string, value any, LikeSearch bo
return dH.db.Where(key+" = ?", value).Delete(model).Error
}
// exists
//
// Description:
//
// Checks whether a record exists matching the specified key/value filter.
//
// Behavior:
// - Performs a `First` query on the database.
// - If `LikeSearch` is true, performs a LIKE query.
// - Returns an error if the record does not exist or query fails.
//
// Parameters:
// - model: Model struct type to search.
// - key: Column name to filter by.
// - value: Value to match.
// - LikeSearch: Whether to use wildcard search.
//
// 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 ?"
} else {
key = key + " = ?"
func (dH *DBHandler) Exists(model any, key string, value any, likeSearch bool) (found bool) {
var query string
var args any
// Detect LIKE and IN behavior
val := reflect.ValueOf(value)
isSlice := val.Kind() == reflect.Slice
switch {
case isSlice:
if val.Len() == 0 {
dH.logger.Error("Exists", fmt.Sprintf("empty slice for %s", key))
return false
}
query = fmt.Sprintf("%s IN ?", key)
args = value
case likeSearch:
str := strings.ReplaceAll(fmt.Sprint(value), "*", "%")
query = fmt.Sprintf("%s LIKE ?", key)
args = str
default:
query = fmt.Sprintf("%s = ?", key)
args = value
}
dH.logger.Debug("exists", "check if exists key "+key+" value "+fmt.Sprint(value))
err = dH.db.Where(key, value).First(model).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return fmt.Errorf("no record found for %s %v", key[:len(key)-1], value)
} else if err != nil {
return fmt.Errorf("query failed: %w", err)
dH.logger.Debug("exists", "checking existence for key "+query+" value "+fmt.Sprint(args))
tx := dH.db.Where(query, args).Find(model)
if tx.Error != nil {
dH.logger.Error("Exists", fmt.Sprintf("query failed: %v", tx.Error))
return false
}
return
return tx.RowsAffected > 0
}