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 := make([]string, 1)
paths[0] = name.(string) paths[0] = name.(string)
for _, p := range ps { for _, p := range ps {
pa := p.([]interface{}) paths = append(paths, p.(string))
px := pa[1:]
for _, pi := range px {
paths = append(paths, pi.(string))
}
} }
fieldPath := parsers.FieldPath{Path: paths} fieldPath := parsers.FieldPath{Path: paths}
@ -97,11 +93,19 @@ TableName <- key:Identifier {
return parsers.Table{Value: key.(string)}, nil 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 })? { asClause:(ws "AS" ws alias:Identifier { return alias, nil })? {
return makeFieldPath(name, path, asClause) 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_]* { Identifier <- [a-zA-Z_][a-zA-Z0-9_]* {
return string(c.text), nil 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) { t.Run("Shoul parse simple SELECT", func(t *testing.T) {
testQueryParse( testQueryParse(
t, t,
`SELECT c.id, c.pk FROM c`, `SELECT c.id, c["pk"] FROM c`,
parsers.SelectStmt{ parsers.SelectStmt{
Columns: []parsers.FieldPath{ Columns: []parsers.FieldPath{
{Path: []string{"c", "id"}}, {Path: []string{"c", "id"}},