Skip to content

Commit 8e1fe83

Browse files
committed
improved information.RenderUUIDPrimaryKeyRefsHTML
1 parent 1f73fbf commit 8e1fe83

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

information/primarykeys.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"html"
1010
"net/http"
11+
"sort"
1112
"strings"
1213
"text/template"
1314

@@ -148,7 +149,7 @@ var RenderUUIDPrimaryKeyRefsHTML = http.HandlerFunc(func(writer http.ResponseWri
148149
var (
149150
title string
150151
mainContent any
151-
style = []string{StyleAllMonospace, StyleDefaultTable}
152+
style = []string{StyleAllMonospace, StyleDefaultTable, `<style>h1 {color:red}</style>`}
152153
)
153154
pk, err := uu.IDFromString(request.URL.Query().Get("pk"))
154155
if err != nil {
@@ -159,7 +160,7 @@ var RenderUUIDPrimaryKeyRefsHTML = http.HandlerFunc(func(writer http.ResponseWri
159160
<input type="submit" value="Look up"/>
160161
</form>`
161162
} else {
162-
title = fmt.Sprintf("UUID %s", pk)
163+
title = pk.String()
163164
ctx := request.Context()
164165
cols, err := GetPrimaryKeyColumnsOfType(ctx, "uuid")
165166
if err != nil {
@@ -171,6 +172,9 @@ var RenderUUIDPrimaryKeyRefsHTML = http.HandlerFunc(func(writer http.ResponseWri
171172
http.Error(writer, err.Error(), http.StatusInternalServerError)
172173
return
173174
}
175+
sort.SliceStable(tableRows, func(i, j int) bool {
176+
return !tableRows[i].ForeignKey && tableRows[j].ForeignKey
177+
})
174178
var b strings.Builder
175179
for _, tableRow := range tableRows { //#nosec
176180
fmt.Fprintf(&b, "<h3>%s</h3>", html.EscapeString(tableRow.Table))
@@ -180,7 +184,11 @@ var RenderUUIDPrimaryKeyRefsHTML = http.HandlerFunc(func(writer http.ResponseWri
180184
id, err := uu.IDFromString(val)
181185
if err == nil {
182186
if id == pk {
183-
fmt.Fprintf(&b, "<tr><td>%s</td><td><b>%s</b></td></tr>", html.EscapeString(title), id)
187+
var fk string
188+
if tableRow.ForeignKey {
189+
fk = " (foreign key)"
190+
}
191+
fmt.Fprintf(&b, "<tr><td>%s</td><td><b style='color:red'>%s</b>%s</td></tr>", html.EscapeString(title), id, fk)
184192
} else {
185193
fmt.Fprintf(&b, "<tr><td>%[1]s</td><td><a href='.?pk=%[2]s'>%[2]s</a></td></tr>", html.EscapeString(title), id)
186194
}

0 commit comments

Comments
 (0)