-
Notifications
You must be signed in to change notification settings - Fork 40
/
order_by.go
50 lines (39 loc) · 975 Bytes
/
order_by.go
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
package clause
import (
"fmt"
"io"
"github.com/stephenafamo/bob"
)
type OrderBy struct {
Expressions []OrderDef
}
func (o *OrderBy) AppendOrder(order OrderDef) {
o.Expressions = append(o.Expressions, order)
}
func (o OrderBy) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
return bob.ExpressSlice(w, d, start, o.Expressions, "ORDER BY ", ", ", "")
}
type OrderDef struct {
Expression any
Direction string // ASC | DESC | USING operator
Nulls string // FIRST | LAST
CollationName string
}
func (o OrderDef) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
if o.CollationName != "" {
w.Write([]byte("COLLATE "))
w.Write([]byte(o.CollationName))
}
args, err := bob.Express(w, d, start, o.Expression)
if err != nil {
return nil, err
}
if o.Direction != "" {
w.Write([]byte(" "))
w.Write([]byte(o.Direction))
}
if o.Nulls != "" {
fmt.Fprintf(w, " NULLS %s", o.Nulls)
}
return args, nil
}