/// <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, SqlStatementVariables vars, SqlStatementParameters ps, int userId, DateTime?userToday) { // (1) Prepare the JOIN's clause var joinTrie = PrepareJoin(); var joinSql = joinTrie.GetSql(sources, FromSql); // Compilation context var today = userToday ?? DateTime.Today; var now = DateTimeOffset.Now; var ctx = new QxCompilationContext(joinTrie, sources, vars, ps, today, now, userId); // (2) Prepare the SELECT clause SqlSelectClause selectClause = PrepareSelect(joinTrie); var selectSql = selectClause.ToSql(IsAncestorExpand); // (3) Prepare the inner join with the principal query (if any) string principalQuerySql = PreparePrincipalQuerySql(ctx); // (4) Prepare the WHERE clause string whereSql = PrepareWhereSql(ctx); // (5) Prepare the ORDERBY clause string orderbySql = PrepareOrderBySql(ctx); // (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, havingSql: null, selectFromTempSql: null ); // (8) Return the result return(new SqlStatement { Sql = sql, ResultDescriptor = ResultDescriptor, ColumnMap = selectClause.GetColumnMap(), Query = this, }); }
// 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, }); }