init push

This commit is contained in:
2026-07-16 22:16:45 +07:00
commit 8b068bdb10
1021 changed files with 332816 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
package sortkey
import "fmt"
// ActivePositiveSortClauses builds ORDER BY clauses where non-negative sort keys
// are treated as prioritized values. NULL/negative are grouped as empty values.
func ActivePositiveSortClauses(tableAlias, activeColumn, sortKeyColumn, fallbackColumn string, descending bool) []string {
active := qualify(tableAlias, activeColumn)
sortKey := qualify(tableAlias, sortKeyColumn)
fallback := qualify(tableAlias, fallbackColumn)
valid := fmt.Sprintf("%s = 1 AND %s IS NOT NULL AND %s >= 0", active, sortKey, sortKey)
direction := "ASC"
if descending {
direction = "DESC"
}
return []string{
fmt.Sprintf("%s DESC", active),
fmt.Sprintf("CASE WHEN %s THEN 0 ELSE 1 END ASC", valid),
fmt.Sprintf("CASE WHEN %s THEN %s END %s", valid, sortKey, direction),
fmt.Sprintf("%s ASC", fallback),
}
}
func qualify(tableAlias, column string) string {
if tableAlias == "" {
return column
}
return tableAlias + "." + column
}

View File

@@ -0,0 +1,34 @@
package sortkey
import (
"reflect"
"testing"
)
func TestActivePositiveSortClauses(t *testing.T) {
t.Run("ascending without alias", func(t *testing.T) {
got := ActivePositiveSortClauses("", "is_active", "sortkey", "designation", false)
want := []string{
"is_active DESC",
"CASE WHEN is_active = 1 AND sortkey IS NOT NULL AND sortkey >= 0 THEN 0 ELSE 1 END ASC",
"CASE WHEN is_active = 1 AND sortkey IS NOT NULL AND sortkey >= 0 THEN sortkey END ASC",
"designation ASC",
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("unexpected clauses: got=%v want=%v", got, want)
}
})
t.Run("descending with alias", func(t *testing.T) {
got := ActivePositiveSortClauses("helicopters", "is_active", "sortkey", "designation", true)
want := []string{
"helicopters.is_active DESC",
"CASE WHEN helicopters.is_active = 1 AND helicopters.sortkey IS NOT NULL AND helicopters.sortkey >= 0 THEN 0 ELSE 1 END ASC",
"CASE WHEN helicopters.is_active = 1 AND helicopters.sortkey IS NOT NULL AND helicopters.sortkey >= 0 THEN helicopters.sortkey END DESC",
"helicopters.designation ASC",
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("unexpected clauses: got=%v want=%v", got, want)
}
})
}