Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14cb20f306 | ||
|
|
2b18549f02 |
75
dbHandler.go
75
dbHandler.go
@@ -245,13 +245,16 @@ func (dH *DBHandler) DeleteById(model any, ids ...uint) error {
|
||||
}
|
||||
|
||||
dH.logger.Debug("deleteById", "delete ids "+fmt.Sprint(ids))
|
||||
if !dH.Exists(model, "id", ids, false) {
|
||||
return errors.New("no matching id(s) found")
|
||||
}
|
||||
|
||||
// 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 nil
|
||||
return err
|
||||
}
|
||||
|
||||
// deleteByKey
|
||||
@@ -282,53 +285,45 @@ 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) {
|
||||
func (dH *DBHandler) Exists(model any, key string, value any, likeSearch bool) (found bool) {
|
||||
var query string
|
||||
var args any
|
||||
|
||||
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 {
|
||||
query = fmt.Sprintf("%s = ?", key)
|
||||
// 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:
|
||||
// Handle any other single value (numbers, bools, etc.)
|
||||
query = fmt.Sprintf("%s = ?", key)
|
||||
args = value
|
||||
}
|
||||
|
||||
dH.logger.Debug("exists", "check if exists key "+query+" value "+fmt.Sprint(value))
|
||||
dH.logger.Debug("exists", "checking existence for key "+query+" value "+fmt.Sprint(args))
|
||||
|
||||
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, value)
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("query failed: %w", err)
|
||||
t := reflect.TypeOf(model)
|
||||
if t.Kind() == reflect.Ptr {
|
||||
t = t.Elem()
|
||||
}
|
||||
|
||||
return nil
|
||||
tx := dH.db.Where(query, args).Find(model)
|
||||
|
||||
if tx.Error != nil {
|
||||
dH.logger.Error("Exists", fmt.Sprintf("query failed: %w", tx.Error))
|
||||
return false
|
||||
}
|
||||
return tx.RowsAffected > 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user