Skip to content

Commit

Permalink
SearchQueryParser: Parse OrNodes by splitting on |
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Oct 5, 2023
1 parent 9e411ff commit 4abc9e6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/library/searchqueryparser.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "library/searchqueryparser.h"

#include <QRegularExpression>
#include <memory>

#include "library/searchquery.h"
#include "track/keyutils.h"

constexpr char kNegatePrefix[] = "-";
Expand Down Expand Up @@ -263,6 +265,28 @@ void SearchQueryParser::parseTokens(QStringList tokens,
}
}

std::unique_ptr<AndNode> SearchQueryParser::parseAndNode(QString query) const {
auto pQuery(std::make_unique<AndNode>());

QStringList tokens = query.split(" ");
parseTokens(std::move(tokens), pQuery.get());

return pQuery;
}

std::unique_ptr<OrNode> SearchQueryParser::parseOrNode(QString query) const {
auto pQuery(std::make_unique<OrNode>());

QStringList rawAndNodes = query.split("|");
for (const QString& rawAndNode : rawAndNodes) {
if (!rawAndNode.isEmpty()) {
pQuery->addNode(parseAndNode(std::move(rawAndNode)));
}
}

return pQuery;
}

std::unique_ptr<QueryNode> SearchQueryParser::parseQuery(
const QString& query,
const QString& extraFilter) const {
Expand All @@ -273,8 +297,7 @@ std::unique_ptr<QueryNode> SearchQueryParser::parseQuery(
}

if (!query.isEmpty()) {
QStringList tokens = query.split(" ");
parseTokens(tokens, pQuery.get());
pQuery->addNode(parseOrNode(query));
}

return pQuery;
Expand Down
4 changes: 4 additions & 0 deletions src/library/searchqueryparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QRegularExpression>
#include <QString>
#include <QtSql>
#include <memory>

#include "library/searchquery.h"
#include "library/trackcollection.h"
Expand All @@ -29,6 +30,9 @@ class SearchQueryParser {
void parseTokens(QStringList tokens,
AndNode* pQuery) const;

std::unique_ptr<AndNode> parseAndNode(QString query) const;
std::unique_ptr<OrNode> parseOrNode(QString query) const;

QString getTextArgument(QString argument,
QStringList* tokens) const;

Expand Down

0 comments on commit 4abc9e6

Please sign in to comment.