-
Notifications
You must be signed in to change notification settings - Fork 38
/
operators.go
55 lines (44 loc) · 1015 Bytes
/
operators.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
51
52
53
54
55
package expr
import (
"fmt"
"io"
"github.com/stephenafamo/bob"
)
// An operator that has a left and right side
type leftRight struct {
operator string
right any
left any
}
func (lr leftRight) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
largs, err := bob.Express(w, d, start, lr.left)
if err != nil {
return nil, err
}
fmt.Fprintf(w, " %s ", lr.operator)
rargs, err := bob.Express(w, d, start+len(largs), lr.right)
if err != nil {
return nil, err
}
return append(largs, rargs...), nil
}
// Generic operator between a left and right val
func OP(operator string, left, right any) bob.Expression {
return leftRight{
right: right,
left: left,
operator: operator,
}
}
// If no separator, a space is used
type Join struct {
Exprs []bob.Expression
Sep string
}
func (s Join) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
sep := s.Sep
if sep == "" {
sep = " "
}
return bob.ExpressSlice(w, d, start, s.Exprs, "", sep, "")
}