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

methods to retrieve note index and ids

This commit is contained in:
Laurent Cozic 2017-01-12 19:11:58 +01:00
parent 8eb93b8848
commit d6fe46c819
6 changed files with 44 additions and 7 deletions

View File

@ -202,13 +202,15 @@ QVector<BaseModel::Field> BaseModel::tableFields(jop::Table table) {
QVector<BaseModel::Field> output;
// TODO: ideally that should be auto-generated based on schema.sql
if (table == jop::FoldersTable) {
output.push_back(createField("id", QMetaType::QString ));
output.push_back(createField("title", QMetaType::QString ));
output.push_back(createField("created_time", QMetaType::Int ));
output.push_back(createField("updated_time", QMetaType::Int ));
} else if (table == jop::NotesTable) {
output.push_back(createField("id", QMetaType::Int ));
output.push_back(createField("id", QMetaType::QString ));
output.push_back(createField("title", QMetaType::QString ));
output.push_back(createField("body", QMetaType::QString ));
output.push_back(createField("parent_id", QMetaType::QString ));
@ -386,6 +388,10 @@ BaseModel::Value BaseModel::id() const {
return value(primaryKey());
}
QString BaseModel::idString() const {
return id().toString();
}
QString BaseModel::valuesToString() const {
QString s;
for (QHash<QString, Value>::const_iterator it = values_.begin(); it != values_.end(); ++it) {

View File

@ -72,6 +72,7 @@ public:
void setValue(const QString& name, int value);
void setValue(const QString& name, const QJsonValue& value, QMetaType::Type type);
Value id() const;
QString idString() const;
QString valuesToString() const;
static QString tableName(Table t);

View File

@ -31,8 +31,9 @@ int Folder::noteCount() const {
QVector<Note> Folder::notes(const QString &orderBy, int limit, int offset) const {
QVector<Note> output;
QSqlQuery q = jop::db().prepare(QString("SELECT %1 FROM notes WHERE parent_id = :parent_id ORDER BY %2 LIMIT %3 OFFSET %4")
QSqlQuery q = jop::db().prepare(QString("SELECT %1 FROM %2 WHERE parent_id = :parent_id ORDER BY %3 LIMIT %4 OFFSET %5")
.arg(BaseModel::sqlTableFields(jop::NotesTable))
.arg(BaseModel::tableName(jop::NotesTable))
.arg(orderBy)
.arg(limit)
.arg(offset));
@ -49,6 +50,24 @@ QVector<Note> Folder::notes(const QString &orderBy, int limit, int offset) const
return output;
}
int Folder::noteIndexById(const QString &orderBy, const QString& id) const {
QSqlQuery q = jop::db().prepare(QString("SELECT id FROM %1 WHERE parent_id = :parent_id ORDER BY %2")
.arg(BaseModel::tableName(jop::NotesTable))
.arg(orderBy));
q.bindValue(":parent_id", idString());
jop::db().execQuery(q);
if (!jop::db().errorCheck(q)) return -1;
int index = 0;
while (q.next()) {
QString qId = q.value(0).toString();
if (qId == id) return index;
index++;
}
return -1;
}
int Folder::count() {
return BaseModel::count(jop::FoldersTable);
}

View File

@ -21,11 +21,7 @@ public:
bool trackChanges() const;
int noteCount() const;
QVector<Note> notes(const QString& orderBy, int limit, int offset) const;
// bool save();
// bool dispose();
private:
int noteIndexById(const QString& orderBy, const QString &id) const;
};

View File

@ -62,6 +62,16 @@ jop::Folder jop::NoteModel::folder() const {
return folder;
}
QString jop::NoteModel::indexToId(int index) const {
Note note = atIndex(index);
return note.idString();
}
int jop::NoteModel::idToIndex(const QString &id) const {
Folder f = this->folder();
return f.noteIndexById(orderBy_, id);
}
QHash<int, QByteArray> jop::NoteModel::roleNames() const {
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
// roles[TitleRole] = "title";

View File

@ -26,6 +26,11 @@ public:
void setFolderId(const QString& v);
Folder folder() const;
public slots:
QString indexToId(int index) const;
int idToIndex(const QString& id) const;
protected:
QHash<int, QByteArray> roleNames() const;