Skip to content

Commit 6bdacf6

Browse files
committed
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
1 parent fd02005 commit 6bdacf6

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

src/Application.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ Application::Application(int& argc, char** argv) :
192192
printArgument(QString("--import-csv <%1>").arg(tr("file")),
193193
tr("Import this CSV file into the passed DB or into a new DB"));
194194
printArgument(QString("-t, --table <%1>").arg(tr("table")),
195-
tr("Browse this table after opening the DB"));
195+
tr("Browse this table, or use it as target of a data import"));
196196
printArgument(QString("-R, --read-only"),
197197
tr("Open database in read-only mode"));
198198
printArgument(QString("-S, --settings <%1>").arg(tr("settings_file")),
@@ -315,8 +315,13 @@ Application::Application(int& argc, char** argv) :
315315
m_mainWindow->refresh();
316316
}
317317
}
318-
if(!csvToImport.empty())
319-
m_mainWindow->importCSVfiles(csvToImport);
318+
if(!csvToImport.empty()) {
319+
if(tableToBrowse.empty()) {
320+
m_mainWindow->importCSVfiles(csvToImport);
321+
} else {
322+
m_mainWindow->importCSVfiles(csvToImport, tableToBrowse.front());
323+
}
324+
}
320325
}
321326

322327
Application::~Application()

src/ImportCsvDialog.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ QChar ImportCsvDialog::getSettingsChar(const std::string& group, const std::stri
3838
return value.toChar();
3939
}
4040

41-
ImportCsvDialog::ImportCsvDialog(const std::vector<QString>& filenames, DBBrowserDB* db, QWidget* parent)
41+
ImportCsvDialog::ImportCsvDialog(const std::vector<QString>& filenames, DBBrowserDB* db, QWidget* parent, const QString& table)
4242
: QDialog(parent),
4343
ui(new Ui::ImportCsvDialog),
4444
csvFilenames(filenames),
@@ -49,10 +49,14 @@ ImportCsvDialog::ImportCsvDialog(const std::vector<QString>& filenames, DBBrowse
4949
// Hide "Advanced" section of the settings
5050
toggleAdvancedSection(false);
5151

52-
// Get the actual file name out of the provided path and use it as the default table name for import
53-
// For importing several files at once, the fields have to be the same so we can safely use the first
54-
QFileInfo file(filenames.front());
55-
ui->editName->setText(file.baseName());
52+
if(!table.isEmpty()) {
53+
ui->editName->setText(table);
54+
} else {
55+
// Get the actual file name out of the provided path and use it as the default table name for import
56+
// For importing several files at once, the fields have to be the same so we can safely use the first
57+
QFileInfo file(filenames.front());
58+
ui->editName->setText(file.baseName());
59+
}
5660

5761
// Create a list of all available encodings and create an auto completion list from them
5862
encodingCompleter = new QCompleter(toStringList(QTextCodec::availableCodecs()), this);
@@ -526,7 +530,7 @@ bool ImportCsvDialog::importCsv(const QString& fileName, const QString& name)
526530
switch (answer) {
527531
case QMessageBox::No:
528532
return true;
529-
533+
530534
// Stop now if the Cancel button has been clicked. But also indicate, that the entire import process should be stopped.
531535
case QMessageBox::Cancel:
532536
return false;

src/ImportCsvDialog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ImportCsvDialog : public QDialog
1919
Q_OBJECT
2020

2121
public:
22-
explicit ImportCsvDialog(const std::vector<QString>& filenames, DBBrowserDB* db, QWidget* parent = nullptr);
22+
explicit ImportCsvDialog(const std::vector<QString>& filenames, DBBrowserDB* db, QWidget* parent = nullptr, const QString& table = QString());
2323
~ImportCsvDialog() override;
2424

2525
private slots:

src/MainWindow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,15 +1351,15 @@ void MainWindow::mainTabSelected(int /*tabindex*/)
13511351
}
13521352
}
13531353

1354-
void MainWindow::importCSVfiles(const std::vector<QString>& inputFiles)
1354+
void MainWindow::importCSVfiles(const std::vector<QString>& inputFiles, const QString& table)
13551355
{
13561356
if (!inputFiles.empty())
13571357
{
13581358
// Allow importing to a new in-memory database that can be saved later.
13591359
if(!db.isOpen())
13601360
fileNewInMemoryDatabase(/* open_create_dialog */ false);
13611361

1362-
ImportCsvDialog dialog(inputFiles, &db, this);
1362+
ImportCsvDialog dialog(inputFiles, &db, this, table);
13631363
if (dialog.exec())
13641364
refreshTableBrowsers();
13651365
}

src/MainWindow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public slots:
158158
void fileDetachTreeViewSelected(QTreeView* treeView);
159159
void reloadSettings();
160160
bool closeFiles();
161-
void importCSVfiles(const std::vector<QString>& inputFiles);
161+
void importCSVfiles(const std::vector<QString>& inputFiles, const QString& table = QString());
162162

163163
private slots:
164164
void createTreeContextMenu(const QPoint & qPoint);

0 commit comments

Comments
 (0)