mirror of
https://github.com/photoprism/photoprism.git
synced 2026-07-22 09:39:01 +00:00
40 lines
1 KiB
Go
40 lines
1 KiB
Go
package entity
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/schema"
|
|
|
|
"github.com/photoprism/photoprism/pkg/convert"
|
|
)
|
|
|
|
// 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(), int64(0)
|
|
|
|
stmt := db.Model(m)
|
|
|
|
// Assume that the caller has passed in Schema named columns and we need to translate to db name columns.
|
|
// Even if they are db name columns, this shouldn't break things.
|
|
mSchema, _ := schema.Parse(m, &sync.Map{}, db.NamingStrategy)
|
|
mTableName := mSchema.Table
|
|
|
|
// Compose where condition.
|
|
for k := range keys {
|
|
stmt.Where("? = ?", gorm.Expr(db.NamingStrategy.ColumnName(mTableName, 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 convert.SafeInt64toint(count)
|
|
}
|