mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-18 16:30:44 +00:00
Implement CONCAT function
This commit is contained in:
@@ -180,6 +180,8 @@ func getFieldValue(field parsers.SelectItem, queryParameters map[string]interfac
|
||||
return strings_StringEquals(typedValue.Arguments, queryParameters, row)
|
||||
case parsers.FunctionCallIsDefined:
|
||||
return typeChecking_IsDefined(typedValue.Arguments, queryParameters, row)
|
||||
case parsers.FunctionCallConcat:
|
||||
return strings_Concat(typedValue.Arguments, queryParameters, row)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -103,4 +103,52 @@ func Test_Execute_StringFunctions(t *testing.T) {
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function CONCAT()", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Alias: "concat",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallConcat,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: " ",
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: 123,
|
||||
},
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "pk"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"concat": "123 123aaa"},
|
||||
map[string]interface{}{"concat": "456 123bbb"},
|
||||
map[string]interface{}{"concat": "789 123AAA"},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -39,3 +39,28 @@ func strings_StringEquals(arguments []interface{}, queryParameters map[string]in
|
||||
|
||||
return str1 == str2
|
||||
}
|
||||
|
||||
func strings_Concat(arguments []interface{}, queryParameters map[string]interface{}, row RowType) string {
|
||||
result := ""
|
||||
|
||||
for _, arg := range arguments {
|
||||
if selectItem, ok := arg.(parsers.SelectItem); ok {
|
||||
value := getFieldValue(selectItem, queryParameters, row)
|
||||
result += convertToString(value)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func convertToString(value interface{}) string {
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
return v
|
||||
case int:
|
||||
return fmt.Sprintf("%d", v)
|
||||
case float32, float64:
|
||||
return fmt.Sprintf("%f", v)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user