Example #1
0
        private static void Parse(string query)
        {
            Console.WriteLine(@"------------------------------------------");
            Console.WriteLine(@"Parsing statement ""{0}""", query);

            var parser = new TSql100Parser(false);

            IList <ParseError> errors;
            IScriptFragment    result = parser.Parse(new StringReader(query), out errors);

            if (errors.Count > 0)
            {
                Console.WriteLine(@"Errors encountered: ""{0}""", errors[0].Message);
                return;
            }


            TSqlStatement statement = ((TSqlScript)result).Batches[0].Statements[0];

            if (statement is SelectStatement)
            {
                TableSource tableSource = (((QuerySpecification)((SelectStatement)statement).QueryExpression).FromClauses[0]);

                Console.WriteLine(@"Top level FROM clause at Line {0}, Column {1} (Character Offset {2})",
                                  tableSource.StartLine, tableSource.StartColumn, tableSource.StartOffset);
                Console.WriteLine();
                Console.WriteLine();
            }
        }
Example #2
0
        protected bool IsSelect(IScriptFragment fragment)
        {
            var script = fragment as TSqlScript;
            IList <TSqlStatement> statements = null;
            bool isSelectStatement           = false;
            int  statementNo = 1;

            foreach (var batch in script.Batches)
            {
                statements = batch.Statements;
                foreach (var st in statements)
                {
                    if (st is SelectStatement)
                    {
                        isSelectStatement = true;
                    }
                    else
                    {
                        if (ErrorMessageList == null)
                        {
                            ErrorMessageList = new List <string>();
                        }
                        ErrorMessageList.Add("Query:#" + statementNo + " is not a select query");
                        isSelectStatement = false;
                        break;
                    }
                    statementNo++;
                }
            }
            return(isSelectStatement);
        }
Example #3
0
        protected bool Parse(out IScriptFragment fragment)
        {
            TSql100Parser      parser = new TSql100Parser(false);
            IList <ParseError> errors;

            fragment = parser.Parse(new StringReader(QueryToValidate), out errors);
            if (errors != null && errors.Count > 0)
            {
                List <string> errorList = new List <string>();
                foreach (var error in errors)
                {
                    if (ErrorMessageList == null)
                    {
                        ErrorMessageList = new List <string>();
                    }
                    ErrorMessageList.Add(error.Message);
                }
            }
            if (ErrorMessageList == null)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #4
0
        // Main run method
        private IList <RuleProblem> Run(TextReader input, IList <Rule> rules)
        {
            List <RuleProblem> problems = new List <RuleProblem>();

            var             parser         = new TSql100Parser(true);
            var             parseErrors    = new List <ParseError>() as IList <ParseError>;
            IScriptFragment scriptFragment = parser.Parse(input, out parseErrors);

            if (parseErrors.Count > 0)
            {
                // TODO: do custom exception
                var error = parseErrors[0];
                var ex    = new ArgumentException(error.Message);
                throw ex;
            }
            var context = new RuleContext
            {
                ScriptFragment = scriptFragment,
            };

            foreach (var rule in rules)
            {
                var rulePoblems = rule.Analyze(context);
                problems.AddRange(rulePoblems);
            }
            return(problems);
        }
Example #5
0
        private void Parse(TSql90Parser parser, string inputScript)
        {
            IList<ParseError> errors;

            using (StringReader sr = new StringReader(inputScript))
            {
                fragment = parser.Parse(sr, out errors);
            }

            if (errors != null && errors.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var error in errors)
                {
                    sb.AppendLine(error.Message);
                    sb.AppendLine("offset " + error.Offset.ToString());
                }
                throw new ArgumentException("InvalidSQLScript", sb.ToString());
            }
        }
Example #6
0
        private void Parse(TSql80Parser parser, string inputScript)
        {
            IList <ParseError> errors;

            using (StringReader sr = new StringReader(inputScript))
            {
                fragment = parser.Parse(sr, out errors);
            }

            if (errors != null && errors.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var error in errors)
                {
                    sb.AppendLine(error.Message);
                    sb.AppendLine("offset " + error.Offset.ToString());
                }
                throw new ArgumentException("InvalidSQLScript", sb.ToString());
            }
        }
Example #7
0
 protected void ValidateIfQueryIsSelectStatement(IScriptFragment fragment)
 {
     QueryValid = IsSelect(fragment);
 }
Example #8
0
 protected void ValidateSyntax(out IScriptFragment fragment)
 {
     QueryValid = Parse(out fragment);
 }