You've already forked go-clickhouse
mirror of
https://github.com/uptrace/go-clickhouse.git
synced 2025-06-27 00:21:13 +02:00
chore: better WhereGroup
This commit is contained in:
@ -358,37 +358,29 @@ func (q *baseQuery) appendSettings(fmter chschema.Formatter, b []byte) (_ []byte
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
type WhereQuery struct {
|
||||
type whereBaseQuery struct {
|
||||
baseQuery
|
||||
|
||||
where []chschema.QueryWithSep
|
||||
}
|
||||
|
||||
func (q *WhereQuery) addWhere(where chschema.QueryWithSep) {
|
||||
func (q *whereBaseQuery) addWhere(where chschema.QueryWithSep) {
|
||||
q.where = append(q.where, where)
|
||||
}
|
||||
|
||||
func (q *WhereQuery) WhereGroup(sep string, fn func(*WhereQuery)) {
|
||||
q.addWhereGroup(sep, fn)
|
||||
func (q *whereBaseQuery) addWhereGroup(sep string, where []chschema.QueryWithSep) {
|
||||
if len(where) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
func (q *WhereQuery) addWhereGroup(sep string, fn func(*WhereQuery)) {
|
||||
q2 := new(WhereQuery)
|
||||
fn(q2)
|
||||
q.addWhere(chschema.SafeQueryWithSep("", nil, sep))
|
||||
q.addWhere(chschema.SafeQueryWithSep("", nil, "("))
|
||||
|
||||
if len(q2.where) > 0 {
|
||||
q2.where[0].Sep = ""
|
||||
where[0].Sep = ""
|
||||
q.where = append(q.where, where...)
|
||||
|
||||
q.addWhere(chschema.SafeQueryWithSep("", nil, sep+"("))
|
||||
q.where = append(q.where, q2.where...)
|
||||
q.addWhere(chschema.SafeQueryWithSep("", nil, ")"))
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
type whereBaseQuery struct {
|
||||
baseQuery
|
||||
WhereQuery
|
||||
}
|
||||
|
||||
func (q *whereBaseQuery) mustAppendWhere(fmter chschema.Formatter, b []byte) ([]byte, error) {
|
||||
if len(q.where) == 0 {
|
||||
@ -421,7 +413,7 @@ func appendWhere(
|
||||
b = append(b, where.Sep...)
|
||||
}
|
||||
|
||||
if where.Query == "" && where.Args == nil {
|
||||
if where.Query == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -85,11 +85,6 @@ func (q *InsertQuery) WhereOr(query string, args ...any) *InsertQuery {
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *InsertQuery) WhereGroup(sep string, fn func(*WhereQuery)) *InsertQuery {
|
||||
q.addWhereGroup(sep, fn)
|
||||
return q
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
func (q *InsertQuery) Operation() string {
|
||||
|
@ -173,8 +173,17 @@ func (q *SelectQuery) WhereOr(query string, args ...any) *SelectQuery {
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *SelectQuery) WhereGroup(sep string, fn func(*WhereQuery)) *SelectQuery {
|
||||
q.addWhereGroup(sep, fn)
|
||||
func (q *SelectQuery) WhereGroup(sep string, fn func(*SelectQuery) *SelectQuery) *SelectQuery {
|
||||
saved := q.where
|
||||
q.where = nil
|
||||
|
||||
q = fn(q)
|
||||
|
||||
where := q.where
|
||||
q.where = saved
|
||||
|
||||
q.addWhereGroup(sep, where)
|
||||
|
||||
return q
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user