Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions QueryBuilder.Tests/SelectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -903,5 +903,33 @@ public void SelectWithFilter()
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());
}

[Fact]
public void SelectWithExists()
{
var q = new Query("Posts").WhereExists(
new Query("Comments").WhereColumns("Comments.PostId", "=", "Posts.Id")
);

var sqlServer = Compilers.CompileFor(EngineCodes.SqlServer, q);
Assert.Equal("SELECT * FROM [Posts] WHERE EXISTS (SELECT 1 FROM [Comments] WHERE [Comments].[PostId] = [Posts].[Id])", sqlServer.ToString());
}

[Fact]
public void SelectWithExists_OmitSelectIsFalse()
{
var q = new Query("Posts").WhereExists(
new Query("Comments").Select("Id").WhereColumns("Comments.PostId", "=", "Posts.Id")
);


var compiler = new SqlServerCompiler
{
OmitSelectInsideExists = false,
};

var sqlServer = compiler.Compile(q).ToString();
Assert.Equal("SELECT * FROM [Posts] WHERE EXISTS (SELECT [Id] FROM [Comments] WHERE [Comments].[PostId] = [Posts].[Id])", sqlServer.ToString());
}

}
}
4 changes: 0 additions & 4 deletions QueryBuilder/Base.Where.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,6 @@ public Q WhereExists(Query query)
throw new ArgumentException($"'{nameof(FromClause)}' cannot be empty if used inside a '{nameof(WhereExists)}' condition");
}

// remove unneeded components
query = query.Clone().ClearComponent("select")
.SelectRaw("1");

return AddComponent("where", new ExistsCondition
{
Query = query,
Expand Down
11 changes: 10 additions & 1 deletion QueryBuilder/Compilers/Compiler.Conditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,16 @@ protected virtual string CompileExistsCondition(SqlResult ctx, ExistsCondition i
{
var op = item.IsNot ? "NOT EXISTS" : "EXISTS";

var subCtx = CompileSelectQuery(item.Query);

// remove unneeded components
var query = item.Query.Clone();

if (OmitSelectInsideExists)
{
query.ClearComponent("select").SelectRaw("1");
}

var subCtx = CompileSelectQuery(query);

ctx.Bindings.AddRange(subCtx.Bindings);

Expand Down
6 changes: 6 additions & 0 deletions QueryBuilder/Compilers/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ protected Compiler()
/// <value></value>
public virtual bool SupportsFilterClause { get; set; } = false;

/// <summary>
/// If true the compiler will remove the SELECT clause for the query used inside WHERE EXISTS
/// </summary>
/// <value></value>
public virtual bool OmitSelectInsideExists { get; set; } = true;

protected virtual string SingleRowDummyTableName { get => null; }

/// <summary>
Expand Down