Skip to content

Commit

Permalink
Add fetchNoteIdByName to fetch only note Id (#1886)
Browse files Browse the repository at this point in the history
* Add fetchNoteIdByName to fetch only note Id

* Cache this-> variable outside the loop
  • Loading branch information
Waqar144 committed Sep 24, 2020
1 parent 615e5b7 commit 9704df6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
26 changes: 26 additions & 0 deletions src/entities/note.cpp
Expand Up @@ -527,6 +527,32 @@ Note Note::fetchByName(const QString &name,
return fetchByName(name, noteSubFolderId);
}

int Note::fetchNoteIdByName(const QString &name, int noteSubFolderId)
{
const QSqlDatabase db = QSqlDatabase::database(QStringLiteral("memory"));
QSqlQuery query(db);

// get the active note subfolder id if none was set
if (noteSubFolderId == -1) {
noteSubFolderId = NoteSubFolder::activeNoteSubFolderId();
}

query.prepare(
QStringLiteral("SELECT id FROM note WHERE name = :name AND "
"note_sub_folder_id = :note_sub_folder_id"));
query.bindValue(QStringLiteral(":name"), name);
query.bindValue(QStringLiteral(":note_sub_folder_id"), noteSubFolderId);

if (!query.exec()) {
qWarning() << __func__ << ": " << query.lastError();
} else {
if (query.first()) {
return query.value(QStringLiteral("id")).toInt();
}
}
return -1;
}

Note Note::fetchByName(const QString &name, int noteSubFolderId) {
const QSqlDatabase db = QSqlDatabase::database(QStringLiteral("memory"));
QSqlQuery query(db);
Expand Down
2 changes: 2 additions & 0 deletions src/entities/note.h
Expand Up @@ -62,6 +62,8 @@ class Note {
const QString &noteSubFolderPathData,
const QString& pathDataSeparator = QStringLiteral("\n"));

static int fetchNoteIdByName(const QString &name, int noteSubFolderId = -1);

static QVector<Note> fetchAll(int limit = -1);

static QVector<Note> fetchAllNotTagged(int activeNoteSubFolderId);
Expand Down
23 changes: 13 additions & 10 deletions src/entities/tag.cpp
Expand Up @@ -683,7 +683,6 @@ QVector<int> Tag::fetchAllLinkedNoteIds(int tagId, const bool fromAllSubfolders,
const bool recursive) {
QSqlDatabase db = DatabaseService::getNoteFolderDatabase();
QSqlQuery query(db);
QVector<int> noteIdList;

if (fromAllSubfolders) {
// 'All notes' selected in note subfolder panel
Expand Down Expand Up @@ -712,23 +711,26 @@ QVector<int> Tag::fetchAllLinkedNoteIds(int tagId, const bool fromAllSubfolders,
if (!query.exec()) {
qWarning() << __func__ << ": " << query.lastError();
} else {
QVector<int> noteIdList;
for (int r = 0; query.next(); r++) {
// always keep in mind that note_file_name is no file name,
// but the base name (so "my-note", instead of "my-note.md")
const QString &name =
query.value(QStringLiteral("note_file_name")).toString();
const QString &noteSubFolderPathData =
query.value(QStringLiteral("note_sub_folder_path")).toString();
const Note &note = Note::fetchByName(name, noteSubFolderPathData,
QStringLiteral("/"));

noteIdList.append(note.getId());
int noteSubFolderId =
NoteSubFolder::fetchByPathData(noteSubFolderPathData, QStringLiteral("/"))
.getId();
int noteId = Note::fetchNoteIdByName(name, noteSubFolderId);
noteIdList.append(noteId);
}
return noteIdList;
}

DatabaseService::closeDatabaseConnection(db, query);

return noteIdList;
return QVector<int>();
}

/**
Expand Down Expand Up @@ -775,10 +777,11 @@ QVector<int> Tag::fetchAllLinkedNoteIdsForFolder(int tagId,
query.value(QStringLiteral("note_file_name")).toString();
const QString &noteSubFolderPathData =
query.value(QStringLiteral("note_sub_folder_path")).toString();
const Note &note = Note::fetchByName(name, noteSubFolderPathData,
QStringLiteral("/"));

noteIdList.append(note.getId());
int noteSubFolderId =
NoteSubFolder::fetchByPathData(noteSubFolderPathData, QStringLiteral("/"))
.getId();
int noteId = Note::fetchNoteIdByName(name, noteSubFolderId);
noteIdList.append(noteId);
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/mainwindow.cpp
Expand Up @@ -8183,17 +8183,19 @@ QTreeWidgetItem *MainWindow::addTagToTagTreeWidget(QTreeWidgetItem *parent,
const int tagId = tag._id;
const QString name = tag._name;
auto hideCount = QSettings().value("tagsPanelHideNoteCount", false).toBool();
const bool isShowNotesRecursively =
NoteSubFolder::isNoteSubfoldersPanelShowNotesRecursively();

QVector<int> linkedNoteIds;
if (!hideCount) {
const QVector<int> tagIdListToCount = Tag::isTaggingShowNotesRecursively() ?
Tag::fetchTagIdsRecursivelyByParentId(tagId) : QVector<int>{tag._id};
const auto selectedSubFolderItems =
ui->noteSubFolderTreeWidget->selectedItems();
const bool showNotesFromAllSubFolders = this->_showNotesFromAllNoteSubFolders;
const bool isShowNotesRecursively =
NoteSubFolder::isNoteSubfoldersPanelShowNotesRecursively();

if (selectedSubFolderItems.count() > 1) {
linkedNoteIds.reserve(tagIdListToCount.size());
for (const int tagIdToCount : tagIdListToCount) {
for (QTreeWidgetItem *folderItem : selectedSubFolderItems) {
int id = folderItem->data(0, Qt::UserRole).toInt();
Expand All @@ -8205,15 +8207,16 @@ QTreeWidgetItem *MainWindow::addTagToTagTreeWidget(QTreeWidgetItem *parent,

linkedNoteIds << Tag::fetchAllLinkedNoteIdsForFolder(
tagIdToCount,
folder, _showNotesFromAllNoteSubFolders,
folder, showNotesFromAllSubFolders,
isShowNotesRecursively);
}
}
} else {
linkedNoteIds.reserve(tagIdListToCount.size());
for (const int tagToCount : tagIdListToCount) {
linkedNoteIds << Tag::fetchAllLinkedNoteIds(
tagToCount,
_showNotesFromAllNoteSubFolders,
showNotesFromAllSubFolders,
isShowNotesRecursively);
}
}
Expand Down

0 comments on commit 9704df6

Please sign in to comment.