async void _build_syntax_btn_Click(object sender, RoutedEventArgs e)
        {
            var syntax_txt = _query_tb.Text;
            var bg_default = _analysis_tb.Background;

            _analysis_tb.Background = Brushes.Gold;

            var analysis_txt = await Task.Factory.StartNew(() => {
                var parser = new TSql130Parser(false);
                IList <ParseError> errors;
                var fragment = parser.Parse(new StringReader(syntax_txt), out errors);
                if (errors.Count > 0)
                {
                    return("ERROR:\n" + String.Join("\n", errors.Select(err => err.Message)));
                }

                fragment.Accept(new MyNaiveMutator());
                var renderer = new Sql130ScriptGenerator();
                string sql;
                renderer.GenerateScript(fragment, out sql);
                return(sql);
            });

            this.Dispatcher.Invoke(() => {
                _analysis_tb.Text       = analysis_txt;
                _analysis_tb.Background = bg_default;
            });
        }
示例#2
0
 public static string GenerateTSql(TSqlFragment script)
 {
     var generator = new Sql130ScriptGenerator(Settings.SavedSettings.Get().GeneratorOptions);
     var builder = new StringBuilder();
     generator.GenerateScript(script, new StringWriter(builder));
     return builder.ToString();
 }
示例#3
0
 /// <summary>
 /// Uses the SQL Formatter to pretty print the code; not strictly necessary as the only place you will see it is in Profiler :)
 /// However, by reparsing the code we ensure that any errors in conversion are caught.
 /// </summary>
 /// <exception cref="Exception">Throws a generic exception if there is a parse error</exception>
 protected void FormatSQL()
 {
     if (reparse)
     {
         // use the features in the ScriptDom namespace to pretty print our T-SQL
         TextReader         rdr    = new StringReader(SqlStmt.ToString());
         IList <ParseError> errors = null;
         TSql130Parser      parser = new TSql130Parser(true);
         TSqlFragment       tree   = parser.Parse(rdr, out errors);
         rdr.Close();
         if (errors.Count > 0)
         {
             Exception e = new Exception(string.Format("Parse Error after converstion on line {0}, column {1}: {2}", errors[0].Line, errors[0].Column, errors[0].Message));
             throw e;
         }
         else
         {
             Sql130ScriptGenerator scrGen = new Sql130ScriptGenerator();
             string formattedSQL          = null;
             scrGen.GenerateScript(tree, out formattedSQL);
             cmd.CommandText = formattedSQL;
         }
     }
     else
     {
         cmd.CommandText = SqlStmt.ToString();
     }
 }
示例#4
0
        public static string GenerateTSql(TSqlFragment script)
        {
            var generator = new Sql130ScriptGenerator(SavedSettings.Get().GeneratorOptions);
            var builder   = new StringBuilder();

            generator.GenerateScript(script, new StringWriter(builder));
            return(builder.ToString());
        }
示例#5
0
        /// <summary>
        /// SQL Query string parse and format
        /// </summary>
        /// <param name="sqlScript">Unformated and unparsed SQL Query String</param>
        /// <param name="useScriptOptions">Do we use special format options or by Default. If set to useScriptOptions true and useConfFile false
        /// gets the settings from Form.</param>
        /// <param name="useConfFile">Do we use the configuration for formats from CustomFormatConfiguration.xlsx file?
        /// Both useScriptOptions and useConfFile should be true!</param>
        /// <param name="confFilePath">The full path to the file "CustomFormatConfiguration.xlsx" holding the custom format logic </param>
        /// <returns></returns>
        public string SQLScriptFormater(string sqlScript, bool useScriptOptions, bool useConfFile, string confFilePath)
        {
            try
            {
                string strFormattedSQL = null;
                using (TextReader rdr = new StringReader(sqlScript))
                {
                    TSql130Parser      parser = new TSql130Parser(true);
                    IList <ParseError> errors = null;
                    TSqlFragment       tree   = parser.Parse(rdr, out errors);

                    if (errors.Count > 0)
                    {
                        foreach (ParseError err in errors)
                        {
                            strFormattedSQL = strFormattedSQL + err.Message + "\rn";
                        }
                        return(strFormattedSQL);
                    }
                    Sql130ScriptGenerator srcGen = new Sql130ScriptGenerator();
                    if (useScriptOptions)
                    {
                        //if we use the Excel ConfigFile
                        if (useConfFile)
                        {
                            SqlScriptGeneratorOptions optionsConfig = new SqlScriptGeneratorOptions();
                            //string confFilePath = System.Environment.CurrentDirectory + "\\" + "CustomFormattingSettings.xlsx";
                            try
                            {
                                if (!File.Exists(confFilePath))
                                {
                                    throw new FileNotFoundException("File not found in App directory", "CustomFormatConfiguration.xlsx");
                                }
                            }
                            catch (FileNotFoundException ex)
                            {
                                MessageBox.Show("Error: " + ex.Message + "\n" + ex.FileName);
                            }

                            DataTable formatConfiguration = ReadExcelFile.getExcellToDtbl(confFilePath);
                            foreach (DataRow dr in formatConfiguration.Rows)
                            {
                                setScriptOptions(ref optionsConfig, null, dr);
                            }
                            addScriptOptionsToScriptGen(optionsConfig, ref srcGen);
                        }
                        //If we use the interface
                        else
                        {
                            //Search for our GroupBox in SQLFormatForm
                            GroupBox formatSettings            = new GroupBox();
                            IEnumerable <Control> listControls = null;
                            foreach (System.Windows.Forms.Form form in Application.OpenForms)
                            {
                                if (form.Name == "SQLFormatForm")
                                {
                                    listControls = GetAll(form, "gbFormatSettings");
                                }
                            }
                            var control = listControls.First();
                            formatSettings = control as GroupBox;
                            //Loop trought all controls in the group box and set format Settings
                            SqlScriptGeneratorOptions optionsConfig = new SqlScriptGeneratorOptions();
                            foreach (Control ctrl in formatSettings.Controls)
                            {
                                if ((ctrl is TextBox) || (ctrl is ComboBox) || (ctrl is CheckBox))
                                {
                                    setScriptOptions(ref optionsConfig, ctrl, null);
                                }
                            }
                            addScriptOptionsToScriptGen(optionsConfig, ref srcGen);
                        }
                    }
                    srcGen.GenerateScript(tree, out strFormattedSQL);
                    return(strFormattedSQL);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }