public override void VisitAdditionalFromClause(Remotion.Linq.Clauses.AdditionalFromClause fromClause, QueryModel queryModel, int index) { _queryAsString.AppendFormat(" from {0} {1} in {2}", fromClause.ItemType.Name, fromClause.ItemName, ExpressionStringConverter.Format(fromClause.FromExpression)); }
protected void $DOMAIN_PROPERTYNAME$Field_MenuItemClick (object sender, Remotion.Web.UI.Controls.WebMenuItemClickEventArgs e) { if (e.Item.ItemID == "AddMenuItem") { $DOMAIN_REFERENCEDCLASSNAME$ row = $DOMAIN_REFERENCEDCLASSNAME$.NewObject(); $DOMAIN_PROPERTYNAME$Field.AddAndEditRow (row); } }
public override void VisitWhereClause(Remotion.Linq.Clauses.WhereClause whereClause, QueryModel queryModel, int index) { if (query.Length > 0) query.Append(" AND "); ExpressionTreeVisitor visitor = new ExpressionTreeVisitor(query); visitor.VisitExpression(whereClause.Predicate); base.VisitWhereClause(whereClause, queryModel, index); }
/// <summary>Default constructor with base <see cref="System.Linq.Expressions.MemberExpression" /> and <see cref="Remotion.Linq.Clauses.FromClauseBase" />.</summary> /// <param name="expression">Base member expression.</param> /// <param name="target">Target of the invocation.</param> internal EntityIdentifierExpression(System.Linq.Expressions.MemberExpression expression, Remotion.Linq.Clauses.FromClauseBase target) : base() { if (!(expression.Member is PropertyInfo)) { throw new ArgumentOutOfRangeException("expression"); } _expression = expression; _target = target; }
public override void VisitMainFromClause(Remotion.Linq.Clauses.MainFromClause fromClause, QueryModel queryModel) { query.Clear(); NodeTypeAliasAttribute attribute = (NodeTypeAliasAttribute)fromClause.ItemType.GetCustomAttributes(typeof(NodeTypeAliasAttribute), false).FirstOrDefault(); if (attribute != null) query.AppendFormat("nodeTypeAlias:{0}", attribute.Name); base.VisitMainFromClause(fromClause, queryModel); }
public override void VisitWhereClause(Remotion.Linq.Clauses.WhereClause whereClause, QueryModel queryModel, int index) { bool alreadyHasQuery = false; var query = new StringBuilder(); queries.Add(query); ExpressionTreeVisitor visitor = new ExpressionTreeVisitor(query); visitor.VisitExpression(whereClause.Predicate); base.VisitWhereClause(whereClause, queryModel, index); }
public override void VisitMainFromClause(Remotion.Linq.Clauses.MainFromClause fromClause, QueryModel queryModel) { queries = new List<StringBuilder>(); NodeTypeAliasAttribute attribute = (NodeTypeAliasAttribute)fromClause.ItemType.GetCustomAttributes(typeof(NodeTypeAliasAttribute), false).FirstOrDefault(); if (attribute != null) { queries.Add(new StringBuilder(string.Format("nodeTypeAlias:{0}", attribute.Name))); } base.VisitMainFromClause(fromClause, queryModel); }
/// <summary> /// Put the result operators in there, one to a line. /// </summary> /// <param name="resultOperator"></param> /// <param name="queryModel"></param> /// <param name="index"></param> public override void VisitResultOperator(Remotion.Linq.Clauses.ResultOperatorBase resultOperator, QueryModel queryModel, int index) { bool first = true; _queryAsString.AppendFormat("{0}(", resultOperator.GetType().Name); resultOperator.TransformExpressions(e => { string comma = ", "; if (first) { comma = ""; first = false; } _queryAsString.AppendFormat("{0}{1}", comma, ExpressionStringConverter.Format(e)); return e; }); _queryAsString.AppendLine(")"); }
/// <summary>Default constructor with base <see cref="System.Linq.Expressions.MemberExpression" />, <see cref="IPropertyMapping" /> and <see cref="Remotion.Linq.Clauses.FromClauseBase" />.</summary> /// <param name="expression">Base member expression.</param> /// <param name="propertyMapping">Ontology property mapping.</param> /// <param name="target">Target of the invocation.</param> /// <param name="name">Name of the element in the property.</param> internal EntityPropertyExpression(System.Linq.Expressions.MemberExpression expression, IPropertyMapping propertyMapping, Remotion.Linq.Clauses.FromClauseBase target, string name) : base() { if (!(expression.Member is PropertyInfo)) { throw new ArgumentOutOfRangeException("expression"); } if (name.Length == 0) { throw new ArgumentOutOfRangeException("name"); } _expression = expression; _propertyMapping = propertyMapping; _target = target; _name = name; }
/// <summary> /// Creates a new <see cref="QueryModelVisitor"/>, and returns the result of running <see cref="QueryModelVisitor.VisitAndGenerateQueryDescription"/> on the /// supplied <paramref name="queryModel"/>. /// </summary> /// <param name="queryModel">The query model.</param> /// <returns></returns> /// <remarks></remarks> public static QueryDescription FromQueryModel(Remotion.Linq.QueryModel queryModel) { Func<AbstractQueryStructureBinder> generateBinder = () => { var resultType = queryModel.MainFromClause.ItemType; var structureBinderAttrib = resultType.GetCustomAttributes(typeof(QueryStructureBinderOfTypeAttribute), true).OfType<QueryStructureBinderOfTypeAttribute>().FirstOrDefault(); if (structureBinderAttrib == null) return new DefaultQueryStructureBinder(); return Activator.CreateInstance(structureBinderAttrib.StructureBinder) as AbstractQueryStructureBinder; }; var defaultQueryStructureBinder = generateBinder.Invoke(); return FromQueryModel(queryModel, defaultQueryStructureBinder, new DefaultExpressionTreeVisitor(defaultQueryStructureBinder)); }
/// <summary>Visits a select clause.</summary> /// <param name="selectClause">Select clause to be visited.</param> /// <param name="queryModel">Query model containing given select clause.</param> public override void VisitSelectClause(SelectClause selectClause, Remotion.Linq.QueryModel queryModel) { QuerySourceReferenceExpression querySource = FindQuerySource(selectClause.Selector); _visitor.VisitExpression(selectClause.Selector); QueryComponent component = _visitor.RetrieveComponent(); _query = _visitor.Query; _mainFromComponent = _query.FindAllComponents<StrongEntityAccessor>().Where(item => item.SourceExpression == querySource.ReferencedQuerySource).First(); if (_query.Subject == null) { _query.Subject = _mainFromComponent.About; UnboundConstrain genericConstrain = new UnboundConstrain(new Identifier("s"), new Identifier("p"), new Identifier("o"), _mainFromComponent.SourceExpression.FromExpression); _mainFromComponent.Elements.Insert(0, genericConstrain); _query.Select.Add(genericConstrain); } _subject = _query.Subject; queryModel.MainFromClause.Accept(this, queryModel); _query.Select.Add(_mainFromComponent); VisitBodyClauses(queryModel.BodyClauses, queryModel); VisitResultOperators(queryModel.ResultOperators, queryModel); OverrideSelector(component, selectClause.Selector); }
/// <summary> /// Visits the main from clause. /// </summary> /// <param name="fromClause">From clause.</param> /// <param name="queryModel">The query model.</param> /// <remarks></remarks> public override void VisitMainFromClause(MainFromClause fromClause, Remotion.Linq.QueryModel queryModel) { var sourceAsConstant = queryModel.MainFromClause.FromExpression as ConstantExpression; if (sourceAsConstant != null) { _queryDescription.SetFromClause(HierarchyScope.Indeterminate, FromClause.RevisionStatusNotSpecifiedType); // Tues 1 Nov: _queryDescription.SetResultFilterClause(fromClause.ItemType, ResultFilterType.Sequence, -1); //_queryDescription.AddResultFilter(GetResultType(fromClause, queryModel), ResultFilterType.Sequence, -1); } RunCustomModifiers(queryModel); var sourceAsSubQuery = fromClause.FromExpression as SubQueryExpression; if (sourceAsSubQuery != null) { var subQueryModel = sourceAsSubQuery.QueryModel; RunCustomModifiers(subQueryModel); // When there is a query like "MyList.Skip(5).Count()" then "MyList.Skip(5)" is the MainFromClause // and it's represented as a SubQuery, with "Count()" being the single ResultOperator on the outer query // Since for our purposes we flatten this all because we don't support subqueries, then // go and visit the ResultOperators of the inner query to add them to our single QueryDescription's // ResultFilters list to make sure we catch both Skip and Count in this example. for (int index = 0; index < subQueryModel.ResultOperators.Count; index++) { var resultOperator = subQueryModel.ResultOperators[index]; VisitResultOperator(resultOperator, sourceAsSubQuery.QueryModel, index); } } // If the Where clause is null, we have a request for "get all", but the VisitWhereClause method never runs (thanks Remotion!) // so let's fake it here if (!queryModel.BodyClauses.Any()) { queryModel.BodyClauses.Add(new WhereClause(Expression.Equal(IgnoreExpression, IgnoreExpression))); } base.VisitMainFromClause(fromClause, queryModel); }
/// <summary> /// Do the select clause, one to a line. /// </summary> /// <param name="selectClause"></param> /// <param name="queryModel"></param> public override void VisitSelectClause(Remotion.Linq.Clauses.SelectClause selectClause, QueryModel queryModel) { _queryAsString.AppendLine($"select {ExpressionStringConverter.Format(selectClause.Selector)}"); }
/// <summary> /// Do the from clause, one item to a line. /// </summary> /// <param name="fromClause"></param> /// <param name="queryModel"></param> public override void VisitMainFromClause(Remotion.Linq.Clauses.MainFromClause fromClause, QueryModel queryModel) { _queryAsString.AppendLine($"from {fromClause.ItemType.Name} {fromClause.ItemName} in {ExpressionStringConverter.Format(fromClause.FromExpression)}"); }
/// <summary> /// Deal with an order by, on a seperate line. /// </summary> /// <param name="ordering"></param> /// <param name="queryModel"></param> /// <param name="orderByClause"></param> /// <param name="index"></param> public override void VisitOrdering(Remotion.Linq.Clauses.Ordering ordering, QueryModel queryModel, Remotion.Linq.Clauses.OrderByClause orderByClause, int index) { var d = ordering.OrderingDirection == Remotion.Linq.Clauses.OrderingDirection.Asc ? "asc" : "desc"; _queryAsString.AppendLine($"orderby {ExpressionStringConverter.Format(ordering.Expression)} {d}"); }
/// <summary> /// Creates a new <see cref="QueryModelVisitor"/>, and returns the result of running <see cref="QueryModelVisitor.VisitAndGenerateQueryDescription"/> on the /// supplied <paramref name="queryModel"/>. /// </summary> /// <param name="queryModel">The query model.</param> /// <param name="structureBinder">The structure binder.</param> /// <param name="treeVisitor">The tree visitor.</param> /// <returns></returns> /// <remarks></remarks> public static QueryDescription FromQueryModel(Remotion.Linq.QueryModel queryModel, AbstractQueryStructureBinder structureBinder, AbstractExpressionTreeVisitor treeVisitor) { var elrmv = new QueryModelVisitor(structureBinder, treeVisitor); return elrmv.VisitAndGenerateQueryDescription(queryModel); }
/// <summary> /// We are going after a query model - bounce this back up a level. /// </summary> /// <param name="expression"></param> /// <returns></returns> protected override System.Linq.Expressions.Expression VisitSubQuery(Remotion.Linq.Clauses.Expressions.SubQueryExpression expression) { _qmVisitor.VisitQueryModel(expression.QueryModel); return expression; }
public override void VisitJoinClause(Remotion.Linq.Clauses.JoinClause joinClause, QueryModel queryModel, int index) { _isInstanceQuery = false; _queryBuilder.AddFromPart(joinClause); var inner = SparqlGeneratorWhereExpressionTreeVisitor.GetSparqlExpression(joinClause.InnerKeySelector, _queryBuilder); var outer = SparqlGeneratorWhereExpressionTreeVisitor.GetSparqlExpression(joinClause.OuterKeySelector, _queryBuilder); if (inner is SelectVariableNameExpression && outer is SelectVariableNameExpression) { var innerVar = inner as SelectVariableNameExpression; var outerVar = outer as SelectVariableNameExpression; if (innerVar.BindingType == VariableBindingType.Literal || outerVar.BindingType == VariableBindingType.Literal) { _queryBuilder.AddFilterExpression(string.Format("(?{0}=?{1})", innerVar.Name, outerVar.Name)); } else { _queryBuilder.AddFilterExpression(string.Format("(sameTerm(?{0}, ?{1}))", innerVar.Name, outerVar.Name)); } } else { throw new NotSupportedException( String.Format("No support for joining expressions of type {0} and {1}", inner.NodeType, outer.NodeType)); } }
/// <summary>Default constructor with aboutness and its source passed.</summary> /// <param name="about">Specifies an entity identifier given accesor uses.</param> /// <param name="sourceExpression">Source of this entity accessor.</param> /// <param name="entityAccessor">Strong entity accessor.</param> internal UnspecifiedEntityAccessor(Identifier about, Remotion.Linq.Clauses.FromClauseBase sourceExpression, StrongEntityAccessor entityAccessor) : base(about, sourceExpression) { _entityAccessor = entityAccessor; }
public override void VisitSelectClause(Remotion.Linq.Clauses.SelectClause selectClause, QueryModel queryModel) { // referenceClaused -> fromCluase1 SparqlGeneratorSelectExpressionTreeVisitor.GetSparqlExpression(selectClause.Selector, _queryBuilder); base.VisitSelectClause(selectClause, queryModel); }
public override void VisitWhereClause(Remotion.Linq.Clauses.WhereClause whereClause, QueryModel queryModel, int index) { SparqlGeneratorWhereExpressionTreeVisitor.GetSparqlExpression(whereClause.Predicate, _queryBuilder); base.VisitWhereClause(whereClause, queryModel, index); }
/// <summary> /// Visits the result operator. /// </summary> /// <param name="resultOperator">The result operator.</param> /// <param name="queryModel">The query model.</param> /// <param name="index">The index.</param> /// <remarks></remarks> public override void VisitResultOperator(ResultOperatorBase resultOperator, Remotion.Linq.QueryModel queryModel, int index) { if (typeof(CountResultOperator).IsAssignableFrom(resultOperator.GetType())) { //_queryDescription.SetResultFilterClause(queryModel.MainFromClause.ItemType, ResultFilterType.Count, 0); _queryDescription.AddResultFilter(queryModel.MainFromClause.ItemType, ResultFilterType.Count, 0); } if (typeof(SingleResultOperator).IsAssignableFrom(resultOperator.GetType())) { var op = resultOperator as SingleResultOperator; var filter = op.ReturnDefaultWhenEmpty ? ResultFilterType.SingleOrDefault : ResultFilterType.Single; //_queryDescription.SetResultFilterClause(queryModel.MainFromClause.ItemType, filter, 0); _queryDescription.AddResultFilter(queryModel.MainFromClause.ItemType, filter, 0); } if (typeof(TakeResultOperator).IsAssignableFrom(resultOperator.GetType())) { var firstResultOperator = resultOperator as TakeResultOperator; var countExpression = firstResultOperator.Count as ConstantExpression; var count = Convert.ToInt32(countExpression.Value); //_queryDescription.SetTakeResultFilter(queryModel.MainFromClause.ItemType, count); _queryDescription.AddTakeResultFilter(queryModel.MainFromClause.ItemType, count); } var skipResultOp = resultOperator as SkipResultOperator; if (skipResultOp != null) { var countExpression = skipResultOp.Count as ConstantExpression; var count = Convert.ToInt32(countExpression.Value); //_queryDescription.SetSkipResultFilter(queryModel.MainFromClause.ItemType, count); _queryDescription.AddSkipResultFilter(queryModel.MainFromClause.ItemType, count); } if (resultOperator is AnyResultOperator) { //_queryDescription.SetResultFilterClause(queryModel.MainFromClause.ItemType, ResultFilterType.Any, 0); _queryDescription.AddResultFilter(queryModel.MainFromClause.ItemType, ResultFilterType.Any, 0); } var allResultOp = resultOperator as AllResultOperator; if (allResultOp != null) { var criteriaExpression = this.GetCriteriaExpression(queryModel, allResultOp.Predicate); _queryDescription.SetCriteria(criteriaExpression); //_queryDescription.SetResultFilterClause(queryModel.MainFromClause.ItemType, ResultFilterType.All, 0); _queryDescription.AddResultFilter(queryModel.MainFromClause.ItemType, ResultFilterType.All, 0); } var firstResultOp = resultOperator as FirstResultOperator; if (firstResultOp != null) { //this._queryDescription.SetResultFilterClause( // queryModel.MainFromClause.ItemType, // firstResultOp.ReturnDefaultWhenEmpty ? ResultFilterType.FirstOrDefault : ResultFilterType.First, // 0); this._queryDescription.AddResultFilter( queryModel.MainFromClause.ItemType, firstResultOp.ReturnDefaultWhenEmpty ? ResultFilterType.FirstOrDefault : ResultFilterType.First, 0); } var lastResultOp = resultOperator as LastResultOperator; if (lastResultOp != null) { this._queryDescription.AddResultFilter( queryModel.MainFromClause.ItemType, lastResultOp.ReturnDefaultWhenEmpty ? ResultFilterType.LastOrDefault : ResultFilterType.Last, 0); } base.VisitResultOperator(resultOperator, queryModel, index); }
public override void VisitMainFromClause(Remotion.Linq.Clauses.MainFromClause fromClause, QueryModel queryModel) { if (fromClause.FromExpression is SubQueryExpression) { var subquery = fromClause.FromExpression as SubQueryExpression; VisitQueryModel(subquery.QueryModel); } else { _queryBuilder.AddFromPart(fromClause); } base.VisitMainFromClause(fromClause, queryModel); }
/// <summary> /// Visits the select clause. /// </summary> /// <param name="selectClause">The select clause.</param> /// <param name="queryModel">The query model.</param> /// <remarks></remarks> public override void VisitSelectClause(SelectClause selectClause, Remotion.Linq.QueryModel queryModel) { // Assess for stuff like Count, Top etc. }
/// <summary> /// A where clause, one to a line. /// </summary> /// <param name="whereClause"></param> /// <param name="queryModel"></param> /// <param name="index"></param> public override void VisitWhereClause(Remotion.Linq.Clauses.WhereClause whereClause, QueryModel queryModel, int index) { _queryAsString.AppendLine($"where {ExpressionStringConverter.Format(whereClause.Predicate)}"); }
public override void VisitOrdering(Remotion.Linq.Clauses.Ordering ordering, QueryModel queryModel, Remotion.Linq.Clauses.OrderByClause orderByClause, int index) { _isInstanceQuery = false; var selector = SparqlGeneratorWhereExpressionTreeVisitor.GetSparqlExpression(ordering.Expression, _queryBuilder); if (selector is SelectVariableNameExpression) { var selectorVar = selector as SelectVariableNameExpression; _queryBuilder.AddOrdering(new SparqlOrdering("?" + selectorVar.Name, ordering.OrderingDirection)); } else { throw new NotSupportedException( String.Format("LINQ-to-SPARQL does not currently support ordering by the expression '{0}'", ordering.Expression)); } base.VisitOrdering(ordering, queryModel, orderByClause, index); }
public static OrderingDirection Get(Remotion.Linq.Clauses.OrderingDirection direction) { return direction == Remotion.Linq.Clauses.OrderingDirection.Asc ? OrderingDirection.Ascending : OrderingDirection.Descending; }
public override void VisitResultOperator(Remotion.Linq.Clauses.ResultOperatorBase resultOperator, QueryModel queryModel, int index) { if (index > 0) _isInstanceQuery = false; if (resultOperator is TakeResultOperator) { var take = resultOperator as TakeResultOperator; _queryBuilder.Limit = take.GetConstantCount(); if (_queryBuilder.Limit != 1) _isInstanceQuery = false; return; } if (resultOperator is SkipResultOperator) { var skip = resultOperator as SkipResultOperator; _queryBuilder.Offset = skip.GetConstantCount(); return; } if (resultOperator is AverageResultOperator && index == 0) { var varName = GetExpressionVariable(queryModel.SelectClause.Selector); if (varName != null) { _queryBuilder.ApplyAggregate("AVG", varName); return; } } if (resultOperator is SumResultOperator && index == 0) { var varName = GetExpressionVariable(queryModel.SelectClause.Selector); if (varName != null) { _queryBuilder.ApplyAggregate("SUM", varName); return; } } if (resultOperator is CountResultOperator && index == 0) { var varName = GetExpressionVariable(queryModel.SelectClause.Selector); if (varName != null) { _queryBuilder.ApplyAggregate("COUNT", varName); return; } } if (resultOperator is LongCountResultOperator && index == 0) { var varName = GetExpressionVariable(queryModel.SelectClause.Selector); if (varName != null) { _queryBuilder.ApplyAggregate("COUNT", varName); return; } } if (resultOperator is MinResultOperator && index == 0) { var varName = GetExpressionVariable(queryModel.SelectClause.Selector); if (varName != null) { _queryBuilder.ApplyAggregate("MIN", varName); return; } } if (resultOperator is MaxResultOperator && index == 0) { var varName = GetExpressionVariable(queryModel.SelectClause.Selector); if (varName != null) { _queryBuilder.ApplyAggregate("MAX", varName); return; } } if (resultOperator is SingleResultOperator && index == 0) { // Grab first 2 rows. If there are two then the outer wrapper will fail. _queryBuilder.Limit = 2; return; } if (resultOperator is FirstResultOperator) { _queryBuilder.Limit = 1; return; } if (resultOperator is OfTypeResultOperator) { var ofType = resultOperator as OfTypeResultOperator; var varName = GetExpressionVariable(queryModel.SelectClause.Selector); if (varName != null) { var typeUri = _queryBuilder.Context.MapTypeToUri(ofType.SearchedItemType); if (typeUri != null) { _queryBuilder.AddTripleConstraint( GraphNode.Variable, varName, GraphNode.Raw, "a", GraphNode.Iri, typeUri); return; } throw new EntityFrameworkException("No URI mapping found for type '{0}'", ofType.SearchedItemType); } } if (resultOperator is DistinctResultOperator) { _queryBuilder.IsDistinct = true; return; } if (resultOperator is GroupResultOperator) { var groupOperator = resultOperator as GroupResultOperator; var keyExpr = groupOperator.KeySelector; var exprVar = GetExpressionVariable(keyExpr); if (exprVar == null) { throw new EntityFrameworkException("Unable to convert GroupBy '{0}' operator to SPARQL.", groupOperator); } _queryBuilder.AddGroupByExpression("?" + exprVar); return; } if (resultOperator is CastResultOperator) { var castOperator = resultOperator as CastResultOperator; var mappedUri = _queryBuilder.Context.MapTypeToUri(castOperator.CastItemType); if (mappedUri == null) { throw new EntityFrameworkException("Unable to cast to type '{0}' as it is not a valid entity type.", castOperator.CastItemType); } return; } if (index > 0) { throw new NotSupportedException( String.Format( "LINQ-to-SPARQL does not currently support the result operator '{0}' as a second or subsequent result operator.", resultOperator)); } throw new NotSupportedException( String.Format("LINQ-to-SPARQL does not currently support the result operator '{0}'", resultOperator)); }
public override void VisitAdditionalFromClause(Remotion.Linq.Clauses.AdditionalFromClause fromClause, QueryModel queryModel, int index) { _isInstanceQuery = false; var fromVar = _queryBuilder.AddFromPart(fromClause); if (!(fromClause.FromExpression is ConstantExpression)) { var fromExpression = SparqlGeneratorWhereExpressionTreeVisitor.GetSparqlExpression(fromClause.FromExpression, _queryBuilder); if (fromExpression is SelectVariableNameExpression) { _queryBuilder.RenameVariable((fromExpression as SelectVariableNameExpression).Name,fromVar); } } base.VisitAdditionalFromClause(fromClause, queryModel, index); }
/// <summary> /// Visits the where clause. /// </summary> /// <param name="whereClause">The where clause.</param> /// <param name="queryModel">The query model.</param> /// <param name="index">The index.</param> /// <remarks></remarks> public override void VisitWhereClause(WhereClause whereClause, Remotion.Linq.QueryModel queryModel, int index) { var wherePredicate = whereClause.Predicate; var criteriaExpression = this.GetCriteriaExpression(queryModel, wherePredicate); _queryDescription.SetCriteria(criteriaExpression); base.VisitWhereClause(whereClause, queryModel, index); }