Esempio n. 1
0
        public override void PopulateTable(ListView l, TreeNode node, string table, string database)
        {
            PopulateTable_iteration++;
            int store = PopulateTable_iteration;

            Main.queuetimer = 5;
            lock (threadlock)
            {
                try
                {
                    if (PopulateTable_iteration != store)
                    {
                        return;
                    }
                    l.Invoke((Common.Action) delegate { l.Columns.Clear(); l.Items.Clear(); });

                    SQLResult columns = this.Query("SHOW COLUMNS FROM `" + Main.Sanitize(database) + "`.`" + Main.Sanitize(table) + "`");
                    foreach (object[] col in columns.Rows)
                    {
                        l.BeginInvoke((Common.Action) delegate { l.Columns.Add((string)col[0]); });
                    }

                    SQLResult rows = this.Query("SELECT * FROM `" + Main.Sanitize(database) + "`.`" + Main.Sanitize(table) + "` LIMIT 0,50");
                    l.Invoke((Common.Action) delegate { l.BeginUpdate(); });
                    foreach (object[] row in rows.Rows)
                    {
                        l.Invoke((Common.Action) delegate
                        {
                            ListViewItem i = new ListViewItem(row[0].ToString());
                            bool temp      = true;
                            foreach (object obj in row)
                            {
                                string tobj = obj.ToString();
                                if (tobj.Length > 100)
                                {
                                    tobj = tobj.Substring(0, 100) + "...";
                                }
                                if (temp)
                                {
                                    temp = false;
                                }
                                else
                                {
                                    i.SubItems.Add(tobj);
                                }
                            }
                            l.Items.Add(i);
                        });
                    }

                    l.Invoke((Common.Action) delegate
                    {
                        foreach (ColumnHeader h in l.Columns)
                        {
                            h.Width = -1;
                        }
                        l.EndUpdate();
                    });
                    while (Main.queuetimer > 0)
                    {
                        Thread.Sleep(100); Main.queuetimer--;
                    }
                    ;
                }
                catch { l.BeginInvoke((Common.Action) delegate { l.EndUpdate(); node.ImageIndex = 3; node.SelectedImageIndex = 3; }); }
            }
        }
Esempio n. 2
0
        private void btnExec_Click(object sender, EventArgs e)
        {
            string[] spl = Regex.Split(Regex.Replace(queryBox.Text, @";$", ""), @";[\r\n][\r\n]?");

            try
            {
                int affected             = 0;
                int selected             = 0;
                int queries              = 0;
                List <SQLResult> results = new List <SQLResult>();
                foreach (string q in spl)
                {
                    if (q == "")
                    {
                        continue;
                    }
                    queries++;

                    SQLResult res = m.SQL.Query(q);
                    if (res.Affected > 0)
                    {
                        affected += res.Affected;
                    }
                    if (res.Result > 0)
                    {
                        selected += res.Result;
                    }

                    results.Add(res);
                    //if (res != null) MessageBox.Show(res.Result + ":" + res.Affected);
                }

                if (selected > 0)
                {
                    m.listResults.Clear();

                    bool caught = false;
                    foreach (TabPage p in m.tabs.TabPages)
                    {
                        if (p == m.tabResults)
                        {
                            caught = true; break;
                        }
                    }
                    if (!caught)
                    {
                        m.tabs.TabPages.Add(m.tabResults);
                    }
                    m.tabs.SelectedTab = m.tabResults;
                    m.tabResults.Select();

                    m.listResults.Select();
                    m.listResults.Focus();

                    this.Activate();

                    m.listResults.BeginUpdate();
                    foreach (SQLResult res in results)
                    {
                        if (res.Result < 1)
                        {
                            continue;
                        }
                        foreach (string column in res.Columns)
                        {
                            m.listResults.Columns.Add(new ColumnHeader()
                            {
                                Text = column
                            });
                        }
                        foreach (object[] obj in res.Rows)
                        {
                            if (obj.Length < 1)
                            {
                                continue;
                            }
                            ListViewItem i    = new ListViewItem(obj[0].ToString());
                            bool         temp = true;
                            foreach (object o in obj)
                            {
                                if (temp)
                                {
                                    temp = false;
                                }
                                else
                                {
                                    string tobj = o.ToString();
                                    if (tobj.Length > 100)
                                    {
                                        tobj = tobj.Substring(0, 100) + "...";
                                    }
                                    i.SubItems.Add(tobj);
                                }
                            }
                            m.listResults.Items.Add(i);
                        }
                        break;
                    }
                    foreach (ColumnHeader h in m.listResults.Columns)
                    {
                        h.Width = -1;
                    }
                    m.listResults.EndUpdate();
                }

                string TO_S_OR_NOT_TO_S /*, that is the question*/ = (queries > 1 ? " queries" : " query");
                if (affected > 0)
                {
                    MessageBox.Show(queries + TO_S_OR_NOT_TO_S + " executed successfully, " + affected + " rows affected", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (queries > 0)
                {
                    MessageBox.Show(queries + TO_S_OR_NOT_TO_S + " executed successfully", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 3
0
        public Main(SQLType sql)
        {
            InitializeComponent();

            this.Icon = OutOfTheBox.Properties.Resources.SQL_icon;

            ImageList imglist = new ImageList();

            imglist.ImageSize = new Size(16, 16);
            imglist.Images.Add(Properties.Resources.SQL_database);
            imglist.Images.Add(Properties.Resources.SQL_table);
            imglist.Images.Add(Properties.Resources.SQL_hourglass);
            imglist.Images.Add(Properties.Resources.SQL_table_error);
            treeSchemata.ImageList = imglist;

            this.SQL = sql;

            this.FormClosed += delegate { SQL.Dispose(); };

            if (sql.GetType() == typeof(MySQL))
            {
                SQLResult q = SQL.Query("SELECT VERSION()");
                if (q.Rows.Length == 1 && q.Rows[0].Length == 1 && q.Rows[0][0].GetType() == typeof(string))
                {
                    version = (string)q.Rows[0][0];
                    //label1.Text = version;
                    string[] ver = version.Split('.');
                    int      v = 0, vminor = 0;
                    if (Int32.TryParse(ver[0].ToString(), out v) && Int32.TryParse(ver[1].ToString(), out v) && (v > 4 || (v == 4 && vminor > 12)))
                    {
                        ((MySQL)sql).schemata = true;
                    }
                }
                //else label1.Text = "Unknown";

                Common.Async.StartAsync((Common.Action) delegate
                {
                    sql.PopulateTree(treeSchemata);
                    BeginInvoke((Common.Action) delegate { lSchemata.Visible = false; });
                });
            }


            #region Events
            treeSchemata.BeforeExpand += (object sender, TreeViewCancelEventArgs e) =>
            {
                if (e.Node.Nodes.Count > 0 && e.Node.Nodes[0].Text == "Populating...")
                {
                    Common.Async.StartAsync((Common.Action) delegate {
                        sql.PopulateNode(e.Node);
                        BeginInvoke((Common.Action) delegate { e.Node.FirstNode.Remove(); });
                    });
                }
            };

            int state = -1;
            treeSchemata.AfterSelect += (object sender, TreeViewEventArgs e) =>
            {
                if (Main.queuetimer > 0)
                {
                    Main.queuetimer = 5;
                }
                if (e.Node.Text == "Populating...")
                {
                    return;
                }

                if (state != e.Node.Level)
                {
                    foreach (TabPage p in tabs.TabPages)
                    {
                        if (p != tabResults)
                        {
                            tabs.TabPages.Remove(p);
                        }
                    }
                }
                if (e.Node.Level == 0)
                {
                    if (state != e.Node.Level)
                    {
                        tabRows.Text = "Tables";
                        tabs.TabPages.Add(tabRows);
                    }
                    menuFileDumpDatabase.Enabled = true;
                    menuFileDumpTable.Enabled    = menuEditInsert.Enabled = false;
                }
                else if (e.Node.Level == 1)
                {
                    if (state != e.Node.Level)
                    {
                        tabRows.Text = "Rows";
                        tabs.TabPages.Add(tabRows);
                        tabs.TabPages.Add(tabStructure);
                    }
                    menuEditInsert.Enabled = menuFileDumpDatabase.Enabled = menuFileDumpTable.Enabled = true;

                    Common.Async.StartAsync((Common.Action) delegate
                    {
                        sql.PopulateTable(listRows, e.Node, e.Node.Text, e.Node.Parent.Text);
                        Main.queuetimer = 0;
                    });
                }
                else
                {
                    menuEditInsert.Enabled            =
                        menuFileDumpDatabase.Enabled  =
                            menuFileDumpTable.Enabled = false;
                }
                state = e.Node.Level;
                (sender as TreeView).Focus();
            };

            listRows.SelectedIndexChanged += delegate
            {
                menuEditEdit.Enabled = menuEditRemove.Enabled = listRows.SelectedItems.Count > 0;
            };
            #endregion

            tabs.SelectedIndexChanged += delegate { linkClose.Visible = tabs.SelectedTab == tabResults; };

            foreach (TabPage p in tabs.TabPages)
            {
                tabs.TabPages.Remove(p);
            }

            SQL.OnInitialized(this);
        }