Skip to content

Commit dad3415

Browse files
committed
Implement InvokeValueConverter
1 parent 950722a commit dad3415

File tree

7 files changed

+63
-9
lines changed

7 files changed

+63
-9
lines changed

StaTypPocoQueries.PetaPoco.Tests/QueryTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,25 @@ public class QueryTests
2727
private class MyClass
2828
{
2929
public int ID { get; set; }
30+
3031
public string Name { get; set; }
32+
3133
[Column("RealColumnName")]
3234
public string PropWithAttribute { get; set; }
35+
36+
public FoodEnum PlainFood { get; set; }
37+
38+
[FoodEnumConverter]
39+
public FoodEnum ConvertedFood { get; set; }
40+
}
41+
42+
public enum FoodEnum { Apple, Banana, Carrot };
43+
44+
private class FoodEnumConverter : ValueConverterAttribute
45+
{
46+
public override object ConvertFromDb(object value) => throw new NotImplementedException();
47+
48+
public override object ConvertToDb(object value) => value.ToString();
3349
}
3450

3551
private Mock<IDatabase> _mockDb;
@@ -66,5 +82,19 @@ public void Query_Should_Use_Column_Attribute(string value)
6682
_mockDb.Object.Query<MyClass>(c => c.PropWithAttribute == value);
6783
_lastSql.Should().BeEquivalentTo(new Sql("WHERE <RealColumnName> = @0", value));
6884
}
85+
86+
[Theory, AutoData]
87+
public void Query_Should_Use_Value_Converter(FoodEnum food)
88+
{
89+
_mockDb.Object.Query<MyClass>(c => c.ConvertedFood == food);
90+
_lastSql.Should().BeEquivalentTo(new Sql("WHERE <ConvertedFood> = @0", food.ToString()));
91+
}
92+
93+
[Theory, AutoData]
94+
public void Query_Should_Use_Plain_Value_With_No_Value_Converter(FoodEnum food)
95+
{
96+
_mockDb.Object.Query<MyClass>(c => c.PlainFood == food);
97+
_lastSql.Should().BeEquivalentTo(new Sql("WHERE <PlainFood> = @0", (int)food));
98+
}
6999
}
70100
}

StaTypPocoQueries.PetaPoco/DatabaseExtensions.FSharp.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,20 @@ public static partial class DatabaseExtensions
3131
private static readonly FSharpFunc<MemberInfo, string> FsExtractColumnName
3232
= ExpressionToSql.AsFsFunc<MemberInfo, string>(ExtractColumnName);
3333

34+
// Helpful precis: https://codeblog.jonskeet.uk/2012/01/30/currying-vs-partial-function-application/
35+
private static readonly FSharpFunc<PropertyInfo, FSharpFunc<object, object>> FsInvokeValueConverter
36+
= ExpressionToSql.AsFsFunc<PropertyInfo, FSharpFunc<object, object>>(
37+
pi => ExpressionToSql.AsFsFunc<object, object>(
38+
input => InvokeValueConverter(pi, input)
39+
)
40+
);
41+
3442
private static Sql ToSql<T>(this FSharpExpr<FSharpFunc<T, bool>> query, IDatabase db)
3543
{
36-
var translated = ExpressionToSql.Translate(new DatabaseQuoter(db), query, true, FsExtractColumnName);
44+
var translated = ExpressionToSql.Translate(new DatabaseQuoter(db), query,
45+
includeWhere: true,
46+
customNameExtractor: FsExtractColumnName,
47+
customParameterValueMap: FsInvokeValueConverter);
3748
return new Sql(translated.Item1, translated.Item2);
3849
}
3950

StaTypPocoQueries.PetaPoco/DatabaseExtensions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,20 @@ private static string ExtractColumnName(MemberInfo mi)
3131
{
3232
return mi.GetCustomAttribute<ColumnAttribute>()?.Name ?? mi.Name;
3333
}
34+
35+
private static object InvokeValueConverter(PropertyInfo pi, object input)
36+
{
37+
var converter = pi.GetCustomAttribute<ValueConverterAttribute>(true);
38+
39+
return converter == null ? input : converter.ConvertToDb(input);
40+
}
3441

3542
private static Sql ToSql<T>(this Expression<Func<T, bool>> query, IDatabase db)
3643
{
37-
var translated = ExpressionToSql.Translate(new DatabaseQuoter(db), query, true, ExtractColumnName);
44+
var translated = ExpressionToSql.Translate(new DatabaseQuoter(db), query,
45+
includeWhere: true,
46+
customNameExtractor: ExtractColumnName,
47+
customParameterValueMap: InvokeValueConverter);
3848
return new Sql(translated.Item1, translated.Item2);
3949
}
4050

StaTypPocoQueries.PetaPoco/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<ItemGroup>
44
<PackageReference Include="Nerdbank.GitVersioning">
5-
<Version>2.3.33-beta</Version>
5+
<Version>3.3.37</Version>
66
<PrivateAssets>all</PrivateAssets>
77
</PackageReference>
88
</ItemGroup>

StaTypPocoQueries.PetaPoco/StaTypPocoQueries.PetaPoco.csproj

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
32
<PropertyGroup>
43
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
54
<AssemblyName>StaTypPocoQueries.PetaPoco</AssemblyName>
@@ -15,15 +14,13 @@
1514
<PackageTags>petapoco statyppocoqueries statically typed</PackageTags>
1615
<PackageReleaseNotes>Methods will now correctly read Column attributes on properties when building SQL</PackageReleaseNotes>
1716
</PropertyGroup>
18-
1917
<PropertyGroup>
2018
<GenerateDocumentationFile>true</GenerateDocumentationFile>
2119
<DocumentationFile>$(AssemblyName).xml</DocumentationFile>
2220
<NoWarn>$(NoWarn);1591;1570;1587</NoWarn>
2321
</PropertyGroup>
24-
2522
<ItemGroup>
2623
<PackageReference Include="PetaPoco.Compiled" Version="[6.0.394,7.0)" />
27-
<PackageReference Include="StaTypPocoQueries.Core" Version="[1.9.0,2)" />
24+
<PackageReference Include="StaTypPocoQueries.Core" Version="[1.13.0,2)" />
2825
</ItemGroup>
29-
</Project>
26+
</Project>

StaTypPocoQueries.PetaPoco/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.3",
2+
"version": "1.4",
33
"publicReleaseRefSpec": [
44
"^refs/heads/master$",
55
"^refs/heads/v\\d+(?:\\.\\d+)?$"

switcher.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"solution": "StaTypPocoQueries.PetaPoco.sln",
3+
"mappings": {
4+
"StaTypPocoQueries.Core": "../statically-typed-poco-queries/StaTypPocoQueries.Core/StaTypPocoQueries.Core.fsproj"
5+
}
6+
}

0 commit comments

Comments
 (0)