1
0
mirror of https://github.com/uptrace/go-clickhouse.git synced 2025-06-29 00:21:46 +02:00

chore: better WhereGroup

This commit is contained in:
Vladimir Mihailenco
2022-11-01 15:22:10 +02:00
parent 92d13a878d
commit 79fddecbb5
3 changed files with 25 additions and 29 deletions

View File

@ -358,37 +358,29 @@ func (q *baseQuery) appendSettings(fmter chschema.Formatter, b []byte) (_ []byte
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type WhereQuery struct { type whereBaseQuery struct {
baseQuery
where []chschema.QueryWithSep where []chschema.QueryWithSep
} }
func (q *WhereQuery) addWhere(where chschema.QueryWithSep) { func (q *whereBaseQuery) addWhere(where chschema.QueryWithSep) {
q.where = append(q.where, where) q.where = append(q.where, where)
} }
func (q *WhereQuery) WhereGroup(sep string, fn func(*WhereQuery)) { func (q *whereBaseQuery) addWhereGroup(sep string, where []chschema.QueryWithSep) {
q.addWhereGroup(sep, fn) if len(where) == 0 {
return
} }
func (q *WhereQuery) addWhereGroup(sep string, fn func(*WhereQuery)) { q.addWhere(chschema.SafeQueryWithSep("", nil, sep))
q2 := new(WhereQuery) q.addWhere(chschema.SafeQueryWithSep("", nil, "("))
fn(q2)
if len(q2.where) > 0 { where[0].Sep = ""
q2.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, ")")) q.addWhere(chschema.SafeQueryWithSep("", nil, ")"))
} }
}
//------------------------------------------------------------------------------
type whereBaseQuery struct {
baseQuery
WhereQuery
}
func (q *whereBaseQuery) mustAppendWhere(fmter chschema.Formatter, b []byte) ([]byte, error) { func (q *whereBaseQuery) mustAppendWhere(fmter chschema.Formatter, b []byte) ([]byte, error) {
if len(q.where) == 0 { if len(q.where) == 0 {
@ -421,7 +413,7 @@ func appendWhere(
b = append(b, where.Sep...) b = append(b, where.Sep...)
} }
if where.Query == "" && where.Args == nil { if where.Query == "" {
continue continue
} }

View File

@ -85,11 +85,6 @@ func (q *InsertQuery) WhereOr(query string, args ...any) *InsertQuery {
return q return q
} }
func (q *InsertQuery) WhereGroup(sep string, fn func(*WhereQuery)) *InsertQuery {
q.addWhereGroup(sep, fn)
return q
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
func (q *InsertQuery) Operation() string { func (q *InsertQuery) Operation() string {

View File

@ -173,8 +173,17 @@ func (q *SelectQuery) WhereOr(query string, args ...any) *SelectQuery {
return q return q
} }
func (q *SelectQuery) WhereGroup(sep string, fn func(*WhereQuery)) *SelectQuery { func (q *SelectQuery) WhereGroup(sep string, fn func(*SelectQuery) *SelectQuery) *SelectQuery {
q.addWhereGroup(sep, fn) saved := q.where
q.where = nil
q = fn(q)
where := q.where
q.where = saved
q.addWhereGroup(sep, where)
return q return q
} }