2019-08-17 11:06:35 +02:00
|
|
|
#include <QStack>
|
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QSqlQuery>
|
|
|
|
#include <QSqlError>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <optional>
|
|
|
|
#include <algorithm>
|
2021-06-12 14:59:58 +02:00
|
|
|
#include "looqsquery.h"
|
2019-08-17 11:06:35 +02:00
|
|
|
|
2021-06-12 14:59:58 +02:00
|
|
|
const QVector<Token> &LooqsQuery::getTokens() const
|
2019-08-17 11:06:35 +02:00
|
|
|
{
|
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
|
2021-06-12 14:59:58 +02:00
|
|
|
const QVector<SortCondition> &LooqsQuery::getSortConditions() const
|
2019-08-17 11:06:35 +02:00
|
|
|
{
|
|
|
|
return sortConditions;
|
|
|
|
}
|
|
|
|
|
2021-06-12 14:59:58 +02:00
|
|
|
QueryType LooqsQuery::getQueryType()
|
2019-08-17 11:06:35 +02:00
|
|
|
{
|
|
|
|
return static_cast<QueryType>(tokensMask & COMBINED);
|
|
|
|
}
|
|
|
|
|
2022-06-13 22:43:23 +02:00
|
|
|
bool LooqsQuery::hasContentSearch() const
|
2022-01-04 11:09:41 +01:00
|
|
|
{
|
|
|
|
return (this->getTokensMask() & FILTER_CONTENT) == FILTER_CONTENT;
|
|
|
|
}
|
|
|
|
|
2022-06-13 22:43:23 +02:00
|
|
|
bool LooqsQuery::hasPathSearch() const
|
2022-01-04 11:09:41 +01:00
|
|
|
{
|
|
|
|
return (this->getTokensMask() & FILTER_PATH) == FILTER_PATH;
|
|
|
|
}
|
|
|
|
|
2021-06-12 14:59:58 +02:00
|
|
|
void LooqsQuery::addSortCondition(SortCondition sc)
|
2019-08-19 21:09:03 +02:00
|
|
|
{
|
|
|
|
this->sortConditions.append(sc);
|
|
|
|
}
|
|
|
|
|
2021-06-12 14:59:58 +02:00
|
|
|
bool LooqsQuery::checkParanthesis(QString expression)
|
2019-08-17 11:06:35 +02:00
|
|
|
{
|
|
|
|
QStack<QChar> open;
|
|
|
|
QStack<QChar> close;
|
|
|
|
|
2020-12-19 12:51:52 +01:00
|
|
|
bool inQuotes = false;
|
|
|
|
|
2019-08-17 11:06:35 +02:00
|
|
|
for(QChar &c : expression)
|
|
|
|
{
|
2020-12-19 12:51:52 +01:00
|
|
|
if(!inQuotes)
|
2019-08-17 11:06:35 +02:00
|
|
|
{
|
2020-12-19 12:51:52 +01:00
|
|
|
if(c == '(')
|
|
|
|
{
|
|
|
|
open.push(c);
|
|
|
|
}
|
|
|
|
if(c == ')')
|
|
|
|
{
|
|
|
|
close.push(c);
|
|
|
|
}
|
2019-08-17 11:06:35 +02:00
|
|
|
}
|
2020-12-19 12:51:52 +01:00
|
|
|
if(c == '"')
|
2019-08-17 11:06:35 +02:00
|
|
|
{
|
2020-12-19 12:51:52 +01:00
|
|
|
inQuotes = !inQuotes;
|
2019-08-17 11:06:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(open.size() != close.size())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while(!open.empty() && !close.empty())
|
|
|
|
{
|
|
|
|
QChar o = open.pop();
|
|
|
|
QChar c = close.pop();
|
|
|
|
if(o != '(' && c != ')')
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<QueryField> fromString(QString fieldString)
|
|
|
|
{
|
|
|
|
if(fieldString == "path" || fieldString == "file.path")
|
|
|
|
{
|
|
|
|
return FILE_PATH;
|
|
|
|
}
|
|
|
|
else if(fieldString == "mtime" || fieldString == "file.mtime")
|
|
|
|
{
|
|
|
|
return FILE_MTIME;
|
|
|
|
}
|
|
|
|
else if(fieldString == "size" || fieldString == "file.size")
|
|
|
|
{
|
|
|
|
return FILE_SIZE;
|
|
|
|
}
|
|
|
|
else if(fieldString == "content.text")
|
|
|
|
{
|
|
|
|
return CONTENT_TEXT;
|
|
|
|
}
|
|
|
|
else if(fieldString == "content.page" || fieldString == "page")
|
|
|
|
{
|
|
|
|
return CONTENT_TEXT_PAGE;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort:(mtime desc, page asc)
|
|
|
|
QVector<SortCondition> createSortConditions(QString sortExpression)
|
|
|
|
{
|
|
|
|
QVector<SortCondition> result;
|
|
|
|
QStringList splitted_inner = sortExpression.split(",");
|
|
|
|
for(int i = 0; i < splitted_inner.length(); i++)
|
|
|
|
{
|
|
|
|
QStringList splitted = splitted_inner[i].split(" ");
|
|
|
|
if(splitted.length() < 1 || splitted.length() > 2)
|
|
|
|
{
|
2021-06-12 14:59:58 +02:00
|
|
|
throw LooqsGeneralException("sort specifier must have format [field] (asc|desc)");
|
2019-08-17 11:06:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString field = splitted[0];
|
|
|
|
auto queryField = fromString(field);
|
|
|
|
if(!queryField)
|
|
|
|
{
|
2021-06-12 14:59:58 +02:00
|
|
|
throw LooqsGeneralException("Unknown sort field supplied");
|
2019-08-17 11:06:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SortOrder order;
|
|
|
|
if(splitted.length() == 2)
|
|
|
|
{
|
|
|
|
QString orderstr = splitted[1];
|
|
|
|
if(orderstr.compare("asc", Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
order = ASC;
|
|
|
|
}
|
|
|
|
else if(orderstr.compare("desc", Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
order = DESC;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-25 17:18:11 +02:00
|
|
|
throw LooqsGeneralException("Unknown order specifier: " + orderstr);
|
2019-08-17 11:06:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
order = ASC;
|
|
|
|
}
|
|
|
|
|
|
|
|
SortCondition condition;
|
|
|
|
condition.field = queryField.value();
|
|
|
|
condition.order = order;
|
|
|
|
result.append(condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-12 14:59:58 +02:00
|
|
|
void LooqsQuery::addToken(Token t)
|
2019-08-17 11:06:35 +02:00
|
|
|
{
|
|
|
|
tokens.append(t);
|
|
|
|
tokensMask |= t.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Builds the query from the supplied expression
|
|
|
|
*
|
|
|
|
* AND is the default boolean operator, when the user does not provide any
|
|
|
|
* thus, "Downloads zip" becomes essentailly "path.contains:(Downloads) AND path.contains:(zip)"
|
|
|
|
*
|
|
|
|
* TODO: It's a bit ugly still*/
|
2021-12-30 12:17:12 +01:00
|
|
|
LooqsQuery LooqsQuery::build(QString expression, TokenType loneWordsTokenType, bool mergeLoneWords)
|
2019-08-17 11:06:35 +02:00
|
|
|
{
|
2022-06-04 13:34:13 +02:00
|
|
|
if(expression.isEmpty())
|
|
|
|
{
|
|
|
|
return LooqsQuery{};
|
|
|
|
}
|
2019-08-17 11:06:35 +02:00
|
|
|
if(!checkParanthesis(expression))
|
|
|
|
{
|
2021-06-12 14:59:58 +02:00
|
|
|
throw LooqsGeneralException("Invalid paranthesis");
|
2019-08-17 11:06:35 +02:00
|
|
|
}
|
|
|
|
|
2021-12-30 12:17:12 +01:00
|
|
|
QStringList loneWords;
|
2021-06-12 14:59:58 +02:00
|
|
|
LooqsQuery result;
|
2022-06-05 23:37:57 +02:00
|
|
|
QRegularExpression rx(
|
2022-06-14 10:15:00 +02:00
|
|
|
"((?<filtername>(\\.|\\w)+):(?<args>\\((?<innerargs>[^\\)]+)\\)|([\\p{L}\\p{N},])+)|(?<boolean>AND|OR)"
|
|
|
|
"|(?<negation>!)|(?<bracket>\\(|\\))|(?<loneword>[\"\\p{L}\\p{N}]+))");
|
2019-08-17 11:06:35 +02:00
|
|
|
QRegularExpressionMatchIterator i = rx.globalMatch(expression);
|
|
|
|
auto previousWasBool = [&result] { return !result.tokens.empty() && ((result.tokens.last().type & BOOL) == BOOL); };
|
|
|
|
auto previousWas = [&result](TokenType t) { return !result.tokens.empty() && (result.tokens.last().type == t); };
|
|
|
|
|
|
|
|
while(i.hasNext())
|
|
|
|
{
|
|
|
|
QRegularExpressionMatch m = i.next();
|
|
|
|
QString boolean = m.captured("boolean");
|
|
|
|
QString negation = m.captured("negation");
|
|
|
|
QString filtername = m.captured("filtername");
|
|
|
|
QString bracket = m.captured("bracket");
|
|
|
|
QString loneword = m.captured("loneword");
|
|
|
|
|
|
|
|
if(boolean != "")
|
|
|
|
{
|
|
|
|
if(previousWasBool())
|
|
|
|
{
|
2021-06-12 14:59:58 +02:00
|
|
|
throw LooqsGeneralException("Can't have two booleans following each other");
|
2019-08-17 11:06:35 +02:00
|
|
|
}
|
|
|
|
if(previousWas(NEGATION))
|
|
|
|
{
|
2021-06-12 14:59:58 +02:00
|
|
|
throw LooqsGeneralException("Can't have a negation preceeding a boolean");
|
2019-08-17 11:06:35 +02:00
|
|
|
}
|
|
|
|
if(boolean == "AND")
|
|
|
|
{
|
|
|
|
result.addToken(Token(BOOL_AND));
|
|
|
|
}
|
|
|
|
else if(boolean == "OR")
|
|
|
|
{
|
|
|
|
result.addToken(Token(BOOL_OR));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(negation != "")
|
|
|
|
{
|
|
|
|
if(previousWas(NEGATION))
|
|
|
|
{
|
2021-06-12 14:59:58 +02:00
|
|
|
throw LooqsGeneralException("Can't have two negations following each other");
|
2019-08-17 11:06:35 +02:00
|
|
|
}
|
|
|
|
result.addToken(Token(NEGATION));
|
|
|
|
}
|
|
|
|
if(bracket != "")
|
|
|
|
{
|
|
|
|
if(bracket == "(")
|
|
|
|
{
|
|
|
|
result.addToken(Token(BRACKET_OPEN));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.addToken(Token(BRACKET_CLOSE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(loneword != "")
|
|
|
|
{
|
2021-12-30 12:17:12 +01:00
|
|
|
if(mergeLoneWords)
|
|
|
|
{
|
|
|
|
loneWords.append(loneword);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.addToken(Token(loneWordsTokenType, loneword));
|
|
|
|
}
|
2019-08-17 11:06:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(filtername != "")
|
|
|
|
{
|
2022-06-04 23:12:58 +02:00
|
|
|
TokenType tokenType = WORD;
|
2019-08-17 11:06:35 +02:00
|
|
|
QString value = m.captured("innerargs");
|
|
|
|
if(value == "")
|
|
|
|
{
|
|
|
|
value = m.captured("args");
|
|
|
|
}
|
2022-01-04 11:07:59 +01:00
|
|
|
if(value == "")
|
|
|
|
{
|
|
|
|
throw LooqsGeneralException("value cannot be empty for filters");
|
|
|
|
}
|
2019-08-17 11:06:35 +02:00
|
|
|
|
2022-06-04 22:01:33 +02:00
|
|
|
if(filtername == "p" || filtername == "path.contains")
|
2019-08-17 11:06:35 +02:00
|
|
|
{
|
|
|
|
tokenType = FILTER_PATH_CONTAINS;
|
|
|
|
}
|
2022-06-04 22:01:33 +02:00
|
|
|
else if(filtername == "pb" || filtername == "path.starts")
|
2019-08-17 11:06:35 +02:00
|
|
|
{
|
|
|
|
tokenType = FILTER_PATH_STARTS;
|
|
|
|
}
|
2022-06-04 22:01:33 +02:00
|
|
|
else if(filtername == "pe" || filtername == "path.ends")
|
2019-08-17 11:06:35 +02:00
|
|
|
{
|
|
|
|
tokenType = FILTER_PATH_ENDS;
|
|
|
|
}
|
|
|
|
else if(filtername == "file.size" || filtername == "size")
|
|
|
|
{
|
|
|
|
tokenType = FILTER_PATH_SIZE;
|
|
|
|
}
|
|
|
|
else if(filtername == "c" || filtername == "contains")
|
|
|
|
{
|
|
|
|
tokenType = FILTER_CONTENT_CONTAINS;
|
|
|
|
}
|
|
|
|
else if(filtername == "page" || filtername == "content.page")
|
|
|
|
{
|
|
|
|
tokenType = FILTER_CONTENT_PAGE;
|
|
|
|
}
|
2022-06-04 23:12:58 +02:00
|
|
|
// TODO: given this is not really a "filter", this feels slightly misplaced here
|
|
|
|
else if(filtername == "sort")
|
2019-08-17 11:06:35 +02:00
|
|
|
{
|
|
|
|
if(!result.sortConditions.empty())
|
|
|
|
{
|
2021-06-12 14:59:58 +02:00
|
|
|
throw LooqsGeneralException("Two sort statements are illegal");
|
2019-08-17 11:06:35 +02:00
|
|
|
}
|
|
|
|
result.sortConditions = createSortConditions(value);
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-04 23:12:58 +02:00
|
|
|
else if(filtername == "limit")
|
|
|
|
{
|
|
|
|
result.limit = value.toInt();
|
|
|
|
continue;
|
|
|
|
}
|
2019-08-17 11:06:35 +02:00
|
|
|
else
|
|
|
|
{
|
2021-06-12 14:59:58 +02:00
|
|
|
throw LooqsGeneralException("Unknown filter provided!");
|
2019-08-17 11:06:35 +02:00
|
|
|
}
|
|
|
|
result.addToken(Token(tokenType, value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-30 12:17:12 +01:00
|
|
|
if(mergeLoneWords)
|
|
|
|
{
|
2022-01-04 11:07:59 +01:00
|
|
|
QString mergedLoneWords = loneWords.join(' ');
|
|
|
|
if(!mergedLoneWords.isEmpty())
|
|
|
|
{
|
|
|
|
result.addToken(Token(loneWordsTokenType, mergedLoneWords));
|
|
|
|
}
|
2021-12-30 12:17:12 +01:00
|
|
|
}
|
|
|
|
|
2022-06-04 23:12:58 +02:00
|
|
|
/* Add our default implicit AND boolean condition where appropriate */
|
|
|
|
QVector<Token> newTokens;
|
|
|
|
|
|
|
|
TokenType prevType = BOOL_AND;
|
2022-07-28 17:51:12 +02:00
|
|
|
int needsBoolean = FILTER_CONTENT | FILTER_PATH | NEGATION | WORD;
|
2022-06-04 23:12:58 +02:00
|
|
|
for(Token &t : result.tokens)
|
|
|
|
{
|
|
|
|
if(t.type == BRACKET_OPEN || t.type & needsBoolean)
|
|
|
|
{
|
|
|
|
if(!((prevType & BOOL) == BOOL) && !((prevType & NEGATION) == NEGATION) &&
|
|
|
|
!((prevType & BRACKET_OPEN) == BRACKET_OPEN))
|
|
|
|
{
|
|
|
|
newTokens.append(Token(BOOL_AND));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prevType = t.type;
|
|
|
|
newTokens.append(t);
|
|
|
|
}
|
|
|
|
result.tokens = newTokens;
|
|
|
|
|
2022-01-04 11:09:41 +01:00
|
|
|
bool contentsearch = result.hasContentSearch();
|
2019-08-17 11:06:35 +02:00
|
|
|
bool sortsForContent = std::any_of(result.sortConditions.begin(), result.sortConditions.end(),
|
|
|
|
[](SortCondition c) { return c.field == CONTENT_TEXT; });
|
|
|
|
|
|
|
|
if(!contentsearch && sortsForContent)
|
|
|
|
{
|
2021-06-12 14:59:58 +02:00
|
|
|
throw LooqsGeneralException("We cannot sort by text if we don't search for it");
|
2019-08-17 11:06:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|