Begin delete command implementation
This commit is contained in:
parent
42a185cdd8
commit
2a1ee328cc
@ -27,7 +27,9 @@ SOURCES += \
|
|||||||
odtprocessor.cpp \
|
odtprocessor.cpp \
|
||||||
utils.cpp \
|
utils.cpp \
|
||||||
odsprocessor.cpp \
|
odsprocessor.cpp \
|
||||||
qssgeneralexception.cpp
|
qssgeneralexception.cpp \
|
||||||
|
commanddelete.cpp \
|
||||||
|
commandupdate.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
encodingdetector.h \
|
encodingdetector.h \
|
||||||
@ -42,5 +44,7 @@ HEADERS += \
|
|||||||
odtprocessor.h \
|
odtprocessor.h \
|
||||||
utils.h \
|
utils.h \
|
||||||
odsprocessor.h \
|
odsprocessor.h \
|
||||||
qssgeneralexception.h
|
qssgeneralexception.h \
|
||||||
|
commanddelete.h \
|
||||||
|
commandupdate.h
|
||||||
INCLUDEPATH += /usr/include/poppler/qt5/ /usr/include/quazip5
|
INCLUDEPATH += /usr/include/poppler/qt5/ /usr/include/quazip5
|
||||||
|
@ -19,6 +19,21 @@ bool Command::fileExistsInDatabase(QSqlDatabase &db, QString path, qint64 mtime)
|
|||||||
return query.value(0).toBool();
|
return query.value(0).toBool();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Command::fileExistsInDatabase(QSqlDatabase &db, QString path)
|
||||||
|
{
|
||||||
|
auto query = QSqlQuery("SELECT 1 FROM file WHERE path = ?", db);
|
||||||
|
query.addBindValue(path);
|
||||||
|
if(!query.exec())
|
||||||
|
{ throw QSSGeneralException("Error while trying to query for file existance");
|
||||||
|
}
|
||||||
|
if(!query.next())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return query.value(0).toBool();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
QSqlDatabase Command::dbConnection()
|
QSqlDatabase Command::dbConnection()
|
||||||
{
|
{
|
||||||
if(dbStore.hasLocalData())
|
if(dbStore.hasLocalData())
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
class Command
|
class Command
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
|
bool fileExistsInDatabase(QSqlDatabase &db, QString path);
|
||||||
bool fileExistsInDatabase(QSqlDatabase &db, QString path, qint64 mtime);
|
bool fileExistsInDatabase(QSqlDatabase &db, QString path, qint64 mtime);
|
||||||
QByteArray readFile(QString path) const;
|
QByteArray readFile(QString path) const;
|
||||||
QString dbConnectionString;
|
QString dbConnectionString;
|
||||||
|
@ -199,7 +199,7 @@ int CommandAdd::handle(QStringList arguments)
|
|||||||
{
|
{
|
||||||
if(result == SKIPPED)
|
if(result == SKIPPED)
|
||||||
{
|
{
|
||||||
qDebug() << "SKIPPED" << path << "as it already exists in the database";
|
qDebug() << "Skipped" << path << "as it already exists in the database";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
107
cli/commanddelete.cpp
Normal file
107
cli/commanddelete.cpp
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
#include <QCommandLineParser>
|
||||||
|
#include <QFile>
|
||||||
|
#include <QFileInfo>
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QRegularExpression>
|
||||||
|
#include <QSqlError>
|
||||||
|
#include "commanddelete.h"
|
||||||
|
|
||||||
|
int CommandDelete::handle(QStringList arguments)
|
||||||
|
{
|
||||||
|
QCommandLineParser parser;
|
||||||
|
parser.addOptions({
|
||||||
|
{ { "v", "verbose" }, "Print path of the files while deleting them" },
|
||||||
|
{ "pattern", "Only delete files from index matching the pattern, e. g. */.git/*", "pattern" },
|
||||||
|
{ "deleted", "Delete all files from the index that don't exist anymore" }
|
||||||
|
});
|
||||||
|
|
||||||
|
parser.addHelpOption();
|
||||||
|
parser.addPositionalArgument("delete", "Delete paths from the index", "delete [paths...]");
|
||||||
|
|
||||||
|
parser.process(arguments);
|
||||||
|
bool removeNonExistant = parser.isSet("deleted");
|
||||||
|
bool verbose = parser.isSet("verbose");
|
||||||
|
QString pattern = parser.value("pattern");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//TODO: port this to QRegularExpression once >5.12 gets more widespread because of this bug
|
||||||
|
//https://bugreports.qt.io/browse/QTBUG-72539?focusedCommentId=439053&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel
|
||||||
|
|
||||||
|
// QRegularExpression regexPattern = QRegularExpression::wildcardToRegularExpression(pattern);
|
||||||
|
|
||||||
|
bool usePattern = ! pattern.isEmpty();
|
||||||
|
QRegExp regexPattern(pattern);
|
||||||
|
regexPattern.setPatternSyntax(QRegExp::PatternSyntax::WildcardUnix);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
QSqlDatabase db = dbConnection();
|
||||||
|
|
||||||
|
|
||||||
|
if(removeNonExistant)
|
||||||
|
{
|
||||||
|
//TODO: try to translate pattern to SQL WHERE
|
||||||
|
QSqlQuery pathsQuery("SELECT path FROM file", db);
|
||||||
|
if(!pathsQuery.exec())
|
||||||
|
{
|
||||||
|
qDebug() << "Failed to query current paths";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(pathsQuery.next())
|
||||||
|
{
|
||||||
|
QString path = pathsQuery.value(0).toString();
|
||||||
|
if(usePattern && regexPattern.exactMatch(path))
|
||||||
|
{
|
||||||
|
QFile file(path);
|
||||||
|
if(!file.exists())
|
||||||
|
{
|
||||||
|
QSqlQuery query("DELETE FROM file WHERE path = ?", db);
|
||||||
|
query.addBindValue(path);
|
||||||
|
if(!query.exec())
|
||||||
|
{
|
||||||
|
qDebug() << "Failed to delete " << path << query.lastError();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if(verbose)
|
||||||
|
{
|
||||||
|
qInfo() << "Deleted " << path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QStringList files = parser.positionalArguments();
|
||||||
|
for(QString &file : files)
|
||||||
|
{
|
||||||
|
QFileInfo fileInfo(file);
|
||||||
|
QString absPath = fileInfo.absoluteFilePath();
|
||||||
|
if(fileExistsInDatabase(db, absPath))
|
||||||
|
{
|
||||||
|
QSqlQuery deletionQuery("DELETE FROM file WHERE path = ?", db);
|
||||||
|
deletionQuery.addBindValue(absPath);
|
||||||
|
if(deletionQuery.exec())
|
||||||
|
{
|
||||||
|
if(verbose)
|
||||||
|
{
|
||||||
|
qInfo() << "Deleted" << absPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qDebug() << "Failed to delete:" << absPath << deletionQuery.lastError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qInfo() << "No such file in database:" << absPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
13
cli/commanddelete.h
Normal file
13
cli/commanddelete.h
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#ifndef COMMANDDELETE_H
|
||||||
|
#define COMMANDDELETE_H
|
||||||
|
#include "command.h"
|
||||||
|
|
||||||
|
class CommandDelete : public Command
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using Command::Command;
|
||||||
|
|
||||||
|
int handle(QStringList arguments) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // COMMANDDELETE_H
|
8
cli/commandupdate.cpp
Normal file
8
cli/commandupdate.cpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include "commandupdate.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int CommandUpdate::handle(QStringList arguments)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
14
cli/commandupdate.h
Normal file
14
cli/commandupdate.h
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#ifndef COMMANDUPDATE_H
|
||||||
|
#define COMMANDUPDATE_H
|
||||||
|
#include "command.h"
|
||||||
|
|
||||||
|
class CommandUpdate : public Command
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CommandUpdate();
|
||||||
|
|
||||||
|
|
||||||
|
int handle(QStringList arguments) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // COMMANDUPDATE_H
|
@ -17,6 +17,7 @@
|
|||||||
#include "defaulttextprocessor.h"
|
#include "defaulttextprocessor.h"
|
||||||
#include "command.h"
|
#include "command.h"
|
||||||
#include "commandadd.h"
|
#include "commandadd.h"
|
||||||
|
#include "commanddelete.h"
|
||||||
void printUsage(QString argv0)
|
void printUsage(QString argv0)
|
||||||
{
|
{
|
||||||
qInfo() << "Usage: " << argv0 << "command";
|
qInfo() << "Usage: " << argv0 << "command";
|
||||||
@ -30,7 +31,7 @@ Command *commandFromName(QString name, QString connectionstring)
|
|||||||
}
|
}
|
||||||
if(name == "delete")
|
if(name == "delete")
|
||||||
{
|
{
|
||||||
|
return new CommandDelete(connectionstring);
|
||||||
}
|
}
|
||||||
if(name == "update")
|
if(name == "update")
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user