Support filtering by since/before dates in the search engine

e.g. added>7 (recent content since 7 days ago) or added<30 (content added before the last 30 days)
This commit is contained in:
Luis Ángel San Martín 2023-05-31 22:11:47 +02:00
parent d3829bc07c
commit e4dbe3d62e
2 changed files with 29 additions and 11 deletions

View File

@ -8,11 +8,11 @@
#include <QsLog.h>
const std::map<QueryParser::FieldType, std::vector<std::string>> QueryParser::fieldNames {
// TODO_METADATA support dates
{ FieldType::numeric, { "numpages", "count", "arccount", "alternateCount", "rating" } },
{ FieldType::text, { "number", "arcnumber", "title", "volume", "storyarc", "genere", "writer", "penciller", "inker", "colorist", "letterer", "coverartist", "publisher", "format", "agerating", "synopsis", "characters", "notes", "editor", "imprint", "teams", "locations", "series", "alternateSeries", "alternateNumber", "languageISO", "seriesGroup", "mainCharacterOrTeam", "review", "tags" } },
{ FieldType::text, { "date", "number", "arcnumber", "title", "volume", "storyarc", "genere", "writer", "penciller", "inker", "colorist", "letterer", "coverartist", "publisher", "format", "agerating", "synopsis", "characters", "notes", "editor", "imprint", "teams", "locations", "series", "alternateSeries", "alternateNumber", "languageISO", "seriesGroup", "mainCharacterOrTeam", "review", "tags" } },
{ FieldType::boolean, { "color", "read", "edited", "hasBeenOpened" } },
{ FieldType::date, { "date", "added", "lastTimeOpened" } },
{ FieldType::date, { "added", "lastTimeOpened" } },
{ FieldType::dateFolder, { "added", "updated" } },
{ FieldType::filename, { "filename" } },
{ FieldType::folder, { "folder" } },
{ FieldType::booleanFolder, { "completed", "finished" } },
@ -20,6 +20,25 @@ const std::map<QueryParser::FieldType, std::vector<std::string>> QueryParser::fi
{ FieldType::enumFieldFolder, { "foldertype" } }
};
std::string operatorToSQLOperator(const std::string &expOperator)
{
if (expOperator == ":" || expOperator == "=" || expOperator == "==") {
return "=";
} else {
return expOperator;
}
}
// this parses N days to date N days ago for now
std::string parseDate(const std::string &dateString, const std::string &expOperator)
{
// TODO_METADATA add semantics so different formats and meanings are supported,
auto now = QDateTime::currentSecsSinceEpoch();
auto date = now - stoi(dateString) * 86400;
return std::to_string(date);
}
int QueryParser::TreeNode::buildSqlString(std::string &sqlString, int bindPosition) const
{
// TODO: add some semantic checks, not all operators apply to all fields
@ -32,14 +51,10 @@ int QueryParser::TreeNode::buildSqlString(std::string &sqlString, int bindPositi
}
sqlString += "UPPER(c.filename) LIKE UPPER(:bindPosition" + std::to_string(bindPosition) + ") OR ";
sqlString += "UPPER(f.name) LIKE UPPER(:bindPosition" + std::to_string(bindPosition) + ")) ";
} else if (isIn(fieldType(children[0].t), { FieldType::numeric })) {
std::string sqlOperator;
if (expOperator == ":" || expOperator == "=" || expOperator == "==") {
sqlOperator = "=";
} else {
sqlOperator = expOperator;
}
sqlString += "ci." + children[0].t + " " + sqlOperator + " :bindPosition" + std::to_string(bindPosition) + " ";
} else if (isIn(fieldType(children[0].t), { FieldType::numeric, FieldType::date })) {
sqlString += "ci." + children[0].t + " " + operatorToSQLOperator(expOperator) + " :bindPosition" + std::to_string(bindPosition) + " ";
} else if (isIn(fieldType(children[0].t), { FieldType::dateFolder })) {
sqlString += "f." + children[0].t + " " + operatorToSQLOperator(expOperator) + " :bindPosition" + std::to_string(bindPosition) + " ";
} else if (isIn(fieldType(children[0].t), { FieldType::boolean, FieldType::enumField })) {
sqlString += "ci." + children[0].t + " = :bindPosition" + std::to_string(bindPosition) + " ";
} else if (fieldType(children[0].t) == FieldType::filename) {
@ -114,6 +129,8 @@ int QueryParser::TreeNode::bindValues(QSqlQuery &selectQuery, int bindPosition)
} else {
selectQuery.bindValue(QString::fromStdString(bind_string), std::stoi(children[1].t));
}
} else if ((isIn(fieldType(children[0].t), { FieldType::date, FieldType::dateFolder }))) {
selectQuery.bindValue(QString::fromStdString(bind_string), QString::fromStdString(parseDate(children[1].t, expOperator)));
} else {
if (expOperator == "=" || expOperator == ":" || expOperator == "") {
selectQuery.bindValue(QString::fromStdString(bind_string), QString::fromStdString("%%" + children[1].t + "%%"));

View File

@ -88,6 +88,7 @@ private:
text,
boolean,
date,
dateFolder,
folder,
booleanFolder,
filename,