Implement IN function

This commit is contained in:
Pijus Kamandulis
2024-02-24 17:26:16 +02:00
parent 332be181ef
commit f37c664c1a
7 changed files with 394 additions and 154 deletions

View File

@@ -192,6 +192,8 @@ func getFieldValue(field parsers.SelectItem, queryParameters map[string]interfac
return strings_ToString(typedValue.Arguments, queryParameters, row)
case parsers.FunctionCallIsDefined:
return typeChecking_IsDefined(typedValue.Arguments, queryParameters, row)
case parsers.FunctionCallIn:
return misc_In(typedValue.Arguments, queryParameters, row)
}
}

View File

@@ -58,4 +58,50 @@ func Test_Execute(t *testing.T) {
},
)
})
t.Run("Should execute IN function", func(t *testing.T) {
testQueryExecute(
t,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{
Path: []string{"c", "id"},
Type: parsers.SelectItemTypeField,
},
},
Table: parsers.Table{Value: "c"},
Filters: parsers.SelectItem{
Type: parsers.SelectItemTypeFunctionCall,
Value: parsers.FunctionCall{
Type: parsers.FunctionCallIn,
Arguments: []interface{}{
parsers.SelectItem{
Path: []string{"c", "id"},
Type: parsers.SelectItemTypeField,
},
parsers.SelectItem{
Type: parsers.SelectItemTypeConstant,
Value: parsers.Constant{
Type: parsers.ConstantTypeString,
Value: "123",
},
},
parsers.SelectItem{
Type: parsers.SelectItemTypeConstant,
Value: parsers.Constant{
Type: parsers.ConstantTypeString,
Value: "456",
},
},
},
},
},
},
mockData,
[]memoryexecutor.RowType{
map[string]interface{}{"id": "456"},
map[string]interface{}{"id": "123"},
},
)
})
}

View File

@@ -0,0 +1,18 @@
package memoryexecutor
import (
"github.com/pikami/cosmium/parsers"
)
func misc_In(arguments []interface{}, queryParameters map[string]interface{}, row RowType) bool {
value := getFieldValue(arguments[0].(parsers.SelectItem), queryParameters, row)
for i := 1; i < len(arguments); i++ {
compareValue := getFieldValue(arguments[i].(parsers.SelectItem), queryParameters, row)
if compareValues(value, compareValue) == 0 {
return true
}
}
return false
}