From 5f14d830e3a5f27196a55ae3500d88606a3a6f22 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Thu, 29 Dec 2016 21:45:38 +0100 Subject: [PATCH] Clean up --- QtClient/JoplinQtClient/JoplinQtClient.pro | 10 +-- QtClient/JoplinQtClient/application.cpp | 10 --- QtClient/JoplinQtClient/application.h | 5 -- QtClient/JoplinQtClient/database.cpp | 71 ++++++++++++++++--- QtClient/JoplinQtClient/database.h | 4 ++ .../models/foldercollection.cpp | 4 +- QtClient/JoplinQtClient/models/foldermodel.h | 6 -- QtClient/JoplinQtClient/models/notemodel.cpp | 4 -- QtClient/JoplinQtClient/models/notemodel.h | 3 - 9 files changed, 68 insertions(+), 49 deletions(-) diff --git a/QtClient/JoplinQtClient/JoplinQtClient.pro b/QtClient/JoplinQtClient/JoplinQtClient.pro index c71c4e70b..2d58a49cb 100755 --- a/QtClient/JoplinQtClient/JoplinQtClient.pro +++ b/QtClient/JoplinQtClient/JoplinQtClient.pro @@ -7,12 +7,9 @@ SOURCES += \ models/item.cpp \ models/folder.cpp \ database.cpp \ - uuid.cpp \ - services/folderservice.cpp \ models/foldermodel.cpp \ models/notemodel.cpp \ models/note.cpp \ - services/noteservice.cpp \ application.cpp \ models/notecollection.cpp \ services/notecache.cpp \ @@ -20,8 +17,7 @@ SOURCES += \ webapi.cpp \ synchronizer.cpp \ settings.cpp \ - models/foldercollection.cpp \ - databaseutils.cpp + models/foldercollection.cpp RESOURCES += qml.qrc \ database.qrc @@ -39,12 +35,9 @@ HEADERS += \ models/folder.h \ models/item.h \ database.h \ - uuid.h \ - services/folderservice.h \ models/foldermodel.h \ models/notemodel.h \ models/note.h \ - services/noteservice.h \ application.h \ models/notecollection.h \ services/notecache.h \ @@ -54,7 +47,6 @@ HEADERS += \ synchronizer.h \ settings.h \ models/foldercollection.h \ - databaseutils.h \ simpletypes.h DISTFILES += diff --git a/QtClient/JoplinQtClient/application.cpp b/QtClient/JoplinQtClient/application.cpp index fa4db3283..bb64f1ee5 100755 --- a/QtClient/JoplinQtClient/application.cpp +++ b/QtClient/JoplinQtClient/application.cpp @@ -13,7 +13,6 @@ Application::Application(int &argc, char **argv) : db_("D:/Web/www/joplin/QtClient/data/notes.sqlite"), api_("http://joplin.local"), synchronizer_(api_, db_), - folderCollection_(db_, 0, "title ASC"), folderModel_(db_) { @@ -27,15 +26,6 @@ Application::Application(int &argc, char **argv) : Settings settings; - //folderCollection_ = FolderCollection(db_, 0, "title ASC"); - - folderService_ = FolderService(db_); - //folderModel_.setService(folderService_); - //folderModel_.setCollection(folderCollection_); - - noteService_ = NoteService(db_); - noteModel_.setService(noteService_); - view_.setResizeMode(QQuickView::SizeRootObjectToView); QQmlContext *ctxt = view_.rootContext(); ctxt->setContextProperty("folderListModel", &folderModel_); diff --git a/QtClient/JoplinQtClient/application.h b/QtClient/JoplinQtClient/application.h index 68758a060..e9ff2a1bd 100755 --- a/QtClient/JoplinQtClient/application.h +++ b/QtClient/JoplinQtClient/application.h @@ -4,8 +4,6 @@ #include #include "database.h" -#include "services/folderservice.h" -#include "services/noteservice.h" #include "models/foldermodel.h" #include "models/notecollection.h" #include "models/foldercollection.h" @@ -29,10 +27,7 @@ private: QQuickView view_; Database db_; - FolderService folderService_; NoteCollection noteCollection_; - FolderCollection folderCollection_; - NoteService noteService_; FolderModel folderModel_; NoteModel noteModel_; QString selectedFolderId() const; diff --git a/QtClient/JoplinQtClient/database.cpp b/QtClient/JoplinQtClient/database.cpp index 6eaf074bd..777eaae5d 100755 --- a/QtClient/JoplinQtClient/database.cpp +++ b/QtClient/JoplinQtClient/database.cpp @@ -31,16 +31,69 @@ QSqlDatabase &Database::database() { return db_; } -//QSqlQuery Database::exec(const QString &sql, const QMap ¶meters) { -// QSqlQuery query; -// query.prepare(sql); +QSqlQuery Database::buildSqlQuery(Database::QueryType type, const QString &tableName, const QStringList &fields, const VariantVector &values, const QString &whereCondition) { + QString sql; -// QMapIterator it(parameters); -// while (it.hasNext()) { -// it.next(); -// qDebug() << i.key() << ": " << i.value(); -// } -//} + if (type == Insert) { + QString fieldString = ""; + QString valueString = ""; + for (int i = 0; i < fields.length(); i++) { + QString f = fields[i]; + if (fieldString != "") fieldString += ", "; + if (valueString != "") valueString += ", "; + fieldString += f; + valueString += ":" + f; + } + + sql = QString("INSERT INTO %1 (%2) VALUES (%3)").arg(tableName).arg(fieldString).arg(valueString); + } else if (type == Update) { + QString fieldString = ""; + for (int i = 0; i < fields.length(); i++) { + QString f = fields[i]; + if (fieldString != "") fieldString += ", "; + fieldString += f + " = :" + f; + } + + sql = QString("UPDATE %1 SET %2").arg(tableName).arg(fieldString); + if (whereCondition != "") sql += " WHERE " + whereCondition; + } + + QSqlQuery query(db_); + query.prepare(sql); + for (int i = 0; i < values.size(); i++) { + QVariant v = values[i]; + QString fieldName = ":" + fields[i]; + if (v.type() == QVariant::String) { + query.bindValue(fieldName, v.toString()); + } else if (v.type() == QVariant::Int) { + query.bindValue(fieldName, v.toInt()); + } else if (v.isNull()) { + query.bindValue(fieldName, (int)NULL); + } else if (v.type() == QVariant::Double) { + query.bindValue(fieldName, v.toDouble()); + } else if (v.type() == (QVariant::Type)QMetaType::Float) { + query.bindValue(fieldName, v.toFloat()); + } else if (v.type() == QVariant::LongLong) { + query.bindValue(fieldName, v.toLongLong()); + } else if (v.type() == QVariant::UInt) { + query.bindValue(fieldName, v.toUInt()); + } else if (v.type() == QVariant::Char) { + query.bindValue(fieldName, v.toChar()); + } else { + qWarning() << Q_FUNC_INFO << "Unsupported variant type:" << v.type(); + } + } + + qDebug() <<"SQL:"< i(query.boundValues()); + while (i.hasNext()) { + i.next(); + qDebug() << i.key() << ":" << i.value().toString(); + } + + return query; +} int Database::version() const { if (version_ >= 0) return version_; diff --git a/QtClient/JoplinQtClient/database.h b/QtClient/JoplinQtClient/database.h index 8f35fc767..cabdedf53 100755 --- a/QtClient/JoplinQtClient/database.h +++ b/QtClient/JoplinQtClient/database.h @@ -2,6 +2,7 @@ #define DATABASE_H #include +#include "simpletypes.h" namespace jop { @@ -9,10 +10,13 @@ class Database { public: + enum QueryType { Select, Insert, Update, Delete }; + Database(const QString& path); Database(); QSqlQuery query(const QString& sql) const; QSqlDatabase& database(); + QSqlQuery buildSqlQuery(Database::QueryType type, const QString& tableName, const QStringList& fields, const VariantVector& values, const QString& whereCondition = ""); private: diff --git a/QtClient/JoplinQtClient/models/foldercollection.cpp b/QtClient/JoplinQtClient/models/foldercollection.cpp index b177fcda8..ad892b956 100755 --- a/QtClient/JoplinQtClient/models/foldercollection.cpp +++ b/QtClient/JoplinQtClient/models/foldercollection.cpp @@ -3,8 +3,6 @@ using namespace jop; -//FolderCollection::FolderCollection() {} - // Note: although parentId is supplied, it is currently not being used. FolderCollection::FolderCollection(Database& db, const QString& parentId, const QString& orderBy) { db_ = db; @@ -50,7 +48,7 @@ Folder FolderCollection::byId(const QString& id) const { } void FolderCollection::update(const QString &id, const QStringList &fields, const VariantVector &values) { - QSqlQuery q = dbUtils::buildSqlQuery(&db_.database(), "update", "folders", fields, values, "id = \"" + id + "\""); + QSqlQuery q = db_.buildSqlQuery(Database::Update, "folders", fields, values, "id = \"" + id + "\""); q.exec(); cache_.clear(); emit changed(0, count() - 1, fields); diff --git a/QtClient/JoplinQtClient/models/foldermodel.h b/QtClient/JoplinQtClient/models/foldermodel.h index 04552b3a3..e34348ea1 100755 --- a/QtClient/JoplinQtClient/models/foldermodel.h +++ b/QtClient/JoplinQtClient/models/foldermodel.h @@ -4,7 +4,6 @@ #include #include "database.h" -#include "services/folderservice.h" #include "models/foldercollection.h" namespace jop { @@ -23,9 +22,6 @@ public: FolderModel(Database& database); - //void setService(FolderService& folderService); - //void setCollection(FolderCollection& folderCollection); - void addFolder(Folder* folder); int rowCount(const QModelIndex & parent = QModelIndex()) const; @@ -41,14 +37,12 @@ protected: private: QList folders_; - FolderService folderService_; FolderCollection folderCollection_; public slots: bool setData(int index, const QVariant &value, int role = Qt::EditRole); void folderCollection_changed(int from, int to, const QStringList &fields); - //bool setDataInt(int index, const QVariant &value, int role = Qt::EditRole); }; diff --git a/QtClient/JoplinQtClient/models/notemodel.cpp b/QtClient/JoplinQtClient/models/notemodel.cpp index 37d36a96b..99748bd8e 100755 --- a/QtClient/JoplinQtClient/models/notemodel.cpp +++ b/QtClient/JoplinQtClient/models/notemodel.cpp @@ -25,10 +25,6 @@ void jop::NoteModel::setFolderId(const QString &v) { folderId_ = v; } -void jop::NoteModel::setService(jop::NoteService &v) { - noteService_ = v; -} - void jop::NoteModel::setCollection(jop::NoteCollection ¬eCollection) { beginResetModel(); collection_ = noteCollection; diff --git a/QtClient/JoplinQtClient/models/notemodel.h b/QtClient/JoplinQtClient/models/notemodel.h index 7b4f4b20a..7e1007ccf 100755 --- a/QtClient/JoplinQtClient/models/notemodel.h +++ b/QtClient/JoplinQtClient/models/notemodel.h @@ -3,7 +3,6 @@ #include -#include "services/noteservice.h" #include "models/notecollection.h" namespace jop { @@ -23,7 +22,6 @@ public: int rowCount(const QModelIndex & parent = QModelIndex()) const; QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; void setFolderId(const QString& v); - void setService(NoteService& v); void setCollection(NoteCollection& noteCollection); protected: @@ -33,7 +31,6 @@ protected: private: QList notes_; - NoteService noteService_; QString folderId_; NoteCollection collection_;