@@ -13,12 +13,14 @@ type selectData struct {
13
13
PlaceholderFormat PlaceholderFormat
14
14
RunWith BaseRunner
15
15
Prefixes []Sqlizer
16
+ CTEs []Sqlizer
16
17
Options []string
17
18
Columns []Sqlizer
18
19
From Sqlizer
19
20
Joins []Sqlizer
20
21
WhereParts []Sqlizer
21
22
GroupBys []string
23
+ Settings []string
22
24
HavingParts []Sqlizer
23
25
OrderByParts []Sqlizer
24
26
Limit string
@@ -78,6 +80,15 @@ func (d *selectData) toSqlRaw() (sqlStr string, args []interface{}, err error) {
78
80
sql .WriteString (" " )
79
81
}
80
82
83
+ if len (d .CTEs ) > 0 {
84
+ sql .WriteString ("WITH " )
85
+ args , err = appendToSql (d .CTEs , sql , ", " , args )
86
+ if err != nil {
87
+ return
88
+ }
89
+ sql .WriteString (" " )
90
+ }
91
+
81
92
sql .WriteString ("SELECT " )
82
93
83
94
if len (d .Options ) > 0 {
@@ -147,6 +158,11 @@ func (d *selectData) toSqlRaw() (sqlStr string, args []interface{}, err error) {
147
158
sql .WriteString (d .Offset )
148
159
}
149
160
161
+ if len (d .Settings ) > 0 {
162
+ sql .WriteString (" SETTINGS " )
163
+ sql .WriteString (strings .Join (d .Settings , ", " ))
164
+ }
165
+
150
166
if len (d .Suffixes ) > 0 {
151
167
sql .WriteString (" " )
152
168
@@ -253,6 +269,22 @@ func (b SelectBuilder) Options(options ...string) SelectBuilder {
253
269
return builder .Extend (b , "Options" , options ).(SelectBuilder )
254
270
}
255
271
272
+ // With adds a non-recursive CTE to the query.
273
+ func (b SelectBuilder ) With (alias string , expr Sqlizer ) SelectBuilder {
274
+ return b .WithCTE (CTE {Alias : alias , ColumnList : []string {}, Recursive : false , Expression : expr })
275
+ }
276
+
277
+ // WithRecursive adds a recursive CTE to the query.
278
+ func (b SelectBuilder ) WithRecursive (alias string , expr Sqlizer ) SelectBuilder {
279
+ return b .WithCTE (CTE {Alias : alias , ColumnList : []string {}, Recursive : true , Expression : expr })
280
+ }
281
+
282
+ // WithCTE adds an arbitrary Sqlizer to the query.
283
+ // The sqlizer will be sandwiched between the keyword WITH and, if there's more than one CTE, a comma.
284
+ func (b SelectBuilder ) WithCTE (cte Sqlizer ) SelectBuilder {
285
+ return builder .Append (b , "CTEs" , cte ).(SelectBuilder )
286
+ }
287
+
256
288
// Columns adds result columns to the query.
257
289
func (b SelectBuilder ) Columns (columns ... string ) SelectBuilder {
258
290
parts := make ([]interface {}, 0 , len (columns ))
@@ -272,7 +304,8 @@ func (b SelectBuilder) RemoveColumns() SelectBuilder {
272
304
// Column adds a result column to the query.
273
305
// Unlike Columns, Column accepts args which will be bound to placeholders in
274
306
// the columns string, for example:
275
- // Column("IF(col IN ("+squirrel.Placeholders(3)+"), 1, 0) as col", 1, 2, 3)
307
+ //
308
+ // Column("IF(col IN ("+squirrel.Placeholders(3)+"), 1, 0) as col", 1, 2, 3)
276
309
func (b SelectBuilder ) Column (column interface {}, args ... interface {}) SelectBuilder {
277
310
return builder .Append (b , "Columns" , newPart (column , args ... )).(SelectBuilder )
278
311
}
@@ -351,6 +384,11 @@ func (b SelectBuilder) GroupBy(groupBys ...string) SelectBuilder {
351
384
return builder .Extend (b , "GroupBys" , groupBys ).(SelectBuilder )
352
385
}
353
386
387
+ // Setting adds SETTINGS expressions to the query.
388
+ func (b SelectBuilder ) Setting (settings ... string ) SelectBuilder {
389
+ return builder .Extend (b , "Settings" , settings ).(SelectBuilder )
390
+ }
391
+
354
392
// Having adds an expression to the HAVING clause of the query.
355
393
//
356
394
// See Where.
0 commit comments