mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-04-28 07:59:39 -05:00
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:
+5
-2
@@ -127,9 +127,10 @@ Application::Application(int& argc, char** argv) :
|
|||||||
if(arguments().at(i) == "-h" || arguments().at(i) == "--help")
|
if(arguments().at(i) == "-h" || arguments().at(i) == "--help")
|
||||||
{
|
{
|
||||||
// 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("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:"));
|
qWarning() << qPrintable(tr("Possible command line arguments:"));
|
||||||
printArgument(QString("-h, --help"),
|
printArgument(QString("-h, --help"),
|
||||||
@@ -156,6 +157,8 @@ Application::Application(int& argc, char** argv) :
|
|||||||
tr("Open this SQLite database"));
|
tr("Open this SQLite database"));
|
||||||
printArgument(QString("<%1>").arg(tr("project")),
|
printArgument(QString("<%1>").arg(tr("project")),
|
||||||
tr("Open this project file (*.sqbpro)"));
|
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;
|
m_showMainWindow = false;
|
||||||
} else if(arguments().at(i) == "-v" || arguments().at(i) == "--version") {
|
} else if(arguments().at(i) == "-v" || arguments().at(i) == "--version") {
|
||||||
qWarning() << qPrintable(versionInformation());
|
qWarning() << qPrintable(versionInformation());
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <QImageReader>
|
#include <QImageReader>
|
||||||
#include <QLocale>
|
#include <QLocale>
|
||||||
#include <QTextCodec>
|
#include <QTextCodec>
|
||||||
|
#include <QFile>
|
||||||
|
|
||||||
#include <algorithm>
|
#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) {
|
bool containsRightToLeft(const QString& text) {
|
||||||
|
|
||||||
for(QChar ch : text) {
|
for(QChar ch : text) {
|
||||||
|
|||||||
@@ -10,6 +10,9 @@
|
|||||||
// text but makes it less reliable
|
// text but makes it less reliable
|
||||||
bool isTextOnly(QByteArray data, const QString& encoding = QString(), bool quickTest = false);
|
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.
|
// This returns true if text contains some character whose direction is right-to-left.
|
||||||
bool containsRightToLeft(const QString& text);
|
bool containsRightToLeft(const QString& text);
|
||||||
|
|
||||||
|
|||||||
@@ -530,6 +530,10 @@ bool MainWindow::fileOpen(const QString& fileName, bool openFromProject, bool re
|
|||||||
if(loadProject(wFile, readOnly))
|
if(loadProject(wFile, readOnly))
|
||||||
{
|
{
|
||||||
retval = true;
|
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 {
|
} else {
|
||||||
// Close the database. If the user didn't want to close it, though, stop here
|
// Close the database. If the user didn't want to close it, though, stop here
|
||||||
if (db.isOpen())
|
if (db.isOpen())
|
||||||
|
|||||||
Reference in New Issue
Block a user