6 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

View File

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