Esempio n. 1
0
/**
 * Searches for text in notes and returns the note ids
 *
 * By default notes that contain every single word will be found, `word1
 * word2` will find all notes that are containing `word1` and `word2`
 *
 * You can search for longer texts by using quotes, `"this word1" word2`
 * will find all notes that are containing `this word1` and `word2`
 */
QList<int> Note::searchInNotes(QString search) {
    QSqlDatabase db = QSqlDatabase::database("memory");
    QSqlQuery query(db);
    QList<int> noteIdList;
    QStringList sqlList;

    // build the string list of the search string
    QStringList queryStrings = buildQueryStringList(search);

    for (int i = 0; i < queryStrings.count(); i++) {
        sqlList.append("note_text LIKE ?");
    }

    QString sql = "SELECT id FROM note WHERE " + sqlList.join( " AND " );
    query.prepare(sql);

    // add the values to the query
    for (int i = 0; i < queryStrings.count(); i++) {
        query.bindValue(i, "%" + queryStrings[i] + "%");
    }

    if (!query.exec()) {
        qWarning() << __func__ << ": " << query.lastError();
    } else {
        for (int r = 0; query.next(); r++) {
            noteIdList.append(query.value("id").toInt());
        }
    }

    return noteIdList;
}
Esempio n. 2
0
/**
 * Searches for text in notes and returns the note ids
 *
 * By default notes that contain every single word will be found, `word1
 * word2` will find all notes that are containing `word1` and `word2`
 *
 * You can search for longer texts by using quotes, `"this word1" word2`
 * will find all notes that are containing `this word1` and `word2`
 */
QList<int> Note::searchInNotes(QString search, int noteSubFolderId) {
    QSqlDatabase db = QSqlDatabase::database("memory");
    QSqlQuery query(db);
    QList<int> noteIdList;
    QStringList sqlList;

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

    // build the string list of the search string
    QStringList queryStrings = buildQueryStringList(search);

    for (int i = 0; i < queryStrings.count(); i++) {
        sqlList.append("note_text LIKE ?");
    }

    QString sql = "SELECT id FROM note WHERE note_sub_folder_id = "
                          ":note_sub_folder_id AND " + sqlList.join(" AND ");
    query.prepare(sql);
    query.bindValue(0, noteSubFolderId);

    // add the values to the query
    for (int i = 0; i < queryStrings.count(); i++) {
        query.bindValue(i + 1, "%" + queryStrings[i] + "%");
    }

    if (!query.exec()) {
        qWarning() << __func__ << ": " << query.lastError();
    } else {
        for (int r = 0; query.next(); r++) {
            noteIdList.append(query.value("id").toInt());
        }
    }

    return noteIdList;
}