Skip to content

Commit 950722a

Browse files
committed
Implement column name extractor
Update Core to v1.9
1 parent d58c7ee commit 950722a

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

StaTypPocoQueries.PetaPoco.Tests/QueryTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ private class MyClass
2828
{
2929
public int ID { get; set; }
3030
public string Name { get; set; }
31+
[Column("RealColumnName")]
32+
public string PropWithAttribute { get; set; }
3133
}
3234

3335
private Mock<IDatabase> _mockDb;
@@ -57,5 +59,12 @@ public void Delete(string name)
5759
_mockDb.Object.Delete<MyClass>(c => c.Name == name);
5860
_lastSql.Should().BeEquivalentTo(new Sql("WHERE <Name> = @0", name));
5961
}
62+
63+
[Theory, AutoData]
64+
public void Query_Should_Use_Column_Attribute(string value)
65+
{
66+
_mockDb.Object.Query<MyClass>(c => c.PropWithAttribute == value);
67+
_lastSql.Should().BeEquivalentTo(new Sql("WHERE <RealColumnName> = @0", value));
68+
}
6069
}
6170
}

StaTypPocoQueries.PetaPoco/DatabaseExtensions.FSharp.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using System;
1818
using System.Collections.Generic;
19+
using System.Reflection;
1920
using System.Threading;
2021
using System.Threading.Tasks;
2122
using Microsoft.FSharp.Core;
@@ -27,9 +28,12 @@ namespace PetaPoco
2728
{
2829
public static partial class DatabaseExtensions
2930
{
31+
private static readonly FSharpFunc<MemberInfo, string> FsExtractColumnName
32+
= ExpressionToSql.AsFsFunc<MemberInfo, string>(ExtractColumnName);
33+
3034
private static Sql ToSql<T>(this FSharpExpr<FSharpFunc<T, bool>> query, IDatabase db)
3135
{
32-
var translated = ExpressionToSql.Translate(new DatabaseQuoter(db), query);
36+
var translated = ExpressionToSql.Translate(new DatabaseQuoter(db), query, true, FsExtractColumnName);
3337
return new Sql(translated.Item1, translated.Item2);
3438
}
3539

StaTypPocoQueries.PetaPoco/DatabaseExtensions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System;
1818
using System.Collections.Generic;
1919
using System.Linq.Expressions;
20+
using System.Reflection;
2021
using System.Threading;
2122
using System.Threading.Tasks;
2223
using StaTypPocoQueries.Core;
@@ -26,9 +27,14 @@ namespace PetaPoco
2627
{
2728
public static partial class DatabaseExtensions
2829
{
30+
private static string ExtractColumnName(MemberInfo mi)
31+
{
32+
return mi.GetCustomAttribute<ColumnAttribute>()?.Name ?? mi.Name;
33+
}
34+
2935
private static Sql ToSql<T>(this Expression<Func<T, bool>> query, IDatabase db)
3036
{
31-
var translated = ExpressionToSql.Translate(new DatabaseQuoter(db), query);
37+
var translated = ExpressionToSql.Translate(new DatabaseQuoter(db), query, true, ExtractColumnName);
3238
return new Sql(translated.Item1, translated.Item2);
3339
}
3440

StaTypPocoQueries.PetaPoco/StaTypPocoQueries.PetaPoco.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<RepositoryType>git</RepositoryType>
1414
<PackageIconUrl>https://raw.githubusercontent.com/asherber/StaTypPocoQueries.PetaPoco/master/media/static-64.png</PackageIconUrl>
1515
<PackageTags>petapoco statyppocoqueries statically typed</PackageTags>
16-
<PackageReleaseNotes>Extension methods now use IDatabase instead of Database</PackageReleaseNotes>
16+
<PackageReleaseNotes>Methods will now correctly read Column attributes on properties when building SQL</PackageReleaseNotes>
1717
</PropertyGroup>
1818

1919
<PropertyGroup>
@@ -24,6 +24,6 @@
2424

2525
<ItemGroup>
2626
<PackageReference Include="PetaPoco.Compiled" Version="[6.0.394,7.0)" />
27-
<PackageReference Include="StaTypPocoQueries.Core" Version="[1.7,2)" />
27+
<PackageReference Include="StaTypPocoQueries.Core" Version="[1.9.0,2)" />
2828
</ItemGroup>
2929
</Project>

0 commit comments

Comments
 (0)