Skip to content

Commit

Permalink
Merge pull request #41 from yv989c/feature/ef8-support
Browse files Browse the repository at this point in the history
Feature/ef8 support
  • Loading branch information
yv989c authored Nov 25, 2023
2 parents 1e3d4b1 + f94f207 commit f4a8d27
Show file tree
Hide file tree
Showing 15 changed files with 201 additions and 20 deletions.
20 changes: 20 additions & 0 deletions BlazarTech.QueryableValues.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QueryableValues.SqlServer.T
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QueryableValues.SqlServer.Benchmarks", "benchmarks\QueryableValues.SqlServer.Benchmarks\QueryableValues.SqlServer.Benchmarks.csproj", "{99FE31A0-BC7E-412C-82E2-DA19E8B68613}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QueryableValues.SqlServer.EFCore8", "src\QueryableValues.SqlServer.EFCore8\QueryableValues.SqlServer.EFCore8.csproj", "{03A17C43-5E66-4A8F-B650-7F611E73BD0B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QueryableValues.SqlServer.Tests.EFCore8", "tests\QueryableValues.SqlServer.Tests.EFCore8\QueryableValues.SqlServer.Tests.EFCore8.csproj", "{9387545B-CABC-4C63-A163-4F64C65A370F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -118,6 +122,22 @@ Global
{99FE31A0-BC7E-412C-82E2-DA19E8B68613}.Test_All|Any CPU.ActiveCfg = Release|Any CPU
{99FE31A0-BC7E-412C-82E2-DA19E8B68613}.Test_All|Any CPU.Build.0 = Release|Any CPU
{99FE31A0-BC7E-412C-82E2-DA19E8B68613}.Test|Any CPU.ActiveCfg = Release|Any CPU
{03A17C43-5E66-4A8F-B650-7F611E73BD0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{03A17C43-5E66-4A8F-B650-7F611E73BD0B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{03A17C43-5E66-4A8F-B650-7F611E73BD0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{03A17C43-5E66-4A8F-B650-7F611E73BD0B}.Release|Any CPU.Build.0 = Release|Any CPU
{03A17C43-5E66-4A8F-B650-7F611E73BD0B}.Test_All|Any CPU.ActiveCfg = Test|Any CPU
{03A17C43-5E66-4A8F-B650-7F611E73BD0B}.Test_All|Any CPU.Build.0 = Test|Any CPU
{03A17C43-5E66-4A8F-B650-7F611E73BD0B}.Test|Any CPU.ActiveCfg = Test|Any CPU
{03A17C43-5E66-4A8F-B650-7F611E73BD0B}.Test|Any CPU.Build.0 = Test|Any CPU
{9387545B-CABC-4C63-A163-4F64C65A370F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9387545B-CABC-4C63-A163-4F64C65A370F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9387545B-CABC-4C63-A163-4F64C65A370F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9387545B-CABC-4C63-A163-4F64C65A370F}.Release|Any CPU.Build.0 = Release|Any CPU
{9387545B-CABC-4C63-A163-4F64C65A370F}.Test_All|Any CPU.ActiveCfg = Test_All|Any CPU
{9387545B-CABC-4C63-A163-4F64C65A370F}.Test_All|Any CPU.Build.0 = Test_All|Any CPU
{9387545B-CABC-4C63-A163-4F64C65A370F}.Test|Any CPU.ActiveCfg = Test|Any CPU
{9387545B-CABC-4C63-A163-4F64C65A370F}.Test|Any CPU.Build.0 = Test|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
9 changes: 5 additions & 4 deletions Version.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project>
<PropertyGroup>
<VersionEFCore3>3.9.0</VersionEFCore3>
<VersionEFCore5>5.9.0</VersionEFCore5>
<VersionEFCore6>6.9.0</VersionEFCore6>
<VersionEFCore7>7.4.0</VersionEFCore7>
<VersionEFCore3>3.9.1</VersionEFCore3>
<VersionEFCore5>5.9.1</VersionEFCore5>
<VersionEFCore6>6.9.1</VersionEFCore6>
<VersionEFCore7>7.4.1</VersionEFCore7>
<VersionEFCore8>8.0.0</VersionEFCore8>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../SharedProjectProperties.xml" />
<Import Project="../../Version.xml" />

<PropertyGroup>
<VersionPrefix>$(VersionEFCore8)</VersionPrefix>
<TargetFramework>net8.0</TargetFramework>
<Configurations>Debug;Release;Test</Configurations>
<DefineConstants>$(DefineConstants);EFCORE;EFCORE8</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="[8.0,)" />
</ItemGroup>
</Project>
42 changes: 42 additions & 0 deletions src/QueryableValues.SqlServer/QueryableValuesEntity.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace BlazarTech.QueryableValues
{
internal class QueryableValuesEntity
{
private static readonly IReadOnlyList<string> DataPropertyNames;

public const string IndexPropertyName = nameof(X);

static QueryableValuesEntity()
{
DataPropertyNames = GetDataPropertyNames();
}

public int X { get; set; }

public int? I { get; set; }
Expand Down Expand Up @@ -150,5 +159,38 @@ internal class QueryableValuesEntity
public char? C7 { get; set; }
public char? C8 { get; set; }
public char? C9 { get; set; }

private static List<string> GetDataPropertyNames()
{
var properties = typeof(QueryableValuesEntity).GetProperties();
var result = new List<string>(properties.Length);

foreach (var property in properties)
{
if (property.Name is IndexPropertyName)
{
continue;
}

result.Add(property.Name);
}

return result;
}

internal static IEnumerable<string> GetUnmappedPropertyNames(IReadOnlyList<EntityPropertyMapping> mappings)
{
var targetProperties = new HashSet<string>(mappings.Select(i => i.Target.Name));

foreach (var propertyName in DataPropertyNames)
{
if (targetProperties.Contains(propertyName))
{
continue;
}

yield return propertyName;
}
}
}
}
12 changes: 8 additions & 4 deletions src/QueryableValues.SqlServer/SqlServer/JsonQueryableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,21 @@ protected override string GetSqlForComplexTypes(IEntityOptionsBuilder entityOpti

sb.Append(" [").Append(QueryableValuesEntity.IndexPropertyName).Append(']');

for (var i = 0; i < mappings.Count; i++)
foreach (var mapping in mappings)
{
sb.Append(", [").Append(mappings[i].Target.Name).Append(']');
sb.Append(", [").Append(mapping.Target.Name).Append(']');
}

foreach (var unmappedPropertyName in QueryableValuesEntity.GetUnmappedPropertyNames(mappings))
{
sb.Append(", NULL[").Append(unmappedPropertyName).Append(']');
}

sb.AppendLine();
sb.Append("FROM OPENJSON({0}) WITH ([").Append(QueryableValuesEntity.IndexPropertyName).Append("] int");

for (var i = 0; i < mappings.Count; i++)
foreach (var mapping in mappings)
{
var mapping = mappings[i];
var propertyOptions = entityOptions.GetPropertyOptions(mapping.Source);

sb.Append(", ");
Expand Down
29 changes: 25 additions & 4 deletions src/QueryableValues.SqlServer/SqlServer/XmlQueryableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ protected override string GetSqlForComplexTypes(IEntityOptionsBuilder entityOpti
sb
.Append("\tI.value('@")
.Append(QueryableValuesEntity.IndexPropertyName)
.Append(" cast as xs:integer?', 'int') AS [")
.Append(" cast as xs:integer?', 'int') [")
.Append(QueryableValuesEntity.IndexPropertyName)
.Append(']');

for (int i = 0; i < mappings.Count; i++)
foreach (var mapping in mappings)
{
var mapping = mappings[i];
var propertyOptions = entityOptions.GetPropertyOptions(mapping.Source);

sb.Append(',').AppendLine();
Expand Down Expand Up @@ -118,9 +117,11 @@ protected override string GetSqlForComplexTypes(IEntityOptionsBuilder entityOpti
throw new NotImplementedException(mapping.TypeName.ToString());
}

sb.Append(") AS [").Append(targetName).Append(']');
sb.Append(") [").Append(targetName).Append(']');
}

AppendUnmappedProperties(sb, mappings);

sb.AppendLine();
sb.Append("FROM {0}.nodes('/R[1]/V') N(I)").AppendLine();
sb.Append("ORDER BY [").Append(QueryableValuesEntity.IndexPropertyName).Append(']');
Expand All @@ -131,6 +132,26 @@ protected override string GetSqlForComplexTypes(IEntityOptionsBuilder entityOpti
{
StringBuilderPool.Return(sb);
}

static void AppendUnmappedProperties(System.Text.StringBuilder sb, IReadOnlyList< EntityPropertyMapping> mappings)
{
var hasUnmappedProperty = false;

foreach (var unmappedPropertyName in QueryableValuesEntity.GetUnmappedPropertyNames(mappings))
{
if (hasUnmappedProperty)
{
sb.Append(',');
}
else
{
hasUnmappedProperty = true;
sb.Append(',').AppendLine().Append('\t');
}

sb.Append("NULL[").Append(unmappedPropertyName).Append(']');
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/SharedProjectProperties.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@
<InternalsVisibleTo Include="BlazarTech.QueryableValues.SqlServer.Tests.EFCore5" />
<InternalsVisibleTo Include="BlazarTech.QueryableValues.SqlServer.Tests.EFCore6" />
<InternalsVisibleTo Include="BlazarTech.QueryableValues.SqlServer.Tests.EFCore7" />
<InternalsVisibleTo Include="BlazarTech.QueryableValues.SqlServer.Tests.EFCore8" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Import Project="../SharedTestProjectProperties.xml" />

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
<TargetFrameworks>net8.0</TargetFrameworks>
<AssemblyName>BlazarTech.QueryableValues.SqlServer.Tests.EFCore3</AssemblyName>
<DefineConstants>$(DefineConstants);TESTS;EFCORE3</DefineConstants>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Import Project="../SharedTestProjectProperties.xml" />

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
<TargetFrameworks>net8.0</TargetFrameworks>
<AssemblyName>BlazarTech.QueryableValues.SqlServer.Tests.EFCore5</AssemblyName>
<DefineConstants>$(DefineConstants);TESTS;EFCORE5</DefineConstants>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Import Project="../SharedTestProjectProperties.xml" />

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks>net8.0</TargetFrameworks>
<AssemblyName>BlazarTech.QueryableValues.SqlServer.Tests.EFCore6</AssemblyName>
<DefineConstants>$(DefineConstants);TESTS;EFCORE6</DefineConstants>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Import Project="../SharedTestProjectProperties.xml" />

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks>net8.0</TargetFrameworks>
<AssemblyName>BlazarTech.QueryableValues.SqlServer.Tests.EFCore7</AssemblyName>
<DefineConstants>$(DefineConstants);TESTS;EFCORE7</DefineConstants>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../SharedTestProjectProperties.xml" />

<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<AssemblyName>BlazarTech.QueryableValues.SqlServer.Tests.EFCore8</AssemblyName>
<DefineConstants>$(DefineConstants);TESTS;EFCORE8</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\QueryableValues.SqlServer.EFCore8\QueryableValues.SqlServer.EFCore8.csproj" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ orderby td.Id
Assert.Equal(2, actual[0].ChildEntity.Count);

Assert.Equal(3, actual[1].Id);
Assert.Equal(1, actual[1].ChildEntity.Count);
Assert.Single(actual[1].ChildEntity);
}

[Fact]
Expand All @@ -920,7 +920,7 @@ orderby td.Id
Assert.Single(actual);

Assert.Equal(3, actual[0].Id);
Assert.Equal(1, actual[0].ChildEntity.Count);
Assert.Single(actual[0].ChildEntity);
}

[Fact]
Expand All @@ -945,6 +945,33 @@ orderby td.Id
Assert.Equal(1, actual[0].Id);
Assert.Equal(2, actual[0].ChildEntity.Count);
}

[Fact]
public async Task JoinWithProjection()
{
var values = new[]
{
new { Id = 1, Value = Guid.Empty },
new { Id = 3, Value = Guid.Parse("f6379213-750f-42df-91b9-73756f28c4b6") }
};

var expected = new[] { 1, 3 };

var query =
from td in _db.TestData
join v in _db.AsQueryableValues(values) on new { td.Id, Value = td.GuidValue } equals new { v.Id, v.Value }
select td.Id;

var query2 =
from td in _db.TestData
join v in query on td.Id equals v
orderby td.Id
select td.Id;

var actual = await query2.ToListAsync();

Assert.Equal(expected, actual);
}
}

[Collection("DbContext")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ internal static class DatabaseName
public const string Name = "QueryableValuesTestsEFCore6";
#elif EFCORE7
public const string Name = "QueryableValuesTestsEFCore7";
#elif EFCORE8
public const string Name = "QueryableValuesTestsEFCore8";
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,27 @@ public async Task MustMatchSequenceOfGuid()
Assert.Equal(expected, actual);
}

[Fact]
public async Task JoinWithProjection()
{
var values = new[] { 1, 3 };

var query =
from td in _db.TestData
join v in _db.AsQueryableValues(values) on td.Id equals v
select td.Id;

var query2 =
from td in _db.TestData
join v in query on td.Id equals v
orderby td.Id
select td.Id;

var actual = await query2.ToListAsync();

Assert.Equal(values, actual);
}

enum ByteEnum : byte
{
None,
Expand Down Expand Up @@ -936,7 +957,7 @@ orderby td.Id
Assert.Equal(2, actual[0].ChildEntity.Count);

Assert.Equal(3, actual[1].Id);
Assert.Equal(1, actual[1].ChildEntity.Count);
Assert.Single(actual[1].ChildEntity);
}

[Fact]
Expand All @@ -959,7 +980,7 @@ orderby td.Id
Assert.Single(actual);

Assert.Equal(3, actual[0].Id);
Assert.Equal(1, actual[0].ChildEntity.Count);
Assert.Single(actual[0].ChildEntity);
}

[Fact]
Expand Down

0 comments on commit f4a8d27

Please sign in to comment.