using System;
using System.Linq.Expressions;
using System.Reflection;
using NPoco;
using Umbraco.Core.Persistence.SqlSyntax;
namespace Umbraco.Core.Persistence
{
///
/// Provides extension methods to .
///
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();
return string.IsNullOrWhiteSpace(attr?.Value) ? string.Empty : attr.Value;
}
private static string GetColumnName(this PropertyInfo column)
{
var attr = column.FirstAttribute();
return string.IsNullOrWhiteSpace(attr?.Name) ? column.Name : attr.Name;
}
///
/// Gets a quoted table and field name.
///
/// The type of the DTO.
/// An .
/// An expression specifying the field.
/// An optional table alias.
///
public static string GetFieldName(this ISqlSyntaxProvider sqlSyntax, Expression> 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);
}
}
}