Documentation
¶
Index ¶
- func BuildOrderBy(sortBy string, sortOrder SortOrder, table string, alias string) string
- func GetDefaultSortField(table string) string
- func GetSortableFieldsForTable(table string) map[string]bool
- func GetValidFieldsForTable(table string) map[string]bool
- func ParseDateTime(value string) (time.Time, error)
- func ResolveSortField(table, sortBy string) string
- func Validate(filters *QueryFilterSet, table string) error
- func ValidateSortByForTable(opts *QueryOptions, table string) error
- func ValidateSortField(sortBy, table string) error
- type BuildResult
- type Operator
- type ParseError
- type ParseOptions
- type ParseResult
- type QueryFilter
- type QueryFilterSet
- type QueryOptions
- type SortOrder
- type TimestampValue
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildOrderBy ¶
BuildOrderBy constructs an ORDER BY clause using only safe, constant column names.
func GetDefaultSortField ¶
GetDefaultSortField returns the default sort field for a table.
func GetSortableFieldsForTable ¶
GetSortableFieldsForTable returns fields that can be sorted. All filterable fields are sortable. For optimal performance on large datasets, consider adding indexes for frequently sorted fields. See docs/performance-tuning.mdx.
func GetValidFieldsForTable ¶
func ResolveSortField ¶
ResolveSortField maps a requested sort field to a safe column name. It returns only string literals from a switch; user input selects which constant to return. This breaks the taint chain for static analyzers.
func Validate ¶
func Validate(filters *QueryFilterSet, table string) error
func ValidateSortByForTable ¶
func ValidateSortByForTable(opts *QueryOptions, table string) error
ValidateSortByForTable validates opts.SortBy against the table's allowed sort fields. Normalizes SortBy (lowercase, trim) and returns an error if invalid. Call this at the database boundary before BuildWithOptions for defense-in-depth.
func ValidateSortField ¶
ValidateSortField validates that the sort field is allowed for the table.
Types ¶
type BuildResult ¶
type BuildResult struct {
CTEs []string
Conditions []string
Args []interface{}
NextArgPos int
OrderBy string // The ORDER BY clause (without "ORDER BY" prefix)
}
func Build ¶
func Build(filters *QueryFilterSet, table string, alias string, startArgPos int) (*BuildResult, error)
func BuildWithOptions ¶
func BuildWithOptions(filters *QueryFilterSet, table string, alias string, startArgPos int, opts *QueryOptions) (*BuildResult, error)
BuildWithOptions builds filter conditions and includes sorting options. It validates both filters and sort options, returning an error if either is invalid.
type Operator ¶
type Operator string
Operator represents supported filter operators.
const ( OpEqual Operator = "eq" OpNotEqual Operator = "ne" OpGreaterThan Operator = "gt" OpGreaterThanOrEqual Operator = "gte" OpLessThan Operator = "lt" OpLessThanOrEqual Operator = "lte" OpIn Operator = "in" OpBetween Operator = "between" OpLike Operator = "like" OpILike Operator = "ilike" OpIsNull Operator = "isnull" OpIsNotNull Operator = "isnotnull" )
func ResolveOperator ¶
type ParseError ¶
type ParseOptions ¶
type ParseResult ¶
type ParseResult struct {
Filters *QueryFilterSet
Errors []ParseError
}
func ParseFromQuery ¶
func ParseFromQuery(queryParams url.Values, opts *ParseOptions) *ParseResult
ParseFromQuery parses URL query parameters into a ParseResult. Returns errors for invalid params rather than silently dropping them.
type QueryFilter ¶
type QueryFilterSet ¶
type QueryFilterSet struct {
Filters []QueryFilter `json:"filters"`
}
type QueryOptions ¶
type QueryOptions struct {
SortBy string `json:"sort_by,omitempty"`
SortOrder SortOrder `json:"sort_order,omitempty"`
IncludeCount bool `json:"include_count,omitempty"`
}
QueryOptions contains sorting and pagination options for queries.
func (*QueryOptions) DefaultSortOrder ¶
func (o *QueryOptions) DefaultSortOrder() SortOrder
DefaultSortOrder returns desc if empty, otherwise validates and returns the order.