Import: support importing the single file argument as a CSV file

This will allow associating DB4S to CSV files and dropping a CSV file to be
importing into a new in-memory database, when there is no DB open yet.

See issue #2589
This commit is contained in:
mgrojo
2021-02-21 15:12:02 +01:00
parent d68c396f5d
commit cfa4dee3be
4 changed files with 24 additions and 2 deletions

View File

@@ -127,9 +127,10 @@ Application::Application(int& argc, char** argv) :
if(arguments().at(i) == "-h" || arguments().at(i) == "--help")
{
// Help
qWarning() << qPrintable(QString("%1: %2 [%3] [<%4>|<%5>]\n").
qWarning() << qPrintable(QString("%1: %2 [%3] [<%4>|<%5>|%6]\n").
arg(tr("Usage")).arg(QFileInfo(argv[0]).fileName()).
arg(tr("options")).arg(tr("database")).arg(tr("project")));
arg(tr("options")).arg(tr("database")).arg(tr("project")).
arg(tr("csv-file")));
qWarning() << qPrintable(tr("Possible command line arguments:"));
printArgument(QString("-h, --help"),
@@ -156,6 +157,8 @@ Application::Application(int& argc, char** argv) :
tr("Open this SQLite database"));
printArgument(QString("<%1>").arg(tr("project")),
tr("Open this project file (*.sqbpro)"));
printArgument(QString("<%1>").arg(tr("csv-file")),
tr("Import this CSV file into an in-memory database"));
m_showMainWindow = false;
} else if(arguments().at(i) == "-v" || arguments().at(i) == "--version") {
qWarning() << qPrintable(versionInformation());

View File

@@ -5,6 +5,7 @@
#include <QImageReader>
#include <QLocale>
#include <QTextCodec>
#include <QFile>
#include <algorithm>
@@ -59,6 +60,17 @@ bool isTextOnly(QByteArray data, const QString& encoding, bool quickTest)
}
}
bool isTextOnlyFile(const QString& fileName)
{
QFile file(fileName);
if (file.open(QFile::ReadOnly)) {
QByteArray data = file.read(512);
return isTextOnly(data);
} else {
return false;
}
}
bool containsRightToLeft(const QString& text) {
for(QChar ch : text) {

View File

@@ -10,6 +10,9 @@
// text but makes it less reliable
bool isTextOnly(QByteArray data, const QString& encoding = QString(), bool quickTest = false);
// Determine if file is a text file reading first 512 bytes and calling isTextOnly().
bool isTextOnlyFile(const QString& fileName);
// This returns true if text contains some character whose direction is right-to-left.
bool containsRightToLeft(const QString& text);

View File

@@ -530,6 +530,10 @@ bool MainWindow::fileOpen(const QString& fileName, bool openFromProject, bool re
if(loadProject(wFile, readOnly))
{
retval = true;
} else if(isTextOnlyFile(wFile)) {
// If it's a text file, cannot be an SQLite/SQLCipher database,
// so try to import it as CSV.
importCSVfiles({wFile});
} else {
// Close the database. If the user didn't want to close it, though, stop here
if (db.isOpen())