Fix regression in db.Not introduced in v1.25.6. (#6844)
* Fix regression in db.Not introduced in 940358e. * Fix
This commit is contained in:
		
							parent
							
								
									f118e55db5
								
							
						
					
					
						commit
						3e2c4fc446
					
				| @ -166,6 +166,15 @@ type NotConditions struct { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (not NotConditions) Build(builder Builder) { | func (not NotConditions) Build(builder Builder) { | ||||||
|  | 	anyNegationBuilder := false | ||||||
|  | 	for _, c := range not.Exprs { | ||||||
|  | 		if _, ok := c.(NegationExpressionBuilder); ok { | ||||||
|  | 			anyNegationBuilder = true | ||||||
|  | 			break | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if anyNegationBuilder { | ||||||
| 		if len(not.Exprs) > 1 { | 		if len(not.Exprs) > 1 { | ||||||
| 			builder.WriteByte('(') | 			builder.WriteByte('(') | ||||||
| 		} | 		} | ||||||
| @ -198,4 +207,34 @@ func (not NotConditions) Build(builder Builder) { | |||||||
| 		if len(not.Exprs) > 1 { | 		if len(not.Exprs) > 1 { | ||||||
| 			builder.WriteByte(')') | 			builder.WriteByte(')') | ||||||
| 		} | 		} | ||||||
|  | 	} else { | ||||||
|  | 		builder.WriteString("NOT ") | ||||||
|  | 		if len(not.Exprs) > 1 { | ||||||
|  | 			builder.WriteByte('(') | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		for idx, c := range not.Exprs { | ||||||
|  | 			if idx > 0 { | ||||||
|  | 				builder.WriteString(AndWithSpace) | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
|  | 			e, wrapInParentheses := c.(Expr) | ||||||
|  | 			if wrapInParentheses { | ||||||
|  | 				sql := strings.ToUpper(e.SQL) | ||||||
|  | 				if wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace); wrapInParentheses { | ||||||
|  | 					builder.WriteByte('(') | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
|  | 			c.Build(builder) | ||||||
|  | 
 | ||||||
|  | 			if wrapInParentheses { | ||||||
|  | 				builder.WriteByte(')') | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		if len(not.Exprs) > 1 { | ||||||
|  | 			builder.WriteByte(')') | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  | |||||||
| @ -105,6 +105,14 @@ func TestWhere(t *testing.T) { | |||||||
| 			"SELECT * FROM `users` WHERE (`users`.`id` <> ? AND NOT `score` <= ?)", | 			"SELECT * FROM `users` WHERE (`users`.`id` <> ? AND NOT `score` <= ?)", | ||||||
| 			[]interface{}{"1", 100}, | 			[]interface{}{"1", 100}, | ||||||
| 		}, | 		}, | ||||||
|  | 		{ | ||||||
|  | 			[]clause.Interface{clause.Select{}, clause.From{}, clause.Where{ | ||||||
|  | 				Exprs: []clause.Expression{clause.Not(clause.Expr{SQL: "`score` <= ?", Vars: []interface{}{100}}, | ||||||
|  | 					clause.Expr{SQL: "`age` <= ?", Vars: []interface{}{60}})}, | ||||||
|  | 			}}, | ||||||
|  | 			"SELECT * FROM `users` WHERE NOT (`score` <= ? AND `age` <= ?)", | ||||||
|  | 			[]interface{}{100, 60}, | ||||||
|  | 		}, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	for idx, result := range results { | 	for idx, result := range results { | ||||||
|  | |||||||
| @ -554,6 +554,11 @@ func TestNot(t *testing.T) { | |||||||
| 	if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*users.*..*name.* <> .+ AND .*users.*..*age.* <> .+").MatchString(result.Statement.SQL.String()) { | 	if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*users.*..*name.* <> .+ AND .*users.*..*age.* <> .+").MatchString(result.Statement.SQL.String()) { | ||||||
| 		t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) | 		t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) | ||||||
| 	} | 	} | ||||||
|  | 
 | ||||||
|  | 	result = dryDB.Not(DB.Where("manager IS NULL").Where("age >= ?", 20)).Find(&User{}) | ||||||
|  | 	if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE NOT \\(manager IS NULL AND age >= .+\\) AND .users.\\..deleted_at. IS NULL").MatchString(result.Statement.SQL.String()) { | ||||||
|  | 		t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) | ||||||
|  | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestNotWithAllFields(t *testing.T) { | func TestNotWithAllFields(t *testing.T) { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 tsuba3
						tsuba3