Ejemplo n.º 1
0
        private void executeSQL(string sql, params object[] args)
        {
            if(args.Length>0)
                sql = String.Format(sql, args);

            Stopwatch watch = new Stopwatch();
            watch.Start();
            DataSet ds = null;

            try
            {
                if (sql.Length > 500000) // massive SQL
                {
                    SQLParser.Tokenizer tokenizer = new SQLParser.Tokenizer(new StringReader(sql));
                    SQLParser.Token token = tokenizer.ReadNextToken();
                    List<string> sqlList = new List<string>();
                    string anSql = "";
                    while (token != null)
                    {
                        if (token.Value == ";")
                        {
                            sqlList.Add(anSql);
                            anSql = "";
                        }
                        else
                        {
                            anSql += " " + token.Value;
                        }
                        token = tokenizer.ReadNextToken();
                    }
                    if (anSql != "") sqlList.Add(anSql);

                    StringBuilder queriesWithError = new StringBuilder();

                    BackgroundWorkerDialog bwd = new BackgroundWorkerDialog();
                    bwd.MessageFormat = "Please wait while {0} query executed.";
                    bwd.DoWork = () => {
                        int sqlListLength = sqlList.Count;
                        for (int i = 0; i < sqlListLength && !bwd.Canceled; i++)
                        {
                            try
                            {
                                Provider.Database.ExecuteNonQuery(sqlList[i]);
                            }
                            catch (Exception ex)
                            {
                                queriesWithError.AppendLine("/* " + ex.Message + " */");
                                queriesWithError.AppendLine(sqlList[i]);
                            }

                            int percent = Convert.ToInt32((i / (float)sqlListLength) * 100);
                            bwd.ReportProgress(percent, sqlList.Count-i);
                        }
                        return "";
                    };
                    bwd.ShowDialog();

                    watch.Stop();
                    SetStatusText("Queries executed succesfully.");

                    statusExecTime.Text = watch.ElapsedMilliseconds + " ms";
                    statusNumberOfRows.Text = "";

                    if (CurrSQLEditor == null) findOrCreateNewSQLEditor();
                    if (queriesWithError.Length > 0)
                        CurrSQLEditor.ShowInfoText("/* QUERIES WITH ERROR */\r\n\r\n" + queriesWithError);
                }
                else // smaller SQL
                {
                    ds = Provider.Database.GetDataSet(sql);
                    watch.Stop();

                    SetStatusText("Query executed succesfully.");

                    statusExecTime.Text = watch.ElapsedMilliseconds + " ms";
                    statusNumberOfRows.Text = (ds.Tables.Count == 0 ? 0 : ds.Tables[0].Rows.Count) + " rows";
                    sql = sql.Replace("\n", " ").Replace("\r", "").Replace("\t", " ") + (sql.EndsWith(";") ? "" : ";");
                    while (sql.Contains("  ")) sql = sql.Replace("  ", " ");
                    if (sql.Length > 1000) sql = sql.Substring(0, 1000) + "...";

                    if (CurrSQLEditor == null) findOrCreateNewSQLEditor();

                    CurrSQLEditor.SQLLog.Text += string.Format(Environment.NewLine + "/*[{0} - {1,5} ms]*/ {2}", DateTime.Now.ToString("hh:mm:ss"), watch.ElapsedMilliseconds, sql);

                    if (ds.Tables.Count > 1)
                        CurrSQLEditor.BindGridResults(ds);
                    else if (ds.Tables.Count == 1)
                        CurrSQLEditor.BindGridResults(ds.Tables[0]);
                    else
                        CurrSQLEditor.BindGridResults(null);
                }
            }
            catch (Exception ex)
            {
                CurrSQLEditor.ShowInfoText(ex.Message + (ex.InnerException != null ? " (" + ex.InnerException.Message + ")" : ""));
            }
        }
Ejemplo n.º 2
0
        private void cmdSearchTableNamesInFiles(string arg)
        {
            var f = new FormSearchProjectFolder();

            if (f.ShowDialog() == DialogResult.OK)
            {
                var allFiles = new List<string>();
                foreach (string ext in f.SearchExtensions)
                    allFiles.AddRange(Directory.EnumerateFiles(f.ProjectFolder, "*" + ext, SearchOption.AllDirectories));

                string[] keyWords;

                string strAllFileContent = null;

                if (f.WhatToSearch == "Image names not used in Project Folder")
                {
                    var imageNames = new List<string>();
                    foreach (string ext in new[] { ".jpg", ".png", ".gif" })
                        imageNames.AddRange(Directory.EnumerateFiles(f.ProjectFolder, "*" + ext,
                                                                     SearchOption.AllDirectories));
                    keyWords = imageNames.ToArray();

                    var allFilesContent = new StringBuilder();
                    foreach (var file in allFiles)
                        allFilesContent.AppendLine(File.ReadAllText(file).ToUpper());
                    strAllFileContent = allFilesContent.ToString();
                    allFilesContent.Clear();
                    allFilesContent = null;
                }
                else if (f.WhatToSearch == "Javascript files not used in Project Folder")
                {
                    var imageNames = new List<string>();
                    imageNames.AddRange(Directory.EnumerateFiles(f.ProjectFolder, "*.JS", SearchOption.AllDirectories));
                    keyWords = imageNames.ToArray();

                    var allFilesContent = new StringBuilder();
                    foreach (var file in allFiles)
                        allFilesContent.AppendLine(File.ReadAllText(file).ToUpper());
                    strAllFileContent = allFilesContent.ToString();
                    allFilesContent.Clear();
                    allFilesContent = null;
                }
                else if (f.WhatToSearch == "CSS files not used in Project Folder")
                {
                    var imageNames = new List<string>();
                    imageNames.AddRange(Directory.EnumerateFiles(f.ProjectFolder, "*.CSS", SearchOption.AllDirectories));
                    keyWords = imageNames.ToArray();

                    StringBuilder allFilesContent = new StringBuilder();
                    foreach (var file in allFiles)
                        allFilesContent.AppendLine(File.ReadAllText(file).ToUpper());
                    strAllFileContent = allFilesContent.ToString();
                    allFilesContent.Clear();
                    allFilesContent = null;
                }
                else if (f.WhatToSearch == "ASPX files not used in Project Folder")
                {
                    var imageNames = new List<string>();
                    imageNames.AddRange(Directory.EnumerateFiles(f.ProjectFolder, "*.ASPX", SearchOption.AllDirectories));
                    keyWords = imageNames.ToArray();

                    StringBuilder allFilesContent = new StringBuilder();
                    foreach (var file in allFiles)
                        allFilesContent.AppendLine(File.ReadAllText(file).ToUpper());
                    strAllFileContent = allFilesContent.ToString();
                    allFilesContent.Clear();
                    allFilesContent = null;
                }
                else
                {
                    if (f.WhatToSearch.StartsWith("Table"))
                        keyWords = Provider.Database.Tables.Where(t => !t.IsView).Select(t => t.Name).ToArray();
                    else
                        keyWords = Provider.Database.Tables.Where(t => t.IsView).Select(t => t.Name).ToArray();
                }

                BackgroundWorkerDialog bwd = new BackgroundWorkerDialog();
                bwd.MessageFormat = "Please wait while " + keyWords.Length + " keywords being searched. ({0})";
                bwd.DoWork = () =>
                    {
                        StringBuilder sb = new StringBuilder();

                        if (f.WhatToSearch != "Image names not used in project folder" && f.WhatToSearch != "Javascript files not used in Project Folder" && f.WhatToSearch != "CSS files not used in Project Folder" && f.WhatToSearch != "ASPX files not used in Project Folder")
                            sb.AppendLine("create table search_results (keyword varchar(255), object_name varchar(255), content text);");

                        for (int i = 0; i < keyWords.Length && !bwd.Canceled; i++)
                        {
                            string kw = keyWords[i];

                            if (f.WhatToSearch == "Image names not used in project folder" || f.WhatToSearch == "Javascript files not used in Project Folder" || f.WhatToSearch == "CSS files not used in Project Folder" || f.WhatToSearch == "ASPX files not used in Project Folder")
                            {
                                if (strAllFileContent.IndexOf(Path.GetFileName(kw).ToUpper()) == -1)
                                    sb.AppendLine(kw);
                            }
                            else
                            {
                                var allMatches = allFiles
                                                     .SelectMany(fn => File.ReadLines(fn), (fn, line) => new {fn, line})
                                                     .Where(@t => @t.line.ToUpper().Contains(kw.ToUpper()) &&
                                                                  (@t.line.ToUpper().Contains("CREATEOBJECTSET") ||
                                                                   @t.line.ToUpper().Contains("ENTİTY") ||
                                                                   @t.line.ToUpper().Contains("CLASS") ||
                                                                   @t.line.ToUpper().Contains("OBJDB") ||
                                                                   @t.line.ToUpper().Contains("SELECT ") ||
                                                                   @t.line.ToUpper().Contains("İNSERT ") ||
                                                                   @t.line.ToUpper().Contains("UPDATE ") ||
                                                                   @t.line.ToUpper().Contains("DELETE ")))
                                                     .Select(@t => new
                                                         {
                                                             Keyword = kw,
                                                             File = @t.fn.Replace(f.ProjectFolder + "\\", ""),
                                                             Line = @t.line.Trim(),
                                                         });

                                foreach (var matchInfo in allMatches)
                                    sb.AppendFormat("insert into search_results values('{0}', '{1}', '{2}');\r\n",
                                                    matchInfo.Keyword,
                                                    matchInfo.File.Replace("\\", "\\\\").Replace("'", "\\'"),
                                                    matchInfo.Line.Replace("'", "\\'"));
                            }

                            int percent = Convert.ToInt32((i/(float) keyWords.Length)*100);
                            bwd.ReportProgress(percent, Path.GetFileName(kw));
                        }

                        return sb.ToString();
                    };
                bwd.Completed = () => addFileEditor("", bwd.Result);
                bwd.Show();
            }
        }