Improve NPoco extensions

This commit is contained in:
Stephan
2018-09-17 13:06:20 +02:00
parent e412fd8802
commit a979e8023e
4 changed files with 172 additions and 34 deletions

View File

@@ -0,0 +1,48 @@
using System;
using System.Linq.Expressions;
using System.Reflection;
using NPoco;
using Umbraco.Core.Persistence.SqlSyntax;
namespace Umbraco.Core.Persistence
{
/// <summary>
/// Provides extension methods to <see cref="ISqlSyntaxProvider"/>.
/// </summary>
public static class SqlSyntaxExtensions
{
private static string GetTableName(this Type type)
{
// todo: returning string.Empty for now
// BUT the code bits that calls this method cannot deal with string.Empty so we
// should either throw, or fix these code bits...
var attr = type.FirstAttribute<TableNameAttribute>();
return string.IsNullOrWhiteSpace(attr?.Value) ? string.Empty : attr.Value;
}
private static string GetColumnName(this PropertyInfo column)
{
var attr = column.FirstAttribute<ColumnAttribute>();
return string.IsNullOrWhiteSpace(attr?.Name) ? column.Name : attr.Name;
}
/// <summary>
/// Gets a quoted table and field name.
/// </summary>
/// <typeparam name="TDto">The type of the DTO.</typeparam>
/// <param name="sqlSyntax">An <see cref="ISqlSyntaxProvider"/>.</param>
/// <param name="fieldSelector">An expression specifying the field.</param>
/// <param name="tableAlias">An optional table alias.</param>
/// <returns></returns>
public static string GetFieldName<TDto>(this ISqlSyntaxProvider sqlSyntax, Expression<Func<TDto, object>> fieldSelector, string tableAlias = null)
{
var field = ExpressionHelper.FindProperty(fieldSelector).Item1 as PropertyInfo;
var fieldName = field.GetColumnName();
var type = typeof(TDto);
var tableName = tableAlias ?? type.GetTableName();
return sqlSyntax.GetQuotedTableName(tableName) + "." + sqlSyntax.GetQuotedColumnName(fieldName);
}
}
}