From 4f016ab3c9eb95259d20c2914e878d3d602c85b7 Mon Sep 17 00:00:00 2001 From: Stash Dev Date: Sat, 27 Jul 2019 13:22:38 -0700 Subject: [PATCH] Random sort now paginates without duplicates --- pkg/models/querybuilder_sql.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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