Skip to content

Commit

Permalink
Query: Improve detection of composible FromSql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
smitpatel committed Nov 29, 2016
1 parent 60ef281 commit 5daca1b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,25 @@ public virtual void From_sql_queryable_composed()
}
}

[Fact]
public virtual void From_sql_queryable_composed_after_removing_whitespaces()
{
using (var context = CreateContext())
{
var actual = context.Set<Customer>()
.FromSql(@"
SELECT
* FROM ""Customers""")
.Where(c => c.ContactName.Contains("z"))
.ToArray();

Assert.Equal(14, actual.Length);
}
}

[Fact]
public virtual void From_sql_queryable_composed_compiled()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,12 @@ var fromSqlAnnotation
tableAlias,
_querySource));

var trimmedSql = fromSqlAnnotation.Sql.TrimStart('\r', '\n', '\t', ' ');

var useQueryComposition
= fromSqlAnnotation.Sql
.TrimStart()
.StartsWith("SELECT ", StringComparison.OrdinalIgnoreCase);
= trimmedSql.StartsWith("SELECT ", StringComparison.OrdinalIgnoreCase)
|| trimmedSql.StartsWith("SELECT" + Environment.NewLine, StringComparison.OrdinalIgnoreCase)
|| trimmedSql.StartsWith("SELECT\t", StringComparison.OrdinalIgnoreCase);

var requiresClientEval = !useQueryComposition;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ WHERE CHARINDEX(N'z', [c].[ContactName]) > 0",
Sql);
}

public override void From_sql_queryable_composed_after_removing_whitespaces()
{
base.From_sql_queryable_composed_after_removing_whitespaces();

Assert.Equal(
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM (
SELECT
* FROM ""Customers""
) AS [c]
WHERE CHARINDEX(N'z', [c].[ContactName]) > 0",
Sql);
}

public override void From_sql_queryable_composed_compiled()
{
base.From_sql_queryable_composed_compiled();
Expand Down

0 comments on commit 5daca1b

Please sign in to comment.