/// <summary> /// Creates an TSQL Count(1) statement /// </summary> /// <typeparam name="TPoco">The type of the poco.</typeparam> /// <typeparam name="TOut">The type of the out.</typeparam> /// <param name="query">The query.</param> /// <returns></returns> public static ElementProducer <TOut> Count <TPoco, TOut>(this IElementProducer <TPoco> query) { var cteName = "countCte" + query.ContainerObject.GetNextParameterId(); return(new ElementProducer <TOut>(new RootQuery(query.ContainerObject.AccessLayer) .WithCte(cteName, f => { var order = false; foreach (var genericQueryPart in query.ContainerObject.Parts) { var partType = genericQueryPart.Builder != null ? genericQueryPart.Builder.GetType() : null; if (partType != null && order) { if (partType != typeof(OrderByColumn <TPoco>) && partType != typeof(OrderStatementQuery <TPoco>)) { order = false; } } if (genericQueryPart.Prefix == "ORDER BY") { order = true; } if (!order) { f = f.Add(genericQueryPart); } } return new SelectQuery <TPoco>(f); }).QueryText("SELECT COUNT(1) FROM " + cteName), cteName)); }
/// <summary> /// Creates an TSQL Count(1) statement /// </summary> /// <typeparam name="TPoco">The type of the poco.</typeparam> /// <typeparam name="TOut">The type of the out.</typeparam> /// <param name="query">The query.</param> /// <returns></returns> private static ElementProducer <TOut> Sum <TPoco, TOut>(this IElementProducer <TPoco> query) { IQueryBuilder newQuery = new RootQuery(query.ContainerObject.AccessLayer); //in case there is a grouping in the query, we must use a SubQuery var ordering = query.ContainerObject.SearchLast <OrderByColumnQueryPart>(); var cteName = query.ContainerObject.CreateAlias(QueryIdentifier.QueryIdTypes.Cte); var item = new CteDefinitionQueryPart.CteInfo() { Name = cteName }; item.CteContentParts.AddRange(query.ContainerObject.Parts.Except(new IQueryPart[] { ordering }).ToArray()); var cteQueryPart = query.ContainerObject.SearchLast <CteDefinitionQueryPart>(); newQuery = newQuery.Add(cteQueryPart ?? (cteQueryPart = new CteDefinitionQueryPart())) .Add(cteQueryPart.AddCte(item)); var subQueryId = query.ContainerObject.CreateAlias(QueryIdentifier.QueryIdTypes.SubQuery); var countQueryPart = newQuery .Add(new CountTargetQueryPart(cteName, subQueryId)); if (ordering != null) { var orderByColumnQueryPart = new OrderByColumnQueryPart(); orderByColumnQueryPart.Descending = ordering.Descending; orderByColumnQueryPart.Columns = orderByColumnQueryPart.Columns .Select(f => new ColumnInfo(f.ColumnName, f, subQueryId, query.ContainerObject)).ToList(); countQueryPart.Add(orderByColumnQueryPart); } return(new ElementProducer <TOut>(countQueryPart)); }
/// <summary> /// Creates a CTE on the start of the Query /// </summary> /// <returns></returns> public RootQuery WithCte <T>(IElementProducer <T> commandQuery, out QueryIdentifier cteName) { IQueryBuilder newQuery = new RootQuery(this); cteName = newQuery.ContainerObject.CreateAlias(QueryIdentifier.QueryIdTypes.Cte); (commandQuery.ContainerObject as IQueryContainerValues)?.TableAlias.Clear(); var cteQueryPart = commandQuery.ContainerObject.SearchLast <CteDefinitionQueryPart>(); newQuery = newQuery.Add(cteQueryPart ?? (cteQueryPart = new CteDefinitionQueryPart())); var cteInfo = new CteDefinitionQueryPart.CteInfo(); cteInfo.Name = cteName; cteInfo.CteContentParts.AddRange(commandQuery.ContainerObject.Parts); return(new RootQuery(newQuery.Add(cteQueryPart.AddCte(cteInfo)))); }
/// <summary> /// Creates an TSQL Count(1) statement /// </summary> /// <typeparam name="TPoco">The type of the poco.</typeparam> /// <param name="query">The query.</param> /// <returns></returns> public static ElementProducer <int> CountInt <TPoco>(this IElementProducer <TPoco> query) { return(query.Count <TPoco, int>()); }
/// <summary> /// Creates an TSQL Count(1) statement /// </summary> /// <typeparam name="TPoco">The type of the poco.</typeparam> /// <param name="query">The query.</param> /// <returns></returns> public static ElementProducer <short> CountShort <TPoco>(this IElementProducer <TPoco> query) { return(query.Count <TPoco, short>()); }
/// <summary> /// Creates an TSQL Count(1) statement /// </summary> /// <typeparam name="TPoco">The type of the poco.</typeparam> /// <param name="query">The query.</param> /// <returns></returns> public static ElementProducer <long> CountLong <TPoco>(this IElementProducer <TPoco> query) { return(query.Count <TPoco, long>()); }
/// <summary> /// Creates a CTE on the start of the Query /// </summary> /// <returns></returns> public RootQuery WithCte <T>(IElementProducer <T> commandQuery, out QueryIdentifier cteName) { return(WithCte(commandQuery as IQueryBuilder, out cteName)); }