Esempio n. 1
0
        public ExplorerControl(NotebookManager manager, IWin32Window mainForm, Slot <bool> operationInProgress)
        {
            InitializeComponent();
            _list.EnableDoubleBuffer();
            _detailsLst.EnableDoubleBuffer();

            _mainForm                = mainForm;
            _manager                 = manager;
            _operationInProgress     = operationInProgress;
            _manager.NotebookChange += (sender, e) => HandleNotebookChange(e);
            _contextMenuStrip.SetMenuAppearance();

            _list.SmallImageList = _detailsLst.SmallImageList = _paddedImageList = _imageList.PadListViewIcons();

            _operationInProgress.Change += (a, b) => {
                if (a && !b)
                {
                    List_SelectedIndexChanged(null, EventArgs.Empty);
                }
            };

            _list.GotFocus += (sender, e) => {
                _manager.CommitOpenEditors();
                _manager.Rescan(notebookItemsOnly: true);
            };

            Load += (sender, e) => {
                _splitContainer.SplitterDistance = _splitContainer.Height - 300;
            };
        }
Esempio n. 2
0
        private void DoDelete()
        {
            if (_list.SelectedItems.Count != 1)
            {
                return;
            }
            var lvi  = _list.SelectedItems[0];
            var name = lvi.Text;
            var type = (NotebookItemType)Enum.Parse(typeof(NotebookItemType), lvi.Group.Name);

            // can't delete tables or views if an operation is in progress
            bool isTableOrView = type == NotebookItemType.Table || type == NotebookItemType.View;

            if (isTableOrView && _operationInProgress)
            {
                MessageForm.ShowError(_mainForm,
                                      "Delete Item",
                                      "Cannot delete tables or views while an operation is in progress.",
                                      "Please wait until the current operation finishes, and then try again.");
                return;
            }

            var deleteBtn = "&Delete";
            var d         = new MessageForm {
                Title   = "Delete Item",
                Message = $"Are you sure you want to delete \"{name}\"?",
                Buttons = new[] { deleteBtn, "Cancel" },
                Icon    = Resources.Warning32
            };

            if (d.ShowDialog(this) != deleteBtn)
            {
                return;
            }

            var item = new NotebookItem(type, name);

            _manager.CloseItem(item);

            new WaitForm("Delete", "Deleting the selected item...", () => {
                _manager.DeleteItem(item);
            }).ShowDialogAndDispose(this);

            _manager.Rescan(notebookItemsOnly: !isTableOrView);
        }
        public static async Task <bool> Start(IWin32Window owner, string filePath, NotebookManager manager)
        {
            DatabaseSchema schema = null;

            manager.PushStatus("Reading notebook...");
            try {
                schema = await Task.Run(() => DatabaseSchema.FromNotebook(manager.Notebook));
            } catch (Exception ex) {
                MessageForm.ShowError(owner,
                                      "Import Error",
                                      "Failed to read the list of tables in the notebook.",
                                      ex.Message);
                return(false);
            }
            manager.PopStatus();

            string importSql;

            using (var f = new ImportCsvForm(filePath, schema, manager)) {
                if (f.ShowDialog(owner) != DialogResult.OK)
                {
                    return(false);
                }
                importSql = f.GeneratedImportSql;
            }

            manager.PushStatus($"Importing \"{Path.GetFileName(filePath)}\"...");
            try {
                await Task.Run(() => {
                    manager.ExecuteScript(importSql, withTransaction: true);
                });

                manager.Rescan();
            } catch (Exception ex) {
                manager.PopStatus();
                MessageForm.ShowError(owner, "Import Error", "The import failed.", ex.GetErrorMessage());
                return(false);
            }

            manager.SetDirty();
            manager.PopStatus();
            return(true);
        }
Esempio n. 4
0
        private async Task DoCommonImport(IImportSession session)
        {
            IReadOnlyList <ImportPreviewForm.SelectedTable> selectedTables = null;
            bool csvHasHeaderRow = false, copyData = false;

            using (var frm = new ImportPreviewForm(session)) {
                if (frm.ShowDialog(_owner) != DialogResult.OK)
                {
                    return;
                }
                selectedTables  = frm.SelectedTables;
                csvHasHeaderRow = frm.CsvHasHeaderRow;
                copyData        = frm.CopyData;
            }

            var fileSession = session as IFileImportSession;
            var dbSession = session as IDatabaseImportSession;

            if (fileSession != null)
            {
                fileSession.FileHasHeaderRow = csvHasHeaderRow;
            }

            Action import = () => {
                _notebook.Invoke(() => {
                    _notebook.Execute("BEGIN");

                    try {
                        if (dbSession != null)
                        {
                            DoDatabaseImport(selectedTables, copyData, dbSession);
                        }
                        else if (fileSession != null)
                        {
                            DoDatabaseImport(selectedTables, fileSession);
                        }
                        else
                        {
                            throw new InvalidOperationException("Unexpected session type.");
                        }

                        _notebook.Execute("COMMIT");
                        session.AddToRecentlyUsed();
                    } catch {
                        try { _notebook.Execute("ROLLBACK"); } catch { }
                        throw;
                    }
                });
            };

            _manager.PushStatus("Importing the selected tables...");
            Exception exception = null;
            await Task.Run(() => {
                try {
                    import();
                } catch (Exception ex) {
                    exception = ex;
                }
            });

            _manager.PopStatus();
            _manager.Rescan();

            if (exception == null)
            {
                _manager.SetDirty();
            }
            else
            {
                MessageForm.ShowError(_owner, "Import Error", "The import failed.", exception.Message);
            }
        }