internal void EnsureAttributeExistInObject(IList<AttributeObject> attrObjects, RelationObject relationToCheckAttributesAgainst)
 {
     foreach(AttributeObject attrName in attrObjects)
     {
         EnsureAttributeExistInObject(attrName.Name, relationToCheckAttributesAgainst);
     }
 }
        public RelationsModel GetRelationModelFromTempRelatinoLazyLoaded(RelationObject relationObject)
        {
            RelationsModel relationModelToReturn = new RelationsModel();

            relationModelToReturn = GetRelationModelFromDatabaseLazyLoaded(relationObject);

            return relationModelToReturn;
        }
        public RelationsModel GetRelationModelFromDatabaseLazyLoaded(RelationObject relationObject)
        {
            RelationsModel relationModelToReturn = new RelationsModel();

            LazyLoadRelationsModel(relationObject, relationModelToReturn);

            return relationModelToReturn;
        }
 internal void EnsureObjectContainsAttributes(RelationObject objectToGetAttributeNamesFrom)
 {
     if (objectToGetAttributeNamesFrom == null)
     {
         ErrorHandlerSURLY.RelationErrors.RelationDoesNotExist();
     }
     else if (objectToGetAttributeNamesFrom.RelationAttributes == null)
     {
         ErrorHandlerSURLY.RelationErrors.RelationDoesNotContainAttributes(objectToGetAttributeNamesFrom.NameOfRelation);
     }
 }
        public static void AddRelationToDatabase(RelationObject relationObjectToAddToDatabase)
        {
            if(Relations == null)
            {
                Relations = new List<RelationObject>();
            }

            Relations.Add(relationObjectToAddToDatabase);

            Console.Write("Relation " + relationObjectToAddToDatabase.NameOfRelation + " added to database");
        }
        internal IList<AttributeObject> GetAllAttributesOfFirstRelationThatTheUserRequestedAndCheckForNextSymbolToBeAND(RelationObject relationObjectToGrabAttributesFrom)
        {
            IList<AttributeObject> attributesRequestedByUser = new List<AttributeObject>();

            string curSymbol = GetAllAttributesOfARelationThatTheUserRequested(relationObjectToGrabAttributesFrom, attributesRequestedByUser);

            if (!curSymbol.Equals(AllSpecialSymbols.AND))
            {
                ErrorHandlerSURLY.RelationErrors.MissingAndStatementForJoin(curSymbol);
            }

            return attributesRequestedByUser;
        }
        internal void EnsureAttributeExistInObject(string curSymbol, RelationObject relationObjectToGrabAttributesFrom)
        {
            IList<string> allAttributeNames = GetAllAttributeNamesFromARelation(relationObjectToGrabAttributesFrom);
            IList<AttributeObject> allAttributesFromRelation = GetSelectedAttributesFromARelation(relationObjectToGrabAttributesFrom);

            foreach(AttributeObject item in allAttributesFromRelation)
            {
                if(allAttributeNames.Contains(item.Name))
                {
                    return;
                }
            }

            ErrorHandlerSURLY.RelationErrors.AttributeDoesNotExistInTheRelation(curSymbol);
        }
        internal static IList<string> GetAllAttributeNamesFromARelation(RelationObject objectToGetAttributeNamesFrom)
        {
            IList<string> AllAttributeNames = new List<string>();

            if (objectToGetAttributeNamesFrom.RelationAttributes == null)
            {
                ErrorHandlerSURLY.RelationErrors.RelationDoesNotContainAttributes(objectToGetAttributeNamesFrom.NameOfRelation);
            }

            foreach (AttributeObject attr in objectToGetAttributeNamesFrom.RelationAttributes)
            {
                AllAttributeNames.Add(attr.Name);
            }

            return AllAttributeNames;
        }
        private void LazyLoadAttributesFromDatabaseToModel(RelationObject relationObject, RelationsModel relationModel)
        {
            IList<AttributesModel> attributesModelList = new List<AttributesModel>();

            foreach (AttributeObject attributeObject in relationObject.RelationAttributes)
            {
                AttributesModel attributeModelToAddToList = new AttributesModel();

                attributeModelToAddToList.Name = attributeObject.Name;
                attributeModelToAddToList.Type = attributeObject.Type;
                attributeModelToAddToList.Size = attributeObject.Size;

                attributesModelList.Add(attributeModelToAddToList);
            }

            relationModel.RelationAttributes = attributesModelList;
        }
        internal RelationObject JoinRelations(RelationObject joinRelation1, 
                                            RelationObject joinRelation2, 
                                            IList<AttributeObject> attributeList1, 
                                            IList<AttributeObject> attributeList2)
        {
            LoadParametersToAttributes(joinRelation1,
                                            joinRelation2,
                                            attributeList1,
                                            attributeList2);

            relationService.EnsureAttributeExistInObject(this.AttributeList1, this.JoinRelation1);
            relationService.EnsureAttributeExistInObject(this.AttributeList2, this.JoinRelation2);

            EnsureAttributeListsAreTheSameSize();

            RelationObject joinedRelation = new RelationObject();

            joinedRelation = LoadJoinedRelationDeprojectsAttributeList2();
            joinedRelation = PruneTuplesToAddToJoinedRelation(joinedRelation);

            return joinedRelation;
        }
        private string GetAllAttributesOfARelationThatTheUserRequested(RelationObject relationObjectToGrabAttributesFrom, IList<AttributeObject> attributesRequestedByUser)
        {
            EnsureObjectContainsAttributes(relationObjectToGrabAttributesFrom);

            string curSymbol = QueryParser.FindNextSymbol();

            while(!AllSpecialSymbols.GetListOfAllSymbols().Contains(curSymbol))
            {
                AttributeObject attributeToAddToAttributesRequestedByUser = new AttributeObject();

                EnsureAttributeExistInObject(curSymbol, relationObjectToGrabAttributesFrom);
                attributeToAddToAttributesRequestedByUser = GetAttributeObjectFromRelationObject(curSymbol, relationObjectToGrabAttributesFrom);

                attributesRequestedByUser.Add(attributeToAddToAttributesRequestedByUser);

                curSymbol = QueryParser.FindNextSymbol();
            }

            return curSymbol;
        }
        internal IList<AttributeObject> GetSelectedAttributesFromARelation(RelationObject objectToGet)
        {
            IList<AttributeObject> allAttributesFromRelation = objectToGet.RelationAttributes;

            return allAttributesFromRelation;
        }
        internal RelationObject GetRelation(string relationName)
        {
            RelationObject relationToReturn = new RelationObject();

            foreach(RelationObject relation in InternalDatabase.Relations)
            {
                if(relation.NameOfRelation.Equals(relationName))
                {
                    relationToReturn = relation;
                    break;
                }
            }

            return relationToReturn;
        }
        internal AttributeObject GetAttributeObjectFromRelationObject(string curSymbol, RelationObject relationObjectToGrabAttributesFrom)
        {
            IList<string> allAttributeNames = GetAllAttributeNamesFromARelation(relationObjectToGrabAttributesFrom);
            IList<AttributeObject> allAttributesFromRelation = GetSelectedAttributesFromARelation(relationObjectToGrabAttributesFrom);

            foreach (AttributeObject item in allAttributesFromRelation)
            {
                if (curSymbol.Equals(item.Name))
                {
                    return item;
                }
            }

            ErrorHandlerSURLY.RelationErrors.AttributeDoesNotExistInTheRelation(curSymbol);
            return null;
        }
 internal void RenewObjects()
 {
     relationToCreate = new RelationObject();
     attributesForRelationToCreate = new List<AttributeObject>();
     currentAttribute = new AttributeObject();
     currentSymbol = string.Empty;
     RelationNamesToDestroy = new List<String>();
     RelationNameToDelete = string.Empty;
     reading = false;
     RelationCRUDService = new RelationCRUDServices(this);
     RelationService = new RelationServices();
     QueryService = new QueryServices();
 }
        internal RelationObject RecursiveExpressionManipulator(RelationObject state, string curSymbol, IList<string> allRelationNames)
        {
            RelationObject joinedObject = new RelationObject();
            RelationObject joinRelation1 = new RelationObject();
            RelationObject joinRelation2 = new RelationObject();

            RelationObject selectedObject = new RelationObject();
            RelationObject selectRelation = new RelationObject();

            RelationObject TrueRelationObject = new RelationObject();

            string nextSymbol = string.Empty;

            switch (curSymbol)
            {
                case "JOIN":
                    IList<AttributeObject> attributeList1 = new List<AttributeObject>();
                    IList<AttributeObject> attributeList2 = new List<AttributeObject>();

                    curSymbol = LexicalAnalyzerForQuery.QueryParser.FindNextSymbol();

                    if (curSymbol.Equals((AllSpecialSymbols.OpentParenthases).ToString()))
                    {
                        joinRelation1 = RecursiveExpressionManipulator(joinRelation1, LexicalAnalyzerForQuery.QueryParser.FindNextSymbol(), allRelationNames);
                        curSymbol = LexicalAnalyzerForQuery.QueryParser.FindNextSymbol();

                        if(curSymbol.Equals(AllSpecialSymbols.AND))
                        {
                            joinRelation2 = RecursiveExpressionManipulator(joinRelation2, LexicalAnalyzerForQuery.QueryParser.FindNextSymbol(), allRelationNames);
                            curSymbol = LexicalAnalyzerForQuery.QueryParser.FindNextSymbol();

                            if(curSymbol.Equals((AllSpecialSymbols.CloseParenthases).ToString()))
                            {
                                curSymbol = LexicalAnalyzerForQuery.QueryParser.FindNextSymbol();
                                if (curSymbol.Equals(AllSpecialSymbols.OVER))
                                {
                                    curSymbol = LexicalAnalyzerForQuery.QueryParser.FindNextSymbol();
                                    if (curSymbol.Equals((AllSpecialSymbols.OpentParenthases).ToString()))
                                    {
                                        attributeList1 = relationService.GetAllAttributesOfFirstRelationThatTheUserRequestedAndCheckForNextSymbolToBeAND(joinRelation1);
                                        attributeList2 = relationService.GetAllAttributesOfSecondRelationThatTheUserRequestedAndCheckForNextSymbolToBeClosingParentases(joinRelation2);

                                        joinedObject = joinServices.JoinRelations(joinRelation1,
                                                        joinRelation2,
                                                        attributeList1,
                                                        attributeList2); // Write method for generating a table based off of what is sent in

                                        if(joinedObject == null)
                                        {
                                            ErrorHandlerSURLY.QueryErrors.JoinProducedNullTable(joinRelation1, joinRelation2);
                                        }

                                        return joinedObject;
                                    }
                                    else
                                    {
                                        ErrorHandlerSURLY.QueryErrors.JoinMissingClosingParentheses(joinRelation1, joinRelation2, curSymbol);
                                    }
                                }
                                else
                                {
                                    ErrorHandlerSURLY.QueryErrors.JoinMissingOver(joinRelation1, joinRelation2, curSymbol);
                                }
                            }
                            else
                            {
                                ErrorHandlerSURLY.QueryErrors.JoinMissingClosingParentheses(joinRelation1, joinRelation2, curSymbol);
                            }

                        }
                        else
                        {
                            ErrorHandlerSURLY.QueryErrors.JoinMissingAnd(joinRelation1, joinRelation2, curSymbol);
                        }
                    }
                    else
                    {
                        ErrorHandlerSURLY.QueryErrors.JoinMissingClosingParentheses(joinRelation1, joinRelation2, curSymbol);
                    }

                    return null;
                    break;

                case "SELECT":
                    curSymbol = LexicalAnalyzerForQuery.QueryParser.FindNextSymbol();

                    if (curSymbol.Equals((AllSpecialSymbols.OpentParenthases).ToString()))
                    {
                        selectRelation = RecursiveExpressionManipulator(selectRelation, LexicalAnalyzerForQuery.QueryParser.FindNextSymbol(), allRelationNames);
                        curSymbol = LexicalAnalyzerForQuery.QueryParser.FindNextSymbol();

                        if (curSymbol.Equals((AllSpecialSymbols.CloseParenthases).ToString()))
                        {
                            curSymbol = LexicalAnalyzerForQuery.QueryParser.FindNextSymbol();
                            if (curSymbol.Equals(AllSpecialSymbols.WHERE))
                            {
                                curSymbol = LexicalAnalyzerForQuery.QueryParser.FindNextSymbol();
                                if (curSymbol.Equals((AllSpecialSymbols.OpentParenthases).ToString()))
                                {
                                    selectedObject = selectService.GenerateRelationBasedOnSelect(selectRelation); //(selectRelation1);  // Make new method that takes in the select relation, then reads user input until ')' and does the operations
                                }
                                else
                                {
                                    ErrorHandlerSURLY.QueryErrors.SelectMissingOpenParenthases(selectRelation, curSymbol);
                                }
                            }
                            else
                            {
                                ErrorHandlerSURLY.QueryErrors.SelectMissingWhere(selectRelation, curSymbol);
                            }
                        }
                    }

                    return selectedObject;
                    break;

                default:
                    if (allRelationNames.Contains(curSymbol))
                    {
                        TrueRelationObject = relationService.GetRelation(curSymbol);
                        return TrueRelationObject;
                    }

                    ErrorHandlerSURLY.QueryErrors.NoRelationCommandOrRelationNameFound(curSymbol);
                    return null;
                    break;
            }
        }
 public void SelectMissingWhere(RelationObject selectRelation, string curSymbol)
 {
     string errorMessage = "Missing 'WHERE' statement for the SELECT of \"" + selectRelation.NameOfRelation + "\".";
     errorMessage += ErrorHandlerSURLY.AdditionalErrorInformation(curSymbol);
     ErrorHandlerSURLY.ThrowError(errorMessage);
 }
        private RelationObject PruneTuplesToAddToJoinedRelation(RelationObject joinedRelation)
        {
            for (int i = 0; i < this.AttributeList1AsArray.Length; i++)
            {
                foreach (TupleObject currentTuple1 in this.JoinRelation1.RelationsTuples)
                {
                    foreach (TupleCellObject currentCellOfTuple1 in currentTuple1.TupleCells)
                    {
                        if (currentCellOfTuple1.AttributeAssociatedWithCell == AttributeList1AsArray[i])
                        {

                    foreach (TupleObject currentTuple2 in this.JoinRelation2.RelationsTuples)
                    {
                                foreach(TupleCellObject currentCellOfTuple2 in currentTuple2.TupleCells)
                                {
                                    if(currentCellOfTuple2.AttributeAssociatedWithCell == AttributeList2AsArray[i])
                                    {

                                        if(currentCellOfTuple1.Value.Equals(currentCellOfTuple2.Value))
                                        {
                                            TupleObject tupleToAddToJoinedRelation = AddTupleExceptForAttributeList2Items(currentTuple1, currentTuple2);
                                            EnsureTupleMatchesJoinedRelationAttributes(joinedRelation, tupleToAddToJoinedRelation);
                                            joinedRelation.RelationsTuples.Add(tupleToAddToJoinedRelation);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            joinedRelation.NameOfRelation = "*TEMPORARY*";

            return joinedRelation;
        }
        public static void DeleteRelation(RelationObject relationToDelete)
        {
            relationToDelete = Relations.FirstOrDefault(r => r == relationToDelete);

            relationToDelete.RelationsTuples = new List<TupleObject>();
        }
        internal RelationObject GenerateRelationBasedOnSelect(RelationObject selectRelation)
        {
            IList<SelectObject> comparisonResultsBetweenAnds = new List<SelectObject>();
            string curSymbol = QueryParser.FindNextSymbol();

            if(curSymbol.Equals((AllSpecialSymbols.CloseParenthases).ToString()))
            {
                ErrorHandlerSURLY.SelectErrors.RequiresAtLeastOneComparison(curSymbol);
            }

            while(!curSymbol.Equals((AllSpecialSymbols.CloseParenthases).ToString()))
            {
                IList<OperationsObject> listOfOperationsBeforeAnOr = new List<OperationsObject>();
                SelectObject containerForOperationsBeforeAnOr = new SelectObject();

                bool firstPassEMERGENCYCHANGEOBJECT = true;

                while (!curSymbol.Equals((AllSpecialSymbols.OR).ToString()) && !curSymbol.Equals((AllSpecialSymbols.CloseParenthases).ToString()))
                {
                    if(firstPassEMERGENCYCHANGEOBJECT)
                    {
                        firstPassEMERGENCYCHANGEOBJECT = false;
                    }
                    else if(curSymbol.Equals(AllSpecialSymbols.AND))
                    {
                        curSymbol = QueryParser.FindNextSymbol();
                    }
                    else
                    {
                        ErrorHandlerSURLY.SelectErrors.UnrecognizeableBooleanOperator(curSymbol);
                    }

                    OperationsObject currentOperaitonObject = new OperationsObject();

                    AttributeObject attributeObjectOfCurrentParameter = relationService.GetAttributeObjectFromRelationObject(curSymbol, selectRelation);
                    string operatorForComparison = QueryParser.FindNextSymbol();
                    string rightsideOfComparison = QueryParser.FindNextSymbol();

                    currentOperaitonObject.comparisonAttribute = attributeObjectOfCurrentParameter;
                    currentOperaitonObject.comparisonOperation = operatorForComparison;
                    currentOperaitonObject.comparisonValue = rightsideOfComparison;

                    listOfOperationsBeforeAnOr.Add(currentOperaitonObject);

                    curSymbol = QueryParser.FindNextSymbol();
                }

                if(curSymbol.Equals(AllSpecialSymbols.OR))
                {
                    curSymbol = QueryParser.FindNextSymbol();
                }

                containerForOperationsBeforeAnOr.ListOfOperationsSeparateByOrs = listOfOperationsBeforeAnOr;
                comparisonResultsBetweenAnds.Add(containerForOperationsBeforeAnOr);
            }

            RelationObject PrunedRelation = PruneRelationObjectBasedOnSelections(selectRelation, comparisonResultsBetweenAnds);

            PrunedRelation.NameOfRelation = "*TEMPORARY*";

            return PrunedRelation;
        }
        private RelationObject PruneRelationObjectBasedOnSelections(RelationObject selectRelation, IList<SelectObject> comparisonResultsBetweenAnds)
        {
            RelationObject PrunedRelation = new RelationObject();

            PrunedRelation.RelationAttributes = selectRelation.RelationAttributes;

            foreach(TupleObject tupleObject in selectRelation.RelationsTuples)
            {
                IList<bool> boolsForOr = new List<bool>();
                foreach (SelectObject selectObject in comparisonResultsBetweenAnds)
                {
                    IList<bool> boolsForAnd = new List<bool>();
                    foreach (OperationsObject operationObject in selectObject.ListOfOperationsSeparateByOrs)
                    {
                        foreach (TupleCellObject tupleCell in tupleObject.TupleCells)
                        {
                            if (operationObject.comparisonAttribute.Equals(tupleCell.AttributeAssociatedWithCell))
                            {
                                boolsForAnd.Add(CompareItemsBasedOnType(tupleCell.Value,
                                                            operationObject.comparisonOperation,
                                                            operationObject.comparisonValue,
                                                            operationObject.comparisonAttribute.Type));
                            }
                        }
                    }

                    bool resultOfAnds = true;

                    foreach(bool item in boolsForAnd)
                    {
                        if(item == false)
                        {
                            resultOfAnds = false;
                            break;
                        }
                    }

                    boolsForOr.Add(resultOfAnds);
                }

                bool resultOfOrs = false;

                foreach (bool item in boolsForOr)
                {
                    if (item == true)
                    {
                        resultOfOrs = true;
                        break;
                    }
                }

                if(resultOfOrs == true)
                {
                    PrunedRelation.RelationsTuples.Add(tupleObject);
                }
            }

            return PrunedRelation;
        }
 public void JoinAttributesDoNotMatch(RelationObject joinRelation1, RelationObject joinRelation2)
 {
     string errorMessage = "SyntaxError: The number of attributes in the lists sent in for the joining of \"" + joinRelation1.NameOfRelation + "\" and \"" + joinRelation2.NameOfRelation + "\" did not match.";
     errorMessage += ErrorHandlerSURLY.AdditionalErrorInformation();
     ErrorHandlerSURLY.ThrowError(errorMessage);
 }
 public void JoinAttributesAndTupleAttributesDidNotMatch(RelationObject joinRelation1, RelationObject joinRelation2, RelationObject joinedRelation)
 {
     string errorMessage = "Critical Error: For the joinging of \"" + joinRelation1.NameOfRelation + "\" and \"" + joinRelation2.NameOfRelation + "\" the attributes in joined relation \"" + joinedRelation.NameOfRelation + "\" did not match the expected tuple attributes.  Inform webmaster.";
     errorMessage += ErrorHandlerSURLY.AdditionalErrorInformation();
     ErrorHandlerSURLY.ThrowError(errorMessage);
 }
 public static void DestroyRelation(RelationObject relationToDestroy)
 {
     Relations.Remove(relationToDestroy);
 }
        private RelationObject LoadJoinedRelationDeprojectsAttributeList2()
        {
            RelationObject joinedRelation = new RelationObject();

            foreach(AttributeObject attrObject in this.JoinRelation1.RelationAttributes)
            {
                joinedRelation.RelationAttributes.Add(attrObject);
            }

            foreach (AttributeObject attrObject in this.JoinRelation2.RelationAttributes)
            {
                if (!this.AttributeList2.Contains(attrObject))
                {
                    joinedRelation.RelationAttributes.Add(attrObject);
                }
            }

            return joinedRelation;
        }
 public void SelectMissingOpenParenthases(RelationObject selectRelation, string curSymbol)
 {
     string errorMessage = "Missing '(' for the SELECT of \"" + selectRelation.NameOfRelation + "\".";
     errorMessage += ErrorHandlerSURLY.AdditionalErrorInformation(curSymbol);
     ErrorHandlerSURLY.ThrowError(errorMessage);
 }
 public void JoinMissingOver(RelationObject joinRelation1, RelationObject joinRelation2, string curSymbol)
 {
     string errorMessage = "Missing 'OVER' for the JOIN between \"" + joinRelation1 + "\" and \"" + joinRelation2.NameOfRelation + "\".";
     errorMessage += ErrorHandlerSURLY.AdditionalErrorInformation(curSymbol);
     ErrorHandlerSURLY.ThrowError(errorMessage);
 }
 private void LoadParametersToAttributes(RelationObject joinRelation1,
                                     RelationObject joinRelation2,
                                     IList<AttributeObject> attributeList1,
                                     IList<AttributeObject> attributeList2)
 {
     this.JoinRelation1 = joinRelation1;
     this.JoinRelation2 = joinRelation2;
     this.AttributeList1 = attributeList1;
     this.AttributeList2 = attributeList2;
     this.AttributeList1AsArray = attributeList1.ToArray();
     this.AttributeList2AsArray = attributeList2.ToArray();
 }
        internal IList<AttributeObject> GetAllAttributesOfSecondRelationThatTheUserRequestedAndCheckForNextSymbolToBeClosingParentases(RelationObject relationObjectToGrabAttributesFrom)
        {
            IList<AttributeObject> attributesRequestedByUser = new List<AttributeObject>();

            string curSymbol = GetAllAttributesOfARelationThatTheUserRequested(relationObjectToGrabAttributesFrom, attributesRequestedByUser);

            if (!curSymbol.Equals((AllSpecialSymbols.CloseParenthases).ToString()))
            {
                ErrorHandlerSURLY.RelationErrors.MissingClosingParenthasesStatementForJoin(curSymbol);
            }

            return attributesRequestedByUser;
        }
 public void JoinProducedNullTable(RelationObject joinRelation1, RelationObject joinRelation2)
 {
     string errorMessage = "The generated table for the JOIN between \"" + joinRelation1 + "\" and \"" + joinRelation2.NameOfRelation + "\" has failed to produce a table.";
     errorMessage += ErrorHandlerSURLY.AdditionalErrorInformation();
     ErrorHandlerSURLY.ThrowError(errorMessage);
 }