-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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.