CLI: Reuse --table as table name for a CSV Import

This allows user to specify the destination table of the import from the
command line.

See issue #2772
This commit is contained in:
mgrojo
2021-07-18 18:36:33 +02:00
parent fd02005913
commit 6bdacf6595
5 changed files with 22 additions and 13 deletions

View File

@@ -192,7 +192,7 @@ Application::Application(int& argc, char** argv) :
printArgument(QString("--import-csv <%1>").arg(tr("file")),
tr("Import this CSV file into the passed DB or into a new DB"));
printArgument(QString("-t, --table <%1>").arg(tr("table")),
tr("Browse this table after opening the DB"));
tr("Browse this table, or use it as target of a data import"));
printArgument(QString("-R, --read-only"),
tr("Open database in read-only mode"));
printArgument(QString("-S, --settings <%1>").arg(tr("settings_file")),
@@ -315,8 +315,13 @@ Application::Application(int& argc, char** argv) :
m_mainWindow->refresh();
}
}
if(!csvToImport.empty())
m_mainWindow->importCSVfiles(csvToImport);
if(!csvToImport.empty()) {
if(tableToBrowse.empty()) {
m_mainWindow->importCSVfiles(csvToImport);
} else {
m_mainWindow->importCSVfiles(csvToImport, tableToBrowse.front());
}
}
}
Application::~Application()

View File

@@ -38,7 +38,7 @@ QChar ImportCsvDialog::getSettingsChar(const std::string& group, const std::stri
return value.toChar();
}
ImportCsvDialog::ImportCsvDialog(const std::vector<QString>& filenames, DBBrowserDB* db, QWidget* parent)
ImportCsvDialog::ImportCsvDialog(const std::vector<QString>& filenames, DBBrowserDB* db, QWidget* parent, const QString& table)
: QDialog(parent),
ui(new Ui::ImportCsvDialog),
csvFilenames(filenames),
@@ -49,10 +49,14 @@ ImportCsvDialog::ImportCsvDialog(const std::vector<QString>& filenames, DBBrowse
// Hide "Advanced" section of the settings
toggleAdvancedSection(false);
// Get the actual file name out of the provided path and use it as the default table name for import
// For importing several files at once, the fields have to be the same so we can safely use the first
QFileInfo file(filenames.front());
ui->editName->setText(file.baseName());
if(!table.isEmpty()) {
ui->editName->setText(table);
} else {
// Get the actual file name out of the provided path and use it as the default table name for import
// For importing several files at once, the fields have to be the same so we can safely use the first
QFileInfo file(filenames.front());
ui->editName->setText(file.baseName());
}
// Create a list of all available encodings and create an auto completion list from them
encodingCompleter = new QCompleter(toStringList(QTextCodec::availableCodecs()), this);
@@ -526,7 +530,7 @@ bool ImportCsvDialog::importCsv(const QString& fileName, const QString& name)
switch (answer) {
case QMessageBox::No:
return true;
// Stop now if the Cancel button has been clicked. But also indicate, that the entire import process should be stopped.
case QMessageBox::Cancel:
return false;

View File

@@ -19,7 +19,7 @@ class ImportCsvDialog : public QDialog
Q_OBJECT
public:
explicit ImportCsvDialog(const std::vector<QString>& filenames, DBBrowserDB* db, QWidget* parent = nullptr);
explicit ImportCsvDialog(const std::vector<QString>& filenames, DBBrowserDB* db, QWidget* parent = nullptr, const QString& table = QString());
~ImportCsvDialog() override;
private slots:

View File

@@ -1351,7 +1351,7 @@ void MainWindow::mainTabSelected(int /*tabindex*/)
}
}
void MainWindow::importCSVfiles(const std::vector<QString>& inputFiles)
void MainWindow::importCSVfiles(const std::vector<QString>& inputFiles, const QString& table)
{
if (!inputFiles.empty())
{
@@ -1359,7 +1359,7 @@ void MainWindow::importCSVfiles(const std::vector<QString>& inputFiles)
if(!db.isOpen())
fileNewInMemoryDatabase(/* open_create_dialog */ false);
ImportCsvDialog dialog(inputFiles, &db, this);
ImportCsvDialog dialog(inputFiles, &db, this, table);
if (dialog.exec())
refreshTableBrowsers();
}

View File

@@ -158,7 +158,7 @@ public slots:
void fileDetachTreeViewSelected(QTreeView* treeView);
void reloadSettings();
bool closeFiles();
void importCSVfiles(const std::vector<QString>& inputFiles);
void importCSVfiles(const std::vector<QString>& inputFiles, const QString& table = QString());
private slots:
void createTreeContextMenu(const QPoint & qPoint);