OrWhere() causing unexpected behavior in Where()
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 18.4k
- Forks
- 3.7k
- Avg merge
- 5h 8m
- Merged PRs (30d)
- 1
Description
A prior issue resulted in this fix.
I guess no one bothered to check the original test case. It is still not "fixed":
var sql = new SqlBuilder();
var tpl = sql.AddTemplate("select * from foo /**where**/");
sql.Where("MyField = @Param");
sql.OrWhere("MyField2 = @Param2");
@rafakwolf expected:
select * from foo WHERE ( MyField = @Param OR MyField2 = @Param2 )
However both the previous and fixed SqlBuilder produces the following:
select * from foo WHERE MyField = @Param AND ( MyField2 = @Param2 )
This is because only the first call to one of Where() or OrWhere() is the one that defines the joiner variable. That is, the variable that dictates what string to place between clauses. If Where() is called first the joiner will be "AND" but if OrWhere() is called first the joiner will be "OR".
Consider this: with the "fix" we get the following behavior (imagine these are in separate functions that don't know about each other):
sql.Where("a = @a");
sql.OrWhere("b = @b1");
sql.OrWhere("b = @b2");
result:
select * from foo WHERE a = @a AND (b = @b1 OR b = @b2)
But switch the order of the calls:
sql.OrWhere("b = @b1");
sql.OrWhere("b = @b2");
sql.Where("a = @a");
result:
select * from foo WHERE a = @a OR (b = @b1 OR b = @b2)
And that is a new bug introduced by the change. IMO under no circumstance should .Where() result in a clause getting OR'd into the query.
As a side comment, with the previous code it was at least consistent in that it always produced the first result irrespective of order. I believe the code change was made in haste and that the original behavior was correct, albeit confusing.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the SqlBuilder Where() and OrWhere() entry points, using the original test case and the linked fix for context. Reproduce the call-order examples from the issue and determine the intended grouping and joiners; done means the resulting SQL is consistent with the expected Where/OrWhere semantics and the regression is covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100