move more files to shared project ; searchresultt.h: use filedata.h
Αυτή η υποβολή περιλαμβάνεται σε:
14
shared/filedata.h
Κανονικό αρχείο
14
shared/filedata.h
Κανονικό αρχείο
@ -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
|
2
shared/qssgeneralexception.cpp
Κανονικό αρχείο
2
shared/qssgeneralexception.cpp
Κανονικό αρχείο
@ -0,0 +1,2 @@
|
||||
#include "qssgeneralexception.h"
|
||||
|
15
shared/qssgeneralexception.h
Κανονικό αρχείο
15
shared/qssgeneralexception.h
Κανονικό αρχείο
@ -0,0 +1,15 @@
|
||||
#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
|
13
shared/searchresult.h
Κανονικό αρχείο
13
shared/searchresult.h
Κανονικό αρχείο
@ -0,0 +1,13 @@
|
||||
#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,18 +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;
|
||||
@ -111,7 +114,7 @@ QString SqliteSearch::createSql(const SqliteSearch::Token &token)
|
||||
return " content.id IN (SELECT content_fts.ROWID FROM content_fts WHERE content_fts.content MATCH '" + value + "' )";
|
||||
}
|
||||
|
||||
throw std::invalid_argument("Unknown filter: " + key.toStdString());
|
||||
throw QSSGeneralException("Unknown filter: " + key);
|
||||
}
|
||||
|
||||
QString SqliteSearch::makeSql(const QVector<SqliteSearch::Token> &tokens)
|
||||
@ -124,9 +127,41 @@ 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,20 +1,21 @@
|
||||
#ifndef SQLITESEARCH_H
|
||||
#define SQLITESEARCH_H
|
||||
#include <QSqlDatabase>
|
||||
#include "searchresult.h"
|
||||
|
||||
class SqliteSearch
|
||||
{
|
||||
class Token
|
||||
{
|
||||
public:
|
||||
QString key;
|
||||
QString value;
|
||||
public:
|
||||
QString key;
|
||||
QString value;
|
||||
|
||||
Token(QString key="", QString value="")
|
||||
{
|
||||
this->key = key;
|
||||
this->value = value;
|
||||
}
|
||||
Token(QString key="", QString value="")
|
||||
{
|
||||
this->key = key;
|
||||
this->value = value;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
@ -27,9 +28,8 @@ private:
|
||||
|
||||
|
||||
public:
|
||||
SqliteSearch();
|
||||
SqliteSearch(QSqlDatabase &db);
|
||||
void search(const QString &query);
|
||||
QVector<SearchResult> search(const QString &query);
|
||||
|
||||
};
|
||||
|
||||
|
Αναφορά σε νέο ζήτημα
Block a user