2016-12-12 22:33:33 +00:00
|
|
|
#include "notecollection.h"
|
|
|
|
|
|
|
|
using namespace jop;
|
|
|
|
|
|
|
|
NoteCollection::NoteCollection() {}
|
|
|
|
|
2016-12-23 19:04:26 +01:00
|
|
|
NoteCollection::NoteCollection(Database& db, const QString& parentId, const QString& orderBy) {
|
2016-12-12 22:33:33 +00:00
|
|
|
db_ = db;
|
|
|
|
parentId_ = parentId;
|
|
|
|
orderBy_ = orderBy;
|
|
|
|
}
|
|
|
|
|
2016-12-14 21:50:26 +00:00
|
|
|
Note NoteCollection::at(int index) const {
|
2017-01-02 13:17:15 +01:00
|
|
|
return Note();
|
|
|
|
// if (parentId_ == "") return Note();
|
2016-12-12 22:33:33 +00:00
|
|
|
|
2017-01-02 13:17:15 +01:00
|
|
|
// if (cache_.isset(index)) return cache_.get(index);
|
2016-12-12 22:33:33 +00:00
|
|
|
|
2017-01-02 13:17:15 +01:00
|
|
|
// std::vector<int> indexes = cache_.availableBufferAround(index, 32);
|
|
|
|
// if (!indexes.size()) {
|
|
|
|
// qWarning() << "Couldn't acquire buffer"; // "Cannot happen"
|
|
|
|
// return Note();
|
|
|
|
// }
|
2016-12-12 22:33:33 +00:00
|
|
|
|
2017-01-02 13:17:15 +01:00
|
|
|
// int from = indexes[0];
|
|
|
|
// int to = indexes[indexes.size() - 1];
|
2016-12-12 22:33:33 +00:00
|
|
|
|
2017-01-02 13:17:15 +01:00
|
|
|
// QSqlQuery q = db_.query("SELECT id, title, body FROM notes WHERE parent_id = :parent_id ORDER BY " + orderBy_ + " LIMIT " + QString::number(to - from + 1) + " OFFSET " + QString::number(from));
|
|
|
|
// q.bindValue(":parent_id", parentId_);
|
|
|
|
// q.exec();
|
2016-12-12 22:33:33 +00:00
|
|
|
|
2017-01-02 13:17:15 +01:00
|
|
|
// int noteIndex = from;
|
|
|
|
// while (q.next()) {
|
|
|
|
// Note note;
|
|
|
|
// note.setId(q.value(0).toString());
|
|
|
|
// note.setTitle(q.value(1).toString());
|
|
|
|
// note.setBody(q.value(2).toString());
|
2016-12-12 22:33:33 +00:00
|
|
|
|
2017-01-02 13:17:15 +01:00
|
|
|
// cache_.set(noteIndex, note);
|
2016-12-12 22:33:33 +00:00
|
|
|
|
2017-01-02 13:17:15 +01:00
|
|
|
// noteIndex++;
|
|
|
|
// }
|
2016-12-12 22:33:33 +00:00
|
|
|
|
2017-01-02 13:17:15 +01:00
|
|
|
// return cache_.get(index);
|
2016-12-12 22:33:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: cache result
|
|
|
|
int NoteCollection::count() const {
|
2016-12-23 19:04:26 +01:00
|
|
|
if (parentId_ == "") return 0;
|
2016-12-12 22:33:33 +00:00
|
|
|
|
|
|
|
QSqlQuery q = db_.query("SELECT count(*) as row_count FROM notes WHERE parent_id = :parent_id");
|
|
|
|
q.bindValue(":parent_id", parentId_);
|
|
|
|
q.exec();
|
|
|
|
q.next();
|
|
|
|
return q.value(0).toInt();
|
|
|
|
}
|
2016-12-14 21:50:26 +00:00
|
|
|
|
2016-12-23 19:04:26 +01:00
|
|
|
Note NoteCollection::byId(const QString& id) const {
|
2017-01-02 13:17:15 +01:00
|
|
|
return Note();
|
|
|
|
// std::vector<int> indexes = cache_.indexes();
|
|
|
|
// for (size_t i = 0; i < indexes.size(); i++) {
|
|
|
|
// Note note = cache_.get(indexes[i]);
|
|
|
|
// if (note.id() == id) return note;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// QSqlQuery q = db_.query("SELECT id, title, body FROM notes WHERE id = :id");
|
|
|
|
// q.bindValue(":id", id);
|
|
|
|
// q.exec();
|
|
|
|
// q.next();
|
|
|
|
// if (!q.isValid()) {
|
|
|
|
// qWarning() << "Invalid note ID:" << id;
|
|
|
|
// return Note();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // TODO: refactor creation of note from SQL query object
|
|
|
|
// Note note;
|
|
|
|
// note.setId(q.value(0).toString());
|
|
|
|
// note.setTitle(q.value(1).toString());
|
|
|
|
// note.setBody(q.value(2).toString());
|
|
|
|
// return note;
|
2016-12-14 21:50:26 +00:00
|
|
|
}
|