mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-19 17:00:37 +00:00
Implement 'GROUP BY' statement
This commit is contained in:
@@ -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
@@ -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 <- ("=" / "!=" / "<" / "<=" / ">" / ">=") {
|
||||
|
||||
Reference in New Issue
Block a user