zitadel/internal/eventstore/repository/search_query.go

120 lines
3.3 KiB
Go
Raw Normal View History

2020-09-30 08:00:05 +00:00
package repository
2020-09-24 06:52:10 +00:00
import (
"database/sql"
"github.com/zitadel/zitadel/internal/errors"
)
2020-09-30 08:00:05 +00:00
// SearchQuery defines the which and how data are queried
2020-09-30 08:00:05 +00:00
type SearchQuery struct {
Columns Columns
Limit uint64
Desc bool
Filters [][]*Filter
Tx *sql.Tx
AllowTimeTravel bool
2020-09-30 08:00:05 +00:00
}
// Columns defines which fields of the event are needed for the query
2020-09-30 08:00:05 +00:00
type Columns int32
const (
2020-10-06 19:28:09 +00:00
//ColumnsEvent represents all fields of an event
ColumnsEvent = iota + 1
//ColumnsMaxSequence represents the latest sequence of the filtered events
ColumnsMaxSequence
// ColumnsInstanceIDs represents the instance ids of the filtered events
ColumnsInstanceIDs
2020-10-06 19:28:09 +00:00
columnsCount
2020-09-24 06:52:10 +00:00
)
2020-10-06 19:28:09 +00:00
func (c Columns) Validate() error {
if c <= 0 || c >= columnsCount {
return errors.ThrowPreconditionFailed(nil, "REPOS-x8R35", "column out of range")
}
return nil
}
// Filter represents all fields needed to compare a field of an event with a value
2020-09-24 06:52:10 +00:00
type Filter struct {
2020-10-05 18:39:36 +00:00
Field Field
Value interface{}
Operation Operation
2020-09-24 06:52:10 +00:00
}
// Operation defines how fields are compared
2020-09-24 06:52:10 +00:00
type Operation int32
const (
2020-10-06 19:28:09 +00:00
// OperationEquals compares two values for equality
OperationEquals Operation = iota + 1
// OperationGreater compares if the given values is greater than the stored one
OperationGreater
// OperationLess compares if the given values is less than the stored one
OperationLess
//OperationIn checks if a stored value matches one of the passed value list
OperationIn
2020-11-23 18:31:12 +00:00
//OperationJSONContains checks if a stored value matches the given json
OperationJSONContains
//OperationNotIn checks if a stored value does not match one of the passed value list
OperationNotIn
2020-10-06 19:28:09 +00:00
operationCount
2020-09-24 06:52:10 +00:00
)
// Field is the representation of a field from the event
2020-09-24 06:52:10 +00:00
type Field int32
const (
2020-10-06 19:28:09 +00:00
//FieldAggregateType represents the aggregate type field
FieldAggregateType Field = iota + 1
//FieldAggregateID represents the aggregate id field
FieldAggregateID
//FieldSequence represents the sequence field
FieldSequence
//FieldResourceOwner represents the resource owner field
FieldResourceOwner
//FieldInstanceID represents the instance id field
FieldInstanceID
2020-10-06 19:28:09 +00:00
//FieldEditorService represents the editor service field
FieldEditorService
//FieldEditorUser represents the editor user field
FieldEditorUser
//FieldEventType represents the event type field
FieldEventType
2020-11-23 18:31:12 +00:00
//FieldEventData represents the event data field
FieldEventData
//FieldCreationDate represents the creation date field
FieldCreationDate
2020-10-06 19:28:09 +00:00
fieldCount
2020-09-24 06:52:10 +00:00
)
// NewFilter is used in tests. Use searchQuery.*Filter() instead
2020-09-24 06:52:10 +00:00
func NewFilter(field Field, value interface{}, operation Operation) *Filter {
return &Filter{
2020-10-05 18:39:36 +00:00
Field: field,
Value: value,
Operation: operation,
2020-09-24 06:52:10 +00:00
}
}
// Validate checks if the fields of the filter have valid values
2020-09-24 06:52:10 +00:00
func (f *Filter) Validate() error {
if f == nil {
2020-09-30 08:00:05 +00:00
return errors.ThrowPreconditionFailed(nil, "REPO-z6KcG", "filter is nil")
2020-09-24 06:52:10 +00:00
}
2020-10-06 19:28:09 +00:00
if f.Field <= 0 || f.Field >= fieldCount {
2020-09-30 08:00:05 +00:00
return errors.ThrowPreconditionFailed(nil, "REPO-zw62U", "field not definded")
2020-09-24 06:52:10 +00:00
}
2020-10-05 18:39:36 +00:00
if f.Value == nil {
2020-09-30 08:00:05 +00:00
return errors.ThrowPreconditionFailed(nil, "REPO-GJ9ct", "no value definded")
2020-09-24 06:52:10 +00:00
}
2020-10-06 19:28:09 +00:00
if f.Operation <= 0 || f.Operation >= operationCount {
2020-09-30 08:00:05 +00:00
return errors.ThrowPreconditionFailed(nil, "REPO-RrQTy", "operation not definded")
2020-09-24 06:52:10 +00:00
}
return nil
}