Use case when for document filter

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-08-05 09:32:09 +02:00
parent 2ebab9d5cf
commit 60c4b945c7

View File

@@ -38,45 +38,41 @@ func NewDocumentTrustCenterFilter() *DocumentFilter {
} }
} }
func (f *DocumentFilter) SQLArguments() pgx.NamedArgs { func (f *DocumentFilter) SQLArguments() pgx.StrictNamedArgs {
args := pgx.NamedArgs{} args := pgx.StrictNamedArgs{}
if f.query != nil { if f.query != nil {
args["query"] = *f.query args["query"] = *f.query
} else {
args["query"] = (*string)(nil)
} }
if f.showOnTrustCenter != nil { if f.showOnTrustCenter != nil {
args["show_on_trust_center"] = *f.showOnTrustCenter args["show_on_trust_center"] = *f.showOnTrustCenter
} else {
args["show_on_trust_center"] = (*bool)(nil)
} }
return args return args
} }
func (f *DocumentFilter) SQLFragment() string { func (f *DocumentFilter) SQLFragment() string {
conditions := []string{} sql := `(
CASE
WHEN @query::text IS NOT NULL AND @query::text != '' THEN
search_vector @@ (
SELECT to_tsquery('simple', string_agg(lexeme || ':*', ' & '))
FROM unnest(regexp_split_to_array(trim(@query::text), '\s+')) AS lexeme
)
ELSE TRUE
END
AND
CASE
WHEN @show_on_trust_center::boolean IS NOT NULL THEN
show_on_trust_center = @show_on_trust_center::boolean
ELSE TRUE
END
)`
if f.query != nil && *f.query != "" { return sql
conditions = append(conditions, `
search_vector @@ (
SELECT to_tsquery('simple', string_agg(lexeme || ':*', ' & '))
FROM unnest(regexp_split_to_array(trim(@query), '\s+')) AS lexeme
)`)
}
if f.showOnTrustCenter != nil {
conditions = append(conditions, "show_on_trust_center = @show_on_trust_center")
}
if len(conditions) == 0 {
return "TRUE"
}
result := ""
for i, condition := range conditions {
if i > 0 {
result += " AND "
}
result += condition
}
return result
} }