Beispiel #1
0
        /// <summary>Visits a type binary expression.</summary>
        /// <param name="expression">Expression to be visited.</param>
        /// <returns>Expression visited</returns>
        protected override System.Linq.Expressions.Expression VisitTypeBinary(System.Linq.Expressions.TypeBinaryExpression expression)
        {
            var classMappings = MappingsRepository.FindMappedClasses(expression.TypeOperand);

            if (classMappings.Any())
            {
                Remotion.Linq.Clauses.FromClauseBase sourceExpression = GetSourceExpression(expression.Expression);
                StrongEntityAccessor entityAccessor = _query.FindAllComponents <StrongEntityAccessor>().FirstOrDefault(item => item.SourceExpression == sourceExpression);
                if (entityAccessor == null)
                {
                    entityAccessor = this.GetEntityAccessor(sourceExpression);
                    _query.Elements.Add(entityAccessor);
                }

                EntityTypeConstrain typeConstrain = this.CreateTypeConstrain(expression.TypeOperand, entityAccessor.SourceExpression.FromExpression);
                _lastComponent = typeConstrain;
                if ((_currentComponent.Count > 0) && (_currentComponent.Peek() is BinaryOperatorNavigator))
                {
                    HandleComponent(typeConstrain);
                }
                else if (!entityAccessor.Elements.Contains(typeConstrain))
                {
                    entityAccessor.Elements.Add(typeConstrain);
                }
            }
            else
            {
                return(base.VisitTypeBinary(expression));
            }

            return(expression);
        }
        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) || ((_fromExpression != null) && (constrain.TargetExpression.EqualsTo(_fromExpression)))
                                                                                                                                                    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;
        }
Beispiel #3
0
        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);
        }
Beispiel #4
0
        /// <summary>Visits an unary expression.</summary>
        /// <param name="expression">Expression to be visited.</param>
        /// <returns>Expression visited</returns>
        protected override System.Linq.Expressions.Expression VisitUnary(System.Linq.Expressions.UnaryExpression expression)
        {
            MethodNames operatorName;

            System.Linq.Expressions.ExpressionType expressionType = expression.NodeType;
            switch (expression.NodeType)
            {
            case System.Linq.Expressions.ExpressionType.Convert:
            case System.Linq.Expressions.ExpressionType.TypeAs:
                return(Visit(expression.Operand));

            default:
                string methodName = Enum.GetNames(typeof(MethodNames)).Where(item => item == expressionType.ToString()).FirstOrDefault();
                if (!System.String.IsNullOrEmpty(methodName))
                {
                    operatorName = (MethodNames)Enum.Parse(typeof(MethodNames), methodName);
                }
                else
                {
                    return(base.VisitUnary(expression));
                }

                break;
            }

            UnaryOperator unaryOperator = new UnaryOperator(operatorName);

            HandleComponent(unaryOperator);

            Visit(expression.Operand);
            CleanupComponent(_lastComponent);

            _lastComponent = unaryOperator;
            return(expression);
        }
        private void VisitContainsResultOperator(ContainsResultOperator containsResultOperator, Remotion.Linq.QueryModel queryModel, int index)
        {
            if (_query.IsSubQuery)
            {
                _visitor.Visit(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."));
            }
        }
Beispiel #6
0
 public override AbstractExprTreePattern GetPattern(QueryComponent queryComponent)
 {
     switch (queryComponent)
     {
         case QueryComponent.Top: //top
             {
                 return new PatternTopSkipOrderBy();
             }
         case QueryComponent.Skip: //skip
             {
                 return new PatternTopSkipOrderBy();
             }
         case QueryComponent.OrderBy: //OrderBy
             {
                 return new PatternTopSkipOrderBy();
             }
         case QueryComponent.SelectSingleEntity: //select single entity
             {
                 return new PatternSelectSingleEntity();
             }
         case QueryComponent.SimpleProjection: //Simple projection
             {
                 return new PatternSimpleProjection();
             }
         default:
             {
                 throw new NotImplementedException();
             }
     }
 }
Beispiel #7
0
        public override AbstractExprTreePattern GetPattern(QueryComponent queryComponent)
        {
            switch (queryComponent)
            {
            case QueryComponent.Top:     //top
            {
                return(new PatternTopSkipOrderBy());
            }

            case QueryComponent.Skip:     //skip
            {
                return(new PatternTopSkipOrderBy());
            }

            case QueryComponent.OrderBy:     //OrderBy
            {
                return(new PatternTopSkipOrderBy());
            }

            case QueryComponent.SelectSingleEntity:     //select single entity
            {
                return(new PatternSelectSingleEntity());
            }

            case QueryComponent.SimpleProjection:     //Simple projection
            {
                return(new PatternSimpleProjection());
            }

            default:
            {
                throw new NotImplementedException();
            }
            }
        }
        private void OverrideSelector(QueryComponent component, System.Linq.Expressions.Expression selector)
        {
            if ((component is Identifier) && (selector is System.Linq.Expressions.MemberExpression))
            {
                System.Linq.Expressions.MemberExpression memberExpression = (System.Linq.Expressions.MemberExpression)selector;
                if (!(memberExpression.Member is PropertyInfo))
                {
                    throw new NotSupportedException(System.String.Format("Selection on members of type '{0}' are not supported.", memberExpression.Member.MemberType));
                }

                PropertyInfo propertyInfo = (PropertyInfo)memberExpression.Member;
                if (typeof(IEntity).IsAssignableFrom(propertyInfo.DeclaringType))
                {
                    _propertyMapping = _entityContext.Mappings.FindPropertyMapping((PropertyInfo)memberExpression.Member);
                    if (typeof(IEntity).IsAssignableFrom(_propertyMapping.ReturnType))
                    {
                        OverrideEntitySelector((Identifier)component);
                    }
                    else
                    {
                        OverrideLiteralSelector(this.GetEntityAccessor((FromClauseBase)FindQuerySource(memberExpression).ReferencedQuerySource));
                    }
                }
            }
            else if (component is StrongEntityAccessor)
            {
                _propertyMapping = IdentifierPropertyMapping.Default;
                OverrideIdentifierSelector((StrongEntityAccessor)component);
            }
        }
 internal NonEntityQueryModelVisitor(EntityQueryVisitor queryVisitor, IExpression mainFromComponent = null)
 {
     _visitor           = queryVisitor;
     _from              = null;
     _mainFromComponent = mainFromComponent;
     _bodies            = new List <QueryComponent>();
 }
        /// <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)
        {
            queryModel.MainFromClause.Accept(this, queryModel);
            QuerySourceReferenceExpression querySource = FindQuerySource(selectClause.Selector);

            _visitor.Visit(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);
                _mainFromComponent.UnboundGraphName = null;
                _query.Select.Add(genericConstrain);
            }

            _subject = _query.Subject;
            _query.Select.Add(_mainFromComponent);
            VisitBodyClauses(queryModel.BodyClauses, queryModel);
            VisitResultOperators(queryModel.ResultOperators, queryModel);
            OverrideSelector(component, selectClause.Selector);
        }
Beispiel #11
0
        /// <summary>Visits a sub-query expression.</summary>
        /// <param name="expression">Expression to be visited.</param>
        /// <returns>Expression visited</returns>
        protected override System.Linq.Expressions.Expression VisitSubQuery(SubQueryExpression expression)
        {
            Remotion.Linq.Clauses.FromClauseBase sourceExpression = (Remotion.Linq.Clauses.FromClauseBase)((QuerySourceReferenceExpression)expression.QueryModel.SelectClause.Selector).ReferencedQuerySource;
            StrongEntityAccessor entityAccessor = (sourceExpression.FromExpression is System.Linq.Expressions.ConstantExpression ? null : this.GetEntityAccessor(sourceExpression));

            if (entityAccessor != null)
            {
                Query query = _query.CreateSubQuery(entityAccessor.About);
                query.AddEntityAccessor(entityAccessor);
                EntityQueryModelVisitor queryModelVisitor = new EntityQueryModelVisitor(query, _entityContext);
                queryModelVisitor.VisitQueryModel(expression.QueryModel);
                _lastComponent = queryModelVisitor.Result;
                HandleComponent(_lastComponent);
            }
            else
            {
                NonEntityQueryModelVisitor       queryModelVisitor = new NonEntityQueryModelVisitor(this, ConstantFromClause);
                Stack <IQueryComponentNavigator> currentComponent  = _currentComponent;
                _currentComponent = new Stack <IQueryComponentNavigator>();
                queryModelVisitor.VisitQueryModel(expression.QueryModel);
                _currentComponent = currentComponent;
                _lastComponent    = queryModelVisitor.Result;
                HandleComponent(_lastComponent);
            }

            return(expression);
        }
Beispiel #12
0
        /// <summary>
        /// Saves a component which does not have an associated artefact.
        /// </summary>
        /// <param name="conceptRef">
        /// the concept which identifies the component
        /// </param>
        /// <param name="value">
        /// the value
        /// </param>
        public void SaveComponent(string conceptRef, string value)
        {
            IDimension     dimension = Utils.GetComponentByName(this._keyFamily, conceptRef);
            IConceptObject concept   = this.GetCachedConcept(dimension);

            this.RemoveComponent(dimension);
            var qc = new QueryComponent(dimension, concept, value);

            this.UpdateComponentIndex(dimension, qc);
        }
Beispiel #13
0
        private void SafeVisitExpression(System.Linq.Expressions.Expression expression)
        {
            Stack <IQueryComponentNavigator> currentStack = _currentComponent;
            QueryComponent lastComponent = _lastComponent;

            _currentComponent = new Stack <IQueryComponentNavigator>();
            Visit(expression);
            _currentComponent = currentStack;
            _lastComponent    = lastComponent;
        }
Beispiel #14
0
        /// <summary>
        /// Saves a component which has an associated artefact (all of them should have).
        /// </summary>
        /// <param name="dimension">
        /// the dimemnsion
        /// </param>
        /// <param name="codes">
        /// the codes
        /// </param>
        public void SaveComponent(IDimension dimension, List <ICode> codes)
        {
            IConceptObject concept = this.GetCachedConcept(dimension);

            this.RemoveComponent(dimension);
            var qc = new QueryComponent(dimension, concept, codes);

            this._codeTreeMap[dimension].CheckCodes(codes);

            this.UpdateComponentIndex(dimension, qc);
        }
Beispiel #15
0
        private string AppendCustomLines(string toReturn, QueryComponent stage)
        {
            var lines = SqlQueryBuilderHelper.GetCustomLinesSQLForStage(this, stage).ToArray();

            if (lines.Any())
            {
                toReturn += Environment.NewLine;
                toReturn += string.Join(Environment.NewLine, lines.Select(l => l.Text));
            }

            return(toReturn);
        }
        /// <summary>Visits a where clause.</summary>
        /// <param name="whereClause">Where clause to be visited.</param>
        /// <param name="queryModel">Query model containing given from clause.</param>
        /// <param name="index">Index of the where clause in the query model.</param>
        public override void VisitWhereClause(WhereClause whereClause, Remotion.Linq.QueryModel queryModel, int index)
        {
            _visitor.Visit(whereClause.Predicate);
            QueryComponent queryComponent = _visitor.RetrieveComponent();

            if (queryComponent != null)
            {
                _bodies.Add(queryComponent);
            }

            base.VisitWhereClause(whereClause, queryModel, index);
        }
Beispiel #17
0
        /// <summary>Visits an Uri property expression called on <see cref="EntityId" />.</summary>
        /// <param name="expression">Expression to be visited.</param>
        /// <returns>Expression visited</returns>
        protected virtual System.Linq.Expressions.Expression VisitEntityIdUri(System.Linq.Expressions.MemberExpression expression)
        {
            bool isInSelectScenario             = (_currentComponent.Count == 0);
            StrongEntityAccessor entityAccessor = (isInSelectScenario ? this.GetEntityAccessor(GetMemberTarget(expression)) : this.GetEntityAccessor(GetSourceExpression(expression)));

            _query.AddEntityAccessor(entityAccessor);

            if (isInSelectScenario)
            {
                _lastComponent = entityAccessor;
            }
            else
            {
                foreach (var entityConstrain in entityAccessor.Elements.OfType <EntityConstrain>())
                {
                    if (entityConstrain.Value is Identifier)
                    {
                        Identifier identifier          = (Identifier)entityConstrain.Value;
                        string     constrainIdentifier = _query.RetrieveIdentifier(identifier.Name);
                        string     propertyIdentifier  = null;
                        Type       propertyType        = null;
                        if (expression.Expression is System.Linq.Expressions.MemberExpression)
                        {
                            PropertyInfo propertyInfo = (PropertyInfo)((System.Linq.Expressions.MemberExpression)expression.Expression).Member;
                            propertyIdentifier = _query.CreateIdentifier(propertyInfo.Name);
                            propertyType       = propertyInfo.PropertyType;
                        }
                        else if (expression.Expression is Remotion.Linq.Clauses.Expressions.QuerySourceReferenceExpression)
                        {
                            Remotion.Linq.Clauses.Expressions.QuerySourceReferenceExpression querySource = (Remotion.Linq.Clauses.Expressions.QuerySourceReferenceExpression)expression.Expression;
                            propertyIdentifier = querySource.ReferencedQuerySource.ItemName;
                            propertyType       = querySource.ReferencedQuerySource.ItemType;
                        }

                        if ((constrainIdentifier == propertyIdentifier) && (identifier.NativeType == propertyType))
                        {
                            _lastComponent = identifier;
                            break;
                        }
                    }
                }

                if (_lastComponent == null)
                {
                    _lastComponent = entityAccessor.About;
                }

                HandleComponent(_lastComponent);
            }

            return(expression);
        }
 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;
     }
 }
        /// <summary>Visits a where clause.</summary>
        /// <param name="whereClause">Where clause to be visited.</param>
        /// <param name="queryModel">Query model containing given from clause.</param>
        /// <param name="index">Index of the where clause in the query model.</param>
        public override void VisitWhereClause(WhereClause whereClause, Remotion.Linq.QueryModel queryModel, int index)
        {
            _visitor.ConstantFromClause = _auxFromComponent;
            _visitor.Visit(whereClause.Predicate);
            QueryComponent           queryComponent          = _visitor.RetrieveComponent();
            IQueryComponentNavigator queryComponentNavigator = queryComponent.GetQueryComponentNavigator();

            if (queryComponentNavigator != null)
            {
                queryComponentNavigator.ReplaceComponent(Identifier.Current, _subject);
            }

            if (queryComponent is QueryElement)
            {
                if ((!(queryComponent is EntityConstrain)) && (!_query.Elements.Contains((QueryElement)queryComponent)))
                {
                    _query.Elements.Add((QueryElement)queryComponent);
                }
            }
            else if (!_query.FindAllComponents <Filter>().Any(item => item.Expression == queryComponent))
            {
                Filter filter = new Filter((IExpression)queryComponent);
                StrongEntityAccessor targetEntityAccessor = null;
                if ((_subject != _mainFromComponent.About) && (queryComponentNavigator.Contains(_subject)))
                {
                    targetEntityAccessor = (from entityAccessor in _query.FindAllComponents <StrongEntityAccessor>()
                                            from constrain in entityAccessor.FindAllComponents <EntityConstrain>()
                                            where (constrain.GetType() == typeof(EntityConstrain)) && (_subject.Equals(constrain.Value))
                                            select entityAccessor).FirstOrDefault();
                }

                if (targetEntityAccessor == null)
                {
                    IEnumerable <StrongEntityAccessor> targetEntityAccessorExression = _query.Elements.OfType <StrongEntityAccessor>();
                    if ((_query.IsSubQuery) || (whereClause.Predicate is Remotion.Linq.Clauses.Expressions.SubQueryExpression))
                    {
                        targetEntityAccessorExression = targetEntityAccessorExression.Except(new StrongEntityAccessor[] { _query.Elements.OfType <StrongEntityAccessor>().LastOrDefault() });
                    }

                    targetEntityAccessor = targetEntityAccessorExression.LastOrDefault() ?? _mainFromComponent;
                }

                if ((!targetEntityAccessor.Elements.Contains(queryComponent)) && (!targetEntityAccessor.Elements.Contains(filter)))
                {
                    targetEntityAccessor.Elements.Add(filter);
                }
            }

            _auxFromComponent = null;
            base.VisitWhereClause(whereClause, queryModel, index);
        }
Beispiel #20
0
        /// <summary>Visits an <see cref="EntityExtensions.Is(RomanticWeb.Entities.IEntity,System.Uri)" /> method call.</summary>
        /// <param name="expression">Expression to be visited.</param>
        /// <returns>Returns visited expression.</returns>
        protected virtual System.Linq.Expressions.Expression VisitIsMethodCall(System.Linq.Expressions.MethodCallExpression expression)
        {
            object objectValue = ((System.Linq.Expressions.ConstantExpression)expression.Arguments[1]).Value;

            if (objectValue != null)
            {
                if (objectValue is Uri)
                {
                    objectValue = new EntityId[] { new EntityId((Uri)objectValue) };
                }
                else if (objectValue is EntityId)
                {
                    objectValue = new EntityId[] { (EntityId)objectValue };
                }
                else if ((objectValue is IEnumerable) && (objectValue.GetType().FindItemType() == typeof(Uri)))
                {
                    objectValue = ((IEnumerable)objectValue).Cast <Uri>().Select(item => new EntityId(item));
                }

                if ((objectValue is IEnumerable) && (objectValue.GetType().FindItemType() == typeof(EntityId)))
                {
                    var types = (IEnumerable <EntityId>)objectValue;
                    int count = types.Count();
                    if (count > 0)
                    {
                        StrongEntityAccessor entityAccessor = this.GetEntityAccessor(this.GetSourceExpression(expression.Arguments[0]));
                        if (entityAccessor != null)
                        {
                            EntityTypeConstrain constrain = new EntityTypeConstrain(
                                types.Select(item => item.Uri).First(),
                                entityAccessor.SourceExpression.FromExpression,
                                (count > 1 ? types.Skip(1).Select(item => item.Uri).ToArray() : new Uri[0]));
                            entityAccessor.Elements.Add(constrain);
                            HandleComponent(constrain);
                            _lastComponent = constrain;
                        }
                    }

                    return(expression);
                }
                else
                {
                    return(base.VisitMethodCall(expression));
                }
            }
            else
            {
                return(base.VisitMethodCall(expression));
            }
        }
 /// <summary>Visits order by clauses.</summary>
 /// <param name="orderings">Order by clauses to be visited.</param>
 /// <param name="queryModel">Query model containing given body clause.</param>
 /// <param name="orderByClause">First order by clause.</param>
 protected override void VisitOrderings(ObservableCollection <Ordering> orderings, QueryModel queryModel, OrderByClause orderByClause)
 {
     foreach (Ordering ordering in orderings)
     {
         _visitor.Visit(ordering.Expression);
         QueryComponent component = _visitor.RetrieveComponent();
         if (component is IExpression)
         {
             IExpression expression = (IExpression)component;
             if (!_query.OrderBy.ContainsKey(expression))
             {
                 _query.OrderBy[expression] = (ordering.OrderingDirection == OrderingDirection.Desc);
             }
         }
     }
 }
Beispiel #22
0
        /// <summary>
        /// Setter for the time dimension
        /// </summary>
        /// <param name="startYear">
        /// The start year
        /// </param>
        /// <param name="endYear">
        /// The end year
        /// </param>
        /// <param name="startDate">
        /// The start date
        /// </param>
        /// <param name="endDate">
        /// The end date
        /// </param>
        public void SetTimeComponent(string startDate, string endDate)
        {
            IDimension     dimension = this.KeyFamily.TimeDimension;
            IConceptObject concept   = this.GetCachedConcept(dimension);

            if (dimension == null)
            {
                throw new NsiClientException(Resources.ExceptionNoTimeDimension);
            }

            this.RemoveComponent(dimension);
            if (!string.IsNullOrEmpty(startDate))
            {
                var qc = new QueryComponent(dimension, concept, startDate, endDate);
                this.UpdateComponentIndex(dimension, qc);
            }
        }
Beispiel #23
0
        private void CleanupComponent(QueryComponent component)
        {
            IQueryComponentNavigator queryComponentNavigator = _lastComponent.GetQueryComponentNavigator();

            if (queryComponentNavigator != null)
            {
                while ((_currentComponent.Count > 0) && (!_currentComponent.Peek().Equals(queryComponentNavigator)))
                {
                    _currentComponent.Pop();
                }

                if (_currentComponent.Count > 0)
                {
                    _currentComponent.Pop();
                }
            }
        }
Beispiel #24
0
        private DbService(SqlConnection connection, ILogger logger = null, bool useNLog = false)
        {
            Lock = new object();

            Connection = connection;

            Logger = logger ?? (useNLog ? new NLogWrapper(LogManager.GetCurrentClassLogger()) : null);
            // TODO: modifiy CurrentClass Logger -> append Type-Parameter

            Logger?.Info($"Creating DbService for entity {typeof(T).FullName}");

            ModelBuilder = new ModelBuilder(typeof(T), logger, useNLog);
            EntityModel  = ModelBuilder.EntityModel;

            QueryComponent            = new QueryComponent <T>(this, EntityModel);
            DataManipulationComponent = new DataManipulationComponent <T>(this, EntityModel);
            ScriptGenerator           = ModelBuilder.ScriptGenerator;
            EntityParser = new EntityParser <T>(EntityModel);
        }
Beispiel #25
0
        /// <summary>Visits a binary expression.</summary>
        /// <param name="expression">Expression to be visited.</param>
        /// <returns>Expression visited</returns>
        protected override System.Linq.Expressions.Expression VisitBinary(System.Linq.Expressions.BinaryExpression expression)
        {
            MethodNames operatorName;

            System.Linq.Expressions.ExpressionType expressionType = expression.NodeType;
            switch (expression.NodeType)
            {
            case System.Linq.Expressions.ExpressionType.OrElse:
                expressionType = System.Linq.Expressions.ExpressionType.Or;
                goto default;

            case System.Linq.Expressions.ExpressionType.AndAlso:
                expressionType = System.Linq.Expressions.ExpressionType.And;
                goto default;

            default:
                string methodName = Enum.GetNames(typeof(MethodNames)).Where(item => item == expressionType.ToString()).FirstOrDefault();
                if (!System.String.IsNullOrEmpty(methodName))
                {
                    operatorName = (MethodNames)Enum.Parse(typeof(MethodNames), methodName);
                }
                else
                {
                    return(base.VisitBinary(expression));
                }

                break;
            }

            BinaryOperator binaryOperator = new BinaryOperator(operatorName);

            HandleComponent(binaryOperator);

            Visit(expression.Left);
            CleanupComponent(_lastComponent);

            Visit(expression.Right);
            CleanupComponent(_lastComponent);

            _lastComponent = binaryOperator;
            return(expression);
        }
        private void VisitContainsResultOperator(ContainsResultOperator containsResultOperator, Remotion.Linq.QueryModel queryModel, int index)
        {
            Call call = new Call(MethodNames.In);

            System.Linq.Expressions.Expression expression = _visitor.TransformUnaryExpression(containsResultOperator.Item);
            Identifier targetIdentifier = Identifier.Current;

            if (expression is System.Linq.Expressions.MemberExpression)
            {
                System.Linq.Expressions.MemberExpression memberExpression = (System.Linq.Expressions.MemberExpression)expression;
                _visitor.Visit(expression);
                QueryComponent component = _visitor.RetrieveComponent();
                if (component is Identifier)
                {
                    targetIdentifier = (Identifier)component;
                }
            }

            call.Arguments.Add(targetIdentifier);
            _result = call;
        }
Beispiel #27
0
        /// <summary>Visits a constant expression.</summary>
        /// <param name="expression">Expression to be visited.</param>
        /// <returns>Expression visited</returns>
        protected override System.Linq.Expressions.Expression VisitConstant(System.Linq.Expressions.ConstantExpression expression)
        {
            if ((expression.Value is IEnumerable) && (!(expression.Value is string)))
            {
                List        list  = new List();
                IEnumerable value = (IEnumerable)expression.Value;
                foreach (object item in value)
                {
                    if (item is IEntity)
                    {
                        list.Values.Add(new Literal(((IEntity)item).Id.Uri));
                    }
                    else if (item is EntityId)
                    {
                        list.Values.Add(new Literal(((EntityId)item).Uri));
                    }
                    else
                    {
                        list.Values.Add(new Literal(ResolveRelativeUriIfNecessery(item)));
                    }
                }

                _lastComponent = list;
            }
            else if (expression.Value is IEntity)
            {
                HandleComponent(_lastComponent = new Literal(((IEntity)expression.Value).Id.Uri));
            }
            else if (expression.Value != null)
            {
                HandleComponent(_lastComponent = new Literal(ResolveRelativeUriIfNecessery(expression.Value)));
            }
            else
            {
                Type valueType = FindLiteralType();
                HandleComponent(_lastComponent = new Literal(valueType));
            }

            return(expression);
        }
Beispiel #28
0
        /// <summary>Visits a query source expression.</summary>
        /// <param name="expression">Expression to be visited.</param>
        /// <returns>Expression visited.</returns>
        protected override System.Linq.Expressions.Expression VisitQuerySourceReference(QuerySourceReferenceExpression expression)
        {
            Remotion.Linq.Clauses.FromClauseBase sourceExpression = GetSourceExpression(expression);
            StrongEntityAccessor entityAccessor = this.GetEntityAccessor(sourceExpression);

            if (entityAccessor != null)
            {
                _query.AddEntityAccessor(entityAccessor);
                _lastComponent = _query;
            }
            else
            {
                _lastComponent = (from entityConstrain in _query.FindAllComponents <EntityConstrain>()
                                  where entityConstrain.GetType() == typeof(EntityConstrain)
                                  let identifier = entityConstrain.Value as Identifier
                                                   let targetExpression = ((IQueryVisitor)this).TransformFromExpression(sourceExpression).FromExpression
                                                                          where (identifier != null) && (targetExpression != null) && (entityConstrain.TargetExpression.EqualsTo(targetExpression))
                                                                          select identifier).FirstOrDefault();
                HandleComponent(_lastComponent);
            }

            return(expression);
        }
Beispiel #29
0
        /// <summary>Visits an entity identifier member.</summary>
        /// <param name="expression">Expression to be visited.</param>
        /// <returns>Expression visited</returns>
        protected virtual System.Linq.Expressions.Expression VisitEntityId(EntityIdentifierExpression expression)
        {
            if (_currentComponent.Count > 0)
            {
                StrongEntityAccessor entityAccessor = this.GetEntityAccessor(expression.Target);
                HandleComponent(_lastComponent = entityAccessor.About);
                if (_currentComponent.Peek() is BinaryOperatorNavigator)
                {
                    BinaryOperator binaryOperator = ((BinaryOperatorNavigator)_currentComponent.Peek()).NavigatedComponent;
                    Filter         filter         = new Filter(binaryOperator);
                    _query.AddEntityAccessor(entityAccessor);
                    entityAccessor.Elements.Add(filter);
                    _currentComponent.Push(filter.GetQueryComponentNavigator());
                    _lastComponent = filter;
                }
            }
            else
            {
                _lastComponent = this.GetEntityAccessor(expression.Target).About;
            }

            return(expression);
        }
Beispiel #30
0
        /// <summary>Visits an entity member.</summary>
        /// <param name="expression">Expression to be visited.</param>
        /// <returns>Expression visited</returns>
        protected virtual System.Linq.Expressions.Expression VisitEntityProperty(EntityPropertyExpression expression)
        {
            StrongEntityAccessor entityAccessor = this.GetEntityAccessor(expression.Target);

            _query.AddEntityAccessor(entityAccessor);
            Identifier      memberIdentifier = null;
            EntityConstrain constrain        = _query.FindAllComponents <EntityConstrain>()
                                               .FirstOrDefault(item => (item.GetType() == typeof(EntityConstrain) && (item.TargetExpression.EqualsTo(expression.Expression))));

            if (constrain == null)
            {
                memberIdentifier = (_query.FindAllComponents <StrongEntityAccessor>()
                                    .Where(item => item.SourceExpression.FromExpression.EqualsTo(expression.Expression))
                                    .Select(item => item.About).FirstOrDefault()) ?? (new Identifier(_query.CreateVariableName(expression.Name), expression.EntityProperty.PropertyType));
                constrain = new EntityConstrain(new Literal(expression.PropertyMapping.Uri), memberIdentifier, expression.Expression);
                if (!entityAccessor.Elements.Contains(constrain))
                {
                    entityAccessor.Elements.Add(constrain);
                }
            }
            else
            {
                if (constrain.ShouldBeOptional(_currentComponent))
                {
                    entityAccessor.Elements.Remove(constrain);
                    OptionalPattern optional = new OptionalPattern();
                    optional.Patterns.Add(constrain);
                    entityAccessor.Elements.Add(optional);
                }

                memberIdentifier = (Identifier)constrain.Value;
            }

            HandleComponent(memberIdentifier);
            _lastComponent = memberIdentifier;
            return(expression);
        }
Beispiel #31
0
        private void HandleComponent(QueryComponent component)
        {
            if (_currentComponent.Count > 0)
            {
                if (_currentComponent.Peek().CanAddComponent(component))
                {
                    if (!_currentComponent.Peek().ContainsComponent(component))
                    {
                        _currentComponent.Peek().AddComponent(component);
                    }
                }
                else
                {
                    _currentComponent.Pop();
                }
            }

            IQueryComponentNavigator queryComponentNavigator = component.GetQueryComponentNavigator();

            if (queryComponentNavigator != null)
            {
                _currentComponent.Push(queryComponentNavigator);
            }
        }
Beispiel #32
0
 public abstract AbstractExprTreePattern GetPattern(QueryComponent queryComponent);