Skip to content

Commit

Permalink
Implement precompiled queries (#33297)
Browse files Browse the repository at this point in the history
Closes #31331
  • Loading branch information
roji committed Apr 29, 2024
1 parent c46e41f commit ebbc5ee
Show file tree
Hide file tree
Showing 80 changed files with 8,856 additions and 648 deletions.
5 changes: 4 additions & 1 deletion EFCore.sln.DotSettings
Expand Up @@ -303,9 +303,9 @@ The .NET Foundation licenses this file to you under the MIT license.
<s:Boolean x:Key="/Default/UserDictionary/Words/=efcore/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=evaluatability/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=evaluatable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Evaluatables/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=evaluatables/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=fallbacks/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Formattable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=funcletization/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Funcletizer/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Includable/@EntryIndexedValue">True</s:Boolean>
Expand All @@ -327,7 +327,10 @@ The .NET Foundation licenses this file to you under the MIT license.&#xD;
<s:Boolean x:Key="/Default/UserDictionary/Words/=Poolable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Postgre/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=precompilation/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=precompiling/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=prunable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=precompilation/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=pubternal/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=pushdown/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=queryables/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=remapper/@EntryIndexedValue">True</s:Boolean>
Expand Down
Expand Up @@ -3,6 +3,7 @@

using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.EntityFrameworkCore.Migrations.Internal;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.Scaffolding.Internal;

namespace Microsoft.EntityFrameworkCore.Design;
Expand Down
44 changes: 34 additions & 10 deletions src/EFCore.Design/Design/Internal/CSharpHelper.cs
Expand Up @@ -1587,13 +1587,25 @@ private string ToSourceCode(SyntaxNode node)
public virtual string Statement(
Expression node,
ISet<string> collectedNamespaces,
ISet<string> unsafeAccessors,
IReadOnlyDictionary<object, string>? constantReplacements,
IReadOnlyDictionary<MemberInfo, QualifiedName>? memberAccessReplacements)
=> ToSourceCode(_translator.TranslateStatement(
node,
constantReplacements,
memberAccessReplacements,
collectedNamespaces));
{
var unsafeAccessorDeclarations = new HashSet<MethodDeclarationSyntax>();

var code = ToSourceCode(
_translator.TranslateStatement(
node,
constantReplacements,
memberAccessReplacements,
collectedNamespaces,
unsafeAccessorDeclarations));

// TODO: Possibly improve this (e.g. expose a single string that contains all the accessors concatenated?)
unsafeAccessors.UnionWith(unsafeAccessorDeclarations.Select(ToSourceCode));

return code;
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -1604,13 +1616,25 @@ private string ToSourceCode(SyntaxNode node)
public virtual string Expression(
Expression node,
ISet<string> collectedNamespaces,
ISet<string> unsafeAccessors,
IReadOnlyDictionary<object, string>? constantReplacements,
IReadOnlyDictionary<MemberInfo, QualifiedName>? memberAccessReplacements)
=> ToSourceCode(_translator.TranslateExpression(
node,
constantReplacements,
memberAccessReplacements,
collectedNamespaces));
{
var unsafeAccessorDeclarations = new HashSet<MethodDeclarationSyntax>();

var code = ToSourceCode(
_translator.TranslateExpression(
node,
constantReplacements,
memberAccessReplacements,
collectedNamespaces,
unsafeAccessorDeclarations));

// TODO: Possibly improve this (e.g. expose a single string that contains all the accessors concatenated?)
unsafeAccessors.UnionWith(unsafeAccessorDeclarations.Select(ToSourceCode));

return code;
}

private static bool IsIdentifierStartCharacter(char ch)
{
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Design/EFCore.Design.csproj
Expand Up @@ -58,6 +58,8 @@
<ItemGroup>
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion)" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="$(MicrosoftCodeAnalysisVersion)" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.5.5" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="$(MicrosoftExtensionsDependencyModelVersion)" />
<PackageReference Include="Microsoft.Extensions.HostFactoryResolver.Sources" PrivateAssets="All" Version="$(MicrosoftExtensionsHostFactoryResolverSourcesVersion)" />
<PackageReference Include="Mono.TextTemplating" Version="3.0.0-preview-0052-g5d0f76c785" />
Expand Down
18 changes: 18 additions & 0 deletions src/EFCore.Design/Properties/DesignStrings.Designer.cs

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

63 changes: 36 additions & 27 deletions src/EFCore.Design/Properties/DesignStrings.resx
@@ -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 @@ -150,6 +150,9 @@
<data name="CircularBaseClassDependency" xml:space="preserve">
<value>You cannot add a migration with the name 'Migration'.</value>
</data>
<data name="CompilationMustBeLoaded" xml:space="preserve">
<value>A compilation must be loaded.</value>
</data>
<data name="CompiledModelConstructorBinding" xml:space="preserve">
<value>The entity type '{entityType}' has a custom constructor binding. Compiled model can't be generated, because custom constructor bindings are not supported. Configure the custom constructor binding in '{customize}' in a partial '{className}' class instead.</value>
</data>
Expand Down Expand Up @@ -192,6 +195,9 @@
<data name="DuplicateMigrationName" xml:space="preserve">
<value>The name '{migrationName}' is used by an existing migration.</value>
</data>
<data name="DynamicQueryNotSupported" xml:space="preserve">
<value>Dynamic LINQ queries are not supported when precompiling queries.</value>
</data>
<data name="EncodingIgnored" xml:space="preserve">
<value>The encoding '{encoding}' specified in the output directive will be ignored. EF Core always scaffolds files using the encoding 'utf-8'.</value>
</data>
Expand Down Expand Up @@ -359,6 +365,9 @@ Change your target project to the migrations project by using the Package Manage
<data name="ProviderReturnedNullModel" xml:space="preserve">
<value>Metadata model returned should not be null. Provider: {providerTypeName}.</value>
</data>
<data name="QueryComprehensionSyntaxNotSupportedInPrecompiledQueries" xml:space="preserve">
<value>LINQ query comprehension syntax is currently not supported in precompiled queries.</value>
</data>
<data name="ReadOnlyFiles" xml:space="preserve">
<value>No files were generated in directory '{outputDirectoryName}'. The following file(s) already exist(s) and must be made writeable to continue: {readOnlyFiles}.</value>
</data>
Expand Down

0 comments on commit ebbc5ee

Please sign in to comment.