From 39a07dc82a40405740398a49f5addc80026c8c60 Mon Sep 17 00:00:00 2001 From: Jesper Noordsij Date: Wed, 19 Feb 2025 14:43:13 +0100 Subject: [PATCH 1/2] Fix and clarify OrWhere caveats in SqlBuilder docs --- Dapper.SqlBuilder/Readme.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Dapper.SqlBuilder/Readme.md b/Dapper.SqlBuilder/Readme.md index ea69d9b80..968b9a7a2 100644 --- a/Dapper.SqlBuilder/Readme.md +++ b/Dapper.SqlBuilder/Readme.md @@ -85,10 +85,13 @@ var count = conn.ExecuteScalar(countTemplate.RawSql, countTemplate.Paramete Limitations and caveats -------- -OrWhere use `and` not `or` to concat sql problem +### Combining the Where and OrWhere methods -[Issue 647](https://github.com/DapperLib/Dapper/issues/647) +The OrWhere method currently groups all `and` and `or` clauses by type, +which may result in possibly unexpected outcomes. +See also [issue 647](https://github.com/DapperLib/Dapper/issues/647). +For example, when providing the following clauses ```csharp sql.Where("a = @a1"); sql.OrWhere("b = @b1"); @@ -97,11 +100,11 @@ sql.OrWhere("b = @b2"); ``` SqlBuilder will generate sql -```sql= -a = @a1 AND b = @b1 AND a = @a2 AND b = @b2 +```sql +a = @a1 AND a = @a2 AND ( b = @b1 OR b = @b2 ) ``` -not +and not say ```sql a = @a1 OR b = @b1 AND a = @a2 OR b = @b2 ``` From caf92e8f58a9db05c3cdc8aae8c8d7b12707ecac Mon Sep 17 00:00:00 2001 From: Jesper Noordsij Date: Wed, 19 Feb 2025 15:01:19 +0100 Subject: [PATCH 2/2] Add example with OrWhere call first --- Dapper.SqlBuilder/Readme.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Dapper.SqlBuilder/Readme.md b/Dapper.SqlBuilder/Readme.md index 968b9a7a2..5a0968f9f 100644 --- a/Dapper.SqlBuilder/Readme.md +++ b/Dapper.SqlBuilder/Readme.md @@ -88,10 +88,13 @@ Limitations and caveats ### Combining the Where and OrWhere methods The OrWhere method currently groups all `and` and `or` clauses by type, -which may result in possibly unexpected outcomes. +then join the groups with `and` or `or` depending on the first call. +This may result in possibly unexpected outcomes. See also [issue 647](https://github.com/DapperLib/Dapper/issues/647). -For example, when providing the following clauses +#### Example Where first + +When providing the following clauses ```csharp sql.Where("a = @a1"); sql.OrWhere("b = @b1"); @@ -108,3 +111,18 @@ and not say ```sql a = @a1 OR b = @b1 AND a = @a2 OR b = @b2 ``` + +#### Example OrWhere first + +When providing the following clauses +```csharp +sql.OrWhere("b = @b1"); +sql.Where("a = @a1"); +sql.OrWhere("b = @b2"); +sql.Where("a = @a2"); +``` + +SqlBuilder will generate sql +```sql +a = @a1 OR a = @a2 OR ( b = @b1 OR b = @b2 ) +```