-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathorderby_executor.go
More file actions
56 lines (48 loc) · 1.09 KB
/
orderby_executor.go
File metadata and controls
56 lines (48 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package executor
import (
"planner"
"xcontext"
"github.com/pkg/errors"
"github.com/xelabs/go-mysqlstack/xlog"
)
var (
_ Executor = &OrderByExecutor{}
)
// OrderByExecutor represents order by executor.
type OrderByExecutor struct {
log *xlog.Log
plan planner.Plan
}
// NewOrderByExecutor creates new orderby executor.
func NewOrderByExecutor(log *xlog.Log, plan planner.Plan) *OrderByExecutor {
return &OrderByExecutor{
log: log,
plan: plan,
}
}
// Execute used to execute the executor.
func (executor *OrderByExecutor) Execute(ctx *xcontext.ResultContext) error {
rs := ctx.Results
plan := executor.plan.(*planner.OrderByPlan)
for _, orderby := range plan.OrderBys {
switch orderby.Direction {
case planner.ASC:
if err := rs.OrderedByAsc(orderby.Table, orderby.Field); err != nil {
return errors.WithStack(err)
}
case planner.DESC:
if err := rs.OrderedByDesc(orderby.Table, orderby.Field); err != nil {
return errors.WithStack(err)
}
}
}
rs.Sort()
return nil
}