using System; using System.Linq.Expressions; using Umbraco.Core.Persistence.Querying; namespace Umbraco.Core.Persistence { /// /// 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 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) { var (sql, _) = sqlContext.VisitModel(field); // going to return " = @0" // take the first part only var pos = sql.IndexOf(' '); return sql.Substring(0, pos); } } }