mirror of
https://github.com/uptrace/go-clickhouse.git
synced 2025-06-06 23:16:18 +02:00
feat: union expr
This commit is contained in:
parent
e1af2632f1
commit
51c9cc65ba
@ -12,6 +12,11 @@ import (
|
||||
"github.com/uptrace/go-clickhouse/ch/internal"
|
||||
)
|
||||
|
||||
type union struct {
|
||||
expr string
|
||||
query *SelectQuery
|
||||
}
|
||||
|
||||
type SelectQuery struct {
|
||||
baseQuery
|
||||
|
||||
@ -26,6 +31,8 @@ type SelectQuery struct {
|
||||
limit int
|
||||
offset int
|
||||
final bool
|
||||
|
||||
union []union
|
||||
}
|
||||
|
||||
var _ Query = (*SelectQuery)(nil)
|
||||
@ -150,6 +157,24 @@ func (q *SelectQuery) ExcludeColumn(columns ...string) *SelectQuery {
|
||||
return q
|
||||
}
|
||||
|
||||
///------------------------------------------------------------------------------
|
||||
|
||||
func (q *SelectQuery) Union(other *SelectQuery) *SelectQuery {
|
||||
return q.addUnion(" UNION ", other)
|
||||
}
|
||||
|
||||
func (q *SelectQuery) UnionAll(other *SelectQuery) *SelectQuery {
|
||||
return q.addUnion(" UNION ALL ", other)
|
||||
}
|
||||
|
||||
func (q *SelectQuery) addUnion(expr string, other *SelectQuery) *SelectQuery {
|
||||
q.union = append(q.union, union{
|
||||
expr: expr,
|
||||
query: other,
|
||||
})
|
||||
return q
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
func (q *SelectQuery) Join(join string, args ...any) *SelectQuery {
|
||||
@ -329,6 +354,10 @@ func (q *SelectQuery) appendQuery(
|
||||
b = append(b, `WITH "_count_wrapper" AS (`...)
|
||||
}
|
||||
|
||||
if len(q.union) > 0 {
|
||||
b = append(b, '(')
|
||||
}
|
||||
|
||||
if len(q.with) > 0 {
|
||||
b, err = q.appendWith(fmter, b)
|
||||
if err != nil {
|
||||
@ -451,7 +480,23 @@ func (q *SelectQuery) appendQuery(
|
||||
b = append(b, " OFFSET "...)
|
||||
b = strconv.AppendInt(b, int64(q.offset), 10)
|
||||
}
|
||||
} else if cteCount {
|
||||
}
|
||||
|
||||
if len(q.union) > 0 {
|
||||
b = append(b, ')')
|
||||
|
||||
for _, u := range q.union {
|
||||
b = append(b, u.expr...)
|
||||
b = append(b, '(')
|
||||
b, err = u.query.AppendQuery(fmter, b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = append(b, ')')
|
||||
}
|
||||
}
|
||||
|
||||
if cteCount {
|
||||
b = append(b, `) SELECT `...)
|
||||
b = append(b, "count()"...)
|
||||
b = append(b, ` FROM "_count_wrapper"`...)
|
||||
|
@ -115,6 +115,16 @@ func TestQuery(t *testing.T) {
|
||||
Group("id").
|
||||
OrderExpr("id")
|
||||
},
|
||||
func(db *ch.DB) chschema.QueryAppender {
|
||||
q1 := db.NewSelect().Model(new(Model)).Where("1")
|
||||
q2 := db.NewSelect().Model(new(Model))
|
||||
return q1.Union(q2)
|
||||
},
|
||||
func(db *ch.DB) chschema.QueryAppender {
|
||||
q1 := db.NewSelect().Model(new(Model)).Where("1")
|
||||
q2 := db.NewSelect().Model(new(Model))
|
||||
return q1.UnionAll(q2)
|
||||
},
|
||||
}
|
||||
|
||||
db := chDB()
|
||||
|
1
ch/testdata/snapshots/TestQuery-17
vendored
Normal file
1
ch/testdata/snapshots/TestQuery-17
vendored
Normal file
@ -0,0 +1 @@
|
||||
(SELECT "model"."id", "model"."string", "model"."bytes" FROM "models" AS "model" WHERE (1)) UNION (SELECT "model"."id", "model"."string", "model"."bytes" FROM "models" AS "model")
|
1
ch/testdata/snapshots/TestQuery-18
vendored
Normal file
1
ch/testdata/snapshots/TestQuery-18
vendored
Normal file
@ -0,0 +1 @@
|
||||
(SELECT "model"."id", "model"."string", "model"."bytes" FROM "models" AS "model" WHERE (1)) UNION ALL (SELECT "model"."id", "model"."string", "model"."bytes" FROM "models" AS "model")
|
Loading…
x
Reference in New Issue
Block a user