Skip to content

Commit e97a919

Browse files
authored
Fix and clarify OrWhere caveats in SqlBuilder docs (#2149)
* Fix and clarify OrWhere caveats in SqlBuilder docs * Add example with OrWhere call first
1 parent bd4f75b commit e97a919

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

Dapper.SqlBuilder/Readme.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,16 @@ var count = conn.ExecuteScalar<int>(countTemplate.RawSql, countTemplate.Paramete
8585
Limitations and caveats
8686
--------
8787

88-
OrWhere use `and` not `or` to concat sql problem
88+
### Combining the Where and OrWhere methods
8989

90-
[Issue 647](https://github.com/DapperLib/Dapper/issues/647)
90+
The OrWhere method currently groups all `and` and `or` clauses by type,
91+
then join the groups with `and` or `or` depending on the first call.
92+
This may result in possibly unexpected outcomes.
93+
See also [issue 647](https://github.com/DapperLib/Dapper/issues/647).
9194

95+
#### Example Where first
96+
97+
When providing the following clauses
9298
```csharp
9399
sql.Where("a = @a1");
94100
sql.OrWhere("b = @b1");
@@ -97,11 +103,26 @@ sql.OrWhere("b = @b2");
97103
```
98104

99105
SqlBuilder will generate sql
100-
```sql=
101-
a = @a1 AND b = @b1 AND a = @a2 AND b = @b2
106+
```sql
107+
a = @a1 AND a = @a2 AND ( b = @b1 OR b = @b2 )
102108
```
103109

104-
not
110+
and not say
105111
```sql
106112
a = @a1 OR b = @b1 AND a = @a2 OR b = @b2
107113
```
114+
115+
#### Example OrWhere first
116+
117+
When providing the following clauses
118+
```csharp
119+
sql.OrWhere("b = @b1");
120+
sql.Where("a = @a1");
121+
sql.OrWhere("b = @b2");
122+
sql.Where("a = @a2");
123+
```
124+
125+
SqlBuilder will generate sql
126+
```sql
127+
a = @a1 OR a = @a2 OR ( b = @b1 OR b = @b2 )
128+
```

0 commit comments

Comments
 (0)