1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
This commit is contained in:
Laurent Cozic 2016-12-29 21:45:38 +01:00
parent 8ee9d82b1c
commit 5f14d830e3
9 changed files with 68 additions and 49 deletions

View File

@ -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 +=

View File

@ -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_);

View File

@ -4,8 +4,6 @@
#include <stable.h>
#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;

View File

@ -31,16 +31,69 @@ QSqlDatabase &Database::database() {
return db_;
}
//QSqlQuery Database::exec(const QString &sql, const QMap<QString, QVariant> &parameters) {
// 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<QString, QVariant> 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:"<<sql;
QMapIterator<QString, QVariant> 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_;

View File

@ -2,6 +2,7 @@
#define DATABASE_H
#include <stable.h>
#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:

View File

@ -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);

View File

@ -4,7 +4,6 @@
#include <stable.h>
#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<Folder> 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);
};

View File

@ -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 &noteCollection) {
beginResetModel();
collection_ = noteCollection;

View File

@ -3,7 +3,6 @@
#include <stable.h>
#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<Note> notes_;
NoteService noteService_;
QString folderId_;
NoteCollection collection_;