Example #1
0
        public ArchiveEntryTabPage(DatabaseObject table, TableDependencyGraph dependencies)
        {
            InitializeComponent();

            filterConditionsPanel.LoadColumns(table);

            ISet <DependencyNode> dependentTables = dependencies.GetAncestors(table);

            dependentTables.Add(dependencies[table]);
            tableLayoutPanelTablesnames.SuspendLayout();
            int columnOffset = 0;

            foreach (DependencyNode dependentTable in dependentTables)
            {
                int   row            = tableLayoutPanelTablesnames.RowCount;
                Label labelTablename = NewLabelTablename();
                labelTablename.Text = dependentTable.DbObject.NameWithSchema;
                tableLayoutPanelTablesnames.Controls.Add(labelTablename, 0 + columnOffset, row);
                TextBox textBoxTableName = NewTextBoxTablename();
                textBoxTableName.Text = dependentTable.DbObject.NameWithSchema;
                tableLayoutPanelTablesnames.Controls.Add(textBoxTableName, 1 + columnOffset, row);
                if (columnOffset > 0)
                {
                    tableLayoutPanelTablesnames.RowStyles.Add(new RowStyle());
                    tableLayoutPanelTablesnames.RowCount = row + 1;
                    columnOffset = 0;
                }
                else
                {
                    columnOffset += 2;
                }
            }
            tableLayoutPanelTablesnames.ResumeLayout(false);
            tableLayoutPanelTablesnames.PerformLayout();

            Dock = DockStyle.Fill;
        }
Example #2
0
        private void ArchiveTablesForm_Load(object sender, EventArgs e)
        {
            try
            {
                _dependencies = Optimizer.Instance.DependencyGraph;
            }
            catch (TableDependencyException exc)
            {
                Debug.WriteLine(exc);
                Cursor.Current = Cursors.Default;
                MessageBox.Show(this, "Dependencies cannot be loaded", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
                return;
            }
            IList <Tuple <DatabaseObject, DatabaseObject> > removedTables = new List <Tuple <DatabaseObject, DatabaseObject> >();

            for (int i = 0; i < _tablesLimit.Count; i++)
            {
                ISet <DatabaseObject> nodes = new HashSet <DatabaseObject>(_dependencies.GetAncestors(_tablesLimit[i]).Select(t => t.DbObject));
                for (int j = 0; j < _tablesLimit.Count; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    if (nodes.Contains(_tablesLimit[j]))
                    {
                        removedTables.Add(new Tuple <DatabaseObject, DatabaseObject>(_tablesLimit[j], _tablesLimit[i]));
                        _tablesLimit.Remove(_tablesLimit[j]);
                        if (j < i)
                        {
                            i--;
                        }
                        j--;
                    }
                }
            }
            IList <string> errorTableList = new List <string>();

            foreach (DatabaseObject table in _tablesLimit)
            {
                try
                {
                    ArchiveEntryTabPage archiveEntryTabPage = new ArchiveEntryTabPage(table, _dependencies);
                    TabPage             page = new TabPage(table.NameWithSchema);
                    tabControlTables.TabPages.Add(page);
                    page.Controls.Add(archiveEntryTabPage);
                }
                catch (DatabaseException exc)
                {
                    Debug.WriteLine(exc);
                    errorTableList.Add(table.NameWithSchema);
                }
            }
            Cursor.Current = Cursors.Default;
            if (errorTableList.Count > 0)
            {
                MessageBox.Show(this, $"Columns of tables {string.Join(", ", errorTableList)} cannot be loaded", "Loading error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (removedTables.Count > 0)
            {
                MessageBox.Show(this, $"These tables are dependent on others and there is no need to archive them multiple times.{Environment.NewLine}{string.Join(Environment.NewLine, removedTables.Select(t => t.Item1 + " -> " + t.Item2))}", "Table dependency check", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Example #3
0
        private static void ProcessArchiveCommand(string args)
        {
            Regex          archiveRegex = new Regex(@"^(?:(?:(?<is>-is)?|(?<all>-all)?|(?<ct>-ct)?) )*(?<filename>(?:\S+|""(?:\S| )+"")) (?<table>(?:(?:\[(?:\S| )+\])|\S|\.)+)(?: (?:-n (?<n>(?: ?(?<oldTable>(?:(?:\[(?:\S| )+\])|\S|\.)+)\=(?<newTable>(?:(?:\[(?:\S| )+\])|\S|\.)+))+)))?(?: (?:-c (?<c>.+)))?$");
            Match          archiveMatch = archiveRegex.Match(args);
            DatabaseObject archiveTable;

            if (!archiveMatch.Success)
            {
                Console.WriteLine($"Error: Invalid syntax for command {ARCHIVE_ENTRIES_COMMAND}, syntax information can be showed by typing: help {ARCHIVE_ENTRIES_COMMAND}");
                return;
            }
            else if (!_tableByName.TryGetValue(archiveMatch.Groups["table"].Value.Replace("[", "").Replace("]", "").ToLower(), out archiveTable))
            {
                Console.WriteLine($"Error: Table {archiveMatch.Groups["table"].Value} not found");
                return;
            }
            try
            {
                using (StreamWriter fileStream = new StreamWriter(archiveMatch.Groups["filename"].Value))
                {
                    ISet <DependencyNode> dependentTables = _dependencies.GetAncestors(archiveTable);
                    dependentTables.Add(_dependencies[archiveTable]);
                    bool   createTable         = false;
                    bool   useInsertIntoSelect = false;
                    bool   saveAll             = false;
                    string conditions          = "";
                    IDictionary <string, string> nameConvertor = new Dictionary <string, string>(dependentTables.Count);
                    foreach (DependencyNode table in dependentTables)
                    {
                        nameConvertor.Add(table.DbObject.NameWithSchema.ToLower(), table.DbObject.NameWithSchemaBrackets);
                    }
                    if (archiveMatch.Groups["n"].Success)
                    {
                        for (int i = 0; i < archiveMatch.Groups["newTable"].Captures.Count; i++)
                        {
                            nameConvertor[archiveMatch.Groups["oldTable"].Captures[i].Value.Replace("[", "").Replace("]", "").ToLower()] = archiveMatch.Groups["newTable"].Captures[i].Value;
                        }
                    }
                    if (archiveMatch.Groups["ct"].Success)
                    {
                        createTable = true;
                    }
                    if (archiveMatch.Groups["c"].Success)
                    {
                        conditions = archiveMatch.Groups["c"].Value;
                    }
                    if (archiveMatch.Groups["is"].Success)
                    {
                        useInsertIntoSelect = true;
                    }
                    if (archiveMatch.Groups["all"].Success)
                    {
                        saveAll = true;
                    }
                    Console.WriteLine("Archiving in progress...");
                    if (_optimizer.ArchiveTable(archiveTable, nameConvertor, createTable, conditions, useInsertIntoSelect, saveAll, fileStream))
                    {
                        Console.WriteLine("Archive has been created and saved");
                    }
                    else
                    {
                        Console.WriteLine("Nothing to archive");
                    }
                }
            }
            catch (DatabaseException exc)
            {
                Debug.WriteLine(exc);
                Console.WriteLine($"Error: cannot get data - {exc.InnerException.Message}");
                return;
            }
            catch (IOException exc)
            {
                Debug.WriteLine(exc);
                Console.WriteLine($"Error: problem data file - {exc.Message}");
                return;
            }
        }