Skip to content

Commit

Permalink
fix: fix Sub QueryCond query expression for build bug #74 (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhoenixL0911 authored Sep 15, 2023
1 parent 0d39664 commit 75724bf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions gplus/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions gplus/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 75724bf

Please sign in to comment.