Skip to content

Commit 98cb5f9

Browse files
fix nested where bug
1 parent c517b84 commit 98cb5f9

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

QueryBuilder.Tests/QueryBuilderTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,5 +336,15 @@ public void CombineRawWithPlaceholders()
336336
Assert.Equal("(SELECT * FROM [Mobiles]) UNION ALL SELECT * FROM [Devices]", c[0]);
337337
Assert.Equal("(SELECT * FROM `Mobiles`) UNION ALL SELECT * FROM `Devices`", c[1]);
338338
}
339+
340+
[Fact]
341+
public void NestedEmptyWhere()
342+
{
343+
var query = new Query("A").Where(q => new Query().Where(q2 => new Query().Where(q3 => new Query())));
344+
345+
var c = Compile(query);
346+
347+
Assert.Equal("SELECT * FROM [A]", c[0]);
348+
}
339349
}
340350
}

QueryBuilder/Compilers/Compiler.Conditions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ protected virtual string CompileNestedCondition<Q>(NestedCondition<Q> x) where Q
115115
var sql = CompileConditions(x.Query.GetComponents<AbstractCondition>("where", EngineCode));
116116
var op = x.IsNot ? "NOT " : "";
117117

118+
if (string.IsNullOrEmpty(sql))
119+
{
120+
return "";
121+
}
122+
118123
return $"{op}({sql})";
119124
}
120125

QueryBuilder/Compilers/Compiler.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,12 @@ protected virtual string CompileWheres(Query query)
484484
}
485485

486486
var conditions = query.GetComponents<AbstractCondition>("where", EngineCode);
487-
var sql = CompileConditions(conditions);
487+
var sql = CompileConditions(conditions).Trim();
488+
489+
if (string.IsNullOrEmpty(sql))
490+
{
491+
return null;
492+
}
488493

489494
return $"WHERE {sql}";
490495
}

0 commit comments

Comments
 (0)