Ejemplo n.º 1
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);
            }
        }