diff --git a/gplus/dao.go b/gplus/dao.go index e4e9c70..dfff50e 100644 --- a/gplus/dao.go +++ b/gplus/dao.go @@ -429,6 +429,10 @@ func buildSqlAndArgs[T any](expressions []any, sqlBuilder *strings.Builder, quer queryArgs = append(queryArgs, segment.value) } case *QueryCond[T]: + // 当子条件不存在查询表达式时,无需进行递归处理 + if len(segment.queryExpressions) == 0 { + continue + } sqlBuilder.WriteString(constants.LeftBracket + " ") // 递归处理条件 queryArgs = buildSqlAndArgs[T](segment.queryExpressions, sqlBuilder, queryArgs) diff --git a/gplus/query.go b/gplus/query.go index e520993..d40a2b8 100644 --- a/gplus/query.go +++ b/gplus/query.go @@ -266,27 +266,31 @@ func (q *QueryCond[T]) Having(having string, args ...any) *QueryCond[T] { // And 拼接 AND func (q *QueryCond[T]) And(fn ...func(q *QueryCond[T])) *QueryCond[T] { - q.addExpression(&sqlKeyword{keyword: constants.And}) if len(fn) > 0 { + // fix bug: https://github.com/acmestack/gorm-plus/issues/74 + q.addExpression(&sqlKeyword{keyword: constants.And}) nestQuery := &QueryCond[T]{} fn[0](nestQuery) q.queryExpressions = append(q.queryExpressions, nestQuery) q.last = nestQuery return q } + q.addExpression(&sqlKeyword{keyword: constants.And}) return q } // Or 拼接 OR func (q *QueryCond[T]) Or(fn ...func(q *QueryCond[T])) *QueryCond[T] { - q.addExpression(&sqlKeyword{keyword: constants.Or}) if len(fn) > 0 { + // fix bug: https://github.com/acmestack/gorm-plus/issues/74 + q.addExpression(&sqlKeyword{keyword: constants.Or}) nestQuery := &QueryCond[T]{} fn[0](nestQuery) q.queryExpressions = append(q.queryExpressions, nestQuery) q.last = nestQuery return q } + q.addExpression(&sqlKeyword{keyword: constants.Or}) return q }