From 2041d209ad513ffdcdec66252eb6005e224c2891 Mon Sep 17 00:00:00 2001 From: YFdyh000 Date: Wed, 26 Feb 2025 08:37:59 +0800 Subject: [PATCH 1/3] Fill default filename when import SQL file or save project file --- src/MainWindow.cpp | 13 ++++++++++--- src/MainWindow.h | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 89546255aa..e1da11ceeb 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -1572,11 +1572,14 @@ void MainWindow::importDatabaseFromSQL() "If you answer no we will attempt to import the data in the SQL file to the current database."), QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) || !db.isOpen()) { + QString basePathName = db.currentFile(); + removeFilenameSuffix(basePathName); newDbFile = FileDialog::getSaveFileName( CreateDatabaseFile, this, tr("Choose a filename to save under"), - FileDialog::getSqlDatabaseFileFilter()); + FileDialog::getSqlDatabaseFileFilter(), + basePathName); if(QFile::exists(newDbFile)) { QMessageBox::information(this, QApplication::applicationName(), tr("File %1 already exists. Please choose a different name.").arg(newDbFile)); @@ -3145,13 +3148,17 @@ static void saveBrowseDataTableSettings(const BrowseDataTableSettings& object, s xml.writeEndElement(); } +void MainWindow::removeFilenameSuffix(QString& filename) { + int dotLen = QFileInfo(filename).suffix().isEmpty() ? 0 : 1; + filename.chop(QFileInfo(filename).suffix().size() + dotLen); +} + void MainWindow::saveProject(const QString& currentFilename) { QString filename; if(currentFilename.isEmpty()) { QString basePathName = db.currentFile(); - // Remove database suffix - basePathName.chop(QFileInfo(basePathName).suffix().size()+1); + removeFilenameSuffix(basePathName); filename = FileDialog::getSaveFileName( CreateProjectFile, this, diff --git a/src/MainWindow.h b/src/MainWindow.h index 25f4818513..eb8947b9fa 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -138,6 +138,7 @@ friend TableBrowserDock; void toggleTabVisible(QWidget* tabWidget, bool show); void restoreOpenTabs(QString tabs); QString saveOpenTabs(); + void removeFilenameSuffix(QString& filename); void saveProject(const QString& currentFilename); bool closeProject(); bool askSaveSqlTab(int index, bool& ignoreUnattachedBuffers); From c8201f7a1643fd56142e0d6ad757b1437a35f6b7 Mon Sep 17 00:00:00 2001 From: YFdyh000 Date: Wed, 26 Feb 2025 09:15:36 +0800 Subject: [PATCH 2/3] Re-ask if the user select an existing file to import SQL file --- src/MainWindow.cpp | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index e1da11ceeb..3e6135f92e 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -1574,20 +1574,23 @@ void MainWindow::importDatabaseFromSQL() { QString basePathName = db.currentFile(); removeFilenameSuffix(basePathName); - newDbFile = FileDialog::getSaveFileName( - CreateDatabaseFile, - this, - tr("Choose a filename to save under"), - FileDialog::getSqlDatabaseFileFilter(), - basePathName); - if(QFile::exists(newDbFile)) - { - QMessageBox::information(this, QApplication::applicationName(), tr("File %1 already exists. Please choose a different name.").arg(newDbFile)); - return; - } else if(newDbFile.size() == 0) { - return; - } - + while (true) + { + newDbFile = FileDialog::getSaveFileName( + CreateDatabaseFile, + this, + tr("Choose a filename to save under"), + FileDialog::getSqlDatabaseFileFilter(), + basePathName); + if (newDbFile.isEmpty()) // canceled + return; + if (QFile::exists(newDbFile)) + { + QMessageBox::information(this, QApplication::applicationName(), tr("File %1 already exists. Please choose a different name.").arg(newDbFile)); + continue; + } + break; + } // Create the new file and open it in the browser db.create(newDbFile); db.close(); From 36fcaca40651469d119321caf3060614883f9ffb Mon Sep 17 00:00:00 2001 From: YFdyh000 Date: Wed, 26 Feb 2025 10:00:36 +0800 Subject: [PATCH 3/3] Allow to choose an existing file to import SQL file when another database is open --- src/MainWindow.cpp | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 3e6135f92e..0ef11dae14 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -1563,13 +1563,15 @@ void MainWindow::importDatabaseFromSQL() if(!QFile::exists(fileName)) return; + bool doNewDb = true; + bool existingDbOpened = false; // If there is already a database file opened ask the user whether to import into // this one or a new one. If no DB is opened just ask for a DB name directly QString newDbFile; if((db.isOpen() && QMessageBox::question(this, QApplication::applicationName(), - tr("Do you want to create a new database file to hold the imported data?\n" - "If you answer no we will attempt to import the data in the SQL file to the current database."), + tr("Do you want to create a new database file or opening another database file to hold the imported data?\n" + "If you answer the No, we will attempt to import the data in the SQL file to the current database."), QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) || !db.isOpen()) { QString basePathName = db.currentFile(); @@ -1586,15 +1588,31 @@ void MainWindow::importDatabaseFromSQL() return; if (QFile::exists(newDbFile)) { - QMessageBox::information(this, QApplication::applicationName(), tr("File %1 already exists. Please choose a different name.").arg(newDbFile)); + if (QMessageBox::question(this, + QApplication::applicationName(), + tr("File %1 already exists.\nDo you want to open the database file to import data?\n" + "If you answer the No, we will re-ask for the path, or you can cancel the import.").arg(newDbFile), + QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) { + existingDbOpened = fileOpen(newDbFile); + if (!existingDbOpened) + { + // If the file is not a valid database file, we will ask the user to choose another. + QMessageBox::warning(this, QApplication::applicationName(), tr("Database file %1 failed to open! Please try choose a path again.").arg(newDbFile)); + continue; + } + doNewDb = false; + break; + } continue; } break; } // Create the new file and open it in the browser - db.create(newDbFile); - db.close(); - fileOpen(newDbFile); + if (!existingDbOpened) { + db.create(newDbFile); + db.close(); + fileOpen(newDbFile); + } } // Defer foreign keys. Just deferring them instead of disabling them should work fine because in the import we only expect CREATE and INSERT