Skip to content

Commit 449959a

Browse files
Merge pull request #618 from sqlkata/iss-581_where_exists_remove_select
Iss 581 where exists remove select
2 parents 31ee1e0 + c3822bd commit 449959a

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

QueryBuilder.Tests/SelectTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,5 +903,33 @@ public void SelectWithFilter()
903903
Assert.Equal("SELECT [Title], SUM(CASE WHEN [Published_Month] = 'Jan' THEN [ViewCount] END) AS [Published_Jan], SUM(CASE WHEN [Published_Month] = 'Feb' THEN [ViewCount] END) AS [Published_Feb] FROM [Posts]", sqlServer.ToString());
904904
}
905905

906+
[Fact]
907+
public void SelectWithExists()
908+
{
909+
var q = new Query("Posts").WhereExists(
910+
new Query("Comments").WhereColumns("Comments.PostId", "=", "Posts.Id")
911+
);
912+
913+
var sqlServer = Compilers.CompileFor(EngineCodes.SqlServer, q);
914+
Assert.Equal("SELECT * FROM [Posts] WHERE EXISTS (SELECT 1 FROM [Comments] WHERE [Comments].[PostId] = [Posts].[Id])", sqlServer.ToString());
915+
}
916+
917+
[Fact]
918+
public void SelectWithExists_OmitSelectIsFalse()
919+
{
920+
var q = new Query("Posts").WhereExists(
921+
new Query("Comments").Select("Id").WhereColumns("Comments.PostId", "=", "Posts.Id")
922+
);
923+
924+
925+
var compiler = new SqlServerCompiler
926+
{
927+
OmitSelectInsideExists = false,
928+
};
929+
930+
var sqlServer = compiler.Compile(q).ToString();
931+
Assert.Equal("SELECT * FROM [Posts] WHERE EXISTS (SELECT [Id] FROM [Comments] WHERE [Comments].[PostId] = [Posts].[Id])", sqlServer.ToString());
932+
}
933+
906934
}
907935
}

QueryBuilder/Base.Where.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,6 @@ public Q WhereExists(Query query)
539539
throw new ArgumentException($"'{nameof(FromClause)}' cannot be empty if used inside a '{nameof(WhereExists)}' condition");
540540
}
541541

542-
// remove unneeded components
543-
query = query.Clone().ClearComponent("select")
544-
.SelectRaw("1");
545-
546542
return AddComponent("where", new ExistsCondition
547543
{
548544
Query = query,

QueryBuilder/Compilers/Compiler.Conditions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,16 @@ protected virtual string CompileExistsCondition(SqlResult ctx, ExistsCondition i
247247
{
248248
var op = item.IsNot ? "NOT EXISTS" : "EXISTS";
249249

250-
var subCtx = CompileSelectQuery(item.Query);
250+
251+
// remove unneeded components
252+
var query = item.Query.Clone();
253+
254+
if (OmitSelectInsideExists)
255+
{
256+
query.ClearComponent("select").SelectRaw("1");
257+
}
258+
259+
var subCtx = CompileSelectQuery(query);
251260

252261
ctx.Bindings.AddRange(subCtx.Bindings);
253262

QueryBuilder/Compilers/Compiler.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ protected Compiler()
3030
/// <value></value>
3131
public virtual bool SupportsFilterClause { get; set; } = false;
3232

33+
/// <summary>
34+
/// If true the compiler will remove the SELECT clause for the query used inside WHERE EXISTS
35+
/// </summary>
36+
/// <value></value>
37+
public virtual bool OmitSelectInsideExists { get; set; } = true;
38+
3339
protected virtual string SingleRowDummyTableName { get => null; }
3440

3541
/// <summary>

0 commit comments

Comments
 (0)