CommandDelete: refactored to use sqlitedbservice
This commit is contained in:
parent
c6a7663ffa
commit
4ece459d77
@ -5,88 +5,71 @@
|
|||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
#include <QSqlError>
|
#include <QSqlError>
|
||||||
#include "commanddelete.h"
|
#include "commanddelete.h"
|
||||||
|
#include "logger.h"
|
||||||
|
|
||||||
|
int CommandDelete::removeNonExistent(bool verbose, bool dryRun, QString pattern)
|
||||||
int CommandDelete::removeNonExistent(QSqlDatabase &db, bool verbose, bool dryRun, QString pattern)
|
|
||||||
{
|
{
|
||||||
|
int offset = 0;
|
||||||
//TODO: port this to QRegularExpression once >5.12 gets more widespread because of this bug
|
int limit = 1000;
|
||||||
//https://bugreports.qt.io/browse/QTBUG-72539?focusedCommentId=439053&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel
|
QVector<FileData> files;
|
||||||
|
int processedRows = this->dbService->getFiles(files, pattern, 0, limit);
|
||||||
// QRegularExpression regexPattern = QRegularExpression::wildcardToRegularExpression(pattern
|
while(processedRows > 0 )
|
||||||
bool usePattern = ! pattern.isEmpty();
|
|
||||||
QRegExp regexPattern(pattern);
|
|
||||||
regexPattern.setPatternSyntax(QRegExp::PatternSyntax::WildcardUnix);
|
|
||||||
|
|
||||||
|
|
||||||
//TODO: try to translate pattern to SQL WHERE statement
|
|
||||||
QSqlQuery pathsQuery("SELECT path FROM file", db);
|
|
||||||
if(!pathsQuery.exec())
|
|
||||||
{
|
{
|
||||||
Utils::error() << "Failed to query current paths" << endl;
|
for(FileData &file : files)
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
while(pathsQuery.next())
|
|
||||||
{
|
|
||||||
QString path = pathsQuery.value(0).toString();
|
|
||||||
bool removeFile = true;
|
|
||||||
if(usePattern)
|
|
||||||
{
|
|
||||||
removeFile = regexPattern.exactMatch(path);
|
|
||||||
}
|
|
||||||
if(removeFile)
|
|
||||||
{
|
|
||||||
QFile file(path);
|
|
||||||
if(!file.exists())
|
|
||||||
{
|
{
|
||||||
if(!dryRun)
|
if(!dryRun)
|
||||||
{
|
{
|
||||||
QSqlQuery query("DELETE FROM file WHERE path = ?", db);
|
QFileInfo fileInfo(file.absPath);
|
||||||
query.addBindValue(path);
|
if(fileInfo.exists())
|
||||||
if(!query.exec())
|
{
|
||||||
|
if(this->dbService->deleteFile(file.absPath))
|
||||||
{
|
{
|
||||||
Utils::error() << "Failed to delete " << path << query.lastError() << endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if(verbose)
|
if(verbose)
|
||||||
{
|
{
|
||||||
Utils::info() << "Deleted " << path << endl;
|
Logger::info() << "Deleted" << file.absPath << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Utils::info() << "Would delete " << path << endl;
|
Logger::error()<< "Failed to delete:" << file.absPath << ", exiting." << endl
|
||||||
|
;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Logger::info() << "Would delete " << file.absPath << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
offset += limit;
|
||||||
|
processedRows = this->dbService->getFiles(files, pattern, 0, limit);
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int CommandDelete::removePaths(const QStringList &paths, QSqlDatabase &db, bool verbose, bool dryRun)
|
int CommandDelete::removePaths(const QStringList &paths, bool verbose, bool dryRun)
|
||||||
{
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
for(const QString &file : paths)
|
for(const QString &file : paths)
|
||||||
{
|
{
|
||||||
QFileInfo fileInfo(file);
|
QFileInfo fileInfo(file);
|
||||||
QString absPath = fileInfo.absoluteFilePath();
|
QString absPath = fileInfo.absoluteFilePath();
|
||||||
if(fileExistsInDatabase(db, absPath))
|
if(this->dbService->fileExistsInDatabase(absPath))
|
||||||
{
|
{
|
||||||
if(!dryRun)
|
if(!dryRun)
|
||||||
{
|
{
|
||||||
QSqlQuery deletionQuery("DELETE FROM file WHERE path = ?", db);
|
if(this->dbService->deleteFile(absPath))
|
||||||
deletionQuery.addBindValue(absPath);
|
|
||||||
if(deletionQuery.exec())
|
|
||||||
{
|
{
|
||||||
if(verbose)
|
if(verbose)
|
||||||
{
|
{
|
||||||
Utils::info() << "Deleted" << absPath << endl;
|
Logger::info() << "Deleted" << absPath << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Utils::error()<< "Failed to delete:" << absPath << deletionQuery.lastError() << endl;
|
Logger::error()<< "Failed to delete:" << absPath << endl;
|
||||||
result = 1;
|
result = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,12 +77,13 @@ int CommandDelete::removePaths(const QStringList &paths, QSqlDatabase &db, bool
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Utils::error() << "No such file in database:" << absPath << endl;
|
Logger::error() << "No such file in database:" << absPath << endl;
|
||||||
result = 1;
|
result = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CommandDelete::handle(QStringList arguments)
|
int CommandDelete::handle(QStringList arguments)
|
||||||
{
|
{
|
||||||
QCommandLineParser parser;
|
QCommandLineParser parser;
|
||||||
@ -117,13 +101,10 @@ int CommandDelete::handle(QStringList arguments)
|
|||||||
bool verbose = parser.isSet("verbose");
|
bool verbose = parser.isSet("verbose");
|
||||||
bool dryRun = parser.isSet("dry-run");
|
bool dryRun = parser.isSet("dry-run");
|
||||||
QString pattern = parser.value("pattern");
|
QString pattern = parser.value("pattern");
|
||||||
QSqlDatabase db = dbConnection();
|
|
||||||
|
|
||||||
|
|
||||||
if(parser.isSet("deleted"))
|
if(parser.isSet("deleted"))
|
||||||
{
|
{
|
||||||
|
|
||||||
int result = removeNonExistent(db, verbose, dryRun, pattern);
|
int result = removeNonExistent(verbose, dryRun, pattern);
|
||||||
if(result != 0)
|
if(result != 0)
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
@ -131,11 +112,15 @@ int CommandDelete::handle(QStringList arguments)
|
|||||||
}
|
}
|
||||||
|
|
||||||
QStringList files = parser.positionalArguments();
|
QStringList files = parser.positionalArguments();
|
||||||
int result = removePaths(files,db, verbose, dryRun);
|
if(files.length() > 0)
|
||||||
|
{
|
||||||
|
int result = removePaths(files, verbose, dryRun);
|
||||||
if(result != 0)
|
if(result != 0)
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -9,8 +9,8 @@ public:
|
|||||||
|
|
||||||
int handle(QStringList arguments) override;
|
int handle(QStringList arguments) override;
|
||||||
private:
|
private:
|
||||||
int removeNonExistent(QSqlDatabase &db, bool verbose, bool dryRun, QString pattern);
|
int removeNonExistent(bool verbose, bool dryRun, QString pattern);
|
||||||
int removePaths(const QStringList &paths, QSqlDatabase &db, bool verbose, bool dryRun);
|
int removePaths(const QStringList &paths, bool verbose, bool dryRun);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // COMMANDDELETE_H
|
#endif // COMMANDDELETE_H
|
||||||
|
Loading…
Reference in New Issue
Block a user