mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-18 16:30:44 +00:00
Implement STRINGEQUALS
This commit is contained in:
@@ -161,6 +161,20 @@ func getFieldValue(field parsers.SelectItem, queryParameters map[string]interfac
|
||||
return typedValue.Value
|
||||
}
|
||||
|
||||
if field.Type == parsers.SelectItemTypeFunctionCall {
|
||||
var typedValue parsers.FunctionCall
|
||||
var ok bool
|
||||
if typedValue, ok = field.Value.(parsers.FunctionCall); !ok {
|
||||
// TODO: Handle error
|
||||
fmt.Println("parsers.Constant has incorrect Value type")
|
||||
}
|
||||
|
||||
switch typedValue.Type {
|
||||
case parsers.FunctionCallStringEquals:
|
||||
return strings_StringEquals(typedValue.Arguments, queryParameters, row)
|
||||
}
|
||||
}
|
||||
|
||||
value := row
|
||||
if len(field.Path) > 1 {
|
||||
for _, pathSegment := range field.Path[1:] {
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
package memoryexecutor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
)
|
||||
|
||||
func Test_Execute_StringFunctions(t *testing.T) {
|
||||
mockData := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "123", "pk": "aaa"},
|
||||
map[string]interface{}{"id": "456", "pk": "bbb"},
|
||||
map[string]interface{}{"id": "789", "pk": "AAA"},
|
||||
}
|
||||
|
||||
t.Run("Should execute function STRINGEQUALS(ex1, ex2, ignoreCase)", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
{
|
||||
Alias: "stringEquals",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallStringEquals,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "pk"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "aaa",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeBoolean,
|
||||
Value: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "123", "stringEquals": true},
|
||||
map[string]interface{}{"id": "456", "stringEquals": false},
|
||||
map[string]interface{}{"id": "789", "stringEquals": true},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function STRINGEQUALS(ex1, ex2)", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
{
|
||||
Alias: "stringEquals",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallStringEquals,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "pk"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "aaa",
|
||||
},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "123", "stringEquals": true},
|
||||
map[string]interface{}{"id": "456", "stringEquals": false},
|
||||
map[string]interface{}{"id": "789", "stringEquals": false},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
41
query_executors/memory_executor/string_functions.go
Normal file
41
query_executors/memory_executor/string_functions.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package memoryexecutor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func strings_StringEquals(arguments []interface{}, queryParameters map[string]interface{}, row RowType) bool {
|
||||
ignoreCase := false
|
||||
if len(arguments) > 2 && arguments[2] != nil {
|
||||
ignoreCaseItem := arguments[2].(parsers.SelectItem)
|
||||
if value, ok := getFieldValue(ignoreCaseItem, queryParameters, row).(bool); ok {
|
||||
ignoreCase = value
|
||||
}
|
||||
}
|
||||
|
||||
ex1Item := arguments[0].(parsers.SelectItem)
|
||||
ex2Item := arguments[1].(parsers.SelectItem)
|
||||
|
||||
ex1 := getFieldValue(ex1Item, queryParameters, row)
|
||||
ex2 := getFieldValue(ex2Item, queryParameters, row)
|
||||
|
||||
var ok bool
|
||||
var str1 string
|
||||
var str2 string
|
||||
|
||||
if str1, ok = ex1.(string); !ok {
|
||||
fmt.Println("StringEquals got parameters of wrong type")
|
||||
}
|
||||
if str2, ok = ex2.(string); !ok {
|
||||
fmt.Println("StringEquals got parameters of wrong type")
|
||||
}
|
||||
|
||||
if ignoreCase {
|
||||
return strings.EqualFold(str1, str2)
|
||||
}
|
||||
|
||||
return str1 == str2
|
||||
}
|
||||
Reference in New Issue
Block a user