mirror of
https://git.vectorsigma.ru/public/photoview.git
synced 2026-08-03 20:49:02 +00:00
Fix SQL errors for Timeline view,
when using Postgres or Sqlite. Closes #194
This commit is contained in:
48
api/database/helpers.go
Normal file
48
api/database/helpers.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// DateComponent a component of a date (day, month, year)
|
||||
type DateComponent string
|
||||
|
||||
const (
|
||||
DateCompYear DateComponent = "YEAR"
|
||||
DateCompMonth DateComponent = "MONTH"
|
||||
DateCompDay DateComponent = "DAY"
|
||||
)
|
||||
|
||||
// DateExtract is a helper function that is used to generate the proper SQL syntax
|
||||
// for extracting date components (day, month, year) for different database backends.
|
||||
func DateExtract(db *gorm.DB, component DateComponent, attribute string) string {
|
||||
|
||||
var result string
|
||||
|
||||
switch db.Dialector.Name() {
|
||||
case "mysql", "postgress":
|
||||
result = fmt.Sprintf("EXTRACT(%s FROM %s)", component, attribute)
|
||||
break
|
||||
case "sqlite":
|
||||
|
||||
var sqliteFormatted string
|
||||
switch component {
|
||||
case DateCompYear:
|
||||
sqliteFormatted = "%Y"
|
||||
case DateCompMonth:
|
||||
sqliteFormatted = "%m"
|
||||
case DateCompDay:
|
||||
sqliteFormatted = "%d"
|
||||
}
|
||||
|
||||
result = fmt.Sprintf("CAST(strftime('%s', %s) AS INTEGER)", sqliteFormatted, attribute)
|
||||
break
|
||||
default:
|
||||
log.Panicf("unsupported database backend: %s", db.Dialector.Name())
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user