Skip to content

Commit

Permalink
Implement precompiled queries
Browse files Browse the repository at this point in the history
Closes #31331
  • Loading branch information
roji committed Apr 7, 2024
1 parent 79eef42 commit 53025c3
Show file tree
Hide file tree
Showing 73 changed files with 8,396 additions and 376 deletions.
5 changes: 4 additions & 1 deletion EFCore.sln.DotSettings
Expand Up @@ -301,9 +301,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 @@ -325,7 +325,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 @@ -19,8 +19,9 @@ public class CosmosQueryCompilationContext : QueryCompilationContext
/// </summary>
public CosmosQueryCompilationContext(
QueryCompilationContextDependencies dependencies,
bool async)
: base(dependencies, async)
bool async,
bool precompiling)
: base(dependencies, async, precompiling)
{
}

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
25 changes: 21 additions & 4 deletions src/EFCore.Design/Design/Internal/CSharpHelper.cs
Expand Up @@ -1556,6 +1556,7 @@ private string ToSourceCode(SyntaxNode node)
public virtual string Statement(
Expression node,
ISet<string> collectedNamespaces,
ISet<string> unsafeAccessors,
IReadOnlyDictionary<object, string>? constantReplacements,
IReadOnlyDictionary<MemberAccess, string>? memberAccessReplacements)
{
Expand All @@ -1581,11 +1582,18 @@ private string ToSourceCode(SyntaxNode node)
}
}

return ToSourceCode(_translator.TranslateStatement(
var unsafeAccessorDeclarations = new HashSet<MethodDeclarationSyntax>();
var code = ToSourceCode(_translator.TranslateStatement(
node,
constantReplacementExpressions,
memberAccessReplacementExpressions,
collectedNamespaces));
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>
Expand All @@ -1597,6 +1605,7 @@ private string ToSourceCode(SyntaxNode node)
public virtual string Expression(
Expression node,
ISet<string> collectedNamespaces,
ISet<string> unsafeAccessors,
IReadOnlyDictionary<object, string>? constantReplacements,
IReadOnlyDictionary<MemberAccess, string>? memberAccessReplacements)
{
Expand All @@ -1622,11 +1631,19 @@ private string ToSourceCode(SyntaxNode node)
}
}

return ToSourceCode(_translator.TranslateExpression(
var unsafeAccessorDeclarations = new HashSet<MethodDeclarationSyntax>();

var code = ToSourceCode(_translator.TranslateExpression(
node,
constantReplacementExpressions,
memberAccessReplacementExpressions,
collectedNamespaces));
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
12 changes: 12 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.

6 changes: 6 additions & 0 deletions src/EFCore.Design/Properties/DesignStrings.resx
Expand Up @@ -192,6 +192,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 +362,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 unsupported 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 53025c3

Please sign in to comment.