diff --git a/pkg/models/querybuilder_sql.go b/pkg/models/querybuilder_sql.go index f81beab1a..35de14d29 100644 --- a/pkg/models/querybuilder_sql.go +++ b/pkg/models/querybuilder_sql.go @@ -6,11 +6,14 @@ import ( "github.com/jmoiron/sqlx" "github.com/stashapp/stash/pkg/database" "github.com/stashapp/stash/pkg/logger" + "math/rand" "reflect" "strconv" "strings" ) +var randomSortFloat = rand.Float64() + func selectAll(tableName string) string { idColumn := getColumn(tableName, "*") return "SELECT " + idColumn + " FROM " + tableName + " " @@ -70,7 +73,11 @@ func getSort(sort string, direction string, tableName string) string { colName := getColumn(tableName, "size") return " ORDER BY cast(" + colName + " as integer) " + direction } else if strings.Compare(sort, "random") == 0 { - return " ORDER BY RANDOM() " + // https://stackoverflow.com/a/24511461 + // TODO seed as a parameter from the UI + colName := getColumn(tableName, "id") + randomSortString := strconv.FormatFloat(randomSortFloat, 'f', 16, 32) + return " ORDER BY " + "(substr(" + colName + " * " + randomSortString + ", length(" + colName + ") + 2))" + " " + direction } else { colName := getColumn(tableName, sort) var additional string