Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgraded JasperFx.CodeGeneration and hardened the compiled query code… #3611

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Marten;
using Marten.Linq;
using Marten.Testing.Harness;
using Shouldly;
using Weasel.Core;

namespace LinqTests.Bugs;

public class Bug_3610_enum_array_as_string_in_compiled_query : BugIntegrationContext
{
[Fact]
public async Task use_the_query()
{
StoreOptions(opts =>
{
opts.UseNewtonsoftForSerialization(enumStorage: EnumStorage.AsString,
nonPublicMembersStorage: NonPublicMembersStorage.NonPublicSetters);
});

var elayne = new Foo { Name = "Elayne", Status = StatusEnum.Active };
theSession.Store(elayne);
await theSession.SaveChangesAsync();

var results = await theSession.QueryAsync(new ActiveFooQuery("Elayne"));

results.Any().ShouldBeTrue();
}
}

public class ActiveFooQuery : ICompiledListQuery<Foo>, IQueryPlanning
{
public string Name { get; set; }
public ActiveFooQuery()
{

}

public ActiveFooQuery(string name)
{
Name = name;
}

public Expression<Func<IMartenQueryable<Foo>, IEnumerable<Foo>>> QueryIs() =>
q => q.Where(x => x.Status.In(StatusEnum.Active) && x.Name == Name);

public void SetUniqueValuesForQueryPlanning()
{
Name = "ActiveFooQuery";
}
}

public class Foo
{
public Guid Id { get; set; }
public StatusEnum Status { get; set; }
public string Name { get; set; }
}

public enum StatusEnum
{
Active,
Inactive
}
19 changes: 18 additions & 1 deletion src/Marten/Internal/CompiledQueries/ParameterUsage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,25 @@ public void GenerateCode(GeneratedMethod method, string parametersVariableName,
else
{
method.Frames.Code($"{parametersVariableName}[{Index}].Value = {{0}};", Constant.For(Parameter.Value));
method.Frames.Code($"{parametersVariableName}[{Index}].{nameof(NpgsqlParameter.NpgsqlDbType)} = {typeof(NpgsqlDbType).FullNameInCode()}.{Parameter.NpgsqlDbType};");

method.Frames.Code($"{parametersVariableName}[{Index}].{nameof(NpgsqlParameter.NpgsqlDbType)} = {npgsqlDataTypeInCodeFor(Parameter)};");
}
}

// Hack for part of GH-3610
private static string npgsqlDataTypeInCodeFor(NpgsqlParameter parameter)
{
if (parameter.Value is string[])
{
return $"{typeof(NpgsqlDbType).FullNameInCode()}.{NpgsqlDbType.Array} | {typeof(NpgsqlDbType).FullNameInCode()}.{NpgsqlDbType.Varchar}";
}

if (parameter.Value is int[])
{
return $"{typeof(NpgsqlDbType).FullNameInCode()}.{NpgsqlDbType.Array} | {typeof(NpgsqlDbType).FullNameInCode()}.{NpgsqlDbType.Integer}";
}

return $"{typeof(NpgsqlDbType).FullNameInCode()}.{parameter.NpgsqlDbType}";
}

private void generateSimpleCode(GeneratedMethod method, MemberInfo member, Type memberType,
Expand Down
4 changes: 2 additions & 2 deletions src/Marten/Marten.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
<ItemGroup>
<PackageReference Include="FSharp.Core" Version="9.0.100" />
<PackageReference Include="JasperFx.Core" Version="1.9.0" />
<PackageReference Include="JasperFx.CodeGeneration" Version="3.7.1" />
<PackageReference Include="JasperFx.RuntimeCompiler" Version="3.7.1" />
<PackageReference Include="JasperFx.CodeGeneration" Version="3.7.2" />
<PackageReference Include="JasperFx.RuntimeCompiler" Version="3.7.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<!-- This is forced by Npgsql peer dependency -->

Expand Down
Loading