Exemplo n.º 1
0
        private void AddDatabaseCommand(object parameter)
        {
            if (!(parameter is TreeNodeViewModel treeNode))
            {
                return;
            }
            if (!(treeNode.NodePayload is DatabaseServer server))
            {
                return;
            }

            IMetadataService    metadata  = Services.GetService <IMetadataService>();
            List <DatabaseInfo> databases = metadata.GetDatabases(server);

            if (databases.Count == 0)
            {
                MessageBox.Show("Список выбора баз данных пуст.",
                                "DaJet", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            SelectDatabaseWindow dialog = new SelectDatabaseWindow(databases);

            _ = dialog.ShowDialog();
            if (dialog.Result == null)
            {
                return;
            }

            if (DatabaseNameExists(server, dialog.Result))
            {
                MessageBox.Show("База данных " + dialog.Result.Name + " уже добавлена.",
                                "DaJet", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            DatabaseFormWindow form = new DatabaseFormWindow(dialog.Result);

            _ = form.ShowDialog();

            if (DatabaseNameExists(server, dialog.Result))
            {
                MessageBox.Show("База данных " + dialog.Result.Name + " уже добавлена.",
                                "DaJet", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            server.Databases.Add(dialog.Result);
            SaveMetadataSettings();

            InitializeMetadata(server, dialog.Result);

            TreeNodeViewModel databaseNode = CreateDatabaseTreeNode(treeNode, dialog.Result);

            treeNode.TreeNodes.Add(databaseNode);
            treeNode.IsExpanded     = true;
            databaseNode.IsSelected = true;

            InitializeDatabaseTreeNodes(databaseNode);
        }
Exemplo n.º 2
0
        private void EditDatabaseCommand(object parameter)
        {
            if (!(parameter is TreeNodeViewModel treeNode))
            {
                return;
            }
            if (!(treeNode.NodePayload is DatabaseInfo database))
            {
                return;
            }
            DatabaseServer server = treeNode.GetAncestorPayload <DatabaseServer>();

            if (server == null)
            {
                return;
            }

            // make copy of database settings to rollback changes if needed
            DatabaseInfo databaseCopy = database.Copy();

            // edit server settings
            DatabaseFormWindow dialog = new DatabaseFormWindow(databaseCopy);

            _ = dialog.ShowDialog();
            if (dialog.Result == null)
            {
                return;
            }

            string databaseCopyName    = databaseCopy.Name;
            string databaseCopyAlias   = databaseCopy.Alias;
            bool   databaseNameChanged = (databaseCopy.Name != database.Name);

            // check if new database name already exists
            if (databaseNameChanged)
            {
                if (DatabaseNameExists(server, databaseCopy))
                {
                    MessageBox.Show("База данных " + databaseCopyName + " уже сущестует.",
                                    "DaJet", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }
            }

            // persist database settings changes
            databaseCopy.CopyTo(database);
            SaveMetadataSettings();

            if (databaseNameChanged)
            {
                for (int i = treeNode.TreeNodes.Count - 1; i > 0; i--)
                {
                    if (treeNode.TreeNodes[i].NodeText != SCRIPTS_NODE_NAME)
                    {
                        treeNode.TreeNodes.RemoveAt(i);
                    }
                }
                database.BaseObjects.Clear();

                InitializeMetadata(server, database);
                InitializeDatabaseTreeNodes(treeNode);
                treeNode.IsSelected = true;
            }
            // show new database name and alias in UI
            treeNode.NodeText    = databaseCopyName;
            treeNode.NodeToolTip = databaseCopyAlias;
        }