1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Moved db operations under model

This commit is contained in:
Laurent Cozic 2017-01-01 23:20:06 +01:00
parent 2491a8dab5
commit bc6f419d1e
4 changed files with 77 additions and 43 deletions

View File

@ -101,7 +101,7 @@ void Application::afterSessionInitialization() {
QString sessionId = settings.value("sessionId").toString();
qDebug() << "Session:" << sessionId;
api_.setSessionId(sessionId);
synchronizer_.start();
//synchronizer_.start();
}
void Application::view_currentFolderChanged() {

View File

@ -1,7 +1,68 @@
#include "models/folder.h"
#include "database.h"
#include "uuid.h"
using namespace jop;
Folder::Folder() {
}
bool Folder::isNew() const {
return id().isEmpty();
}
bool Folder::save() {
bool isNew = this->isNew();
QStringList fields;
VariantVector values;
if (isNew) {
setId(uuid::createUuid());
fields << "id";
values << id();
}
fields << "title" << "synced";
values << title() << QVariant(0);
if (isNew) {
QSqlQuery q = jop::db().buildSqlQuery(Database::Insert, "folders", fields, values);
q.exec();
return jop::db().errorCheck(q);
} else {
QSqlQuery q = jop::db().buildSqlQuery(Database::Update, "folders", fields, values, "id = \"" + id() + "\"");
q.exec();
return jop::db().errorCheck(q);
}
}
bool Folder::dispose() {
QSqlQuery q(jop::db().database());
q.prepare("DELETE FROM folders WHERE id = :id");
q.bindValue(":id", id());
q.exec();
return jop::db().errorCheck(q);
}
int Folder::count() {
QSqlQuery q = jop::db().query("SELECT count(*) as row_count FROM folders");
q.exec();
q.next();
return q.value(0).toInt();
}
QVector<Folder> Folder::all(const QString &orderBy) {
QSqlQuery q = jop::db().query("SELECT " + Folder::dbFields().join(",") + " FROM folders ORDER BY " + orderBy);
q.exec();
QVector<Folder> output;
while (q.next()) {
Folder folder;
folder.fromSqlQuery(q);
output.push_back(folder);
}
return output;
}

View File

@ -11,8 +11,12 @@ class Folder : public Item {
public:
Folder();
bool isNew() const;
bool save();
bool dispose();
static int count();
static QVector<Folder> all(const QString& orderBy);
private:

View File

@ -8,11 +8,7 @@ FolderModel::FolderModel(Database &database) : QAbstractListModel(), db_(databas
}
int FolderModel::rowCount(const QModelIndex & parent) const {
Q_UNUSED(parent);
QSqlQuery q = db_.query("SELECT count(*) as row_count FROM folders");
q.exec();
q.next();
return q.value(0).toInt() + (virtualItemShown_ ? 1 : 0);
return Folder::count() + (virtualItemShown_ ? 1 : 0);
}
// NOTE: to lazy load - send back "Loading..." if item not currently loaded
@ -42,17 +38,8 @@ bool FolderModel::setData(const QModelIndex &index, const QVariant &value, int r
Folder folder = atIndex(index.row());
if (role == Qt::EditRole) {
emit dataChanging();
QStringList fields;
VariantVector values;
fields << "title" << "synced";
values << value << QVariant(0);
QSqlQuery q = db_.buildSqlQuery(Database::Update, "folders", fields, values, "id = \"" + folder.id() + "\"");
q.exec();
if (!db_.errorCheck(q)) return false;
folder.setTitle(value.toString());
if (!folder.save()) return false;
cache_.clear();
QVector<int> roles;
@ -77,14 +64,7 @@ Folder FolderModel::atIndex(int index) const {
cache_.clear();
QSqlQuery q = db_.query("SELECT " + Folder::dbFields().join(",") + " FROM folders ORDER BY " + orderBy_);
q.exec();
while (q.next()) {
Folder folder;
folder.fromSqlQuery(q);
cache_.push_back(folder);
}
cache_ = Folder::all(orderBy_);
if (!cache_.size()) {
qWarning() << "Invalid folder index:" << index;
@ -144,19 +124,13 @@ QHash<int, QByteArray> FolderModel::roleNames() const {
}
void FolderModel::addData(const QString &title) {
QStringList fields;
VariantVector values;
QString folderId = uuid::createUuid();
fields << "id" << "title" << "synced";
values << folderId << QVariant(title) << QVariant(0);
QSqlQuery q = db_.buildSqlQuery(Database::Insert, "folders", fields, values);
q.exec();
if (!db_.errorCheck(q)) return;
Folder folder;
folder.setTitle(title);
if (!folder.save()) return;
cache_.clear();
lastInsertId_ = folderId;
lastInsertId_ = folder.id();
QVector<int> roles;
roles << Qt::DisplayRole;
@ -173,13 +147,8 @@ void FolderModel::addData(const QString &title) {
}
void FolderModel::deleteData(const int index) {
QString folderId = idAtIndex(index);
QSqlQuery q(db_.database());
q.prepare("DELETE FROM folders WHERE id = :id");
q.bindValue(":id", folderId);
q.exec();
if (!db_.errorCheck(q)) return;
Folder folder = atIndex(index);
if (!folder.dispose()) return;
cache_.clear();