Implement 'GROUP BY' statement

This commit is contained in:
Pijus Kamandulis
2024-03-11 17:50:20 +02:00
parent 18edb925bf
commit b72bba86c8
6 changed files with 1120 additions and 942 deletions

View File

@@ -8,6 +8,7 @@ type SelectStmt struct {
Count int
Parameters map[string]interface{}
OrderExpressions []OrderExpression
GroupBy []SelectItem
}
type Table struct {

View File

@@ -63,6 +63,24 @@ func Test_Parse(t *testing.T) {
)
})
t.Run("Should parse SELECT with GROUP BY", func(t *testing.T) {
testQueryParse(
t,
`SELECT c.id, c["pk"] FROM c GROUP BY c.id, c.pk`,
parsers.SelectStmt{
SelectItems: []parsers.SelectItem{
{Path: []string{"c", "id"}},
{Path: []string{"c", "pk"}},
},
Table: parsers.Table{Value: "c"},
GroupBy: []parsers.SelectItem{
{Path: []string{"c", "id"}},
{Path: []string{"c", "pk"}},
},
},
)
})
t.Run("Should parse IN function", func(t *testing.T) {
testQueryParse(
t,

File diff suppressed because it is too large Load Diff

View File

@@ -6,7 +6,7 @@ import "github.com/pikami/cosmium/parsers"
func makeSelectStmt(
columns, table,
whereClause interface{}, distinctClause interface{},
count interface{}, orderList interface{},
count interface{}, groupByClause interface{}, orderList interface{},
) (parsers.SelectStmt, error) {
selectStmt := parsers.SelectStmt{
SelectItems: columns.([]parsers.SelectItem),
@@ -30,6 +30,10 @@ func makeSelectStmt(
selectStmt.OrderExpressions = orderExpressions
}
if groupByClause != nil {
selectStmt.GroupBy = groupByClause.([]parsers.SelectItem)
}
return selectStmt, nil
}
@@ -149,8 +153,10 @@ SelectStmt <- Select ws
topClause:TopClause? ws columns:Selection ws
From ws table:TableName ws
whereClause:(ws Where ws condition:Condition { return condition, nil })?
groupByClause:(ws GroupBy ws columns:ColumnList { return columns, nil })?
orderByClause:OrderByClause? {
return makeSelectStmt(columns, table, whereClause, distinctClause, topClause, orderByClause)
return makeSelectStmt(columns, table, whereClause,
distinctClause, topClause, groupByClause, orderByClause)
}
DistinctClause <- "DISTINCT"i
@@ -285,6 +291,8 @@ And <- "AND"i
Or <- "OR"i
GroupBy <- "GROUP"i ws "BY"i
OrderBy <- "ORDER"i ws "BY"i
ComparisonOperator <- ("=" / "!=" / "<" / "<=" / ">" / ">=") {