Random sort now paginates without duplicates

This commit is contained in:
Stash Dev 2019-07-27 13:22:38 -07:00
parent e8397ea9b2
commit 4f016ab3c9
1 changed files with 8 additions and 1 deletions

View File

@ -6,11 +6,14 @@ import (
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/stashapp/stash/pkg/database" "github.com/stashapp/stash/pkg/database"
"github.com/stashapp/stash/pkg/logger" "github.com/stashapp/stash/pkg/logger"
"math/rand"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
) )
var randomSortFloat = rand.Float64()
func selectAll(tableName string) string { func selectAll(tableName string) string {
idColumn := getColumn(tableName, "*") idColumn := getColumn(tableName, "*")
return "SELECT " + idColumn + " FROM " + tableName + " " return "SELECT " + idColumn + " FROM " + tableName + " "
@ -70,7 +73,11 @@ func getSort(sort string, direction string, tableName string) string {
colName := getColumn(tableName, "size") colName := getColumn(tableName, "size")
return " ORDER BY cast(" + colName + " as integer) " + direction return " ORDER BY cast(" + colName + " as integer) " + direction
} else if strings.Compare(sort, "random") == 0 { } 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 { } else {
colName := getColumn(tableName, sort) colName := getColumn(tableName, sort)
var additional string var additional string