move more files to shared project ; searchresultt.h: use filedata.h
This commit is contained in:
14
shared/filedata.h
Normal file
14
shared/filedata.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef FILEDATA_H
|
||||
#define FILEDATA_H
|
||||
#include <QString>
|
||||
|
||||
class FileData
|
||||
{
|
||||
public:
|
||||
QString absPath;
|
||||
uint mtime;
|
||||
uint size;
|
||||
QChar filetype;
|
||||
};
|
||||
|
||||
#endif // FILEDATA_H
|
1
shared/qssgeneralexception.cpp
Normal file
1
shared/qssgeneralexception.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "qssgeneralexception.h"
|
24
shared/qssgeneralexception.h
Normal file
24
shared/qssgeneralexception.h
Normal file
@ -0,0 +1,24 @@
|
||||
#ifndef QSSGENERALEXCEPTION_H
|
||||
#define QSSGENERALEXCEPTION_H
|
||||
|
||||
#include <QException>
|
||||
|
||||
class QSSGeneralException : public QException
|
||||
{
|
||||
public:
|
||||
QString message;
|
||||
QSSGeneralException(QString message)
|
||||
{
|
||||
this->message = message;
|
||||
}
|
||||
void raise() const override
|
||||
{
|
||||
throw *this;
|
||||
}
|
||||
QSSGeneralException *clone() const override
|
||||
{
|
||||
return new QSSGeneralException(*this);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // QSSGENERALEXCEPTION_H
|
12
shared/searchresult.h
Normal file
12
shared/searchresult.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef SEARCHRESULT_H
|
||||
#define SEARCHRESULT_H
|
||||
#include "filedata.h"
|
||||
|
||||
class SearchResult
|
||||
{
|
||||
public:
|
||||
FileData fileData;
|
||||
unsigned int page;
|
||||
};
|
||||
|
||||
#endif // SEARCHRESULT_H
|
@ -23,9 +23,13 @@ DEFINES += QT_DEPRECATED_WARNINGS
|
||||
# You can also select to disable deprecated APIs only up to a certain version of Qt.
|
||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
||||
SOURCES += sqlitesearch.cpp
|
||||
SOURCES += sqlitesearch.cpp \
|
||||
qssgeneralexception.cpp
|
||||
|
||||
HEADERS += sqlitesearch.h
|
||||
HEADERS += sqlitesearch.h \
|
||||
filedata.h \
|
||||
searchresult.h \
|
||||
qssgeneralexception.h
|
||||
unix {
|
||||
target.path = /usr/lib
|
||||
INSTALLS += target
|
||||
|
@ -1,17 +1,21 @@
|
||||
#include <QStack>
|
||||
#include <QRegularExpression>
|
||||
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlError>
|
||||
#include <QDebug>
|
||||
#include "sqlitesearch.h"
|
||||
#include "qssgeneralexception.h"
|
||||
|
||||
SqliteSearch::SqliteSearch()
|
||||
SqliteSearch::SqliteSearch(QSqlDatabase &db)
|
||||
{
|
||||
this->db = &db;
|
||||
}
|
||||
|
||||
QVector<SqliteSearch::Token> SqliteSearch::tokenize(QString expression)
|
||||
{
|
||||
if(!checkParanthesis(expression))
|
||||
{
|
||||
throw std::invalid_argument("Invalid paranthesis");
|
||||
throw QSSGeneralException("Invalid paranthesis");
|
||||
}
|
||||
// TODO: merge lonewords
|
||||
QVector<SqliteSearch::Token> result;
|
||||
@ -108,7 +112,7 @@ QString SqliteSearch::createSql(const SqliteSearch::Token &token)
|
||||
"' )";
|
||||
}
|
||||
|
||||
throw std::invalid_argument("Unknown filter: " + key.toStdString());
|
||||
throw QSSGeneralException("Unknown filter: " + key);
|
||||
}
|
||||
|
||||
QString SqliteSearch::makeSql(const QVector<SqliteSearch::Token> &tokens)
|
||||
@ -121,8 +125,45 @@ QString SqliteSearch::makeSql(const QVector<SqliteSearch::Token> &tokens)
|
||||
return result;
|
||||
}
|
||||
|
||||
void SqliteSearch::search(const QString &query)
|
||||
QVector<SearchResult> SqliteSearch::search(const QString &query)
|
||||
{
|
||||
QVector<SearchResult> results;
|
||||
QString whereSql = makeSql(tokenize(query));
|
||||
QString prep;
|
||||
// TODO: hack, as we don't wanna look into content and get redundant results, when we don't even care about content
|
||||
if(whereSql.contains("content."))
|
||||
{
|
||||
prep = "SELECT file.path AS path, content.page AS page, file.mtime AS mtime, file.size AS size, file.filetype "
|
||||
"AS filetype FROM file INNER JOIN content ON file.id = content.fileid WHERE 1=1 AND " +
|
||||
whereSql + " ORDER By file.mtime DESC, content.page ASC";
|
||||
}
|
||||
else
|
||||
{
|
||||
prep = "SELECT file.path AS path, 0 as page, file.mtime AS mtime, file.size AS size, file.filetype AS "
|
||||
"filetype FROM file WHERE " +
|
||||
whereSql + " ORDER by file.mtime DESC";
|
||||
}
|
||||
QSqlQuery dbquery(*db);
|
||||
dbquery.prepare(prep);
|
||||
bool success = dbquery.exec();
|
||||
if(!success)
|
||||
{
|
||||
qDebug() << "prepped: " << prep;
|
||||
qDebug() << dbquery.lastError();
|
||||
throw QSSGeneralException("SQL Error: " + dbquery.lastError().text());
|
||||
}
|
||||
|
||||
while(dbquery.next())
|
||||
{
|
||||
SearchResult result;
|
||||
result.fileData.absPath = dbquery.value("path").toString();
|
||||
result.fileData.mtime = dbquery.value("mtime").toUInt();
|
||||
result.fileData.size = dbquery.value("filesize").toUInt();
|
||||
result.fileData.filetype = dbquery.value("filetype").toChar();
|
||||
result.page = dbquery.value("page").toUInt();
|
||||
results.append(result);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
bool SqliteSearch::checkParanthesis(QString expression)
|
||||
|
@ -1,6 +1,7 @@
|
||||
#ifndef SQLITESEARCH_H
|
||||
#define SQLITESEARCH_H
|
||||
#include <QSqlDatabase>
|
||||
#include "searchresult.h"
|
||||
|
||||
class SqliteSearch
|
||||
{
|
||||
@ -25,9 +26,8 @@ class SqliteSearch
|
||||
bool checkParanthesis(QString expression);
|
||||
|
||||
public:
|
||||
SqliteSearch();
|
||||
SqliteSearch(QSqlDatabase &db);
|
||||
void search(const QString &query);
|
||||
QVector<SearchResult> search(const QString &query);
|
||||
};
|
||||
|
||||
#endif // SQLITESEARCH_H
|
||||
|
Reference in New Issue
Block a user