int Note::storeDirtyNotesToDisk( Note ¤tNote ) { QSqlQuery query; Note note; // qDebug() << "storeDirtyNotesToDisk"; query.prepare( "SELECT * FROM note WHERE has_dirty_data = 1" ); if( !query.exec() ) { qDebug() << __func__ << ": " << query.lastError(); return 0; } else { int count = 0; for( int r=0; query.next(); r++ ) { note = noteFromQuery( query ); QString oldFileName = note.getFileName(); note.storeNoteTextFileToDisk(); QString newFileName = note.getFileName(); // reassign currentNote if filename of currentNote has changed if ( ( oldFileName == currentNote.getFileName() ) && ( oldFileName != newFileName ) ) { currentNote = note; qDebug() << "filename of currentNote has changed to: " << newFileName; } qDebug() << "stored note: " << note; count++; } return count; } }
QList<Note> Note::fetchAll(int limit) { QSqlDatabase db = QSqlDatabase::database("memory"); QSqlQuery query(db); QList<Note> noteList; QString sql = "SELECT * FROM note ORDER BY file_last_modified DESC"; if (limit >= 0) { sql += " LIMIT :limit"; } query.prepare(sql); if (limit >= 0) { query.bindValue(":limit", limit); } if (!query.exec()) { qWarning() << __func__ << ": " << query.lastError(); } else { for (int r = 0; query.next(); r++) { Note note = noteFromQuery(query); noteList.append(note); } } return noteList; }
Note Note::fetchByName(QString name, int noteSubFolderId) { QSqlDatabase db = QSqlDatabase::database("memory"); QSqlQuery query(db); Note note; // get the active note subfolder id if none was set if (noteSubFolderId == -1) { noteSubFolderId = NoteSubFolder::activeNoteSubFolderId(); } query.prepare("SELECT * FROM note WHERE name = :name AND " "note_sub_folder_id = :note_sub_folder_id"); query.bindValue(":name", name); query.bindValue(":note_sub_folder_id", noteSubFolderId); if (!query.exec()) { qWarning() << __func__ << ": " << query.lastError(); } else { if (query.first()) { note = noteFromQuery(query); } } return note; }
/** * Stores all notes that were changed to disk * * @param currentNote will be set by this method if the filename of the current * note has changed * @param currentNoteChanged true if current note was changed * @param noteWasRenamed true if a note was renamed * @return amount of notes that were saved */ int Note::storeDirtyNotesToDisk(Note ¤tNote, bool *currentNoteChanged, bool *noteWasRenamed) { QSqlDatabase db = QSqlDatabase::database("memory"); QSqlQuery query(db); ScriptingService* scriptingService = ScriptingService::instance(); Note note; // qDebug() << "storeDirtyNotesToDisk"; query.prepare("SELECT * FROM note WHERE has_dirty_data = 1"); if (!query.exec()) { qWarning() << __func__ << ": " << query.lastError(); return 0; } else { int count = 0; for (int r = 0; query.next(); r++) { note = noteFromQuery(query); QString oldName = note.getName(); note.storeNoteTextFileToDisk(); QString newName = note.getName(); // check if the file name has changed if (oldName != newName) { // rename the note file names of note tag links Tag::renameNoteFileNamesOfLinks(oldName, newName); *noteWasRenamed = true; // override the current note because the file name has changed currentNote = note; } // emit the signal for the QML that the note was stored emit scriptingService->noteStored( QVariant::fromValue( static_cast<QObject*>(NoteApi::fromNote(note)))); // reassign currentNote if filename of currentNote has changed if (note.isSameFile(currentNote)) { *currentNoteChanged = true; } qDebug() << "stored note: " << note; count++; } return count; } }
QList<Note> Note::fetchAll() { QSqlDatabase db = QSqlDatabase::database("memory"); QSqlQuery query(db); QList<Note> noteList; query.prepare("SELECT * FROM note ORDER BY file_last_modified DESC"); if (!query.exec()) { qWarning() << __func__ << ": " << query.lastError(); } else { for (int r = 0; query.next(); r++) { Note note = noteFromQuery(query); noteList.append(note); } } return noteList; }
Note Note::fetch(int id) { QSqlDatabase db = QSqlDatabase::database("memory"); QSqlQuery query(db); Note note; query.prepare("SELECT * FROM note WHERE id = :id"); query.bindValue(":id", id); if (!query.exec()) { qWarning() << __func__ << ": " << query.lastError(); } else { if (query.first()) { note = noteFromQuery(query); } } return note; }
Note Note::fetchByName(QString name) { QSqlDatabase db = QSqlDatabase::database("memory"); QSqlQuery query(db); Note note; query.prepare("SELECT * FROM note WHERE name = :name"); query.bindValue(":name", name); if (!query.exec()) { qWarning() << __func__ << ": " << query.lastError(); } else { if (query.first()) { note = noteFromQuery(query); } } return note; }
QList<Note> Note::search(QString text) { QSqlDatabase db = QSqlDatabase::database("memory"); QSqlQuery query(db); QList<Note> noteList; query.prepare("SELECT * FROM note WHERE note_text LIKE :text " "ORDER BY file_last_modified DESC"); query.bindValue(":text", "%" + text + "%"); if (!query.exec()) { qWarning() << __func__ << ": " << query.lastError(); } else { for (int r = 0; query.next(); r++) { Note note = noteFromQuery(query); noteList.append(note); } } return noteList; }
int Note::storeDirtyNotesToDisk(Note ¤tNote) { QSqlDatabase db = QSqlDatabase::database("memory"); QSqlQuery query(db); ScriptingService* scriptingService = ScriptingService::instance(); Note note; // qDebug() << "storeDirtyNotesToDisk"; query.prepare("SELECT * FROM note WHERE has_dirty_data = 1"); if (!query.exec()) { qWarning() << __func__ << ": " << query.lastError(); return 0; } else { int count = 0; for (int r = 0; query.next(); r++) { note = noteFromQuery(query); QString oldFileName = note.getFileName(); note.storeNoteTextFileToDisk(); QString newFileName = note.getFileName(); // emit the signal for the QML that the note was stored emit scriptingService->noteStored( QVariant::fromValue( static_cast<QObject*>(NoteApi::fromNote(note)))); // reassign currentNote if filename of currentNote has changed if ((oldFileName == currentNote.getFileName()) && (oldFileName != newFileName)) { currentNote = note; qDebug() << "filename of currentNote has changed to: " << newFileName; } qDebug() << "stored note: " << note; count++; } return count; } }
Note Note::fetch( int id ) { QSqlQuery query; Note note; query.prepare( "SELECT * FROM note WHERE id = :id" ); query.bindValue( ":id", id ); if( !query.exec() ) { qDebug() << __func__ << ": " << query.lastError(); } else { if ( query.first() ) { note = noteFromQuery( query ); } } return note; }
QList<Note> Note::fetchAllByNoteSubFolderId(int noteSubFolderId) { QSqlDatabase db = QSqlDatabase::database("memory"); QSqlQuery query(db); QList<Note> noteList; QString sql = "SELECT * FROM note WHERE note_sub_folder_id = " ":note_sub_folder_id ORDER BY file_last_modified DESC"; query.prepare(sql); query.bindValue(":note_sub_folder_id", noteSubFolderId); if (!query.exec()) { qWarning() << __func__ << ": " << query.lastError(); } else { for (int r = 0; query.next(); r++) { Note note = noteFromQuery(query); noteList.append(note); } } return noteList; }