photoprism/internal/query/places.go
Michael Mayer c8287e462f Backend: Refactor queries for compatibility with SQLite #3124 #3742
Signed-off-by: Michael Mayer <michael@photoprism.app>
2023-10-15 09:31:10 +02:00

38 lines
873 B
Go

package query
import (
"github.com/photoprism/photoprism/internal/entity"
)
type Cell struct {
ID string
PlaceID string
}
type Cells []Cell
// CellIDs returns all known S2 cell ids as Cell slice.
func CellIDs() (result Cells, err error) {
tableName := entity.Cell{}.TableName()
var count int64
if err = UnscopedDb().Table(tableName).Where("id <> 'zz'").Count(&count).Error; err != nil {
return result, err
}
result = make(Cells, 0, count)
err = UnscopedDb().Table(tableName).Select("id, place_id").Where("id <> 'zz'").Order("id").Scan(&result).Error
return result, err
}
// PurgePlaces removes unused entries from the places table.
func PurgePlaces() error {
query := `DELETE FROM places
WHERE id NOT IN (SELECT DISTINCT place_id FROM cells)
AND id NOT IN (SELECT DISTINCT place_id FROM photos)`
return Db().Exec(query).Error
}