Add support for array field access

This commit is contained in:
Pijus Kamandulis 2024-02-12 00:55:07 +02:00
parent ca49228c66
commit bdf9970ce6
3 changed files with 311 additions and 233 deletions

File diff suppressed because it is too large Load Diff

View File

@ -24,11 +24,7 @@ func makeFieldPath(name interface{}, path interface{}, alias interface{}) (parse
paths := make([]string, 1)
paths[0] = name.(string)
for _, p := range ps {
pa := p.([]interface{})
px := pa[1:]
for _, pi := range px {
paths = append(paths, pi.(string))
}
paths = append(paths, p.(string))
}
fieldPath := parsers.FieldPath{Path: paths}
@ -97,11 +93,19 @@ TableName <- key:Identifier {
return parsers.Table{Value: key.(string)}, nil
}
FieldPath <- name:Identifier path:("." Identifier)*
FieldPath <- name:Identifier path:(DotFieldAccess / ArrayFieldAccess)*
asClause:(ws "AS" ws alias:Identifier { return alias, nil })? {
return makeFieldPath(name, path, asClause)
}
DotFieldAccess <- "." id:Identifier {
return id, nil
}
ArrayFieldAccess <- "[\"" id:Identifier "\"]" {
return id, nil
}
Identifier <- [a-zA-Z_][a-zA-Z0-9_]* {
return string(c.text), nil
}

View File

@ -41,7 +41,7 @@ func Test_Parse(t *testing.T) {
t.Run("Shoul parse simple SELECT", func(t *testing.T) {
testQueryParse(
t,
`SELECT c.id, c.pk FROM c`,
`SELECT c.id, c["pk"] FROM c`,
parsers.SelectStmt{
Columns: []parsers.FieldPath{
{Path: []string{"c", "id"}},