Skip to content

Extending it with IColumnNameFormatter #109

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapper.Contrib", "src\Dapper.Contrib\Dapper.Contrib.csproj", "{FA57085B-01A0-42BC-8B52-03712371C4AB}"
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
{FA57085B-01A0-42BC-8B52-03712371C4AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FA57085B-01A0-42BC-8B52-03712371C4AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA57085B-01A0-42BC-8B52-03712371C4AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA57085B-01A0-42BC-8B52-03712371C4AB}.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}
{FA57085B-01A0-42BC-8B52-03712371C4AB} = {4E956F6B-6BD8-46F5-BC85-49292FF8F9AB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {928A4226-96F3-409A-8A83-9E7444488710}
Expand Down
8 changes: 8 additions & 0 deletions src/Dapper.Contrib/ColumnNameFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Dapper.Contrib.Extensions
{
public enum ColumnNameFormat : short
{
None = 0,
LowerCase = 10, //I'll only implement this.
}
}
29 changes: 29 additions & 0 deletions src/Dapper.Contrib/DefaultColumnNameFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Dapper.Contrib.Extensions
{
public class DefaultColumnNameFormatter : IColumnNameFormatter
{

public ColumnNameFormat ColumnNameFormat { get; set; }
public DefaultColumnNameFormatter()
{
}

public DefaultColumnNameFormatter(ColumnNameFormat columnNameFormat)
{
ColumnNameFormat = columnNameFormat;
}

public string Format(string name)
{
switch(ColumnNameFormat)
{
case ColumnNameFormat.LowerCase:
return name.ToLower();
break;
default:
return name;
}
}
}

}
7 changes: 7 additions & 0 deletions src/Dapper.Contrib/IColumnNameFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Dapper.Contrib.Extensions
{
public interface IColumnNameFormatter
{
string Format(string name);
}
}
11 changes: 11 additions & 0 deletions src/Dapper.Contrib/LowerCaseColumnNameFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Dapper.Contrib.Extensions
{
public class LowerCaseColumnNameFormatter : IColumnNameFormatter
{
public string Format(string name)
{
return name.ToLower();
}
}

}
4 changes: 2 additions & 2 deletions src/Dapper.Contrib/SqlMapperExtensions.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private static async Task<IEnumerable<T>> GetAllAsyncImpl<T>(IDbConnection conne
/// <param name="sqlAdapter">The specific ISqlAdapter to use, auto-detected based on connection if null</param>
/// <returns>Identity of inserted entity</returns>
public static Task<int> InsertAsync<T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null,
int? commandTimeout = null, ISqlAdapter sqlAdapter = null) where T : class
int? commandTimeout = null, ISqlAdapter sqlAdapter = null, IColumnNameFormatter columnNameFormatter = null) where T : class
{
var type = typeof(T);
sqlAdapter ??= GetFormatter(connection);
Expand Down Expand Up @@ -172,7 +172,7 @@ 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, columnNameFormatter == null ? property.Name : columnNameFormatter.Format(property.Name));
if (i < allPropertiesExceptKeyAndComputed.Count - 1)
sbColumnList.Append(", ");
}
Expand Down
4 changes: 2 additions & 2 deletions src/Dapper.Contrib/SqlMapperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private static string GetTableName(Type type)
/// <param name="transaction">The transaction to run under, null (the default) if none</param>
/// <param name="commandTimeout">Number of seconds before command execution timeout</param>
/// <returns>Identity of inserted entity, or number of inserted rows if inserting a list</returns>
public static long Insert<T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
public static long Insert<T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int? commandTimeout = null, IColumnNameFormatter columnNameFormatter = null) where T : class
{
var isList = false;

Expand Down Expand Up @@ -354,7 +354,7 @@ public static long Insert<T>(this IDbConnection connection, T entityToInsert, ID
for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count; i++)
{
var property = allPropertiesExceptKeyAndComputed[i];
adapter.AppendColumnName(sbColumnList, property.Name); //fix for issue #336
adapter.AppendColumnName(sbColumnList, columnNameFormatter == null ? property.Name : columnNameFormatter.Format(property.Name)); //fix for issue #336
if (i < allPropertiesExceptKeyAndComputed.Count - 1)
sbColumnList.Append(", ");
}
Expand Down