Support UNION in Squirrel
Option: add UNION to selectData
- PR 1
- PR 2
- PR 3. This PR is not feasible because it assumes
- single union only
- No group by/join when union is added to the builder
type selectData struct {
PlaceholderFormat PlaceholderFormat
RunWith BaseRunner
Prefixes exprs
Options []string
Columns []Sqlizer
From Sqlizer
Joins []Sqlizer
WhereParts []Sqlizer
GroupBys []string
HavingParts []Sqlizer
OrderBys []string
Limit string
Offset string
Union []Sqlizer -- data type is same as Joins and From
UnionAll []Sqlizer
Suffixes exprs --previous used to simulate union
}
Inside ToSql, attach UNION and UNION ALL after suffixes
if len(d.Suffixes) > 0 {
sql.WriteString(" ")
args, _ = d.Suffixes.AppendToSql(sql, " ", args)
}
if len(d.Union) > 0 {
sql.WriteString(" UNION ")
args, err = appendToSql(d.Union, sql, " UNION ", args)
if err != nil {
return
}
}
if len(d.UnionAll) > 0 {
sql.WriteString(" UNION ALL ")
args, err = appendToSql(d.UnionAll, sql, " UNION ALL ", args)
if err != nil {
return
}
}
sqlStr = sql.String()
return
- Union and union all should probably be before the suffix
- Union still needs to support limit and order by
- This code assumes no mix of UNION and UNION ALL in the same SQL
Option: UnionBuilder
type unionSelect struct {
op string // e.g. "UNION"
selector squirrel.SelectBuilder
}
type unionData struct {
Selects []*unionSelect
Limit string
OrderBy []string
PlaceholderFormat sq.PlaceholderFormat
}
type UnionBuilder builder.Builder