예제 #1
0
        public void AddFile(FileInfo file)
        {
            var name        = file.FullName;
            var lastChanged = file.LastWriteTimeUtc;

            if (_files.ContainsKey(name))
            {   //do we need to clear out some old ones??
                var existingScripts = _files[name];
                foreach (var f in existingScripts)
                {
                    IList <ParseError> err;
                    var parsed = _parser.Parse(new StringReader("--"), out err);
                    _model.AddOrUpdateObjects(parsed as TSqlScript, f, new TSqlObjectOptions());
                }
            }

            var scripts = GetScripts(name);

            _caches[name] = lastChanged;
            var scriptList = new List <string>();

            foreach (var script in scripts)
            {
                IList <ParseError> err;
                var parsed = _parser.Parse(new StringReader(script.Content), out err);
                _model.AddOrUpdateObjects(parsed as TSqlScript, script.Path, new TSqlObjectOptions());
                scriptList.Add(script.Path);
            }

            _files[name] = scriptList;
        }
예제 #2
0
        public TSqlFragment GetFragment(TextReader txtRdr, out IList <ParseError> errors, IEnumerable <IOverride> overrides = null)
        {
            TSqlFragment fragment;

            OverrideCompatabilityLevel compatibilityLevel = null;

            if (overrides != null)
            {
                foreach (var lintingOverride in overrides)
                {
                    if (lintingOverride is OverrideCompatabilityLevel overrideCompatability)
                    {
                        compatibilityLevel = overrideCompatability;
                    }
                }
            }

            if (compatibilityLevel != null)
            {
                var tempParser = GetSqlParser(compatibilityLevel.CompatabilityLevel);
                fragment = tempParser.Parse(txtRdr, out errors);
                return(fragment?.FirstTokenIndex != -1 ? fragment : null);
            }

            fragment = parser.Parse(txtRdr, out errors);
            return(fragment?.FirstTokenIndex != -1 ? fragment : null);
        }
예제 #3
0
        public ParserOutput ParseString(string inputString)
        {
            TSqlScript         tsqlScript;
            IList <ParseError> parsingErrors;

            using (StringReader reader = new StringReader(inputString))
            {
                tsqlScript = (TSqlScript)_scriptParser.Parse(reader, out parsingErrors);
            }

            return(new ParserOutput()
            {
                TsqlScript = tsqlScript,
                ParsingErrors = parsingErrors
            });
        }
예제 #4
0
        private ScanResults AppendResults(ScanResults results, string batch, string path, int offset, int lineOffset)
        {
            var reader = new StringReader(batch);
            IList <ParseError> errors;
            var fragment = _parser.Parse(reader, out errors);
            var visitor  = new TestVisitor(path, offset, lineOffset);

            fragment.Accept(visitor);

            results.FoundProperties.AddRange(visitor.ExtendedProperties);
            results.FoundClasses.AddRange(visitor.Schemas);
            results.FoundPotentialTests.AddRange(visitor.Procedures);

            return(results);
        }
예제 #5
0
        public IList <string> SplitInBatches(string script)
        {
            using (StringReader sr = new StringReader(script))
            {
                var fragment = _parser.Parse(sr, out IList <ParseError> errors);
                if (errors.Any())
                {
                    throw new System.Exception(errors.First().Message);
                }

                return(GetBatches(fragment)
                       .Where(x => !string.IsNullOrWhiteSpace(x))
                       .ToList());
            }
        }
예제 #6
0
        private static TSqlFragment GetAbstractSyntaxTree(string script)
        {
            using (TextReader textReader = new StringReader(script))
            {
                TSqlParser   parser       = GetTSqlParser(true);
                TSqlFragment rootFragment = parser.Parse(textReader, out IList <ParseError> parsingErrors); // Get the parse tree

                // if we could not parse the SQL we will throw an exception. Better here than on the server
                if (parsingErrors.Count > 0)
                {
                    throw new ParameterizationParsingException(parsingErrors[0].Line, parsingErrors[0].Column, parsingErrors[0].Message);
                }

                return(rootFragment);
            }
        }
예제 #7
0
        public static IEnumerable <TableInfo> FromFile(TSqlParser parser, string sqlFilePath)
        {
            using (var reader = new StreamReader(sqlFilePath))
            {
                IList <ParseError> errors;
                var result = parser.Parse(reader, out errors);
                var script = result as TSqlScript;

                foreach (var ts in script.Batches)
                {
                    foreach (var st in ts.Statements.OfType <CreateTableStatement>())
                    {
                        yield return(new TableInfo(st));
                    }
                }
            }
        }
예제 #8
0
            public static string ParseSqlString(string sqlString, out IList <ParseError> errors)
            {
                using (var sr = new StringReader(sqlString)) {
                    var fragment = Parser.Parse(sr, out errors);

                    if (errors.Any())
                    {
                        return(string.Empty);
                    }

                    var sb = new StringBuilder();
                    using (var strStream = new StringWriter(sb)) {
                        ScriptGen.GenerateScript(fragment, strStream);
                    }

                    return(sb.ToString());
                }
            }
예제 #9
0
        public TSqlFragment Parse(string input)
        {
            using var reader = new StringReader(input);
            var result = _parser.Parse(reader, out IList <ParseError> errors);

            QsiSyntaxErrorException[] syntaxErrors = errors
                                                     .Select(error => new QsiSyntaxErrorException(error.Line, error.Column, error.Message))
                                                     .ToArray();

            if (syntaxErrors.Length == 1)
            {
                throw syntaxErrors[0];
            }

            if (syntaxErrors.Length > 1)
            {
                throw new AggregateException(syntaxErrors.Cast <Exception>());
            }

            return(result);
        }
예제 #10
0
        public TSqlFragment GetFragment(TextReader txtRdr, out IList <ParseError> errors)
        {
            var fragment = parser.Parse(txtRdr, out errors);

            return(fragment?.FirstTokenIndex != -1 ? fragment : null);
        }