use type NamespacedCondition

This commit is contained in:
Elio Bischof
2023-07-04 14:56:52 +02:00
parent d3635bb96b
commit 2d611c73d9
5 changed files with 36 additions and 47 deletions

View File

@@ -312,7 +312,7 @@ func Not(condition handler.Condition) 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, whereEqual []handler.Column, opts ...execOption) *handler.Statement {
func NewCopyStatement(event eventstore.Event, conflictCols, from, to []handler.Column, nsCond []handler.NamespacedCondition, opts ...execOption) *handler.Statement {
columnNames := make([]string, len(to))
selectColumns := make([]string, len(from))
updateColumns := make([]string, len(columnNames))
@@ -331,13 +331,12 @@ func NewCopyStatement(event eventstore.Event, conflictCols, from, to, whereEqual
}
}
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)
cond := make([]handler.Condition, len(nsCond))
for i := range nsCond {
cond[i] = nsCond[i]("copy_table")
}
wheres, values := conditionsToWhere(cond, len(args))
args = append(args, values...)
conflictTargets := make([]string, len(conflictCols))
for i, conflictCol := range conflictCols {
@@ -352,7 +351,7 @@ func NewCopyStatement(event eventstore.Event, conflictCols, from, to, whereEqual
config.err = handler.ErrNoValues
}
if len(whereEqual) == 0 {
if len(cond) == 0 {
config.err = handler.ErrNoCondition
}

View File

@@ -921,7 +921,7 @@ func TestNewCopyStatement(t *testing.T) {
conflictingCols []handler.Column
from []handler.Column
to []handler.Column
conds []handler.Column
conds []handler.NamespacedCondition
}
type want struct {
aggregateType eventstore.AggregateType
@@ -945,11 +945,8 @@ func TestNewCopyStatement(t *testing.T) {
sequence: 1,
previousSequence: 0,
},
conds: []handler.Column{
{
Name: "col2",
Value: 1,
},
conds: []handler.NamespacedCondition{
handler.NewNamespacedCondition("col2", 1),
},
},
want: want{
@@ -974,7 +971,7 @@ func TestNewCopyStatement(t *testing.T) {
sequence: 1,
previousSequence: 0,
},
conds: []handler.Column{},
conds: []handler.NamespacedCondition{},
from: []handler.Column{
{
Name: "col",
@@ -1008,7 +1005,7 @@ func TestNewCopyStatement(t *testing.T) {
sequence: 1,
previousSequence: 0,
},
conds: []handler.Column{},
conds: []handler.NamespacedCondition{},
from: []handler.Column{
{
Name: "col",
@@ -1045,10 +1042,8 @@ func TestNewCopyStatement(t *testing.T) {
sequence: 1,
previousSequence: 0,
},
conds: []handler.Column{
{
Name: "col",
},
conds: []handler.NamespacedCondition{
handler.NewNamespacedCondition("col2", nil),
},
from: []handler.Column{},
},
@@ -1103,15 +1098,9 @@ func TestNewCopyStatement(t *testing.T) {
Name: "col_b",
},
},
conds: []handler.Column{
{
Name: "id",
Value: 2,
},
{
Name: "state",
Value: 3,
},
conds: []handler.NamespacedCondition{
handler.NewNamespacedCondition("id", 2),
handler.NewNamespacedCondition("state", 3),
},
},
want: want{
@@ -1122,7 +1111,7 @@ func TestNewCopyStatement(t *testing.T) {
executer: &wantExecuter{
params: []params{
{
query: "INSERT INTO my_table (state, id, col_a, col_b) SELECT $1, id, col_a, col_b FROM my_table AS copy_table WHERE copy_table.id = $2 AND copy_table.state = $3 ON CONFLICT () DO UPDATE SET (state, id, col_a, col_b) = ($1, EXCLUDED.id, EXCLUDED.col_a, EXCLUDED.col_b)",
query: "INSERT INTO my_table (state, id, col_a, col_b) SELECT $1, id, col_a, col_b FROM my_table AS copy_table WHERE (copy_table.id = $2) AND (copy_table.state = $3) ON CONFLICT () DO UPDATE SET (state, id, col_a, col_b) = ($1, EXCLUDED.id, EXCLUDED.col_a, EXCLUDED.col_b)",
args: []interface{}{1, 2, 3},
},
},
@@ -1170,15 +1159,9 @@ func TestNewCopyStatement(t *testing.T) {
Name: "col_d",
},
},
conds: []handler.Column{
{
Name: "id",
Value: 2,
},
{
Name: "state",
Value: 3,
},
conds: []handler.NamespacedCondition{
handler.NewNamespacedCondition("id", 2),
handler.NewNamespacedCondition("state", 3),
},
},
want: want{
@@ -1189,7 +1172,7 @@ func TestNewCopyStatement(t *testing.T) {
executer: &wantExecuter{
params: []params{
{
query: "INSERT INTO my_table (state, id, col_c, col_d) SELECT $1, id, col_a, col_b FROM my_table AS copy_table WHERE copy_table.id = $2 AND copy_table.state = $3 ON CONFLICT () DO UPDATE SET (state, id, col_c, col_d) = ($1, EXCLUDED.id, EXCLUDED.col_a, EXCLUDED.col_b)",
query: "INSERT INTO my_table (state, id, col_c, col_d) SELECT $1, id, col_a, col_b FROM my_table AS copy_table WHERE (copy_table.id = $2) AND (copy_table.state = $3) ON CONFLICT () DO UPDATE SET (state, id, col_c, col_d) = ($1, EXCLUDED.id, EXCLUDED.col_a, EXCLUDED.col_b)",
args: []interface{}{1, 2, 3},
},
},

View File

@@ -4,7 +4,6 @@ import (
"database/sql"
"encoding/json"
"errors"
"github.com/zitadel/logging"
"github.com/zitadel/zitadel/internal/eventstore"
@@ -64,8 +63,16 @@ func NewJSONCol(name string, value interface{}) Column {
type Condition func(param string) (string, interface{})
type NamespacedCondition func(namespace string) Condition
func NewCond(name string, value interface{}) Condition {
return func(param string) (string, interface{}) {
return name + " = " + param, value
}
}
func NewNamespacedCondition(name string, value interface{}) NamespacedCondition {
return func(namespace string) Condition {
return NewCond(namespace+"."+name, value)
}
}