Added support for 'SELECT *'

This commit is contained in:
Pijus Kamandulis 2024-02-15 23:11:46 +02:00
parent a36b578a9b
commit f183f308fb
5 changed files with 347 additions and 282 deletions

File diff suppressed because it is too large Load Diff

View File

@ -112,7 +112,13 @@ TopClause <- Top ws count:Integer {
return count, nil return count, nil
} }
Selection <- SelectValueSpec / ColumnList Selection <- SelectValueSpec / ColumnList /SelectAsterisk
SelectAsterisk <- "*" {
selectItem, _ := makeSelectItem("c", make([]interface{}, 0), parsers.SelectItemTypeField)
selectItem.IsTopLevel = true
return makeColumnList(selectItem, make([]interface{}, 0))
}
ColumnList <- column:SelectItem other_columns:(ws "," ws coll:SelectItem {return coll, nil })* { ColumnList <- column:SelectItem other_columns:(ws "," ws coll:SelectItem {return coll, nil })* {
return makeColumnList(column, other_columns) return makeColumnList(column, other_columns)

View File

@ -79,6 +79,19 @@ func Test_Parse(t *testing.T) {
) )
}) })
t.Run("Should parse SELECT *", func(t *testing.T) {
testQueryParse(
t,
`SELECT * FROM c`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{Path: []string{"c"}, IsTopLevel: true},
},
Table: parsers.Table{Value: "c"},
},
)
})
t.Run("Should parse SELECT array", func(t *testing.T) { t.Run("Should parse SELECT array", func(t *testing.T) {
testQueryParse( testQueryParse(
t, t,

View File

@ -112,11 +112,13 @@ func getFieldValue(field parsers.SelectItem, row RowType) interface{} {
} }
value := row value := row
for _, pathSegment := range field.Path[1:] { if len(field.Path) > 1 {
if nestedValue, ok := value.(map[string]interface{}); ok { for _, pathSegment := range field.Path[1:] {
value = nestedValue[pathSegment] if nestedValue, ok := value.(map[string]interface{}); ok {
} else { value = nestedValue[pathSegment]
return nil } else {
return nil
}
} }
} }
return value return value

View File

@ -86,6 +86,20 @@ func Test_Execute(t *testing.T) {
) )
}) })
t.Run("Should execute SELECT *", func(t *testing.T) {
testQueryExecute(
t,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{Path: []string{"c"}, IsTopLevel: true},
},
Table: parsers.Table{Value: "c"},
},
mockData,
mockData,
)
})
t.Run("Should execute SELECT array", func(t *testing.T) { t.Run("Should execute SELECT array", func(t *testing.T) {
testQueryExecute( testQueryExecute(
t, t,