fix text array contains

This commit is contained in:
Elio Bischof
2023-06-30 02:11:17 +02:00
parent d5d365d154
commit 39e8e869ac
12 changed files with 84 additions and 92 deletions

View File

@@ -137,9 +137,9 @@ func (h *StatementHandler) SearchQuery(ctx context.Context, instanceIDs []string
}
queryBuilder.
AddQuery().
AggregateTypes(aggregateType).
SequenceGreater(seq).
InstanceID(instanceID).
AggregateTypes(aggregateType)
InstanceID(instanceID)
}
}

View File

@@ -236,12 +236,6 @@ func AddDeleteStatement(conditions []handler.Condition, opts ...execOption) func
}
}
func AddCopyStatement(conflict, from, to []handler.Column, conditions []handler.Condition, opts ...execOption) func(eventstore.Event) Exec {
return func(event eventstore.Event) Exec {
return NewCopyStatement(event, conflict, from, to, conditions, opts...).Execute
}
}
func NewArrayAppendCol(column string, value interface{}) handler.Column {
return handler.Column{
Name: column,
@@ -286,42 +280,31 @@ func NewCopyCol(column, from string) handler.Column {
}
}
func NewIsNullCond(column string) handler.Condition {
return handler.Condition{
Name: column,
ParameterOpt: func(string) string {
return " IS NULL"
},
}
}
func NewNotEqualCond(column, value string) handler.Condition {
return handler.Condition{
Name: column,
Value: value,
ParameterOpt: func(param string) string {
return fmt.Sprintf(" != %s", param)
},
}
}
func NewLessThanCond(column string, value interface{}) handler.Condition {
return handler.Condition{
Name: column,
Value: value,
ParameterOpt: func(placeholder string) string {
return " < " + placeholder
},
return func(param string) (string, interface{}) {
return column + " < " + param, value
}
}
func NewContainsCond(column string, value interface{}) handler.Condition {
return handler.Condition{
Name: column,
Value: value,
ParameterOpt: func(placeholder string) string {
return fmt.Sprintf(" @> ARRAY[%s]", placeholder)
},
func NewIsNullCond(column string) handler.Condition {
return func(param string) (string, interface{}) {
return column + " IS NULL", nil
}
}
// NewTextArrayContainsCond returns a handler.Condition that checks if the column that stores an array of text contains the given value
func NewTextArrayContainsCond(column string, value string) handler.Condition {
return func(param string) (string, interface{}) {
return column + " @> " + param, database.StringArray{value}
}
}
// Not is a function instead of a method, so that calling it is well readable
// For example conditions := []handler.Condition{ Not(NewTextArrayContainsCond())}
func Not(condition handler.Condition) handler.Condition {
return func(param string) (string, interface{}) {
cond, value := condition(param)
return "NOT ( " + cond + " )", value
}
}
@@ -330,7 +313,7 @@ func NewContainsCond(column string, value interface{}) handler.Condition {
// if the value of a col is empty the data will be copied from the selected row
// if the value of a col is not empty the data will be set by the static value
// conds represent the conditions for the selection subquery
func NewCopyStatement(event eventstore.Event, conflictCols, from, to []handler.Column, conds []handler.Condition, opts ...execOption) *handler.Statement {
func NewCopyStatement(event eventstore.Event, conflictCols, from, to, whereEqual []handler.Column, opts ...execOption) *handler.Statement {
columnNames := make([]string, len(to))
selectColumns := make([]string, len(from))
updateColumns := make([]string, len(columnNames))
@@ -350,8 +333,8 @@ func NewCopyStatement(event eventstore.Event, conflictCols, from, to []handler.C
}
wheres := make([]string, len(conds))
for i, cond := range conds {
wheres := make([]string, len(whereEqual))
for i, cond := range whereEqual {
argCounter++
wheres[i] = "copy_table." + cond.Name + " = $" + strconv.Itoa(argCounter)
args = append(args, cond.Value)
@@ -370,7 +353,7 @@ func NewCopyStatement(event eventstore.Event, conflictCols, from, to []handler.C
config.err = handler.ErrNoValues
}
if len(conds) == 0 {
if len(whereEqual) == 0 {
config.err = handler.ErrNoCondition
}
@@ -424,21 +407,16 @@ func columnsToQuery(cols []handler.Column) (names []string, parameters []string,
return names, parameters, values[:parameterIndex]
}
func conditionsToWhere(cols []handler.Condition, paramOffset int) (wheres []string, values []interface{}) {
wheres = make([]string, len(cols))
values = make([]interface{}, 0, len(cols))
for i, col := range cols {
param := "$" + strconv.Itoa(i+1+paramOffset)
wheres[i] = "(" + col.Name + " = " + param + ")"
if col.ParameterOpt != nil {
wheres[i] = "(" + col.Name + col.ParameterOpt(param) + ")"
}
if col.Value != nil {
values = append(values, col.Value)
func conditionsToWhere(conditions []handler.Condition, paramOffset int) (wheres []string, values []interface{}) {
wheres = make([]string, len(conditions))
values = make([]interface{}, 0, len(conditions))
for i, conditionFunc := range conditions {
condition, value := conditionFunc("$" + strconv.Itoa(i+1+paramOffset))
wheres[i] = fmt.Sprintf("(%s)", condition)
if value != nil {
values = append(values, value)
}
}
return wheres, values
}

View File

@@ -118,10 +118,6 @@ func (h *ProjectionHandler) Trigger(ctx context.Context, instances ...string) er
if len(instances) > 0 {
ids = instances
}
return h.processEvents(ctx, ids...)
}
func (h *ProjectionHandler) processEvents(ctx context.Context, ids ...string) error {
for {
events, hasLimitExceeded, err := h.FetchEvents(ctx, ids...)
if err != nil {

View File

@@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"github.com/zitadel/logging"
@@ -62,11 +63,10 @@ func NewJSONCol(name string, value interface{}) Column {
return NewCol(name, marshalled)
}
type Condition Column
type Condition func(param string) (string, interface{})
func NewCond(name string, value interface{}) Condition {
return Condition{
Name: name,
Value: value,
return func(param string) (string, interface{}) {
return fmt.Sprintf("%s = %s", name, param), value
}
}