From 7167ce41b61d2ba2cdb526777a4233eb84a3b66a Mon Sep 17 00:00:00 2001 From: Unit 193 Date: Sat, 6 Dec 2014 17:33:25 -0500 Subject: Imported Upstream version 2.99.6 --- Plugins/CsvImport/CsvImport.pro | 28 +++++ Plugins/CsvImport/CsvImportOptions.ui | 103 +++++++++++++++++ Plugins/CsvImport/csvimport.cpp | 206 ++++++++++++++++++++++++++++++++++ Plugins/CsvImport/csvimport.h | 55 +++++++++ Plugins/CsvImport/csvimport.json | 7 ++ Plugins/CsvImport/csvimport.qrc | 5 + Plugins/CsvImport/csvimport_global.h | 12 ++ 7 files changed, 416 insertions(+) create mode 100644 Plugins/CsvImport/CsvImport.pro create mode 100644 Plugins/CsvImport/CsvImportOptions.ui create mode 100644 Plugins/CsvImport/csvimport.cpp create mode 100644 Plugins/CsvImport/csvimport.h create mode 100644 Plugins/CsvImport/csvimport.json create mode 100644 Plugins/CsvImport/csvimport.qrc create mode 100644 Plugins/CsvImport/csvimport_global.h (limited to 'Plugins/CsvImport') diff --git a/Plugins/CsvImport/CsvImport.pro b/Plugins/CsvImport/CsvImport.pro new file mode 100644 index 0000000..dfdb15b --- /dev/null +++ b/Plugins/CsvImport/CsvImport.pro @@ -0,0 +1,28 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2014-05-17T20:38:58 +# +#------------------------------------------------- + +include($$PWD/../../SQLiteStudio3/plugins.pri) + +QT -= gui + +TARGET = CsvImport +TEMPLATE = lib + +DEFINES += CSVIMPORT_LIBRARY + +SOURCES += csvimport.cpp + +HEADERS += csvimport.h\ + csvimport_global.h + +FORMS += \ + CsvImportOptions.ui + +OTHER_FILES += \ + csvimport.json + +RESOURCES += \ + csvimport.qrc diff --git a/Plugins/CsvImport/CsvImportOptions.ui b/Plugins/CsvImport/CsvImportOptions.ui new file mode 100644 index 0000000..5a1c6ab --- /dev/null +++ b/Plugins/CsvImport/CsvImportOptions.ui @@ -0,0 +1,103 @@ + + + csvImportOptions + + + + 0 + 0 + 333 + 90 + + + + Form + + + + + + CsvImport.Separator + + + + , (comma) + + + + + ; (semicolon) + + + + + \t (tab) + + + + + (whitespace) + + + + + Custom: + + + + + + + + CsvImport.FirstRowAsColumns + + + First row represents column names + + + + + + + Field separator: + + + + + + + + 30 + 16777215 + + + + CsvImport.CustomSeparator + + + + + + + CsvImport.NullValues + + + NULL values: + + + + + + + If your CSV data contains null values, define how are they represented in the CSV. + + + CsvImport.NullValueString + + + + + + + + diff --git a/Plugins/CsvImport/csvimport.cpp b/Plugins/CsvImport/csvimport.cpp new file mode 100644 index 0000000..cec81df --- /dev/null +++ b/Plugins/CsvImport/csvimport.cpp @@ -0,0 +1,206 @@ +#include "csvimport.h" +#include "services/importmanager.h" +#include "sqlitestudio.h" +#include "services/notifymanager.h" +#include +#include +#include + +CsvImport::CsvImport() +{ +} + +QString CsvImport::getDataSourceTypeName() const +{ + return "CSV"; +} + +ImportManager::StandardConfigFlags CsvImport::standardOptionsToEnable() const +{ + return ImportManager::CODEC|ImportManager::FILE_NAME; +} + +bool CsvImport::beforeImport(const ImportManager::StandardImportConfig& config) +{ + defineCsvFormat(); + + file = new QFile(config.inputFileName); + if (!file->open(QFile::ReadOnly) || !file->isReadable()) + { + notifyError(tr("Cannot read file %1").arg(config.inputFileName)); + safe_delete(file); + return false; + } + + stream = new QTextStream(file); + stream->setCodec(config.codec.toLatin1().data()); + + if (!extractColumns()) + { + safe_delete(stream); + safe_delete(file); + return false; + } + + return true; +} + +void CsvImport::afterImport() +{ + safe_delete(stream); + safe_delete(file); +} + +bool CsvImport::extractColumns() +{ + QString line = stream->readLine(); + while (line.trimmed().isEmpty() && !stream->atEnd()) + line = stream->readLine(); + + if (line.trimmed().isEmpty()) + { + notifyError(tr("Could not find any data in the file %1.").arg(file->fileName())); + return false; + } + + QStringList deserialized = CsvSerializer::deserialize(line.trimmed(), csvFormat).first(); + if (cfg.CsvImport.FirstRowAsColumns.get()) + { + columnNames = deserialized; + } + else + { + static const QString colTmp = QStringLiteral("column%1"); + columnNames.clear(); + for (int i = 1, total = deserialized.size(); i <= total; ++i) + columnNames << colTmp.arg(i); + + stream->seek(0); + } + + return true; +} + +void CsvImport::defineCsvFormat() +{ + csvFormat = CsvFormat(); + csvFormat.rowSeparator = '\n'; + + switch (cfg.CsvImport.Separator.get()) + { + case 0: + csvFormat.columnSeparator = ','; + break; + case 1: + csvFormat.columnSeparator = ';'; + break; + case 2: + csvFormat.columnSeparator = '\t'; + break; + case 3: + csvFormat.columnSeparator = ' '; + break; + default: + csvFormat.columnSeparator = cfg.CsvImport.CustomSeparator.get(); + break; + } +} + +QList CsvImport::getColumns() const +{ + QList columnList; + for (const QString& colName : columnNames) + columnList << ImportPlugin::ColumnDefinition(colName, QString()); + + return columnList; +} + +QList CsvImport::next() +{ + QString line = stream->readLine(); + if (line.isNull()) + return QList(); + + QList values; + QList deserialized = CsvSerializer::deserialize(line, csvFormat); + if (deserialized.size() > 0) + { + if (cfg.CsvImport.NullValues.get()) + { + QString nullVal = cfg.CsvImport.NullValueString.get(); + for (const QString& val : deserialized.first()) + { + if (val == nullVal) + values << QVariant(QVariant::String); + else + values << val; + } + } + else + { + for (const QString& val : deserialized.first()) + values << val; + } + } + + return values; +} + +CfgMain* CsvImport::getConfig() +{ + return &cfg; +} + +QString CsvImport::getImportConfigFormName() const +{ + return "csvImportOptions"; +} + +bool CsvImport::validateOptions() +{ + bool isValid = true; + if (cfg.CsvImport.Separator.get() >= 4) + { + IMPORT_MANAGER->updateVisibilityAndEnabled(cfg.CsvImport.CustomSeparator, true, true); + + bool valid = !cfg.CsvImport.CustomSeparator.get().isEmpty(); + IMPORT_MANAGER->handleValidationFromPlugin(valid, cfg.CsvImport.CustomSeparator, tr("Enter the custom separator character.")); + isValid &= valid; + } + else + { + IMPORT_MANAGER->updateVisibilityAndEnabled(cfg.CsvImport.CustomSeparator, true, false); + IMPORT_MANAGER->handleValidationFromPlugin(true, cfg.CsvImport.CustomSeparator); + } + + if (cfg.CsvImport.NullValues.get()) + { + IMPORT_MANAGER->updateVisibilityAndEnabled(cfg.CsvImport.NullValueString, true, true); + + bool valid = !cfg.CsvImport.NullValueString.get().isEmpty(); + IMPORT_MANAGER->handleValidationFromPlugin(valid, cfg.CsvImport.NullValueString, tr("Enter the value that will be interpreted as a NULL.")); + isValid &= valid; + } + else + { + IMPORT_MANAGER->updateVisibilityAndEnabled(cfg.CsvImport.NullValueString, true, false); + IMPORT_MANAGER->handleValidationFromPlugin(true, cfg.CsvImport.NullValueString); + } + return isValid; +} + +QString CsvImport::getFileFilter() const +{ + return tr("CSV files (*.csv);;Text files (*.txt);;All files (*)"); +} + +bool CsvImport::init() +{ + Q_INIT_RESOURCE(csvimport); + return GenericPlugin::init(); +} + +void CsvImport::deinit() +{ + Q_CLEANUP_RESOURCE(csvimport); +} diff --git a/Plugins/CsvImport/csvimport.h b/Plugins/CsvImport/csvimport.h new file mode 100644 index 0000000..cec803c --- /dev/null +++ b/Plugins/CsvImport/csvimport.h @@ -0,0 +1,55 @@ +#ifndef CSVIMPORT_H +#define CSVIMPORT_H + +#include "csvimport_global.h" +#include "plugins/importplugin.h" +#include "plugins/genericplugin.h" +#include "config_builder.h" +#include "csvserializer.h" + +CFG_CATEGORIES(CsvImportConfig, + CFG_CATEGORY(CsvImport, + CFG_ENTRY(bool, FirstRowAsColumns, false) + CFG_ENTRY(int, Separator, 0) + CFG_ENTRY(QString, CustomSeparator, QString()) + CFG_ENTRY(bool, NullValues, false) + CFG_ENTRY(QString, NullValueString, QString()) + ) +) + +class QFile; +class QTextStream; + +class CSVIMPORTSHARED_EXPORT CsvImport : public GenericPlugin, public ImportPlugin +{ + Q_OBJECT + SQLITESTUDIO_PLUGIN("csvimport.json") + + public: + CsvImport(); + + QString getDataSourceTypeName() const; + ImportManager::StandardConfigFlags standardOptionsToEnable() const; + bool beforeImport(const ImportManager::StandardImportConfig& config); + void afterImport(); + QList getColumns() const; + QList next(); + CfgMain* getConfig(); + QString getImportConfigFormName() const; + bool validateOptions(); + QString getFileFilter() const; + bool init(); + void deinit(); + + private: + bool extractColumns(); + void defineCsvFormat(); + + QFile* file = nullptr; + QTextStream* stream = nullptr; + QStringList columnNames; + CsvFormat csvFormat; + CFG_LOCAL(CsvImportConfig, cfg) +}; + +#endif // CSVIMPORT_H diff --git a/Plugins/CsvImport/csvimport.json b/Plugins/CsvImport/csvimport.json new file mode 100644 index 0000000..016c539 --- /dev/null +++ b/Plugins/CsvImport/csvimport.json @@ -0,0 +1,7 @@ +{ + "type": "ImportPlugin", + "title": "CSV import", + "description": "CSV format support for importing data", + "version": 10000, + "author": "SalSoft" +} diff --git a/Plugins/CsvImport/csvimport.qrc b/Plugins/CsvImport/csvimport.qrc new file mode 100644 index 0000000..a071122 --- /dev/null +++ b/Plugins/CsvImport/csvimport.qrc @@ -0,0 +1,5 @@ + + + CsvImportOptions.ui + + diff --git a/Plugins/CsvImport/csvimport_global.h b/Plugins/CsvImport/csvimport_global.h new file mode 100644 index 0000000..cee5321 --- /dev/null +++ b/Plugins/CsvImport/csvimport_global.h @@ -0,0 +1,12 @@ +#ifndef CSVIMPORT_GLOBAL_H +#define CSVIMPORT_GLOBAL_H + +#include + +#if defined(CSVIMPORT_LIBRARY) +# define CSVIMPORTSHARED_EXPORT Q_DECL_EXPORT +#else +# define CSVIMPORTSHARED_EXPORT Q_DECL_IMPORT +#endif + +#endif // CSVIMPORT_GLOBAL_H -- cgit v1.2.3