-
Notifications
You must be signed in to change notification settings - Fork 36
/
table.go
53 lines (41 loc) · 891 Bytes
/
table.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
package clause
import (
"io"
"github.com/stephenafamo/bob"
)
type Table struct {
Expression any
Alias string
Columns []string
Partitions []string // MySQL
}
func (t Table) As(alias string, columns ...string) Table {
t.Alias = alias
t.Columns = append(t.Columns, columns...)
return t
}
func (t Table) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
args, err := bob.Express(w, d, start, t.Expression)
if err != nil {
return nil, err
}
if t.Alias != "" {
w.Write([]byte(" AS "))
d.WriteQuoted(w, t.Alias)
}
if len(t.Columns) > 0 {
w.Write([]byte(" ("))
for k, cAlias := range t.Columns {
if k != 0 {
w.Write([]byte(", "))
}
d.WriteQuoted(w, cAlias)
}
w.Write([]byte(")"))
}
_, err = bob.ExpressSlice(w, d, start, t.Partitions, " PARTITION (", ", ", ")")
if err != nil {
return nil, err
}
return args, nil
}