forked from stephenafamo/bob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrom.go
191 lines (152 loc) · 4.18 KB
/
from.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package clause
import (
"fmt"
"io"
"github.com/stephenafamo/bob"
)
/*
https://www.postgresql.org/docs/current/sql-select.html#SQL-WITH
where from_item can be one of:
[ ONLY ] table_name [ * ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
[ TABLESAMPLE sampling_method ( argument [, ...] ) [ REPEATABLE ( seed ) ] ]
[ LATERAL ] ( select ) [ AS ] alias [ ( column_alias [, ...] ) ]
with_query_name [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
[ LATERAL ] function_name ( [ argument [, ...] ] )
[ WITH ORDINALITY ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
[ LATERAL ] function_name ( [ argument [, ...] ] ) [ AS ] alias ( column_definition [, ...] )
[ LATERAL ] function_name ( [ argument [, ...] ] ) AS ( column_definition [, ...] )
[ LATERAL ] ROWS FROM( function_name ( [ argument [, ...] ] ) [ AS ( column_definition [, ...] ) ] [, ...] )
[ WITH ORDINALITY ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
from_item [ NATURAL ] join_type from_item [ ON join_condition | USING ( join_column [, ...] ) [ AS join_using_alias ] ]
SQLite: https://www.sqlite.org/syntax/table-or-subquery.html
MySQL: https://dev.mysql.com/doc/refman/8.0/en/join.html
*/
type From struct {
Table any
// Aliases
Alias string
Columns []string
// Dialect specific modifiers
Only bool // Postgres
Lateral bool // Postgres & MySQL
WithOrdinality bool // Postgres
IndexedBy *string // SQLite
Partitions []string // MySQL
IndexHints []IndexHint // MySQL
// Joins
Joins []Join
}
func (f *From) SetTable(table any) {
f.Table = table
}
func (f *From) SetTableAlias(alias string, columns ...string) {
f.Alias = alias
f.Columns = columns
}
func (f *From) SetOnly(only bool) {
f.Only = only
}
func (f *From) SetLateral(lateral bool) {
f.Lateral = lateral
}
func (f *From) SetWithOrdinality(to bool) {
f.WithOrdinality = to
}
func (f *From) SetIndexedBy(i *string) {
f.IndexedBy = i
}
func (f *From) AppendJoin(j Join) {
f.Joins = append(f.Joins, j)
}
func (f *From) AppendPartition(partitions ...string) {
f.Partitions = append(f.Partitions, partitions...)
}
func (f *From) AppendIndexHint(i IndexHint) {
f.IndexHints = append(f.IndexHints, i)
}
func (f From) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
if f.Table == nil {
return nil, nil
}
if f.Only {
w.Write([]byte("ONLY "))
}
if f.Lateral {
w.Write([]byte("LATERAL "))
}
_, isQuery := f.Table.(bob.Query)
if isQuery {
w.Write([]byte("("))
}
args, err := bob.Express(w, d, start, f.Table)
if err != nil {
return nil, err
}
if isQuery {
w.Write([]byte(")"))
}
if f.WithOrdinality {
w.Write([]byte(" WITH ORDINALITY"))
}
_, err = bob.ExpressSlice(w, d, start, f.Partitions, " PARTITION (", ", ", ")")
if err != nil {
return nil, err
}
if f.Alias != "" {
w.Write([]byte(" AS "))
d.WriteQuoted(w, f.Alias)
}
if len(f.Columns) > 0 {
w.Write([]byte("("))
for k, cAlias := range f.Columns {
if k != 0 {
w.Write([]byte(", "))
}
d.WriteQuoted(w, cAlias)
}
w.Write([]byte(")"))
}
// No args for index hints
_, err = bob.ExpressSlice(w, d, start+len(args), f.IndexHints, "\n", " ", "")
if err != nil {
return nil, err
}
switch {
case f.IndexedBy == nil:
break
case *f.IndexedBy == "":
w.Write([]byte(" NOT INDEXED"))
default:
w.Write([]byte(" INDEXED BY "))
w.Write([]byte(*f.IndexedBy))
}
joinArgs, err := bob.ExpressSlice(w, d, start+len(args), f.Joins, "\n", "\n", "")
if err != nil {
return nil, err
}
args = append(args, joinArgs...)
return args, nil
}
type IndexHint struct {
Type string // USE, FORCE or IGNORE
Indexes []string
For string // JOIN, ORDER BY or GROUP BY
}
func (f IndexHint) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
if f.Type == "" {
return nil, nil
}
fmt.Fprintf(w, "%s INDEX ", f.Type)
_, err := bob.ExpressIf(w, d, start, f.For, f.For != "", " FOR ", "")
if err != nil {
return nil, err
}
// Always include the brackets
fmt.Fprint(w, " (")
_, err = bob.ExpressSlice(w, d, start, f.Indexes, "", ", ", "")
if err != nil {
return nil, err
}
fmt.Fprint(w, ")")
return nil, nil
}