Exemplo n.º 1
0
        public SelectOutput(string selectParam, DeclarationsArray declarationsArray)
        {
            SuchThat suchThat = new SuchThat(declarationsArray);

            SelectParam = selectParam;

            queryResults = new List <MethodResultList>();
            //validation completed
            foreach (var methodToDo in SuchThatValidator.GetMethodsToDo())
            {
                queryResults.Add(suchThat.DoSuchThatMethod(methodToDo.MethodName, methodToDo.Param1, methodToDo.Param2));
            }

            //foreach (var qr in queryResults)
            //{
            //    Console.WriteLine($"\n{qr.ListType1}\t{qr.ListType2}");
            //    for (int i = 0; i < qr.List1.Count; i++)
            //    {
            //        Console.WriteLine($"{((AbstractAuxiliaryGrammar) qr.List1[i]).entry}\t{((AbstractAuxiliaryGrammar) qr.List2[i]).entry}");
            //    }
            //}


            ResultTableRelations resultTableRelations = new ResultTableRelations();

            for (int i = 0; i < queryResults.Count; i++)
            {
                MethodResultList qr = queryResults[i];

                if (qr.ListType1 == typeof(Ident))
                {
                    if (qr.ListType2 == typeof(Ident))
                    {
                        resultTableRelations.AddRelation(qr.QueryParam1, qr.QueryParam2, i);
                    }
                    else //qr.ListType2 == typeof(Ident)
                    {
                        resultTableRelations.AddRelation(qr.QueryParam1, i);
                    }
                }
                else //qr.ListType1 != typeof(Ident)
                {
                    if (qr.ListType2 == typeof(Ident))
                    {
                        resultTableRelations.AddRelation(qr.QueryParam2, i);
                    }
                    else //qr.ListType2 != typeof(Ident)
                    {
                        resultTableRelations.AddRelation(qr.QueryParam2, qr.QueryParam1, i);
                    }
                }
            }

            MergedResultTableRelations mergedResultTableRelations = new MergedResultTableRelations(resultTableRelations);

            StackList = mergedResultTableRelations.ExecuteAllMerges(selectParam);
        }
Exemplo n.º 2
0
 private bool IsGrammarCorrect(string methodName, ref string param1, ref string param2)
 // ... Parent (n, s) -> methodName, param1, param2 --- cez2 Assignment1
 {
     if (SuchThatValidator.ContainsMethodName(methodName) &&
         SuchThatValidator.ExtractParams(ref param1, ref param2) &&
         ShouldBeDeclared(param1) && ShouldBeDeclared(param2))
     {
         return(true);
     }
     else
     {
         throw new SelectValidatorException($"#Invalid method name or params for such that - found: {methodName}, {param1}, {param2}");
     }
 }
Exemplo n.º 3
0
        public bool ValidateQuery(string query)
        {
            query = query.ToUpper();

            // It removes all multiple spaces in whole query
            // e.g. procedure  p   ;  select     p
            // will be replaced to: procedure p ; select p
            query = Regex.Replace(query, @"\s+", " ");

            string[] statements = query.Split(';');

            foreach (string statement in statements)
            {
                string firstWord = statement.Trim().Split(' ')[0];
                if (firstWord == "SELECT")
                {
                    SelectValidator = new SelectValidator(DeclarationValidator.GetDeclarationsArray());

                    SuchThatValidator.SetList(new List <SuchThatValidator.MethodToDo>());

                    // TODO: ValidateSelectQuery method
                    if (!SelectValidator.ValidateSelectQuery(statement.Trim()))
                    {
                        throw new InvalidQueryException("#Invalid Select Query");
                    }

                    //tu dodać
                    SelectOutput selectOutput = new SelectOutput(SelectValidator.SelectParam, DeclarationValidator.GetDeclarationsArray());

                    Console.WriteLine(selectOutput.GenerateResult());

                    //Console.ReadKey();
                }
                else
                {
                    if (!DeclarationValidator.ValidateDeclarationQuery(statement.Trim()))
                    {
                        throw new InvalidQueryException("#Invalid Declaration Query");
                    }
                }
            }

            return(true);
        }
Exemplo n.º 4
0
        public bool ValidateSelectQuery(string queryToValidate)
        {
            string[] queryWords = queryToValidate.Split(' ');

            if (!IsThereSelectAndParam(queryWords[0], queryWords[1]))
            {
                throw new SelectValidatorException("#Missing keywords 'Select' or 'argument'");
            }

            SelectParam = queryWords[1];

            for (int i = 2; i < queryWords.Length - 1;) //note that i++ is missing for a reason
            {
                CurrentType = SuchThatPatternWithAnd(queryWords[i], queryWords[i + 1]);

                if (CurrentType == Types.None)
                {
                    throw new SelectValidatorException($"#Wrong keywords. Expected 'SUCH THAT', 'AND', 'WITH', 'PATTERN'.\nGot: {queryWords[i]} and {queryWords[i + 1]}");
                }

                if (CurrentType == Types.SuchThat)
                {
                    i += 2;
                    if (!IsGrammarCorrect(queryWords[i], ref queryWords[i + 1], ref queryWords[i + 2]))
                    {
                        throw new SelectValidatorException($"#Invalid SUCH THAT syntax. Got {queryWords[0]}, {queryWords[1]}, {queryWords[2]}");
                    }
                    else
                    {
                        SuchThatValidator.ValidateMethodAndParams(queryWords[i], queryWords[i + 1], queryWords[i + 2]);
                    }
                    i += 3;
                }
                else if (CurrentType == Types.SuchThatAnd)
                {
                    i++;
                    if (!IsGrammarCorrect(queryWords[i], ref queryWords[i + 1], ref queryWords[i + 2]))
                    {
                        throw new SelectValidatorException($"#Invalid SUCH THAT .. AND syntax. Got {queryWords[0]}, {queryWords[1]}, {queryWords[2]}");
                    }
                    else
                    {
                        SuchThatValidator.ValidateMethodAndParams(queryWords[i], queryWords[i + 1], queryWords[i + 2]);
                    }
                    i += 3;
                }
                else if (CurrentType == Types.Pattern)
                {
                    i += 1;

                    string name = queryWords[i].Contains("(") ? queryWords[i].Split('(')[0] : queryWords[i];

                    if (Declarations.GetDeclarationByName(name) == null)
                    {
                        throw new InvalidQueryException("#Invalid 'PATTERN' query - have you declared first argument earlier?");
                    }

                    if (!queryWords[i].Contains("("))
                    {
                        i += 1;
                    }

                    if (queryWords[i][queryWords[i].Length - 1] == ',')
                    {
                        // General grammar

                        if (!queryWords[i].Contains("(") ||
                            queryWords[i].Split('(').Length != 2 ||
                            queryWords[i + 1].Length < 2 ||
                            !queryWords[i + 1].Contains(")") ||
                            (!(queryWords[i + 1][queryWords[i + 1].Length - 1] == ')') &&
                             !(queryWords[i + 1].Length < 3 &&
                               queryWords[i + 1][queryWords[i + 1].Length - 2] == ')' &&
                               queryWords[i + 1][queryWords[i + 1].Length - 1] == ';'
                               )
                            )
                            )
                        {
                            throw new InvalidQueryException("#Invalid 'PATTERN' query - lexical mistake, maybe missing brackets or something?");
                        }

                        // Verifying whether the first pattern argument has been declared (if not in quotes)

                        if (!(queryWords[i].Split('(')[1][0] == '\'' &&
                              queryWords[i].Split('(')[1][queryWords[i].Split('(')[1].Split(',')[0].Length - 1] == '\'') &&
                            !(queryWords[i].Split('(')[1][0] == '"' &&
                              queryWords[i].Split('(')[1][queryWords[i].Split('(')[1].Split(',')[0].Length - 1] == '"') &&
                            !(int.TryParse(queryWords[i].Split('(')[1].Split(',')[0], out int n)) &&
                            queryWords[i].Split('(')[1].Split(',')[0] != "_")
                        {
                            if (Declarations.GetDeclarationByName(queryWords[i].Split('(')[1].Split(',')[0]) == null)
                            {
                                throw new InvalidQueryException("#Invalid 'PATTERN' query - have you declared first argument earlier?");
                            }
                        }

                        i += 1;

                        // Verifying whether the second pattern argument has been declared (if not in quotes)

                        if (!(queryWords[i][0] == '\'' &&
                              queryWords[i][queryWords[i].Split(')')[0].Length - 1] == '\'') &&
                            !(queryWords[i][0] == '"' &&
                              queryWords[i][queryWords[i].Split(')')[0].Length - 1] == '"') &&
                            !(int.TryParse(queryWords[i].Split(')')[0], out int m)) &&
                            queryWords[i].Split(')')[0] != "_")
                        {
                            if (Declarations.GetDeclarationByName(queryWords[i].Split('(')[1].Split(',')[0]) == null)
                            {
                                throw new InvalidQueryException("#Invalid 'PATTERN' query - have you declared the second argument earlier?");
                            }
                        }

                        i += 1;
                    }

                    else if (queryWords[i][queryWords[i].Length - 1] == ')' || queryWords[i][queryWords[i].Length - 1] == ';')
                    {
                        // General grammar

                        if (!queryWords[i].Contains("(") ||
                            !queryWords[i].Contains(")") ||
                            (!(queryWords[i][queryWords[i].Length] == ')') &&
                             !(queryWords[i + 1].Length < 3 &&
                               queryWords[i][queryWords[i].Length - 2] == ')' &&
                               queryWords[i][queryWords[i].Length - 1] == ';'
                               )
                            )
                            )
                        {
                            throw new InvalidQueryException("#Invalid 'PATTERN' query (missing brackets or something)");
                        }

                        // Verifying whether the first pattern argument has been declared (if not in quotes)

                        if (!(queryWords[i][0] == '\'' &&
                              queryWords[i].Split(',')[0][queryWords[i].Split(',')[0].Split(')')[0].Length - 1] == '\'') &&
                            !(queryWords[i][0] == '"' &&
                              queryWords[i].Split(',')[0][queryWords[i].Split(',')[0].Split(')')[0].Length - 1] == '"') &&
                            !(int.TryParse(queryWords[i].Split(')')[0].Split(',')[0], out int n)) &&
                            queryWords[i].Split(')')[0].Split(',')[0] != "_")
                        {
                            if (Declarations.GetDeclarationByName(queryWords[i].Split('(')[1].Split(',')[0]) == null)
                            {
                                throw new InvalidQueryException("#Invalid 'PATTERN' query - have you declared the first argument earlier?");
                            }
                        }

                        // Verifying whether the second pattern argument has been declared (if not in quotes)

                        if (!(queryWords[i].Split(',')[1][0] == '\'' &&
                              queryWords[i].Split(',')[1][queryWords[i].Split(',')[1].Split(')')[0].Length - 1] == '\'') &&
                            !(queryWords[i].Split(',')[1][0] == '"' &&
                              queryWords[i].Split(',')[1][queryWords[i].Split(',')[1].Split(')')[0].Length - 1] == '"') &&
                            !(int.TryParse(queryWords[i].Split(')')[0].Split(',')[1], out int m)) &&
                            queryWords[i].Split(')')[0].Split(',')[1] != "_")
                        {
                            if (Declarations.GetDeclarationByName(queryWords[i].Split('(')[1].Split(',')[0]) == null)
                            {
                                throw new InvalidQueryException("#Invalid 'PATTERN' query - have you declared the second argument earlier?");
                            }
                        }


                        i += 1;
                    }

                    else
                    {
                        throw new InvalidQueryException("#Invalid 'PATTERN' query (missing brackets or something)");
                    }
                }
                else if (CurrentType == Types.With)
                {
                    With   with      = new With(queryWords, i);
                    string withEntry = with.getWithEntry();
                    i += with.Offset;

                    SelectWithValidator selectWithValidator = new SelectWithValidator(withEntry, Declarations);
                    if (!selectWithValidator.isGrammarCorrect())
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }