/// <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); }
private void ProcessSelectableConditionalConstrainSelector(ConditionalConstrainSelector expression, bool isSubQuery, Func <string, string> createVariableName, StrongEntityAccessor mainEntityAccessor) { if (!isSubQuery) { if (mainEntityAccessor != null) { _currentStrongEntityAccessorVisitDelegate = entityAccessor => { if (_metaGraphVariableName == null) { _commandText.AppendFormat("?G{0}", entityAccessor.About.Name); } else { _commandText.Append("IF(ISBLANK("); VisitComponent(entityAccessor.About); _commandText.Append("),"); VisitComponent(mainEntityAccessor.About); _commandText.Append(","); VisitComponent(entityAccessor.About); _commandText.AppendFormat(") AS ?{0} ?{1}", _ownerVariableName, _entityVariableName); } }; _ownerVariableName = "entity"; } _commandText.Append("DISTINCT "); int index = 0; foreach (IExpression selectable in ((ISelectableQueryComponent)expression).Expressions) { switch (index) { case 0: _subjectVariableName = (selectable is Alias ? ((Alias)selectable).Name.Name : (selectable is Identifier ? ((Identifier)selectable).Name : null)); break; case 1: _predicateVariableName = (selectable is Alias ? ((Alias)selectable).Name.Name : (selectable is Identifier ? ((Identifier)selectable).Name : null)); break; case 2: _objectVariableName = (selectable is Alias ? ((Alias)selectable).Name.Name : (selectable is Identifier ? ((Identifier)selectable).Name : null)); break; } index++; } } }
private void ProcessSelectableIdentifier(Identifier expression, bool isSubQuery, Func <string, string> createVariableName, StrongEntityAccessor mainEntityAccessor) { if (!isSubQuery) { if (_subjectVariableName == null) { _subjectVariableName = expression.Name; } else if (_predicateVariableName == null) { _predicateVariableName = expression.Name; } else if (_objectVariableName == null) { _objectVariableName = expression.Name; } else if ((_entityVariableName == null) || (_entityVariableName == _ownerVariableName)) { _entityVariableName = expression.Name; } } }
private void ProcessSelectableAlias(Alias expression, bool isSubQuery, Func <string, string> createVariableName, StrongEntityAccessor mainEntityAccessor) { if ((!isSubQuery) && (_scalarVariableName == null) && (expression.Name != null)) { _scalarVariableName = expression.Name.Name; } }
private void OverrideIdentifierSelector(StrongEntityAccessor entityAccessor) { IdentifierEntityAccessor identifierEntityAccessor = new IdentifierEntityAccessor(entityAccessor.About, entityAccessor); int indexOf = -1; if ((indexOf = _query.Elements.IndexOf(entityAccessor)) != -1) { _query.Elements.RemoveAt(indexOf); _query.Elements.Insert(indexOf, identifierEntityAccessor); } _query.Select.Clear(); _query.Select.Add(identifierEntityAccessor); _mainFromComponent = identifierEntityAccessor; }
/// <summary>Visit a strong entity accessor.</summary> /// <param name="entityAccessor">Strong entity accessor to be visited.</param> protected override void VisitStrongEntityAccessor(StrongEntityAccessor entityAccessor) { int startIndex = _commandText.Length; _currentEntityAccessor.Push(entityAccessor); _commandText.AppendFormat("GRAPH ?G{0} {{ ", entityAccessor.About.Name); foreach (QueryElement element in entityAccessor.Elements) { VisitComponent(element); } foreach (IQueryComponent element in _injectedComponents) { VisitComponent(element); if (element is Call) { _commandText.Append(" "); } } if ((_entityAccessorToExpand != null) && (_entityAccessorToExpand.Equals(entityAccessor))) { int currentStartIndex = _commandText.Length; _entityAccessorToExpand.FindAllComponents<Identifier>().Select(item => _variableNameOverride[item] = item.Name + "_sub").ToList(); _commandText.Append("{ SELECT DISTINCT "); VisitComponent(_entityAccessorToExpand.About); _commandText.AppendFormat(" WHERE {{ GRAPH ?G{0} {{ ", _variableNameOverride[_entityAccessorToExpand.About]); foreach (QueryElement element in entityAccessor.Elements.SkipWhile(item => item is UnboundConstrain)) { VisitComponent(element); } _commandText.AppendFormat("}} GRAPH <{0}> {{ ?G{1} <{2}> ?{1} . }} }} ", MetaGraphUri, _variableNameOverride[_entityAccessorToExpand.About], Foaf.primaryTopic); VisitQueryResultModifiers(_entityAccessorToExpand.OwnerQuery.OrderBy, _entityAccessorToExpand.OwnerQuery.Offset, _entityAccessorToExpand.OwnerQuery.Limit); _commandText.Append("} FILTER ("); VisitComponent(_entityAccessorToExpand.About); _variableNameOverride.Clear(); _commandText.Append("="); VisitComponent(_entityAccessorToExpand.About); _commandText.Append(") "); int currentLength = _commandText.Length - currentStartIndex; string expansionText = _commandText.ToString().Substring(currentStartIndex, currentLength); _commandText = _commandText.Remove(currentStartIndex, currentLength).Insert(startIndex, expansionText); _visitedComponents.Update(startIndex, currentLength); } _commandText.AppendFormat("}} GRAPH <{0}> {{ ?G{1} <{2}> ?{1} . }} ", MetaGraphUri, entityAccessor.About.Name, Foaf.primaryTopic); _currentEntityAccessor.Pop(); }
/// <summary>Default constructor with aboutness assuming that a source is a variable.</summary> /// <param name="about">Points to the primary topic of given entity accessor.</param> /// <param name="entityAccessor">Strong entity accessor.</param> internal UnspecifiedEntityAccessor(Identifier about, StrongEntityAccessor entityAccessor) : base(about) { _entityAccessor = entityAccessor; }
private System.Linq.Expressions.Expression VisitPredicateMethodCallUnsafe(System.Linq.Expressions.MethodCallExpression expression, Uri predicate, StrongEntityAccessor entityAccessor) { _query.AddEntityAccessor(entityAccessor); Identifier memberIdentifier = new Identifier(_query.CreateVariableName(predicate.GetFragmentOrLastSegment())); EntityConstrain constrain = new EntityConstrain(); constrain.Predicate = new Literal(predicate); constrain.Value = memberIdentifier; if (!entityAccessor.Elements.Contains(constrain)) { entityAccessor.Elements.Add(constrain); } if (constrain.ShouldBeOptional(_currentComponent)) { entityAccessor.Elements.Remove(constrain); OptionalPattern optional = new OptionalPattern(); optional.Patterns.Add(constrain); entityAccessor.Elements.Add(optional); } HandleComponent(memberIdentifier); _lastComponent = memberIdentifier; return(expression); }
private void ProcessSelectable(bool isSubQuery, ISelectableQueryComponent expression, Func <string, string> createVariableName, StrongEntityAccessor mainEntityAccessor) { System.Reflection.MethodInfo processSelectable = typeof(GenericSparqlQueryVisitor).GetTypeInfo().GetMethod("ProcessSelectable" + expression.GetType().Name, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); if (processSelectable != null) { processSelectable.Invoke(this, new object[] { expression, isSubQuery, createVariableName, mainEntityAccessor }); } foreach (IExpression selectableExpression in expression.Expressions) { VisitComponent(selectableExpression); _commandText.Append(" "); } }
private void ExpandEntityAccessor(StrongEntityAccessor entityAccessor, ref int startIndex) { int currentStartIndex = _commandText.Length; _entityAccessorToExpand.FindAllComponents <Identifier>().Select(item => _variableNameOverride[item] = item.Name + "_sub").ToList(); bool indentationChanged = false; if (Indentation.Length > 0) { Indentation = Indentation.Substring(0, Indentation.Length - 1); indentationChanged = true; } _commandText.Append(Indentation); _commandText.AppendLine("{ "); Indentation += "\t"; _commandText.Append(Indentation); _commandText.Append("SELECT DISTINCT "); VisitComponent(_entityAccessorToExpand.About); _commandText.AppendLine(); _commandText.Append(Indentation); _commandText.AppendLine("WHERE { "); Indentation += "\t"; _commandText.Append(Indentation); _commandText.AppendFormat("GRAPH ?G{0} {{ ", _variableNameOverride[_entityAccessorToExpand.About]); _commandText.AppendLine(); Indentation += "\t"; foreach (QueryElement element in entityAccessor.Elements.SkipWhile(item => item is UnboundConstrain)) { VisitComponent(element); } if (_entityAccessorToExpand.UnboundGraphName == null) { Indentation = Indentation.Substring(0, Indentation.Length - 1); _commandText.Append(Indentation); _commandText.AppendLine("} "); _commandText.Append(Indentation); _commandText.AppendFormat("GRAPH <{0}> {{ ?G{1} <{2}> ?{1} . }} ", MetaGraphUri, _variableNameOverride[_entityAccessorToExpand.About], Foaf.primaryTopic); _commandText.AppendLine(); Indentation = Indentation.Substring(0, Indentation.Length - 1); _commandText.Append(Indentation); _commandText.AppendLine("} "); } else { Indentation = Indentation.Substring(0, Indentation.Length - 1); _commandText.Append(Indentation); _commandText.AppendLine("} "); Indentation = Indentation.Substring(0, Indentation.Length - 1); _commandText.Append(Indentation); _commandText.AppendLine("} "); } VisitQueryResultModifiers(_entityAccessorToExpand.OwnerQuery.OrderBy, _entityAccessorToExpand.OwnerQuery.Offset, _entityAccessorToExpand.OwnerQuery.Limit); Indentation = Indentation.Substring(0, Indentation.Length - 1); _commandText.Append(Indentation); _commandText.AppendLine("} "); _commandText.Append(Indentation); _commandText.Append("FILTER ("); VisitComponent(_entityAccessorToExpand.About); _variableNameOverride.Clear(); _commandText.Append("="); VisitComponent(_entityAccessorToExpand.About); _commandText.AppendLine(") "); int currentLength = _commandText.Length - currentStartIndex; string expansionText = _commandText.ToString().Substring(currentStartIndex, currentLength); _commandText = _commandText.Remove(currentStartIndex, currentLength).Insert(startIndex, expansionText); _visitedComponents.Update(startIndex, currentLength); if (indentationChanged) { Indentation += "\t"; } }
private void VisitStrongEntityAccessorInternalQuick(StrongEntityAccessor entityAccessor) { VisitComponent(entityAccessor.About); }
/// <summary>Visit a strong entity accessor.</summary> /// <param name="entityAccessor">Strong entity accessor to be visited.</param> protected override void VisitStrongEntityAccessor(StrongEntityAccessor entityAccessor) { _currentStrongEntityAccessorVisitDelegate(entityAccessor); }
private void BeginQuery(bool isSubQuery, QueryForms queryForm, IList <ISelectableQueryComponent> select, Func <string, string> createVariableName, StrongEntityAccessor mainEntityAccessor) { if ((_commandText.Length > 0) && (_commandText[_commandText.Length - 1] != '\n')) { _commandText.AppendLine(); } if (isSubQuery) { Indentation += "\t"; } _commandText.Append(Indentation); _commandText.AppendFormat("{0} ", queryForm.ToString().ToUpper()); if (queryForm == QueryForms.Select) { if (select.Count > 0) { Action <StrongEntityAccessor> temp = _currentStrongEntityAccessorVisitDelegate; foreach (ISelectableQueryComponent expression in select) { ProcessSelectable(isSubQuery, expression, createVariableName, mainEntityAccessor); } _currentStrongEntityAccessorVisitDelegate = temp; } else { _commandText.Append("* "); } } _commandText.AppendLine(); if (queryForm != QueryForms.Ask) { _commandText.Append(Indentation); _commandText.Append("WHERE "); } _commandText.Append("{ "); _commandText.AppendLine(); Indentation += "\t"; }
/// <summary>Default constructor with nagivated entity accessor.</summary> /// <param name="entityAccessor">Nagivated entity accessor.</param> internal StrongEntityAccessorNavigator(StrongEntityAccessor entityAccessor) : base(entityAccessor) { }
private void ProcessSelectableIdentifierEntityAccessor(IdentifierEntityAccessor expression, bool isSubQuery, Func <string, string> createVariableName, StrongEntityAccessor mainEntityAccessor) { if (_currentStrongEntityAccessorVisitDelegate == VisitStrongEntityAccessorInternal) { _currentStrongEntityAccessorVisitDelegate = VisitStrongEntityAccessorInternalQuick; } if (!isSubQuery) { if ((_metaGraphVariableName == null) && (_entityVariableName == null)) { _metaGraphVariableName = String.Format("G{0}", expression.About.Name); _entityVariableName = _ownerVariableName = expression.About.Name + "_Distinct"; } _subjectVariableName = expression.About.Name; _predicateVariableName = expression.About.Name + "_Predicate"; _objectVariableName = expression.About.Name + "_Object"; _commandText.AppendFormat( "DISTINCT(?{2}) AS ?{1} ?{0} ?{3} ?{4} ?{5} ", _metaGraphVariableName, _entityVariableName, expression.About.Name, _predicateVariableName, _objectVariableName, _ownerVariableName); } }
private System.Linq.Expressions.Expression VisitPredicateMethodCallInternal(System.Linq.Expressions.MethodCallExpression expression, Uri predicate, StrongEntityAccessor entityAccessor) { Type type = null; string name = null; if (predicate == Rdf.subject) { name = "Id"; type = typeof(IEntity); } else { IEntityMapping entityMapping = _entityContext.Mappings.MappingFor(expression.Arguments[0].Type); if (entityMapping == null) { return(VisitPredicateMethodCallUnsafe(expression, predicate, entityAccessor)); } IPropertyMapping propertyMapping = entityMapping.Properties.FirstOrDefault(item => item.Uri.AbsoluteUri == predicate.AbsoluteUri); if (propertyMapping == null) { ExceptionHelper.ThrowMappingException(predicate); } if (propertyMapping.DeclaringType != expression.Arguments[0].Type) { propertyMapping = _entityContext.Mappings.MappingFor(propertyMapping.DeclaringType).Properties.FirstOrDefault(item => item.Uri.AbsoluteUri == predicate.AbsoluteUri); if (propertyMapping == null) { ExceptionHelper.ThrowMappingException(predicate); } } type = propertyMapping.EntityMapping.EntityType; name = propertyMapping.Name; } System.Linq.Expressions.MemberExpression propertyExpression = System.Linq.Expressions.Expression.Property(expression.Arguments[0], type, name); return(VisitMember(propertyExpression)); }
private void ProcessSelectableStrongEntityAccessor(StrongEntityAccessor expression, bool isSubQuery, Func <string, string> createVariableName, StrongEntityAccessor mainEntityAccessor) { if (_currentStrongEntityAccessorVisitDelegate == VisitStrongEntityAccessorInternal) { _currentStrongEntityAccessorVisitDelegate = VisitStrongEntityAccessorInternalQuick; } if (!isSubQuery) { if ((expression.UnboundGraphName != null) && (expression.UnboundGraphName != expression.About)) { _metaGraphVariableName = (_metaGraphVariableName ?? "graph"); _entityVariableName = (_entityVariableName ?? expression.About.Name); _ownerVariableName = (_ownerVariableName ?? expression.About.Name); _commandText.AppendFormat("IF(BOUND(?G{0}),?G{0},?G{1}) AS ?{2} ", expression.About.Name, expression.UnboundGraphName.Name, _metaGraphVariableName); } else { _entityVariableName = (_entityVariableName ?? expression.About.Name); _metaGraphVariableName = (_metaGraphVariableName ?? String.Format("G{0}", _entityVariableName)); _ownerVariableName = (_ownerVariableName ?? expression.About.Name); _commandText.AppendFormat("?{0} ?{1} ", _metaGraphVariableName, _entityVariableName); } } }
/// <summary>Visit a strong entity accessor.</summary> /// <param name="entityAccessor">Strong entity accessor to be visited.</param> protected abstract void VisitStrongEntityAccessor(StrongEntityAccessor entityAccessor);
private void ProcessSelectableUnboundConstrain(UnboundConstrain expression, bool isSubQuery, Func <string, string> createVariableName, StrongEntityAccessor mainEntityAccessor) { if ((!isSubQuery) && (expression.Subject is Identifier) && (expression.Predicate is Identifier) && (expression.Value is Identifier)) { _subjectVariableName = ((Identifier)expression.Subject).Name; _predicateVariableName = ((Identifier)expression.Predicate).Name; _objectVariableName = ((Identifier)expression.Value).Name; } }
/// <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; }
private void ProcessSelectableCall(Call expression, bool isSubQuery, Func <string, string> createVariableName, StrongEntityAccessor mainEntityAccessor) { if ((!isSubQuery) && (_scalarVariableName == null)) { _scalarVariableName = createVariableName(expression.Member.ToString()); } }
/// <summary>Visit a query.</summary> /// <param name="query">Query to be visited.</param> public override void VisitQuery(Query query) { InitQuery(query.IsSubQuery ? null : query.Prefixes); BeginQuery(query.IsSubQuery, query.QueryForm, query.Select, query.CreateVariableName); _entityAccessorToExpand = ((query.Limit >= 0) || (query.Offset >= 0) || (query.OrderBy.Count > 0) ? (StrongEntityAccessor)query.Elements.LastOrDefault(item => item is StrongEntityAccessor) : null); foreach (QueryElement element in query.Elements) { VisitComponent(element); } EndQuery(query.IsSubQuery, (_entityAccessorToExpand != null ? query.OrderBy : null)); }
private void OverrideLiteralSelector(StrongEntityAccessor entityAccessor) { if (_mainFromComponent.Elements[0] is UnboundConstrain) { entityAccessor.Elements.Add(new Filter(new BinaryOperator(MethodNames.Equal, ((UnboundConstrain)_mainFromComponent.Elements[0]).Predicate, new Literal(_propertyMapping.Uri)))); } }