Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
97a3ebf3a0 | ||
|
|
f59d3e9b1b | ||
|
|
14cb20f306 | ||
|
|
2b18549f02 |
68
dbHandler.go
68
dbHandler.go
@@ -245,6 +245,9 @@ func (dH *DBHandler) DeleteById(model any, ids ...uint) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dH.logger.Debug("deleteById", "delete ids "+fmt.Sprint(ids))
|
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
|
// 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 {
|
if err := dH.db.Model(model).Where("id IN ?", ids).Delete(nil).Error; err != nil {
|
||||||
@@ -282,53 +285,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) {
|
||||||
//
|
|
||||||
// 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) {
|
|
||||||
var query string
|
var query string
|
||||||
|
var args any
|
||||||
|
|
||||||
switch v := value.(type) {
|
// Detect LIKE and IN behavior
|
||||||
case []string, []int, []uint, []int64, []uint64, []float64:
|
val := reflect.ValueOf(value)
|
||||||
// Handle slice case — use IN clause
|
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)
|
query = fmt.Sprintf("%s IN ?", key)
|
||||||
case string:
|
args = value
|
||||||
if likeSearch {
|
|
||||||
v = strings.ReplaceAll(v, "*", "%")
|
case likeSearch:
|
||||||
|
str := strings.ReplaceAll(fmt.Sprint(value), "*", "%")
|
||||||
query = fmt.Sprintf("%s LIKE ?", key)
|
query = fmt.Sprintf("%s LIKE ?", key)
|
||||||
value = v
|
args = str
|
||||||
} else {
|
|
||||||
query = fmt.Sprintf("%s = ?", key)
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
// Handle any other single value (numbers, bools, etc.)
|
|
||||||
query = fmt.Sprintf("%s = ?", key)
|
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
|
tx := dH.db.Where(query, args).Find(model)
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
if tx.Error != nil {
|
||||||
|
dH.logger.Error("Exists", fmt.Sprintf("query failed: %v", tx.Error))
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return tx.RowsAffected > 0
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user