filter

package
v0.0.0-...-8412c28 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultPageSize = 10
)
View Source
var (
	Operators = map[string]*Operator{
		"$eq":  {Function: basicComparison("="), RequiredArguments: 1},
		"$ne":  {Function: basicComparison("<>"), RequiredArguments: 1},
		"$gt":  {Function: basicComparison(">"), RequiredArguments: 1},
		"$lt":  {Function: basicComparison("<"), RequiredArguments: 1},
		"$gte": {Function: basicComparison(">="), RequiredArguments: 1},
		"$lte": {Function: basicComparison("<="), RequiredArguments: 1},
		"$starts": {
			Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
				if dataType != DataTypeText && dataType != DataTypeEnum {
					return filter.Where(tx, "FALSE")
				}
				query := castEnumAsText(column, dataType) + " ILIKE ?"
				value := sqlutil.EscapeLike(filter.Args[0]) + "%"
				return filter.Where(tx, query, value)
			},
			RequiredArguments: 1,
		},
		"$ends": {
			Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
				if dataType != DataTypeText && dataType != DataTypeEnum {
					return filter.Where(tx, "FALSE")
				}
				query := castEnumAsText(column, dataType) + " ILIKE ?"
				value := "%" + sqlutil.EscapeLike(filter.Args[0])
				return filter.Where(tx, query, value)
			},
			RequiredArguments: 1,
		},
		"$cont": {
			Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
				if dataType != DataTypeText && dataType != DataTypeEnum {
					return filter.Where(tx, "FALSE")
				}
				query := castEnumAsText(column, dataType) + " ILIKE ?"
				value := "%" + sqlutil.EscapeLike(filter.Args[0]) + "%"
				return filter.Where(tx, query, value)
			},
			RequiredArguments: 1,
		},
		"$excl": {
			Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
				if dataType != DataTypeText && dataType != DataTypeEnum {
					return filter.Where(tx, "FALSE")
				}
				query := castEnumAsText(column, dataType) + " NOT ILIKE ?"
				value := "%" + sqlutil.EscapeLike(filter.Args[0]) + "%"
				return filter.Where(tx, query, value)
			},
			RequiredArguments: 1,
		},
		"$in":    {Function: multiComparison("IN"), RequiredArguments: 1},
		"$notin": {Function: multiComparison("NOT IN"), RequiredArguments: 1},
		"$isnull": {
			Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
				return filter.Where(tx, column+" IS NULL")
			},
			RequiredArguments: 0,
		},
		"$istrue": {
			Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
				if dataType != DataTypeBool {
					return filter.Where(tx, "FALSE")
				}
				return filter.Where(tx, column+" IS TRUE")
			},
			RequiredArguments: 0,
		},
		"$isfalse": {
			Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
				if dataType != DataTypeBool {
					return filter.Where(tx, "FALSE")
				}
				return filter.Where(tx, column+" IS FALSE")
			},
			RequiredArguments: 0,
		},
		"$notnull": {
			Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
				return filter.Where(tx, column+" IS NOT NULL")
			},
			RequiredArguments: 0,
		},
		"$between": {
			Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB {
				if dataType.IsArray() {
					return filter.Where(tx, "FALSE")
				}

				args, ok := ConvertArgsToSafeType(filter.Args[:2], dataType)
				if !ok {
					return filter.Where(tx, "FALSE")
				}
				query := castEnumAsText(column, dataType) + " BETWEEN ? AND ?"
				return filter.Where(tx, query, args...)
			},
			RequiredArguments: 2,
		},
	}
)
View Source
var Separator = "||"

Functions

func ApplyValidation

func ApplyValidation(set validation.RuleSet)

func ApplyValidationRules

func ApplyValidationRules(set *validation.Rules)

func ConvertArgsToSafeType

func ConvertArgsToSafeType(args []string, dataType DataType) ([]interface{}, bool)

func ConvertToSafeType

func ConvertToSafeType(arg string, dataType DataType) (interface{}, bool)

func Scope

func Scope(db *gorm.DB, request *goyave.Request, dest interface{}) (*database.Paginator, *gorm.DB)

func ScopeUnpaginated

func ScopeUnpaginated(db *gorm.DB, request *goyave.Request, dest interface{}) *gorm.DB

Types

type Blacklist

type Blacklist struct {
	Relations          map[string]*Blacklist
	FieldsBlacklist    []string
	RelationsBlacklist []string
	IsFinal            bool
}

type DataType

type DataType string
const (
	DataTypeText      DataType = "text"
	DataTypeTextArray DataType = "text[]"

	DataTypeEnum      DataType = "enum"
	DataTypeEnumArray DataType = "enum[]"

	DataTypeBool      DataType = "bool"
	DataTypeBoolArray DataType = "bool[]"

	DataTypeInt8       DataType = "int8"
	DataTypeInt8Array  DataType = "int8[]"
	DataTypeInt16      DataType = "int16"
	DataTypeInt16Array DataType = "int16[]"
	DataTypeInt32      DataType = "int32"
	DataTypeInt32Array DataType = "int32[]"
	DataTypeInt64      DataType = "int64"
	DataTypeInt64Array DataType = "int64[]"

	DataTypeUint8       DataType = "uint8"
	DataTypeUint8Array  DataType = "uint8[]"
	DataTypeUint16      DataType = "uint16"
	DataTypeUint16Array DataType = "uint16[]"
	DataTypeUint32      DataType = "uint32"
	DataTypeUint32Array DataType = "uint32[]"
	DataTypeUint64      DataType = "uint64"
	DataTypeUint64Array DataType = "uint64[]"

	DataTypeFloat32      DataType = "float32"
	DataTypeFloat32Array DataType = "float32[]"
	DataTypeFloat64      DataType = "float64"
	DataTypeFloat64Array DataType = "float64[]"

	DataTypeTime      DataType = "time"
	DataTypeTimeArray DataType = "time[]"

	DataTypeUnsupported DataType = "-"
)

func (DataType) IsArray

func (d DataType) IsArray() bool

type Filter

type Filter struct {
	Field    string
	Operator *Operator
	Args     []string
	Or       bool
}

func ParseFilter

func ParseFilter(filter string) (*Filter, error)

func (*Filter) Scope

func (f *Filter) Scope(settings *Settings, sch *schema.Schema) (func(*gorm.DB) *gorm.DB, func(*gorm.DB) *gorm.DB)

func (*Filter) Where

func (f *Filter) Where(tx *gorm.DB, query string, args ...interface{}) *gorm.DB

Where applies a condition to given transaction, automatically taking the "Or" filter value into account.

type Join

type Join struct {
	Relation string
	Fields   []string
	// contains filtered or unexported fields
}

func ParseJoin

func ParseJoin(join string) (*Join, error)

func (*Join) Scopes

func (j *Join) Scopes(settings *Settings, schema *schema.Schema) []func(*gorm.DB) *gorm.DB

type Operator

type Operator struct {
	Function          func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB
	RequiredArguments uint8
}

Operator used by filters to build the SQL query. The operator function modifies the GORM statement (most of the time by adding a WHERE condition) then returns the modified statement.

Operators may need arguments (e.g. "$eq", equals needs a value to compare the field to); RequiredArguments define the minimum number of arguments a client must send in order to use this operator in a filter. RequiredArguments is checked during Filter parsing.

Operators may return the given tx without change if they don't support the given dataType or add a condition that will always be false.

type Search struct {
	Query    string
	Operator *Operator
	Fields   []string
}

func (*Search) Scope

func (s *Search) Scope(schema *schema.Schema) func(*gorm.DB) *gorm.DB

type Settings

type Settings struct {
	FieldsSearch   []string
	SearchOperator *Operator

	Blacklist

	DisableFields bool
	DisableFilter bool
	DisableSort   bool
	DisableJoin   bool
	DisableSearch bool
}

func (*Settings) Scope

func (s *Settings) Scope(db *gorm.DB, request *goyave.Request, dest interface{}) (*database.Paginator, *gorm.DB)

func (*Settings) ScopeUnpaginated

func (s *Settings) ScopeUnpaginated(db *gorm.DB, request *goyave.Request, dest interface{}) *gorm.DB

type Sort

type Sort struct {
	Field string
	Order SortOrder
}

func ParseSort

func ParseSort(sort string) (*Sort, error)

func (*Sort) Scope

func (s *Sort) Scope(settings *Settings, schema *schema.Schema) func(*gorm.DB) *gorm.DB

type SortOrder

type SortOrder string
const (
	SortAscending  SortOrder = "ASC"
	SortDescending SortOrder = "DESC"
)

Jump to

Keyboard shortcuts

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