/// <summary>Visit an identifier entity accessor.</summary>
 /// <param name="entityAccessor">Identifier entity accessor to be visited.</param>
 protected override void VisitIdentifierEntityAccessor(IdentifierEntityAccessor entityAccessor)
 {
     Call predicateBind = new Call(MethodNames.Bind);
     predicateBind.Arguments.Add(new Alias(new Literal(Rdf.predicate), new Identifier(entityAccessor.About.Name + "_Predicate")));
     Call objectBind = new Call(MethodNames.Bind);
     objectBind.Arguments.Add(new Alias(new Literal(Rdf.@object), new Identifier(entityAccessor.About.Name + "_Object")));
     _injectedComponents.Add(predicateBind);
     _injectedComponents.Add(objectBind);
     VisitStrongEntityAccessor(entityAccessor.EntityAccessor);
     _injectedComponents.Clear();
 }
 /// <summary>Visit a function call.</summary>
 /// <param name="call">Function call to be visited.</param>
 protected abstract void VisitCall(Call call);
        /// <summary>Visit a function call.</summary>
        /// <param name="call">Function call to be visited.</param>
        protected override void VisitCall(Call call)
        {
            string functionName = call.Member.ToString().ToUpper();
            string openingBracket = "(";
            string closingBracket = ")";
            string separator = String.Empty;
            string targetAccessor = String.Empty;
            ICollection<IExpression> arguments = call.Arguments;
            IExpression target = null;
            switch (call.Member)
            {
                case MethodNames.Any:
                    openingBracket = closingBracket = " ";
                    goto default;
                case MethodNames.In:
                    targetAccessor = separator = " ";
                    target = (call.Arguments.Count > 0 ? call.Arguments.First() : null);
                    arguments = (call.Arguments.Count > 1 ? call.Arguments.Skip(1).ToList() : new List<IExpression>());
                    goto default;
                case MethodNames.RandomInt:
                    closingBracket += "*1000000000";
                    goto default;
                default:
                    if (MethodNameMap.ContainsKey(call.Member))
                    {
                        functionName = MethodNameMap[call.Member];
                    }

                    break;
            }

            if (target != null)
            {
                VisitComponent(target);
            }

            _commandText.AppendFormat("{0}{1}{2}{3}", targetAccessor, functionName, separator, openingBracket);
            int index = 0;
            foreach (IExpression argument in arguments)
            {
                VisitComponent(argument);
                if (index < arguments.Count - 1)
                {
                    _commandText.Append(",");
                }

                index++;
            }

            _commandText.AppendFormat("{0}", closingBracket);
        }
 private void VisitContainsResultOperator(ContainsResultOperator containsResultOperator, Remotion.Linq.QueryModel queryModel, int index)
 {
     Call call = new Call(MethodNames.In);
     call.Arguments.Add(Identifier.Current);
     _result = call;
 }
        private void VisitAnyResultOperator(AnyResultOperator anyResultOperator, Remotion.Linq.QueryModel queryModel, int index)
        {
            string targetIdentifierString = _visitor.Query.CreateIdentifier(_visitor.ItemNameOverride);
            IList<EntityConstrain> entityConstrains = null;
            StrongEntityAccessor entityAccessor = (from accessor in _visitor.Query.GetQueryComponentNavigator().FindAllComponents<StrongEntityAccessor>()
                                                   let constrains = accessor.Elements.OfType<EntityConstrain>()
                                                   from constrain in constrains
                                                   let predicate = constrain.Predicate as Literal
                                                   where predicate != null
                                                   let predicateUri = (Uri)predicate.Value
                                                   let identifier = constrain.Value as Identifier
                                                   where identifier != null
                                                   let identifierString = _visitor.Query.RetrieveIdentifier(identifier.Name)
                                                   where identifierString == targetIdentifierString
                                                   where (entityConstrains = constrains.Where(item =>
                                                       (item.Predicate is Literal) && (((Uri)((Literal)item.Predicate).Value).AbsoluteUri == predicateUri.AbsoluteUri)).ToList()).Count > 0
                                                   select accessor).FirstOrDefault();
            if (entityAccessor != null)
            {
                foreach (EntityConstrain entityConstrain in entityConstrains)
                {
                    int indexOf = entityAccessor.Elements.IndexOf(entityConstrain);
                    entityAccessor.Elements.RemoveAt(indexOf);
                    OptionalPattern optional = new OptionalPattern();
                    optional.Patterns.Add(entityConstrain);
                    entityAccessor.Elements.Insert(indexOf, optional);
                }
            }

            Call call = new Call(MethodNames.Bound);
            _result = call;
        }
        private void VisitCountResultOperator(CountResultOperator countResultOperator, Remotion.Linq.QueryModel queryModel, int index)
        {
            Call distinct = new Call(MethodNames.Distinct);
            distinct.Arguments.Add(_mainFromComponent.About);
            UnboundConstrain constrain = (UnboundConstrain)_mainFromComponent.Elements.FirstOrDefault(item => item.GetType() == typeof(UnboundConstrain));
            if (constrain != null)
            {
                _mainFromComponent.Elements.Remove(constrain);
            }

            Call count = new Call(MethodNames.Count);
            count.Arguments.Add(distinct);
            _query.Select.Clear();
            Alias alias = new Alias(count, new Identifier(_query.CreateVariableName(_query.RetrieveIdentifier(_mainFromComponent.About.Name) + "Count"), typeof(int)));
            _query.Select.Add(alias);
        }
        private void VisitContainsResultOperator(ContainsResultOperator containsResultOperator, Remotion.Linq.QueryModel queryModel, int index)
        {
            if (_query.IsSubQuery)
            {
                _visitor.VisitExpression(containsResultOperator.Item);
                QueryComponent item = _visitor.RetrieveComponent();
                if (item is IExpression)
                {
                    Filter filter = new Filter(new BinaryOperator(MethodNames.Equal, _mainFromComponent.About, (IExpression)item));
                    if (!_mainFromComponent.Elements.Contains(filter))
                    {
                        _mainFromComponent.Elements.Add(filter);
                    }
                }

                EntityConstrain constrain = new EntityConstrain(
                    new Identifier(_mainFromComponent.About.Name + "_p"),
                    new Identifier(_mainFromComponent.About.Name + "_o"),
                    containsResultOperator.Item);
                if (!_mainFromComponent.Elements.Contains(constrain))
                {
                    _mainFromComponent.Elements.Add(constrain);
                }

                Call call = new Call(MethodNames.Any);
                call.Arguments.Add(_query);
                _result = call;
            }
            else
            {
                throw new NotSupportedException(System.String.Format("Cannot perform 'Contains' operation on top level query."));
            }
        }
 private void VisitAnyResultOperator(AnyResultOperator anyResultOperator, Remotion.Linq.QueryModel queryModel, int index)
 {
     if (_query.IsSubQuery)
     {
         Call call = new Call(MethodNames.Any);
         call.Arguments.Add(_query);
         _result = call;
     }
     else
     {
         _query.QueryForm = QueryForms.Ask;
     }
 }