Skip to content
Draft
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
14 changes: 7 additions & 7 deletions Dapper.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
version.json = version.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapper.Contrib", "Dapper.Contrib\Dapper.Contrib.csproj", "{4E409F8F-CFBB-4332-8B0A-FD5A283051FD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapper.Tests.Contrib", "tests\Dapper.Tests.Contrib\Dapper.Tests.Contrib.csproj", "{DAB3C5B7-BCD1-4A5F-BB6B-50D2BB63DB4A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4E956F6B-6BD8-46F5-BC85-49292FF8F9AB}"
Expand All @@ -32,27 +30,29 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{568BD46C
tests\docker-compose.yml = tests\docker-compose.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapper.Contrib", "src\Dapper.Contrib\Dapper.Contrib.csproj", "{F36D3111-38BC-488F-BE7F-533B5DC8070A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4E409F8F-CFBB-4332-8B0A-FD5A283051FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4E409F8F-CFBB-4332-8B0A-FD5A283051FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4E409F8F-CFBB-4332-8B0A-FD5A283051FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4E409F8F-CFBB-4332-8B0A-FD5A283051FD}.Release|Any CPU.Build.0 = Release|Any CPU
{DAB3C5B7-BCD1-4A5F-BB6B-50D2BB63DB4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DAB3C5B7-BCD1-4A5F-BB6B-50D2BB63DB4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DAB3C5B7-BCD1-4A5F-BB6B-50D2BB63DB4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DAB3C5B7-BCD1-4A5F-BB6B-50D2BB63DB4A}.Release|Any CPU.Build.0 = Release|Any CPU
{F36D3111-38BC-488F-BE7F-533B5DC8070A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F36D3111-38BC-488F-BE7F-533B5DC8070A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F36D3111-38BC-488F-BE7F-533B5DC8070A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F36D3111-38BC-488F-BE7F-533B5DC8070A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{4E409F8F-CFBB-4332-8B0A-FD5A283051FD} = {4E956F6B-6BD8-46F5-BC85-49292FF8F9AB}
{DAB3C5B7-BCD1-4A5F-BB6B-50D2BB63DB4A} = {568BD46C-1C65-4D44-870C-12CD72563262}
{F36D3111-38BC-488F-BE7F-533B5DC8070A} = {4E956F6B-6BD8-46F5-BC85-49292FF8F9AB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {928A4226-96F3-409A-8A83-9E7444488710}
Expand Down
12 changes: 12 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ Dapper.Contrib makes use of some optional attributes:
```
* `[Write(true/false)]` - this property is (not) writeable
* `[Computed]` - this property is computed and should not be part of updates
* `[Column("Columnname")]` - this property has a different name in the Database
* Property is called EmployeeId but Column in DB is called employee_id

```csharp
public class Employee
{
[ExplicitKey]
[Column("employee_id")]
public Guid EmployeeId { get; set; }
public string Name { get; set; }
}
```

Limitations and caveats
-------
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Note: to get the latest pre-release build, add ` -Pre` to the end of the command

### Unreleased

(note: new PRs will not be merged until they add release note wording here)
* Added Column Attribute for different column name to property name

### Previous Releases

Expand Down
7 changes: 6 additions & 1 deletion src/Dapper.Contrib/Dapper.Contrib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
<TargetFrameworks>net461;netstandard2.0;net5.0</TargetFrameworks>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<NoWarn>$(NoWarn);CA1050</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>Dapper.Contrib.WithColumn</PackageId>
<PackageVersion>1.0.3</PackageVersion>
<PackageProjectUrl>https://github.com/Tiefseetauchner/Dapper.Contrib</PackageProjectUrl>
<RepositoryUrl>https://github.com/Tiefseetauchner/Dapper.Contrib</RepositoryUrl>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.78" />
Expand All @@ -19,4 +24,4 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
</Project>
</Project>
74 changes: 66 additions & 8 deletions src/Dapper.Contrib/SqlMapperExtensions.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Dapper.Contrib.Extensions;

namespace Dapper.Contrib.Extensions
{
Expand All @@ -29,8 +30,31 @@ public static async Task<T> GetAsync<T>(this IDbConnection connection, dynamic i
{
var key = GetSingleKey<T>(nameof(GetAsync));
var name = GetTableName(type);

var sb = new StringBuilder();

sql = $"SELECT * FROM {name} WHERE {key.Name} = @id";
var allProperties = TypePropertiesCache(type);

var adapter = GetFormatter(connection);

for (var i = 0; i < allProperties.Count; i++)
{
var property = allProperties[i];

if (HasColumnName(property))
{
adapter.AppendColumnNameAsPropertyName(sb, GetColumnName(property), property.Name);
}
else
{
adapter.AppendColumnName(sb, GetColumnName(property));
}

if (i < allProperties.Count - 1)
sb.Append(", ");
}

sql = $"SELECT {sb} FROM {name} WHERE {GetColumnName(key)} = @id";
GetQueries[type.TypeHandle] = sql;
}

Expand Down Expand Up @@ -88,7 +112,30 @@ public static Task<IEnumerable<T>> GetAllAsync<T>(this IDbConnection connection,
GetSingleKey<T>(nameof(GetAll));
var name = GetTableName(type);

sql = "SELECT * FROM " + name;
var sb = new StringBuilder();

var allProperties = TypePropertiesCache(type);

var adapter = GetFormatter(connection);

for (var i = 0; i < allProperties.Count; i++)
{
var property = allProperties[i];

if (HasColumnName(property))
{
adapter.AppendColumnNameAsPropertyName(sb, GetColumnName(property), property.Name);
}
else
{
adapter.AppendColumnName(sb, GetColumnName(property));
}

if (i < allProperties.Count - 1)
sb.Append(", ");
}

sql = $"SELECT {sb} FROM {name}";
GetQueries[cacheType.TypeHandle] = sql;
}

Expand All @@ -107,7 +154,7 @@ private static async Task<IEnumerable<T>> GetAllAsyncImpl<T>(IDbConnection conne
{
var obj = ProxyGenerator.GetInterfaceProxy<T>();
foreach (var property in TypePropertiesCache(type))
{
{
var val = res[property.Name];
if (val == null) continue;
if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
Expand Down Expand Up @@ -172,7 +219,8 @@ public static Task<int> InsertAsync<T>(this IDbConnection connection, T entityTo
for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count; i++)
{
var property = allPropertiesExceptKeyAndComputed[i];
sqlAdapter.AppendColumnName(sbColumnList, property.Name);

sqlAdapter.AppendColumnName(sbColumnList, GetColumnName(property));
if (i < allPropertiesExceptKeyAndComputed.Count - 1)
sbColumnList.Append(", ");
}
Expand All @@ -181,6 +229,7 @@ public static Task<int> InsertAsync<T>(this IDbConnection connection, T entityTo
for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count; i++)
{
var property = allPropertiesExceptKeyAndComputed[i];

sbParameterList.AppendFormat("@{0}", property.Name);
if (i < allPropertiesExceptKeyAndComputed.Count - 1)
sbParameterList.Append(", ");
Expand Down Expand Up @@ -252,15 +301,19 @@ public static async Task<bool> UpdateAsync<T>(this IDbConnection connection, T e
for (var i = 0; i < nonIdProps.Count; i++)
{
var property = nonIdProps[i];
adapter.AppendColumnNameEqualsValue(sb, property.Name);

adapter.AppendColumnNameEqualsValue(sb, GetColumnName(property), property.Name);
if (i < nonIdProps.Count - 1)
sb.Append(", ");
}
sb.Append(" where ");
for (var i = 0; i < keyProperties.Count; i++)
{
var property = keyProperties[i];
adapter.AppendColumnNameEqualsValue(sb, property.Name);

var columnAttribute = property.GetCustomAttribute<ColumnAttribute>();

adapter.AppendColumnNameEqualsValue(sb, GetColumnName(property), property.Name);
if (i < keyProperties.Count - 1)
sb.Append(" and ");
}
Expand Down Expand Up @@ -317,7 +370,10 @@ public static async Task<bool> DeleteAsync<T>(this IDbConnection connection, T e
for (var i = 0; i < allKeyProperties.Count; i++)
{
var property = allKeyProperties[i];
adapter.AppendColumnNameEqualsValue(sb, property.Name);

var columnAttribute = property.GetCustomAttribute<ColumnAttribute>();

adapter.AppendColumnNameEqualsValue(sb, GetColumnName(property), property.Name);
if (i < allKeyProperties.Count - 1)
sb.Append(" AND ");
}
Expand Down Expand Up @@ -493,7 +549,8 @@ public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction tran
if (!first)
sb.Append(", ");
first = false;
sb.Append(property.Name);

sb.Append(GetColumnName(property));
}
}

Expand All @@ -510,6 +567,7 @@ public async Task<int> InsertAsync(IDbConnection connection, IDbTransaction tran
}
return id;
}
private static string GetColumnName(PropertyInfo property) => property.GetCustomAttribute<ColumnAttribute>() == null ? property.Name : property.GetCustomAttribute<ColumnAttribute>().Name;
}

public partial class SQLiteAdapter
Expand Down
Loading