diff --git a/ch/query_base.go b/ch/query_base.go index f2ee4c7..f8918cb 100644 --- a/ch/query_base.go +++ b/ch/query_base.go @@ -358,36 +358,28 @@ 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 *WhereQuery) addWhereGroup(sep string, fn func(*WhereQuery)) { - q2 := new(WhereQuery) - fn(q2) - - if len(q2.where) > 0 { - q2.where[0].Sep = "" - - q.addWhere(chschema.SafeQueryWithSep("", nil, sep+"(")) - q.where = append(q.where, q2.where...) - q.addWhere(chschema.SafeQueryWithSep("", nil, ")")) +func (q *whereBaseQuery) addWhereGroup(sep string, where []chschema.QueryWithSep) { + if len(where) == 0 { + return } -} -//------------------------------------------------------------------------------ + q.addWhere(chschema.SafeQueryWithSep("", nil, sep)) + q.addWhere(chschema.SafeQueryWithSep("", nil, "(")) -type whereBaseQuery struct { - baseQuery - WhereQuery + where[0].Sep = "" + q.where = append(q.where, where...) + + q.addWhere(chschema.SafeQueryWithSep("", nil, ")")) } func (q *whereBaseQuery) mustAppendWhere(fmter chschema.Formatter, b []byte) ([]byte, error) { @@ -421,7 +413,7 @@ func appendWhere( b = append(b, where.Sep...) } - if where.Query == "" && where.Args == nil { + if where.Query == "" { continue } diff --git a/ch/query_insert.go b/ch/query_insert.go index e3aa805..88c19a5 100644 --- a/ch/query_insert.go +++ b/ch/query_insert.go @@ -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 { diff --git a/ch/query_select.go b/ch/query_select.go index 5e314d1..8c3dc89 100644 --- a/ch/query_select.go +++ b/ch/query_select.go @@ -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 }