using System.Linq.Expressions;
using System.Reflection;
using NPoco;
using Umbraco.Cms.Core;
using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax;
namespace Umbraco.Extensions;
///
/// Provides extension methods to .
///
public static class SqlSyntaxExtensions
{
///
/// 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();
Type type = typeof(TDto);
var tableName = tableAlias ?? type.GetTableName();
return sqlSyntax.GetQuotedTableName(tableName) + "." + sqlSyntax.GetQuotedColumnName(fieldName);
}
private static string GetColumnName(this PropertyInfo column)
{
ColumnAttribute? attr = column.FirstAttribute();
return string.IsNullOrWhiteSpace(attr?.Name) ? column.Name : attr.Name;
}
}