/// <summary> /// Create a <see cref="SqlStatement"/> that contains all the needed information to execute the query /// as an INNER JOIN of any one of the other queries that uses it as a principal query /// IMPORTANT: Calling this method will keep a permanent cache of some parts of the result, therefore /// if the arguments need to change after that, a new <see cref="QueryInternal"/> must be created /// </summary> private string PrepareStatementAsPrincipal( Func <Type, string> sources, SqlStatementParameters ps, bool isAncestorExpand, ArraySegment <string> pathToCollectionProperty, int userId, DateTime?userToday) { // (1) Prepare the JOIN's clause JoinTree joinTree = PrepareJoin(pathToCollectionProperty); var joinSql = joinTree.GetSql(sources, FromSql); // (2) Prepare the SELECT clause SqlSelectClause selectClause = PrepareSelectAsPrincipal(joinTree, pathToCollectionProperty, isAncestorExpand); var selectSql = selectClause.ToSql(IsAncestorExpand); // (3) Prepare the inner join with the principal query (if any) string principalQuerySql = PreparePrincipalQuery(sources, ps, userId, userToday); // (4) Prepare the WHERE clause string whereSql = PrepareWhere(sources, joinTree, ps, userId, userToday); // (5) Prepare the ORDERBY clause string orderbySql = PrepareOrderBy(joinTree); // (6) Prepare the OFFSET and FETCH clauses string offsetFetchSql = PrepareOffsetFetch(); if (string.IsNullOrWhiteSpace(offsetFetchSql)) { // In a principal query, order by is only added if there is an offset-fetch (usually in the root query) orderbySql = ""; } // (7) Finally put together the final SQL statement and return it string sql = QueryTools.CombineSql( selectSql: selectSql, joinSql: joinSql, principalQuerySql: principalQuerySql, whereSql: whereSql, orderbySql: orderbySql, offsetFetchSql: offsetFetchSql, groupbySql: null ); // (8) Return the result return(sql); }
// Functionality /// <summary> /// Create a <see cref="SqlStatement"/> that contains all the needed information to execute the query against a SQL Server database and load and hydrate the entities /// IMPORTANT: Calling this method will keep a permanent cache of some parts of the result, therefore if the arguments need to change after /// that, a new <see cref="QueryInternal"/> must be created /// </summary> public SqlStatement PrepareStatement( Func <Type, string> sources, SqlStatementParameters ps, int userId, DateTime?userToday) { // (1) Prepare the JOIN's clause var joinTree = PrepareJoin(); var joinSql = joinTree.GetSql(sources, FromSql); // (2) Prepare the SELECT clause SqlSelectClause selectClause = PrepareSelect(joinTree); var selectSql = selectClause.ToSql(IsAncestorExpand); // (3) Prepare the inner join with the principal query (if any) string principalQuerySql = PreparePrincipalQuery(sources, ps, userId, userToday); // (4) Prepare the WHERE clause string whereSql = PrepareWhere(sources, joinTree, ps, userId, userToday); // (5) Prepare the ORDERBY clause string orderbySql = PrepareOrderBy(joinTree); // (6) Prepare the OFFSET and FETCH clauses string offsetFetchSql = PrepareOffsetFetch(); // (7) Finally put together the final SQL statement and return it string sql = QueryTools.CombineSql( selectSql: selectSql, joinSql: joinSql, principalQuerySql: principalQuerySql, whereSql: whereSql, orderbySql: orderbySql, offsetFetchSql: offsetFetchSql, groupbySql: null ); // (8) Return the result return(new SqlStatement { Sql = sql, ResultType = ResultType, ColumnMap = selectClause.GetColumnMap(), Query = this, }); }