Skip to content

Commit

Permalink
Upgraded JasperFx.CodeGeneration and hardened the compiled query code…
Browse files Browse the repository at this point in the history
… generation for various types of primitive arrays as parameter values. Closes GH-3610
  • Loading branch information
jeremydmiller committed Dec 26, 2024
1 parent 5c4b552 commit 53e0257
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
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

0 comments on commit 53e0257

Please sign in to comment.