mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-19 17:00:37 +00:00
Implement STRINGEQUALS
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -146,7 +146,7 @@ TopClause <- Top ws count:Integer {
|
||||
return count, nil
|
||||
}
|
||||
|
||||
Selection <- SelectValueSpec / ColumnList /SelectAsterisk
|
||||
Selection <- SelectValueSpec / ColumnList / SelectAsterisk
|
||||
|
||||
SelectAsterisk <- "*" {
|
||||
selectItem, _ := makeSelectItem("c", make([]interface{}, 0), parsers.SelectItemTypeField)
|
||||
@@ -158,7 +158,7 @@ ColumnList <- column:SelectItem other_columns:(ws "," ws coll:SelectItem {return
|
||||
return makeColumnList(column, other_columns)
|
||||
}
|
||||
|
||||
SelectValueSpec <- "VALUE" ws column:SelectItem {
|
||||
SelectValueSpec <- "VALUE"i ws column:SelectItem {
|
||||
selectItem := column.(parsers.SelectItem)
|
||||
selectItem.IsTopLevel = true
|
||||
return makeColumnList(selectItem, make([]interface{}, 0))
|
||||
@@ -186,7 +186,7 @@ SelectProperty <- name:Identifier path:(DotFieldAccess / ArrayFieldAccess)* {
|
||||
return makeSelectItem(name, path, parsers.SelectItemTypeField)
|
||||
}
|
||||
|
||||
SelectItem <- selectItem:(Literal / SelectArray / SelectObject / SelectProperty) asClause:AsClause? {
|
||||
SelectItem <- selectItem:(Literal / StringEqualsExpression / SelectArray / SelectObject / SelectProperty) asClause:AsClause? {
|
||||
var itemResult parsers.SelectItem
|
||||
switch typedValue := selectItem.(type) {
|
||||
case parsers.SelectItem:
|
||||
@@ -196,6 +196,11 @@ SelectItem <- selectItem:(Literal / SelectArray / SelectObject / SelectProperty)
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: typedValue,
|
||||
}
|
||||
case parsers.FunctionCall:
|
||||
itemResult = parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: typedValue,
|
||||
}
|
||||
}
|
||||
|
||||
if aliasValue, ok := asClause.(string); ok {
|
||||
@@ -244,34 +249,36 @@ OrderExpression <- field:SelectProperty ws order:OrderDirection? {
|
||||
return makeOrderExpression(field, order)
|
||||
}
|
||||
|
||||
OrderDirection <- ("ASC" / "asc" / "DESC" / "desc") {
|
||||
switch string(c.text) {
|
||||
case "DESC", "desc":
|
||||
OrderDirection <- ("ASC"i / "DESC"i) {
|
||||
if strings.EqualFold(string(c.text), "DESC") {
|
||||
return parsers.OrderDirectionDesc, nil
|
||||
}
|
||||
}
|
||||
|
||||
return parsers.OrderDirectionAsc, nil
|
||||
}
|
||||
|
||||
Select <- ("select" / "SELECT")
|
||||
Select <- "SELECT"i
|
||||
|
||||
Top <- ("top" / "TOP")
|
||||
Top <- "TOP"i
|
||||
|
||||
As <- ("as" / "AS")
|
||||
As <- "AS"i
|
||||
|
||||
From <- ("from" / "FROM")
|
||||
From <- "FROM"i
|
||||
|
||||
Where <- ("where" / "WHERE")
|
||||
Where <- "WHERE"i
|
||||
|
||||
And <- ("and" / "AND")
|
||||
And <- "AND"i
|
||||
|
||||
Or <- ("or" / "OR")
|
||||
Or <- "OR"i
|
||||
|
||||
OrderBy <- ("order" / "ORDER") ws ("by" / "BY")
|
||||
OrderBy <- "ORDER"i ws "BY"i
|
||||
|
||||
ComparisonOperator <- "=" / "!=" / "<" / "<=" / ">" / ">=" {
|
||||
return string(c.text), nil
|
||||
}
|
||||
|
||||
StringEquals <- "STRINGEQUALS"i
|
||||
|
||||
Literal <- FloatLiteral / IntegerLiteral / StringLiteral / BooleanLiteral / ParameterConstant / NullConstant
|
||||
|
||||
ParameterConstant <- "@" Identifier {
|
||||
@@ -291,11 +298,15 @@ FloatLiteral <- [0-9]+"."[0-9]+ {
|
||||
floatValue, _ := strconv.ParseFloat(string(c.text), 64)
|
||||
return parsers.Constant{Type: parsers.ConstantTypeFloat, Value: floatValue}, nil
|
||||
}
|
||||
BooleanLiteral <- ("true" / "false") {
|
||||
BooleanLiteral <- ("true"i / "false"i) {
|
||||
boolValue, _ := strconv.ParseBool(string(c.text))
|
||||
return parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: boolValue}, nil
|
||||
}
|
||||
|
||||
StringEqualsExpression <- StringEquals ws "(" ws ex1:SelectItem ws "," ws ex2:SelectItem ws ignoreCase:("," ws boolean:SelectItem { return boolean, nil })? ")" {
|
||||
return parsers.FunctionCall{Type: parsers.FunctionCallStringEquals, Arguments: []interface{}{ex1, ex2, ignoreCase}}, nil
|
||||
}
|
||||
|
||||
Integer <- [0-9]+ {
|
||||
return strconv.Atoi(string(c.text))
|
||||
}
|
||||
|
||||
80
parsers/nosql/nosql_select_string_functions_test.go
Normal file
80
parsers/nosql/nosql_select_string_functions_test.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package nosql_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func Test_Execute_StringFunctions(t *testing.T) {
|
||||
|
||||
t.Run("Should execute function STRINGEQUALS(ex1, ex2, ignoreCase)", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT STRINGEQUALS(c.id, "123", true) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallStringEquals,
|
||||
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.ConstantTypeBoolean,
|
||||
Value: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should execute function STRINGEQUALS(ex1, ex2)", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT STRINGEQUALS(c.id, "123") FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallStringEquals,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "id"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeConstant,
|
||||
Value: parsers.Constant{
|
||||
Type: parsers.ConstantTypeString,
|
||||
Value: "123",
|
||||
},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user