public void Test_WalkExpressions_None(bool filteringOnly, int expected) { StructuredQuery sq = new StructuredQuery( ); sq.RootEntity = new ResourceEntity( ); var res = StructuredQueryHelper.WalkExpressions(sq, filteringOnly); Assert.That(res.Count( ), Is.EqualTo(expected)); }
public void Test_WalkExpressions_GroupBy(bool filteringOnly, int expected) { StructuredQuery sq = new StructuredQuery( ); AggregateEntity root = new AggregateEntity( ); sq.RootEntity = root; root.GroupedEntity = new ResourceEntity( ); root.GroupBy.Add(new IdExpression( )); root.GroupBy.Add(new MutateExpression { Expression = new IdExpression( ) }); var res = StructuredQueryHelper.WalkExpressions(sq, filteringOnly); Assert.That(res.Count( ), Is.EqualTo(expected)); }
public void Test_WalkExpressions_NodeCondition(bool filteringOnly, int expected) { StructuredQuery sq = new StructuredQuery( ); sq.RootEntity = new ResourceEntity { Conditions = new List <ScalarExpression>( ) }; sq.RootEntity.Conditions.Add(new IdExpression( )); sq.RootEntity.Conditions.Add(new MutateExpression { Expression = new IdExpression( ) }); var res = StructuredQueryHelper.WalkExpressions(sq, filteringOnly); Assert.That(res.Count( ), Is.EqualTo(expected)); }
public void Test_WalkExpressions_OrderBy(bool filteringOnly, int expected) { StructuredQuery sq = new StructuredQuery( ); sq.RootEntity = new ResourceEntity( ); sq.OrderBy.Add(new OrderByItem { Expression = new IdExpression() }); sq.OrderBy.Add(new OrderByItem { Expression = new MutateExpression { Expression = new IdExpression( ) } }); var res = StructuredQueryHelper.WalkExpressions(sq, filteringOnly); Assert.That(res.Count( ), Is.EqualTo(expected)); }
/// <summary> /// Get the field types referenced in the query condition only. Ignore fields /// elsewhere. /// </summary> /// <param name="structuredQuery"> /// The <see cref="StructuredQuery"/> to check. This cannot be null. /// </param> /// <returns> /// A list of field types or empty, if the query references no fields. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="structuredQuery"/> cannot be null. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="structuredQuery"/>'s Conditions property cannot be null. /// </exception> public static IList <EntityRef> GetFieldTypesReferencedByCondition(StructuredQuery structuredQuery) { if (structuredQuery == null) { throw new ArgumentNullException("structuredQuery"); } if (structuredQuery.Conditions == null) { throw new ArgumentNullException("structuredQuery", "Conditions property cannot be null"); } return(structuredQuery.Conditions .SelectMany(cond => StructuredQueryHelper.WalkExpressions(cond.Expression)) .Where(se => se is ResourceDataColumn) .Cast <ResourceDataColumn>() .Select(rdc => rdc.FieldId) .Distinct(EntityRefComparer.Instance) .ToList()); }
/// <summary> /// Get the relationship types referenced in the query. /// </summary> /// <param name="structuredQuery"> /// The <see cref="StructuredQuery"/> to check. This cannot be null. /// </param> /// <returns> /// A list of relationship types or empty, if the query references no relationships. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="structuredQuery"/> cannot be null. /// </exception> /// <exception cref="ArgumentNullException"> /// <paramref name="structuredQuery"/>'s RootEntity property cannot be null. /// </exception> public static IList <EntityRef> GetReferencedRelationshipTypes(StructuredQuery structuredQuery) { if (structuredQuery == null) { throw new ArgumentNullException("structuredQuery"); } if (structuredQuery.RootEntity == null) { throw new ArgumentNullException("structuredQuery", "RootEntity cannot be null"); } HashSet <EntityRef> result; result = new HashSet <EntityRef>(); IEnumerable <ResourceDataColumn> resourceDataColumns; RelatedResource relatedResource; resourceDataColumns = structuredQuery.Conditions .SelectMany(cond => StructuredQueryHelper.WalkExpressions(cond.Expression)) .Where(se => se is ResourceDataColumn) .Cast <ResourceDataColumn>(); foreach (ResourceDataColumn resourceDataColumn in resourceDataColumns) { StructuredQueryHelper.VisitNodes(structuredQuery.RootEntity, (node, ancestors) => { if (node.NodeId == resourceDataColumn.NodeId) { relatedResource = node as RelatedResource; if (relatedResource != null) { result.Add(relatedResource.RelationshipTypeId); result.UnionWith( ancestors.Where(n => n is RelatedResource) .Cast <RelatedResource>() .Select(rr => rr.RelationshipTypeId)); } } }); } return(result.ToList()); }
/// <summary> /// Assigns the column node unique identifier for expression. /// </summary> /// <param name="columns">The columns.</param> /// <param name="reportColumn">The report column.</param> /// <param name="context">The context.</param> private static void AssignColumnNodeGuidForExpression(IEnumerable <SelectColumn> columns, ReportColumn reportColumn, FromEntityContext context) { // Get the GUID of the report column Guid reportColumnGuid; if (context.ReportColumnMap.ContainsKey(reportColumn.Id)) { reportColumnGuid = context.ReportColumnMap[reportColumn.Id]; } else { return; } // Look up the select column using the GUID SelectColumn selectColumn = columns.FirstOrDefault(c => c.ColumnId == reportColumnGuid); if (selectColumn == null) { return; } // Handle column reference expressions if (selectColumn.Expression != null) { ColumnReferenceExpression columnReferenceExpression = reportColumn.ColumnExpression.As <ColumnReferenceExpression>(); if (columnReferenceExpression != null) { long rootNodeId = columnReferenceExpression.ExpressionReferencesColumn.Id; // Get the GUID for the referenced column rather than root node and populate ColumnReference columnReference = selectColumn.Expression as ColumnReference; if (columnReference != null) { columnReference.ColumnId = context.ReportColumnMap[rootNodeId]; } } else { ResolveExpressionToNode(StructuredQueryHelper.WalkExpressions(selectColumn.Expression), context); } } }
/// <summary> /// Resolves the references to hook up various node and column identifiers from the entity model context. /// </summary> /// <param name="query">The query.</param> /// <param name="report">The report.</param> /// <param name="context">The context.</param> internal static void ResolveReferences(StructuredQuery query, Report report, FromEntityContext context) { if (query.RootEntity is AggregateEntity) { AggregateEntity ae = query.RootEntity as AggregateEntity; foreach (ResourceDataColumn rdc in ae.GroupBy.OfType <ResourceDataColumn>()) { rdc.NodeId = context.ReportNodeMap[rdc.SourceNodeEntityId]; } } // Resolve the Node Expressions for report columns foreach (ReportColumn reportColumn in report.ReportColumns) { AssignColumnNodeGuidForExpression(query.SelectColumns, reportColumn, context); } // Resolve the Node expressions for the analyser foreach (QueryCondition queryCondition in query.Conditions) { ResolveExpressionToNode(StructuredQueryHelper.WalkExpressions(queryCondition.Expression), context); ColumnReference columnReference = queryCondition.Expression as ColumnReference; //if the expression is ColumnReferenceExpression, the referenced report column must exists if (columnReference != null && context.ColumnReferenceMap.ContainsKey(columnReference.ExpressionId) && context.ReportColumnMap.ContainsKey(context.ColumnReferenceMap[columnReference.ExpressionId])) { columnReference.ColumnId = context.ReportColumnMap[context.ColumnReferenceMap[columnReference.ExpressionId]]; } } // Resolve the order by expressions (NOTE: Am assuming that order by can only contain column references here) foreach (ColumnReference columnReference in query.OrderBy.Select(orderByItem => orderByItem.Expression).OfType <ColumnReference>()) { //the orderby expression is ColumnReferenceExpression, the referenced report column must exists if (context.ColumnReferenceMap.ContainsKey(columnReference.ExpressionId) && context.ReportColumnMap.ContainsKey(context.ColumnReferenceMap[columnReference.ExpressionId])) { columnReference.ColumnId = context.ReportColumnMap[context.ColumnReferenceMap[columnReference.ExpressionId]]; } } }