Skip to content

Commit

Permalink
CLI: Reuse --table as table name for a CSV Import
Browse files Browse the repository at this point in the history
This allows user to specify the destination table of the import from the
command line.

See issue sqlitebrowser#2772
  • Loading branch information
mgrojo committed Jul 18, 2021
1 parent fd02005 commit 6bdacf6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
11 changes: 8 additions & 3 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down Expand Up @@ -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()
Expand Down
16 changes: 10 additions & 6 deletions src/ImportCsvDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/ImportCsvDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1351,15 +1351,15 @@ 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())
{
// Allow importing to a new in-memory database that can be saved later.
if(!db.isOpen())
fileNewInMemoryDatabase(/* open_create_dialog */ false);

ImportCsvDialog dialog(inputFiles, &db, this);
ImportCsvDialog dialog(inputFiles, &db, this, table);
if (dialog.exec())
refreshTableBrowsers();
}
Expand Down
2 changes: 1 addition & 1 deletion src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6bdacf6

Please sign in to comment.