Exemplo n.º 1
0
        /// <summary>
        /// Creates the result manager and adds all selected elements
        /// </summary>
        private SelectResultManager CreateResultManager(SecurityToken mySecurityToken, Int64 myTransactionToken, SelectDefinition selectDefinition, Dictionary <String, IVertexType> myTypeList)
        {
            var _SelectResultManager = new SelectResultManager(_graphdb, _pluginManager);

            foreach (var selection in selectDefinition.SelectedElements)
            {
                #region Add all selection elements to SelectResultManager

                if (selection.Item1 is IDChainDefinition)
                {
                    #region IDChainDefinition

                    var idChainSelection = (selection.Item1 as IDChainDefinition);

                    idChainSelection.Validate(_pluginManager, _graphdb, mySecurityToken, myTransactionToken, true);

                    if (idChainSelection.SelectType != TypesOfSelect.None)
                    {
                        #region Asterisk, Minus, Rhomb, Ad

                        //there's no limitation
                        foreach (var typeRef in myTypeList)
                        {
                            if (idChainSelection.TypeName != null)
                            {
                                var type = _graphdb.GetVertexType(
                                    mySecurityToken,
                                    myTransactionToken,
                                    new RequestGetVertexType(idChainSelection.TypeName),
                                    (stats, vType) => vType);

                                if (type == null)
                                {
                                    throw new VertexTypeDoesNotExistException(idChainSelection.TypeName, "");
                                }

                                Int64 typeID = type.ID;
                                _SelectResultManager.AddSelectionType(typeRef.Key, typeRef.Value, idChainSelection.SelectType, typeID);
                            }
                            else
                            {
                                _SelectResultManager.AddSelectionType(typeRef.Key, typeRef.Value, idChainSelection.SelectType);
                            }
                        }

                        #endregion

                        continue;
                    }

                    #region Either IDNode or parameterless function

                    if (idChainSelection.Reference == null)
                    { /// this might be a parameterless function without a calling attribute
                        _SelectResultManager.AddElementToSelection(selection.Item2, null, idChainSelection, false);
                    }
                    else //if (!(aColumnItemNode.ColumnSourceValue is AggregateNode))
                    {
                        IVertexType theType   = null;
                        String      reference = idChainSelection.Reference.Item1;
                        // this will happen, if the user selected FROM User u SELECT Name
                        if (!myTypeList.ContainsKey(reference))
                        {
                            // if there is only one type, than we can treat this as the reference
                            if (myTypeList.Count == 1)
                            {
                                theType = myTypeList.First().Value;
                            }
                            else
                            {
                                throw new Exception("Missing type reference for " + reference);
                            }
                        }
                        else
                        {
                            theType = myTypeList[reference];
                        }

                        //if (theType != null && theType.GetAncestorVertexTypesAndSelf().Select(x => x.ID).Contains(idChainSelection.LastAttribute.RelatedType.ID))
                        //    reference = theType.GetAncestorVertexTypesAndSelf().Where(x => x.ID == idChainSelection.LastAttribute.RelatedType.ID).First().Name;

                        if (idChainSelection.SelectType != TypesOfSelect.None)
                        {
                            _SelectResultManager.AddSelectionType(reference, theType, idChainSelection.SelectType);
                        }
                        else
                        {
                            _SelectResultManager.AddElementToSelection(selection.Item2, reference, idChainSelection, false, selection.Item3);
                        }
                        //else
                        //{
                        //    return new Exceptional<SelectResultManager>(new Error_NotImplemented(new System.Diagnostics.StackTrace(true)));
                        //}
                    }
                    #endregion

                    #endregion
                }

                else if (selection.Item1 is AggregateDefinition)
                {
                    #region Aggregate

                    var aggregateSelection = selection.Item1 as AggregateDefinition;

                    aggregateSelection.ChainPartAggregateDefinition.Validate(_pluginManager, _graphdb, mySecurityToken, myTransactionToken);

                    var selPartAggr = new SelectionElementAggregate(aggregateSelection.ChainPartAggregateDefinition.Aggregate, selection.Item2,
                                                                    new EdgeList(aggregateSelection.ChainPartAggregateDefinition.Parameter.Edges), new LevelKey(aggregateSelection.ChainPartAggregateDefinition.Parameter.Edges, _graphdb, mySecurityToken, myTransactionToken),
                                                                    aggregateSelection.ChainPartAggregateDefinition.Parameter, aggregateSelection);

                    _SelectResultManager.AddAggregateElementToSelection(mySecurityToken, myTransactionToken, selection.Item2, aggregateSelection.ChainPartAggregateDefinition.Parameter.Reference.Item1, selPartAggr);


                    #endregion
                }

                else
                {
                    throw new NotImplementedQLException("");
                }

                #endregion
            }

            #region Add groupings

            if (selectDefinition.GroupByIDs.IsNotNullOrEmpty())
            {
                foreach (var group in selectDefinition.GroupByIDs)
                {
                    group.Validate(_pluginManager, _graphdb, mySecurityToken, myTransactionToken, true);

                    _SelectResultManager.AddGroupElementToSelection(group.Reference.Item1, group);
                }
            }

            #endregion

            #region Add having

            if (selectDefinition.Having != null)
            {
                selectDefinition.Having.Validate(_pluginManager, _graphdb, mySecurityToken, myTransactionToken);

                _SelectResultManager.AddHavingToSelection(selectDefinition.Having);
            }

            #endregion

            return(_SelectResultManager);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds an aggregate to the selection. It will check whether it is an index aggregate or not.
        /// Aggregates on attributes with level > 1 will return an error.
        /// </summary>
        /// <param name="myReference"></param>
        /// <param name="myAlias"></param>
        /// <param name="myAStructureNode"></param>
        /// <param name="myGraphType"></param>
        public void AddAggregateElementToSelection(SecurityToken mySecurityToken, 
                                                    Int64 myTransactionToken, 
                                                    string myAlias, 
                                                    string myReference, 
                                                    SelectionElementAggregate mySelectionPartAggregate)
        {

            if (mySelectionPartAggregate.EdgeList.Level > 1)
            {
                throw new AggregateIsNotValidOnThisAttributeException(mySelectionPartAggregate.ToString());
            }

            #region Check for index aggregate

            foreach (var edge in mySelectionPartAggregate.EdgeList.Edges)
            {
                var vertexType = _graphdb.GetVertexType<IVertexType>(
                        mySecurityToken,
                        myTransactionToken,
                        new RequestGetVertexType(edge.VertexTypeID),
                        (stats, vType) => vType);

                #region COUNT(*)

                if (!edge.IsAttributeSet)
                {
                    //mySelectionPartAggregate.Element = _DBContext.DBTypeManager.GetUUIDTypeAttribute();
                    mySelectionPartAggregate.Element = vertexType.GetAttributeDefinition("VertexID");
                }

                #endregion

                else
                {
                    // if the GetAttributeIndex did not return null we will pass this as the aggregate operation value
                    mySelectionPartAggregate.Element = vertexType.GetAttributeDefinition(edge.AttributeID);
                }
            }

            #endregion

            if (!_Aggregates.ContainsKey(myReference))
            {
                _Aggregates.Add(myReference, new Dictionary<EdgeList, List<SelectionElementAggregate>>());
            }

            var level = mySelectionPartAggregate.EdgeList.GetPredecessorLevel();
            if (!_Aggregates[myReference].ContainsKey(level))
            {
                _Aggregates[myReference].Add(level, new List<SelectionElementAggregate>());
            }

            _Aggregates[myReference][level].Add(mySelectionPartAggregate);
        }