-
Notifications
You must be signed in to change notification settings - Fork 45
/
expression.go
108 lines (90 loc) · 2.76 KB
/
expression.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package bob
import (
"context"
"database/sql"
"errors"
"fmt"
"io"
)
var ErrNoNamedArgs = errors.New("Dialect does not support named arguments")
// Dialect provides expressions with methods to write parts of the query
type Dialect interface {
// WriteArg should write an argument placeholder to the writer with the given index
WriteArg(w io.Writer, position int)
// WriteQuoted writes the given string to the writer surrounded by the appropriate
// quotes for the dialect
WriteQuoted(w io.Writer, s string)
}
// DialectWithNamed is a [Dialect] with the additional ability to WriteNamedArgs
type DialectWithNamed interface {
Dialect
// WriteNamedArg should write an argument placeholder to the writer with the given name
WriteNamedArg(w io.Writer, name string)
}
// Expression represents a section of a query
type Expression interface {
// Writes the textual representation of the expression to the writer
// using the given dialect.
// start is the beginning index of the args if it needs to write any
WriteSQL(ctx context.Context, w io.Writer, d Dialect, start int) (args []any, err error)
}
type ExpressionFunc func(ctx context.Context, w io.Writer, d Dialect, start int) ([]any, error)
func (e ExpressionFunc) WriteSQL(ctx context.Context, w io.Writer, d Dialect, start int) ([]any, error) {
return e(ctx, w, d, start)
}
func Express(ctx context.Context, w io.Writer, d Dialect, start int, e any) ([]any, error) {
switch v := e.(type) {
case string:
w.Write([]byte(v))
return nil, nil
case []byte:
w.Write(v)
return nil, nil
case sql.NamedArg:
dn, ok := d.(DialectWithNamed)
if !ok {
return nil, ErrNoNamedArgs
}
dn.WriteNamedArg(w, v.Name)
return []any{v}, nil
case Expression:
return v.WriteSQL(ctx, w, d, start)
default:
fmt.Fprint(w, e)
return nil, nil
}
}
// ExpressIf expands an express if the condition evaluates to true
// it can also add a prefix and suffix
func ExpressIf(ctx context.Context, w io.Writer, d Dialect, start int, e any, cond bool, prefix, suffix string) ([]any, error) {
if !cond {
return nil, nil
}
w.Write([]byte(prefix))
args, err := Express(ctx, w, d, start, e)
if err != nil {
return nil, err
}
w.Write([]byte(suffix))
return args, nil
}
// ExpressSlice is used to express a slice of expressions along with a prefix and suffix
func ExpressSlice[T any](ctx context.Context, w io.Writer, d Dialect, start int, expressions []T, prefix, sep, suffix string) ([]any, error) {
if len(expressions) == 0 {
return nil, nil
}
var args []any
w.Write([]byte(prefix))
for k, e := range expressions {
if k != 0 {
w.Write([]byte(sep))
}
newArgs, err := Express(ctx, w, d, start+len(args), e)
if err != nil {
return args, err
}
args = append(args, newArgs...)
}
w.Write([]byte(suffix))
return args, nil
}