Support parameter in bracket #8

This commit is contained in:
Pijus Kamandulis 2024-12-25 21:28:42 +02:00
parent 39cd9e2357
commit 928ca29fe4
6 changed files with 1405 additions and 1306 deletions

View File

@ -147,6 +147,21 @@ func Test_Documents(t *testing.T) {
) )
}) })
t.Run("Should query document with query parameters as accessor", func(t *testing.T) {
testCosmosQuery(t, collectionClient,
`select c.id
FROM c
WHERE c[@param]="67890"
ORDER BY c.id`,
[]azcosmos.QueryParameter{
{Name: "@param", Value: "id"},
},
[]interface{}{
map[string]interface{}{"id": "67890"},
},
)
})
t.Run("Should query array accessor", func(t *testing.T) { t.Run("Should query array accessor", func(t *testing.T) {
testCosmosQuery(t, collectionClient, testCosmosQuery(t, collectionClient,
`SELECT c.id, `SELECT c.id,

File diff suppressed because it is too large Load Diff

View File

@ -325,6 +325,7 @@ DotFieldAccess <- "." id:Identifier {
ArrayFieldAccess <- "[\"" id:Identifier "\"]" { return id, nil } ArrayFieldAccess <- "[\"" id:Identifier "\"]" { return id, nil }
/ "[" id:Integer "]" { return strconv.Itoa(id.(int)), nil } / "[" id:Integer "]" { return strconv.Itoa(id.(int)), nil }
/ "[" id:ParameterConstant "]" { return id.(parsers.Constant).Value.(string), nil }
Identifier <- [a-zA-Z_][a-zA-Z0-9_]* { Identifier <- [a-zA-Z_][a-zA-Z0-9_]* {
return string(c.text), nil return string(c.text), nil

View File

@ -22,6 +22,20 @@ func Test_Parse_Select(t *testing.T) {
) )
}) })
t.Run("Should parse SELECT with query parameters as accessor", func(t *testing.T) {
testQueryParse(
t,
`SELECT c.id, c[@param] FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{Path: []string{"c", "id"}},
{Path: []string{"c", "@param"}},
},
Table: parsers.Table{Value: "c"},
},
)
})
t.Run("Should parse SELECT DISTINCT", func(t *testing.T) { t.Run("Should parse SELECT DISTINCT", func(t *testing.T) {
testQueryParse( testQueryParse(
t, t,

View File

@ -317,6 +317,10 @@ func (r rowContext) applyProjection(selectItems []parsers.SelectItem) RowType {
} else { } else {
destinationName = fmt.Sprintf("$%d", index+1) destinationName = fmt.Sprintf("$%d", index+1)
} }
if destinationName[0] == '@' {
destinationName = r.parameters[destinationName].(string)
}
} }
row[destinationName] = r.resolveSelectItem(selectItem) row[destinationName] = r.resolveSelectItem(selectItem)
@ -572,6 +576,9 @@ func (r rowContext) selectItem_SelectItemTypeField(selectItem parsers.SelectItem
if len(selectItem.Path) > 1 { if len(selectItem.Path) > 1 {
for _, pathSegment := range selectItem.Path[1:] { for _, pathSegment := range selectItem.Path[1:] {
if pathSegment[0] == '@' {
pathSegment = r.parameters[pathSegment].(string)
}
switch nestedValue := value.(type) { switch nestedValue := value.(type) {
case map[string]interface{}: case map[string]interface{}:

View File

@ -35,6 +35,29 @@ func Test_Execute_Select(t *testing.T) {
) )
}) })
t.Run("Should execute SELECT with query parameters as accessor", func(t *testing.T) {
testQueryExecute(
t,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{Path: []string{"c", "id"}},
{Path: []string{"c", "@param"}},
},
Table: parsers.Table{Value: "c"},
Parameters: map[string]interface{}{
"@param": "pk",
},
},
mockData,
[]memoryexecutor.RowType{
map[string]interface{}{"id": "12345", "pk": 123},
map[string]interface{}{"id": "67890", "pk": 456},
map[string]interface{}{"id": "456", "pk": 456},
map[string]interface{}{"id": "123", "pk": 456},
},
)
})
t.Run("Should execute SELECT DISTINCT", func(t *testing.T) { t.Run("Should execute SELECT DISTINCT", func(t *testing.T) {
testQueryExecute( testQueryExecute(
t, t,