void medDatabaseRemover::removePatient ( int patientDbId ) { QSqlDatabase db(d->db); QSqlQuery query ( db ); QString patientName; QString patientBirthdate; QString patientId; query.prepare ( "SELECT thumbnail, patientId FROM " + d->T_PATIENT + " WHERE id = :patient " ); query.bindValue ( ":patient", patientDbId ); EXEC_QUERY ( query ); if ( query.next() ) { QString thumbnail = query.value ( 0 ).toString(); this->removeFile ( thumbnail ); patientId = query.value ( 1 ).toString(); } if( removeTableRow ( d->T_PATIENT, patientDbId ) ) emit removed(medDataIndex(1, patientDbId, -1, -1, -1)); medDatabaseController * dbi = medDatabaseController::instance(); QDir patientDir ( medStorage::dataLocation() + "/" + dbi->stringForPath ( patientId ) ); if ( patientDir.exists() ) patientDir.rmdir ( patientDir.path() ); // only removes if empty }
/** * Moves study and its series from one patient to another and returns the list of new indexes * @param const medDataIndex & indexStudy The data index of the study to be moved * @param const medDataIndex & toPatient The data index to move the study to. */ QList<medDataIndex> medDatabaseController::moveStudy( const medDataIndex& indexStudy, const medDataIndex& toPatient) { QSqlQuery query(this->database()); bool result = false; QList<medDataIndex> newIndexList; medDataIndex newIndex; if(indexStudy.isValidForStudy() && toPatient.isValidForPatient()) { query.prepare("UPDATE study SET patient=:patientId WHERE id=:studyId"); query.bindValue(":patientId", toPatient.patientId()); query.bindValue(":studyId", indexStudy.studyId()); result = EXEC_QUERY(query); if(result) { // we need to update patient id in study index newIndex = indexStudy; newIndex.setPatientId(toPatient.patientId()); newIndexList << newIndex; // and update patient id in series indexes QList<medDataIndex> seriesIndexList = series(indexStudy); foreach(medDataIndex newSerieIndex, seriesIndexList) { newSerieIndex.setPatientId(toPatient.patientId()); newIndexList << newSerieIndex; } }
bool medDatabaseRemover::removeTableRow ( const QString &table, int id ) { QSqlDatabase db(d->db); QSqlQuery query ( db ); query.prepare ( "DELETE FROM " + table + " WHERE id = :id" ); query.bindValue ( ":id", id ); EXEC_QUERY ( query ); return (query.numRowsAffected()==1); }
bool medDatabaseRemover::isPatientEmpty ( int patientDbId ) { QSqlDatabase db(d->db); QSqlQuery query ( db ); query.prepare ( "SELECT id FROM " + d->T_STUDY + " WHERE patient = :patient " ); query.bindValue ( ":patient", patientDbId ); EXEC_QUERY ( query ); return !query.next(); }
bool medDatabaseRemover::isStudyEmpty ( int studyDbId ) { QSqlDatabase db(d->db); QSqlQuery query ( db ); query.prepare ( "SELECT id FROM " + d->T_SERIES + " WHERE study = :study " ); query.bindValue ( ":study", studyDbId ); EXEC_QUERY ( query ); return !query.next(); }
bool medDatabaseRemover::isSeriesEmpty ( int seriesDbId ) { QSqlDatabase db(d->db); QSqlQuery query ( db ); query.prepare ( "SELECT id FROM " + d->T_IMAGE + " WHERE series = :series " ); query.bindValue ( ":series", seriesDbId ); EXEC_QUERY ( query ); return !query.next(); }
void medDatabaseRemover::removeImage ( int patientDbId, int studyDbId, int seriesDbId, int imageId ) { QSqlDatabase db(d->db); QSqlQuery query ( db ); query.prepare ( "SELECT thumbnail FROM " + d->T_IMAGE + " WHERE id = :imageId " ); query.bindValue ( ":id", imageId ); EXEC_QUERY ( query ); if ( query.next() ) { QString thumbnail = query.value ( 0 ).toString(); this->removeFile ( thumbnail ); } removeTableRow ( d->T_IMAGE, imageId ); }
void medDatabaseRemover::removeStudy ( int patientDbId, int studyDbId ) { QSqlDatabase db(d->db); QSqlQuery query ( db ); query.prepare ( "SELECT thumbnail, name, uid FROM " + d->T_STUDY + " WHERE id = :id " ); query.bindValue ( ":id", studyDbId ); EXEC_QUERY ( query ); QString thumbnail; if ( query.next() ) { thumbnail = query.value ( 0 ).toString(); this->removeFile ( thumbnail ); } if( removeTableRow ( d->T_STUDY, studyDbId ) ) emit removed(medDataIndex(1, patientDbId, studyDbId, -1, -1)); }
void medDatabaseRemover::removeSeries ( int patientDbId, int studyDbId, int seriesDbId ) { QSqlDatabase db(d->db); QSqlQuery query ( db ); query.prepare ( "SELECT thumbnail, path, name FROM " + d->T_SERIES + " WHERE id = :series " ); query.bindValue ( ":series", seriesDbId ); EXEC_QUERY ( query ); QString thumbnail; if ( query.next() ) { thumbnail = query.value ( 0 ).toString(); this->removeFile ( thumbnail ); QString path = query.value ( 1 ).toString(); // if path is empty then it was an indexed series if ( !path.isNull() && !path.isEmpty() ) this->removeDataFile ( medDataIndex::makeSeriesIndex ( d->index.dataSourceId(), patientDbId, studyDbId, seriesDbId ) , path ); } if( removeTableRow ( d->T_SERIES, seriesDbId ) ) emit removed(medDataIndex(1, patientDbId, studyDbId, seriesDbId, -1)); // we want to remove the directory if empty QFileInfo seriesFi ( medStorage::dataLocation() + thumbnail ); if ( seriesFi.dir().exists() ) { bool res = seriesFi.dir().rmdir ( seriesFi.absolutePath() ); // only removes if empty // the serie's directory has been deleted, let's check if the patient directory is empty // this can happen after moving series if(res) { QDir parentDir = seriesFi.dir(); res = parentDir.cdUp(); if ( res && parentDir.exists() ) { res = seriesFi.dir().rmdir ( parentDir.absolutePath() ); // only removes if empty } } } }
void medDatabaseRemover::internalRun() { QSqlDatabase db( d->db ); QSqlQuery ptQuery ( db ); const medDataIndex index = d->index; if ( index.isValidForPatient() ) { ptQuery.prepare ( "SELECT id FROM " + d->T_PATIENT + " WHERE id = :id" ); ptQuery.bindValue ( ":id", index.patientId() ); } else { ptQuery.prepare ( "SELECT id FROM " + d->T_PATIENT ); } EXEC_QUERY ( ptQuery ); while ( ptQuery.next() ) { if ( d->isCancelled ) break; int patientDbId = ptQuery.value ( 0 ).toInt(); QSqlQuery stQuery ( db ); if ( index.isValidForStudy() ) { stQuery.prepare ( "SELECT id FROM " + d->T_STUDY + " WHERE id = :id AND patient = :patient" ); stQuery.bindValue ( ":id", index.studyId() ); } else { stQuery.prepare ( "SELECT id FROM " + d->T_STUDY + " WHERE patient = :patient" ); } stQuery.bindValue ( ":patient", patientDbId ); EXEC_QUERY ( stQuery ); while ( stQuery.next() ) { if ( d->isCancelled ) break; int studyDbId = stQuery.value ( 0 ).toInt(); QSqlQuery seQuery ( db ); if ( index.isValidForSeries() ) { seQuery.prepare ( "SELECT id FROM " + d->T_SERIES + " WHERE id = :id AND study = :study" ); seQuery.bindValue ( ":id", index.seriesId() ); } else { seQuery.prepare ( "SELECT id FROM " + d->T_SERIES + " WHERE study = :study" ); } seQuery.bindValue ( ":study", studyDbId ); EXEC_QUERY ( seQuery ); while ( seQuery.next() ) { if ( d->isCancelled ) break; int seriesDbId = seQuery.value ( 0 ).toInt(); QSqlQuery imQuery ( db ); if ( index.isValidForImage() ) { imQuery.prepare ( "SELECT id FROM " + d->T_IMAGE + " WHERE id = :id AND series = :seriesId" ); imQuery.bindValue ( ":id", index.imageId() ); } else { imQuery.prepare ( "SELECT id FROM " + d->T_IMAGE + " WHERE series = :series" ); } imQuery.bindValue ( ":series", seriesDbId ); EXEC_QUERY ( imQuery ); imQuery.last(); double nbImage = imQuery.at(); imQuery.first(); do { int imageId = imQuery.value ( 0 ).toInt(); this->removeImage ( patientDbId, studyDbId, seriesDbId, imageId ); emit progress (this, imQuery.at() / nbImage * 100 ); } while ( imQuery.next() ); if ( this->isSeriesEmpty ( seriesDbId ) ) this->removeSeries ( patientDbId, studyDbId, seriesDbId ); } // seQuery.next if ( this->isStudyEmpty ( studyDbId ) ) this->removeStudy ( patientDbId, studyDbId ); } // stQuery.next if ( this->isPatientEmpty ( patientDbId ) ) this->removePatient ( patientDbId ); } // ptQuery.next if ( d->isCancelled ) emit failure ( this ); else emit success ( this ); return; }