photoprism/internal/entity/entity_count.go
Michael Mayer a2b7615c93 Go: Apply go fix modernizations across backend packages
Run `go fix ./...` and keep mechanical modernization updates.

- Replace `interface{}` with `any` in signatures and local types
- Apply formatter/style cleanups from go1.26 tooling
- Keep `omitempty` behavior-preserving simplifications suggested by fix
- No functional feature changes intended

Validation:
- go test ./... -run '^$' -count=1 (Go 1.26.0)
- GOTOOLCHAIN=go1.24.10 go test ./... -run '^$' -count=1

Signed-off-by: Michael Mayer <michael@photoprism.app>
2026-02-20 03:54:33 +01:00

30 lines
623 B
Go

package entity
import (
"github.com/jinzhu/gorm"
)
// Count returns the number of records for a given a model and key values.
func Count(m any, keys []string, values []any) int {
if m == nil || len(keys) != len(values) {
log.Debugf("entity: invalid parameters (count records)")
return -1
}
db, count := UnscopedDb(), 0
stmt := db.Model(m)
// Compose where condition.
for k := range keys {
stmt.Where("? = ?", gorm.Expr(keys[k]), values[k])
}
// Fetch count from database.
if err := stmt.Count(&count).Error; err != nil {
log.Debugf("entity: %s (count records)", err)
return -1
}
return count
}