static int Main(string[] args) { var app = new CommandLineApplication(); app.HelpOption(); var scriptFileName = app.Argument <string>("script", "SQL script file name") .IsRequired(); var stripComments = app.Option("--strip", "Strip comments", CommandOptionType.NoValue); var singleStatement = app.Option("-s|--single", "Output single statements", CommandOptionType.NoValue); app.OnExecute( () => { // The default range searchers var rangeSearchers = new List <IRangeSearcher> { new MultiLineComment(), new DoubleDashSingleLineComment(), new PoundSignSingleLineComment(), new SqlString(), }; // The special token searchers var specialTokenSearchers = new List <ISpecialTokenSearcher>(); // Add SQL server specific range searcher rangeSearchers.Add(new SqlServerIdentifier()); // Add SQL server specific token searcher specialTokenSearchers.Add(new GoSearcher()); // We want every single SQL statement _outputEverySqlStatement = singleStatement.HasValue(); if (_outputEverySqlStatement) { specialTokenSearchers.Add(new SemicolonSearcher()); } var batchParser = new SqlBatchParser(rangeSearchers, specialTokenSearchers); batchParser.SpecialToken += BatchParserOnSpecialToken; batchParser.SqlText += BatchParserOnSqlText; using (var source = new TextReaderSource(new StreamReader(scriptFileName.Value), true)) { batchParser.Process(source, stripComments.HasValue()); } RunSql(); return(0); }); return(app.Execute(args)); }
private IEnumerable <string> SplitIntoSingleStatements(string sql) { var sqlStatements = new List <string>(); // The default range searchers var rangeSearchers = new List <IRangeSearcher> { new MultiLineComment(), new DoubleDashSingleLineComment(), new PoundSignSingleLineComment(), new SqlString(), new SqlServerIdentifier(), }; // The special token searchers var specialTokenSearchers = new List <ISpecialTokenSearcher>() { new GoSearcher(), new SemicolonSearcher(), }; var parser = new SqlBatchParser(rangeSearchers, specialTokenSearchers); parser.SqlText += (sender, args) => { var content = args.SqlText.Trim(); if (!string.IsNullOrEmpty(content)) { sqlStatements.Add(content + ";"); } }; using (var source = new TextReaderSource(new StringReader(sql), true)) { parser.Process(source); } return(sqlStatements); }