filterbuilder

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 8 Imported by: 0

README

FilterBuilder

The filterbuilder is a stand-alone library to create filter for the querybuilder library. The library would return an output of an SQL with parameterized code and the variables for the parameters.

Here is a sample code taken from its test file:

    fb := NewFilter(
		[]Pair{
			{Column: "first_name", Value: Value{Src: "Zaldy", Raw: true}},
			{Column: "last_name", Value: Value{Src: "Baguinon", Raw: true}},
		}, true, "@p")

	fb.Ne = append(fb.Ne, Pair{Column: "first_name", Value: Value{Src: "James", Raw: true}})
	fb.Ne = append(fb.Ne, Pair{Column: "last_name", Value: Value{Src: "Lumibao", Raw: true}})

	sql, args, err := fb.Build()
	if err != nil {
		t.Fail()
	}

	t.Log(sql)
	t.Log(args)

The filters are defined with a structure as follows:

    type Filter struct {
        Data           any              `json:"data,omitempty"`
        Eq             []Pair           `json:"eq,omitempty"`               // Equality pair
        Ne             []Pair           `json:"ne,omitempty"`               // Not equality pair
        Lk             []Pair           `json:"lk,omitempty"`               // Like pair
        In             []MultiFieldPair `json:"in,omitempty"`               // In column pair.
        NotIn          []MultiFieldPair `json:"not_in,omitempty"`           // Not In column pair
        Between        []MultiFieldPair `json:"between,omitempty"`          // Between column pair
        Placeholder    string           `json:"placeholder,omitempty"`      // Parameter place holder
        InSequence     bool             `json:"in_sequence,omitempty"`      // Parameter place holders would be numbered in sequence
        Offset         int              `json:"offset,omitempty"`           // Sets the start of parameter number
        AllowNoFilters bool             `json:"allow_no_filters,omitempty"` // Allow no filter upon building
    }

It can be set programmatically or as Json object to be parsed and fed into a REST endpoint. This is the Json equivalent of the structure above:

    {
        "eq": [
            {"column": "first_name", "value": "Eagle"},
            {"column": "last_name", "value": "Bush"},
        ]
    }

The rest of the fields aside from eq,ne, lk, in, not_in, between can also be set via Json snippet, but it can cause security issues.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoFilterSet                 error = errors.New("no filters set")
	ErrColumnNotFound              error = errors.New("column not found")
	ErrDataNotSet                  error = errors.New("data was not set")
	ErrInvalidFieldName            error = errors.New("invalid field name")
	ErrDataIsNotStruct             error = errors.New("data is not struct")
	ErrDataAssertionMismatch       error = errors.New("data assertion mismatch")
	ErrTypeReflectionInvalid       error = errors.New("type reflection invalid")
	ErrPairTypeMustHaveMoreThanTwo error = errors.New("pair type must have more than two")
)

Functions

func SetMultiPair

func SetMultiPair(selector *[]MultiFieldPair, column string, value []Value)

SetMultiPair sets a multi-pair array with the specified column and value

If the column does not exist, it will create one

func SetPair

func SetPair(selector *[]Pair, column string, value Value)

SetPair sets a pair array with the specified column and value

If the column does not exist, it will create one

func ValueFor

func ValueFor[T FilterConstraints](fb *Filter, col string) (T, error)

ValueFor is a static way to get the value of the filter by column lookup

Types

type Filter

type Filter struct {
	Data           any              `json:"data,omitempty"`
	Eq             []Pair           `json:"eq,omitempty"`               // Equality pairs
	Ne             []Pair           `json:"ne,omitempty"`               // Not equality pairs
	Lk             []Pair           `json:"lk,omitempty"`               // Like pairs
	Or             [][]Pair         `json:"or,omitempty"`               // Or pairs
	In             []MultiFieldPair `json:"in,omitempty"`               // In column pair.
	NotIn          []MultiFieldPair `json:"not_in,omitempty"`           // Not In column pair
	Between        []MultiFieldPair `json:"between,omitempty"`          // Between column pair
	Placeholder    string           `json:"placeholder,omitempty"`      // Parameter place holder
	InSequence     bool             `json:"in_sequence,omitempty"`      // Parameter place holders would be numbered in sequence
	Offset         int              `json:"offset,omitempty"`           // Sets the start of parameter number
	AllowNoFilters bool             `json:"allow_no_filters,omitempty"` // Allow no filter upon building
}

Filter - the filter struct

func NewFilter

func NewFilter(eq []Pair, paramInSequence bool, paramPlaceHolder string) *Filter

NewFilter creates a new Filter object

func (*Filter) Build

func (fb *Filter) Build() ([]string, []any, error)

Build the filter query

func (*Filter) BuildFunc

func (fb *Filter) BuildFunc(poff int, pchar string, pseq bool) ([]string, []any)

BuildFunc is a builder compatible with QueryBuilder's FilterFunc

func (*Filter) Hash

func (fb *Filter) Hash() string

Hash creates a hash of the filters created

func (*Filter) MakeKey

func (fb *Filter) MakeKey() string

MakeKey creates a unique key out of the filters created

func (*Filter) Valid

func (fb *Filter) Valid() bool

Valid checks if any filters were defined

func (*Filter) Value

func (fb *Filter) Value(p Value) (any, error)

Value gets the actual value of the struct field or the raw value that has been set

func (*Filter) ValueFor

func (fb *Filter) ValueFor(col string) (any, error)

ValueFor gets the value of the filter instance by column lookup

func (*Filter) Weld

func (fb *Filter) Weld(sql string, args []any, paramoffset int) (string, []any, error)

Weld joins an existing SQL string and its arguments with the results from the Build function

type FilterConstraints

type FilterConstraints interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 |
		~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string |
		~[]int | ~[]int8 | ~[]int16 | ~[]int32 | ~[]int64 | ~[]uint | ~[]uint8 | ~[]uint16 |
		~[]uint32 | ~[]uint64 | ~[]uintptr | ~[]float32 | ~[]float64 | ~[]string |
		time.Time | ssd.Decimal | bool | []time.Time | []ssd.Decimal | []bool
}

type MultiFieldPair

type MultiFieldPair struct {
	Column string  `json:"column,omitempty"` // Database table column
	Value  []Value `json:"value,omitempty"`  // Struct field to get value
}

MultiFieldPair struct

func DataMultiPair

func DataMultiPair(column string, fieldName ...string) MultiFieldPair

DataMultiPair simplifies data multi-pair. Pairs reads the Data field values via fieldName argument.

func NewMultiPairs

func NewMultiPairs(pairs ...MultiFieldPair) []MultiFieldPair

NewMultiPairs simplify initialization of multi-field pairs

func RawMultiPair

func RawMultiPair(column string, value ...any) MultiFieldPair

RawMultiPair simplifies raw multi-pair. Pairs reads the value argument raw.

type Null

type Null bool

Null indicates the column should evaluate for NULL

type Pair

type Pair struct {
	Column string `json:"column,omitempty"` // Database table column
	Value  Value  `json:"value,omitempty"`  // Struct field to get value or the value itself
}

Pair struct

func DataPair

func DataPair(column string, fieldName string) Pair

DataPair simplifies data pair. Pairs reads the Data field values via fieldName argument.

func NewPairs

func NewPairs(pairs ...Pair) []Pair

NewPairs simplify initialization of Pairs

func RawPair

func RawPair(column string, value any) Pair

RawPair simplifies raw pair. Pairs reads the value argument raw.

type Value

type Value struct {
	Src any  `json:"src,omitempty"` // Struct field to get value or the value itself
	Raw bool `json:"raw,omitempty"` // When true, the Src was set to a raw value. When false, the value is retrieved from the struct field in the Data.
}

Value struct

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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