query

package
v1.27.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Search(q *bun.SelectQuery, term string, columns ...string) *bun.SelectQuery

Search narrows q to rows where any of the columns contains term, case insensitively. The term is matched as if it had no NUL bytes, since Postgres text cannot hold one and bun refuses to format a string with one: they are dropped, then the term is trimmed, so a term of only NUL bytes and spaces is empty. An empty term or no columns leaves q unchanged. Otherwise the columns become one parenthesised condition, (a ILIKE ? OR b ILIKE ?), added with Where so it ANDs with the rest of the WHERE clause.

The LIKE metacharacters \, % and _ in term are escaped with a backslash, so they match literally; this relies on backslash being the Postgres default LIKE escape character, so the query sets no ESCAPE clause.

Columns are only safe with trusted input: each one is interpolated raw into SQL (so bun resolves placeholders such as ?TableAlias, and expressions work), so it must come from code, never from client input. The term is not transformed, so an expression such as unaccent(name) only matches a term the caller has unaccented too. A column must not contain a bare ?, which bun would read as a placeholder. The pattern is not a bound parameter: bun inlines it into the SQL text as a string literal escaped for Postgres, and that literal keeps the LIKE backslashes only while standard_conforming_strings is on (the Postgres default).

ILIKE with a leading wildcard cannot use a btree index. A pg_trgm GIN index on the column makes it indexed:

CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE INDEX users_name_trgm_idx ON users USING gin (name gin_trgm_ops);

Types

type Filter

type Filter struct {
	Field      string
	Value      interface{}
	Comparison string
}

Filter represents a query filter

func ParseDateToFilter

func ParseDateToFilter(column, fromDate, toDate string) ([]Filter, error)

Handler helper to convert date strings to filters

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL