示例#1
0
        public static RunScriptResult RunScript(string script, string connectionString)
        {
            var runErrors = new List <string>();

            using (TextReader reader = new StringReader(script))
            {
                TSqlParser   parser   = new TSql110Parser(true);
                TSqlFragment fragment = parser.Parse(reader, out var errors);
                if (errors != null && errors.Count > 0)
                {
                    foreach (ParseError error in errors)
                    {
                        runErrors.Add($"Line: {error.Line}, Column: {error.Column}: {error.Message}");
                    }

                    return(new RunScriptResult {
                        Errors = runErrors.ToArray()
                    });
                }

                SqlScriptGenerator sqlScriptGenerator = new Sql110ScriptGenerator();
                if (!(fragment is TSqlScript sqlScript))
                {
                    sqlScriptGenerator.GenerateScript(fragment, out var sql);
                    if (!ExecuteSql(sql, connectionString, runErrors))
                    {
                        return(new RunScriptResult {
                            Errors = runErrors.ToArray()
                        });
                    }
                }
        public override void ExplicitVisit(InsertStatement node)
        {
            base.ExplicitVisit(node);

            NewStatement.StartPos = node.StartOffset;
            NewStatement.Length = node.FragmentLength;

            var whyle = BuildWhileStatement(node.InsertSpecification);

            var scriptGen = new Sql110ScriptGenerator();
            string script;
            scriptGen.GenerateScript(whyle, out script);

            NewStatement.NewText = script;
        }
        public override void ExplicitVisit(InsertStatement node)
        {
            base.ExplicitVisit(node);

            NewStatement.StartPos = node.StartOffset;
            NewStatement.Length   = node.FragmentLength;

            var whyle = BuildWhileStatement(node.InsertSpecification);

            var    scriptGen = new Sql110ScriptGenerator();
            string script;

            scriptGen.GenerateScript(whyle, out script);


            NewStatement.NewText = script;
        }
示例#4
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());
                }
            }
示例#5
0
        public static string FormatSqlCode(string query)
        {
            var parser = new TSql110Parser(false);
            IList <ParseError> errors;
            var parsedQuery = parser.Parse(new StringReader(query), out errors);

            var generator = new Sql110ScriptGenerator(new SqlScriptGeneratorOptions()
            {
                KeywordCasing              = KeywordCasing.Uppercase,
                IncludeSemicolons          = true,
                NewLineBeforeFromClause    = true,
                NewLineBeforeOrderByClause = true,
                NewLineBeforeWhereClause   = true,
                AlignClauseBodies          = false
            });
            string formattedQuery;

            generator.GenerateScript(parsedQuery, out formattedQuery);
            return(formattedQuery);
        }
示例#6
0
        private void button1_Click(object sender, EventArgs e)
        {
            TextReader rdr = new StringReader(textBox1.Text);

            IList <ParseError> errors = null;
            TSql110Parser      parser = new TSql110Parser(true);
            TSqlFragment       tree   = parser.Parse(rdr, out errors);

            foreach (ParseError err in errors)
            {
                Console.WriteLine(err.Message);
            }

            Sql110ScriptGenerator scrGen = new Sql110ScriptGenerator();
            string formattedSQL          = null;

            scrGen.GenerateScript(tree, out formattedSQL);

            textBox2.Text = formattedSQL;

            rdr.Dispose();
        }