Skip to content

Commit

Permalink
Query: Throw better exception message for non-composable SQL in FromSql
Browse files Browse the repository at this point in the history
Also fix issues tags
- Tags are erased when SelectExpression is recreated during visitation
- Tags are not printed for non-composed FromSql*

Resolves #17558
  • Loading branch information
smitpatel committed Oct 17, 2019
1 parent afd6ca6 commit bbf01ee
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 232 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 31 additions & 28 deletions src/EFCore.Relational/Properties/RelationalStrings.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -494,4 +494,7 @@
<data name="SetOperationNotWithinEntityTypeHierarchy" xml:space="preserve">
<value>Set operations (Union, Concat, Intersect, Except) are only supported over entity types within the same type hierarchy.</value>
</data>
</root>
<data name="FromSqlNonComposable" xml:space="preserve">
<value>FromSqlRaw or FromSqlInterpolated was called with non-composable SQL and with a query composing over it. Consider calling `AsEnumerable` after the FromSqlRaw or FromSqlInterpolated method to perform the composition on the client side.</value>
</data>
</root>
13 changes: 11 additions & 2 deletions src/EFCore.Relational/Query/QuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.Internal;
Expand All @@ -13,6 +15,8 @@ namespace Microsoft.EntityFrameworkCore.Query
{
public class QuerySqlGenerator : SqlExpressionVisitor
{
private static readonly Regex _composibleSql
= new Regex(@"^\s*?SELECT\b", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(value: 1000.0));
private readonly IRelationalCommandBuilderFactory _relationalCommandBuilderFactory;
private readonly ISqlGenerationHelper _sqlGenerationHelper;
private IRelationalCommandBuilder _relationalCommandBuilder;
Expand Down Expand Up @@ -50,14 +54,14 @@ public virtual IRelationalCommand GetCommand(SelectExpression selectExpression)
{
_relationalCommandBuilder = _relationalCommandBuilderFactory.Create();

GenerateTagsHeaderComment(selectExpression);

if (selectExpression.IsNonComposedFromSql())
{
GenerateFromSql((FromSqlExpression)selectExpression.Tables[0]);
}
else
{
GenerateTagsHeaderComment(selectExpression);

VisitSelect(selectExpression);
}

Expand Down Expand Up @@ -312,6 +316,11 @@ protected override Expression VisitFromSql(FromSqlExpression fromSqlExpression)
{
_relationalCommandBuilder.AppendLine("(");

if (!_composibleSql.IsMatch(fromSqlExpression.Sql))
{
throw new InvalidOperationException(RelationalStrings.FromSqlNonComposable);
}

using (_relationalCommandBuilder.Indent())
{
GenerateFromSql(fromSqlExpression);
Expand Down
22 changes: 20 additions & 2 deletions src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,8 @@ protected override Expression VisitChildren(ExpressionVisitor visitor)
Having = havingExpression,
Offset = offset,
Limit = limit,
IsDistinct = IsDistinct
IsDistinct = IsDistinct,
Tags = Tags
};

newSelectExpression._identifier.AddRange(_identifier);
Expand Down Expand Up @@ -1564,6 +1565,11 @@ private bool Equals(SelectExpression selectExpression)
}
}

if (!Tags.SequenceEqual(selectExpression.Tags))
{
return false;
}

if (!_tables.SequenceEqual(selectExpression._tables))
{
return false;
Expand Down Expand Up @@ -1637,7 +1643,8 @@ public SelectExpression Update(
Having = havingExpression,
Offset = offset,
Limit = limit,
IsDistinct = distinct
IsDistinct = distinct,
Tags = Tags
};
}

Expand All @@ -1652,6 +1659,11 @@ public override int GetHashCode()
hash.Add(projectionMapping.Value);
}

foreach (var tag in Tags)
{
hash.Add(tag);
}

foreach (var table in _tables)
{
hash.Add(table);
Expand Down Expand Up @@ -1692,6 +1704,12 @@ public override void Print(ExpressionPrinter expressionPrinter)
}

expressionPrinter.AppendLine();

foreach (var tag in Tags)
{
expressionPrinter.Append($"-- {tag}");
}

IDisposable indent = null;

if (Alias != null)
Expand Down
Loading

0 comments on commit bbf01ee

Please sign in to comment.