fix: generate sql incorrect when use soft_delete and only one OR (#4969)
* fix: generate sql incorrect when use soft_delete and only one OR
This commit is contained in:
		
							parent
							
								
									2c3fc2db28
								
							
						
					
					
						commit
						8dde09e0be
					
				| @ -92,9 +92,14 @@ func (where Where) MergeClause(clause *Clause) { | |||||||
| func And(exprs ...Expression) Expression { | func And(exprs ...Expression) Expression { | ||||||
| 	if len(exprs) == 0 { | 	if len(exprs) == 0 { | ||||||
| 		return nil | 		return nil | ||||||
| 	} else if len(exprs) == 1 { |  | ||||||
| 		return exprs[0] |  | ||||||
| 	} | 	} | ||||||
|  | 
 | ||||||
|  | 	if len(exprs) == 1 { | ||||||
|  | 		if _, ok := exprs[0].(OrConditions); !ok { | ||||||
|  | 			return exprs[0] | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	return AndConditions{Exprs: exprs} | 	return AndConditions{Exprs: exprs} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -65,7 +65,7 @@ func (sd SoftDeleteQueryClause) MergeClause(*clause.Clause) { | |||||||
| func (sd SoftDeleteQueryClause) ModifyStatement(stmt *Statement) { | func (sd SoftDeleteQueryClause) ModifyStatement(stmt *Statement) { | ||||||
| 	if _, ok := stmt.Clauses["soft_delete_enabled"]; !ok && !stmt.Statement.Unscoped { | 	if _, ok := stmt.Clauses["soft_delete_enabled"]; !ok && !stmt.Statement.Unscoped { | ||||||
| 		if c, ok := stmt.Clauses["WHERE"]; ok { | 		if c, ok := stmt.Clauses["WHERE"]; ok { | ||||||
| 			if where, ok := c.Expression.(clause.Where); ok && len(where.Exprs) > 1 { | 			if where, ok := c.Expression.(clause.Where); ok && len(where.Exprs) >= 1 { | ||||||
| 				for _, expr := range where.Exprs { | 				for _, expr := range where.Exprs { | ||||||
| 					if orCond, ok := expr.(clause.OrConditions); ok && len(orCond.Exprs) == 1 { | 					if orCond, ok := expr.(clause.OrConditions); ok && len(orCond.Exprs) == 1 { | ||||||
| 						where.Exprs = []clause.Expression{clause.And(where.Exprs...)} | 						where.Exprs = []clause.Expression{clause.And(where.Exprs...)} | ||||||
|  | |||||||
| @ -83,3 +83,13 @@ func TestDeletedAtUnMarshal(t *testing.T) { | |||||||
| 		t.Errorf("Failed, result.DeletedAt: %v is not same as expected.DeletedAt: %v", result.DeletedAt, expected.DeletedAt) | 		t.Errorf("Failed, result.DeletedAt: %v is not same as expected.DeletedAt: %v", result.DeletedAt, expected.DeletedAt) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func TestDeletedAtOneOr(t *testing.T) { | ||||||
|  | 	actualSQL := DB.ToSQL(func(tx *gorm.DB) *gorm.DB { | ||||||
|  | 		return tx.Or("id = ?", 1).Find(&User{}) | ||||||
|  | 	}) | ||||||
|  | 
 | ||||||
|  | 	if !regexp.MustCompile(` WHERE id = 1 AND .users.\..deleted_at. IS NULL`).MatchString(actualSQL) { | ||||||
|  | 		t.Fatalf("invalid sql generated, got %v", actualSQL) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 kinggo
						kinggo