From e00722b202554edd5a327c8a943fbd5d36a69414 Mon Sep 17 00:00:00 2001 From: whatwewant Date: Fri, 19 Apr 2024 11:52:32 +0800 Subject: [PATCH 1/2] feat: support where or --- where.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/where.go b/where.go index 719f17c..2d8a892 100644 --- a/where.go +++ b/where.go @@ -36,6 +36,8 @@ type Where struct { Items []WhereOne // FullTextSearchFields []string + // + OR []*Where } // SetWhereOptions is the options for SetWhere. @@ -191,6 +193,26 @@ func (w *Where) Build() (query string, args []interface{}, err error) { } whereClause := strings.Join(whereClauses, " AND ") + // apply or + if len(w.OR) >= 0 { + whereClauseOr := []string{ + whereClause, + } + whereValuesOr := whereValues + for _, wi := range w.OR { + wiWhereClause, wiWhereValues, err := wi.Build() + if err != nil { + return "", nil, err + } + + whereClauseOr = append(whereClauseOr, wiWhereClause) + whereValuesOr = append(whereValuesOr, wiWhereValues...) + } + + whereClause = strings.Join(whereClauseOr, " OR ") + whereValues = whereValuesOr + } + return whereClause, whereValues, nil } @@ -212,3 +234,9 @@ func (w *Where) Debug() { func (w *Where) Reset() { w.Items = []WhereOne{} } + +// Or creates or. +func (w *Where) Or(where *Where) *Where { + w.OR = append(w.OR, where) + return w +} From 2c6262c6fb0e7cb63641872819ddfd33339abb88 Mon Sep 17 00:00:00 2001 From: Zero Date: Sat, 20 Apr 2024 09:13:28 +0800 Subject: [PATCH 2/2] fix(where): OR should with bracket --- where.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/where.go b/where.go index 2d8a892..0865a98 100644 --- a/where.go +++ b/where.go @@ -196,7 +196,7 @@ func (w *Where) Build() (query string, args []interface{}, err error) { // apply or if len(w.OR) >= 0 { whereClauseOr := []string{ - whereClause, + fmt.Sprintf("(%s)", whereClause), } whereValuesOr := whereValues for _, wi := range w.OR { @@ -205,7 +205,7 @@ func (w *Where) Build() (query string, args []interface{}, err error) { return "", nil, err } - whereClauseOr = append(whereClauseOr, wiWhereClause) + whereClauseOr = append(whereClauseOr, fmt.Sprintf("(%s)", wiWhereClause)) whereValuesOr = append(whereValuesOr, wiWhereValues...) }