using System.Linq.Expressions; using Umbraco.Cms.Infrastructure.Persistence; using Umbraco.Cms.Infrastructure.Persistence.Querying; namespace Umbraco.Extensions; /// /// Provides extension methods to . /// public static class SqlContextExtensions { /// /// Visit an expression. /// /// The type of the DTO. /// An . /// An expression to visit. /// An optional table alias. /// A SQL statement, and arguments, corresponding to the expression. public static (string Sql, object[] Args) VisitDto(this ISqlContext sqlContext, Expression> expression, string? alias = null) { var visitor = new PocoToSqlExpressionVisitor(sqlContext, alias); var visited = visitor.Visit(expression); return (visited, visitor.GetSqlParameters()); } /// /// Visit an expression. /// /// The type of the DTO. /// The type returned by the expression. /// An . /// An expression to visit. /// An optional table alias. /// A SQL statement, and arguments, corresponding to the expression. public static (string Sql, object[] Args) VisitDto(this ISqlContext sqlContext, Expression> expression, string? alias = null) { var visitor = new PocoToSqlExpressionVisitor(sqlContext, alias); var visited = visitor.Visit(expression); return (visited, visitor.GetSqlParameters()); } /// /// Visit an expression. /// /// The type of the first DTO. /// The type of the second DTO. /// An . /// An expression to visit. /// An optional table alias for the first DTO. /// An optional table alias for the second DTO. /// A SQL statement, and arguments, corresponding to the expression. public static (string Sql, object[] Args) VisitDto(this ISqlContext sqlContext, Expression> expression, string? alias1 = null, string? alias2 = null) { var visitor = new PocoToSqlExpressionVisitor(sqlContext, alias1, alias2); var visited = visitor.Visit(expression); return (visited, visitor.GetSqlParameters()); } /// /// Visit an expression. /// /// The type of the first DTO. /// The type of the second DTO. /// The type of the third DTO. /// The type returned by the expression. /// An . /// An expression to visit. /// An optional table alias for the first DTO. /// An optional table alias for the second DTO. /// An optional table alias for the third DTO. /// A SQL statement, and arguments, corresponding to the expression. public static (string Sql, object[] Args) VisitDto(this ISqlContext sqlContext, Expression> expression, string? alias1 = null, string? alias2 = null, string? alias3 = null) { var visitor = new PocoToSqlExpressionVisitor(sqlContext, alias1, alias2, alias3); var visited = visitor.Visit(expression); return (visited, visitor.GetSqlParameters()); } /// /// Visit an expression. /// /// The type of the first DTO. /// The type of the second DTO. /// The type returned by the expression. /// An . /// An expression to visit. /// An optional table alias for the first DTO. /// An optional table alias for the second DTO. /// A SQL statement, and arguments, corresponding to the expression. public static (string Sql, object[] Args) VisitDto(this ISqlContext sqlContext, Expression> expression, string? alias1 = null, string? alias2 = null) { var visitor = new PocoToSqlExpressionVisitor(sqlContext, alias1, alias2); var visited = visitor.Visit(expression); return (visited, visitor.GetSqlParameters()); } /// /// Visit a model expression. /// /// The type of the model. /// An . /// An expression to visit. /// A SQL statement, and arguments, corresponding to the expression. public static (string Sql, object[] Args) VisitModel( this ISqlContext sqlContext, Expression> expression) { var visitor = new ModelToSqlExpressionVisitor(sqlContext.SqlSyntax, sqlContext.Mappers); var visited = visitor.Visit(expression); return (visited, visitor.GetSqlParameters()); } /// /// Visit a model expression representing a field. /// /// The type of the model. /// An . /// An expression to visit, representing a field. /// The name of the field. public static string VisitModelField(this ISqlContext sqlContext, Expression> field) { (string sql, object[] _) = sqlContext.VisitModel(field); // going to return " = @0" // take the first part only var pos = sql.IndexOf(' '); return sql.Substring(0, pos); } }