Commit 4f226717 authored by David Barchiesi's avatar David Barchiesi Committed by Daniel Vrátil

Refactor FileSearchQuery and create base class SearchQuery

Summary: As discussed in  [[ https://phabricator.kde.org/T10521#177460 | T10521 ]], it is useful to refactor out the generic query building logic in FileSearchQuery and create the base class SearchQuery. This would allow us to create other domain specific queries without having to reimplement all the building logic, as will be the case with TeamdriveSearchQuery.

Test Plan: All tests defined in FileSearchQueryTest should remain valid and, as of this patch, passing.

Reviewers: dvratil

Reviewed By: dvratil

Subscribers: kde-pim, #libkgapi

Tags: #kde_pim, #libkgapi

Maniphest Tasks: T10521

Differential Revision: https://phabricator.kde.org/D19490
parent 2c3f6af2
......@@ -10,6 +10,7 @@ set(kgapidrive_SRCS
childreferencedeletejob.cpp
childreferencefetchjob.cpp
driveservice.cpp
searchquery.cpp
file.cpp
fileabstractdatajob.cpp
fileabstractmodifyjob.cpp
......@@ -54,6 +55,7 @@ ecm_generate_headers(kgapidrive_CamelCase_HEADERS
ChildReferenceCreateJob
ChildReferenceDeleteJob
ChildReferenceFetchJob
SearchQuery
File
FileAbstractDataJob
FileAbstractModifyJob
......
/*
* Copyright (C) 2014 Daniel Vrátil <dvratil@redhat.com>
* Copyright (C) 2019 David Barchiesi <david@barchie.si>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
......@@ -26,46 +27,8 @@
using namespace KGAPI2;
using namespace KGAPI2::Drive;
class Q_DECL_HIDDEN FileSearchQuery::Private : public QSharedData
{
public:
Private();
Private(const Private &other);
~Private();
static QString fieldToString(Field field);
static QString compareOperatorToString(CompareOperator op);
static QString logicOperatorToString(LogicOperator op);
static QString valueToString(Field field, const QVariant &var);
QList<FileSearchQuery> subqueries;
QVariant value;
Field field;
CompareOperator compareOp;
LogicOperator logicOp;
};
FileSearchQuery::Private::Private()
: QSharedData()
{
}
FileSearchQuery::Private::Private(const Private &other)
: QSharedData(other)
, subqueries(other.subqueries)
, value(other.value)
, field(other.field)
, compareOp(other.compareOp)
, logicOp(other.logicOp)
{
}
FileSearchQuery::Private::~Private()
{
}
QString FileSearchQuery::Private::fieldToString(Field field)
QString FileSearchQuery::fieldToString(Field field)
{
switch (field) {
case Title:
......@@ -98,47 +61,7 @@ QString FileSearchQuery::Private::fieldToString(Field field)
return QString();
}
QString FileSearchQuery::Private::compareOperatorToString(CompareOperator op)
{
switch (op) {
case Contains:
return QStringLiteral(" contains ");
case Equals:
return QStringLiteral(" = ");
case NotEquals:
return QStringLiteral(" != ");
case Less:
return QStringLiteral(" < ");
case LessOrEqual:
return QStringLiteral(" <= ");
case Greater:
return QStringLiteral(" > ");
case GreaterOrEqual:
return QStringLiteral(" >= ");
case In:
return QStringLiteral(" in ");
case Has:
return QStringLiteral(" has ");
}
Q_ASSERT(false);
return QString();
}
QString FileSearchQuery::Private::logicOperatorToString(FileSearchQuery::LogicOperator op)
{
switch (op) {
case And:
return QStringLiteral(" and ");
case Or:
return QStringLiteral(" or ");
}
Q_ASSERT(false);
return QString();
}
QString FileSearchQuery::Private::valueToString(FileSearchQuery::Field field, const QVariant &var)
QString FileSearchQuery::valueToString(FileSearchQuery::Field field, const QVariant &var)
{
switch (field) {
case Title:
......@@ -162,28 +85,6 @@ QString FileSearchQuery::Private::valueToString(FileSearchQuery::Field field, co
return QString();
}
FileSearchQuery::FileSearchQuery(FileSearchQuery::LogicOperator op)
: d(new Private)
{
d->logicOp = op;
}
FileSearchQuery::FileSearchQuery(const FileSearchQuery &other)
: d(other.d)
{
}
FileSearchQuery::~FileSearchQuery()
{
}
FileSearchQuery &FileSearchQuery::operator=(const FileSearchQuery &other)
{
d = other.d;
return *this;
}
void FileSearchQuery::addQuery(FileSearchQuery::Field field, FileSearchQuery::CompareOperator op, const QVariant &value)
{
switch (field) {
......@@ -216,50 +117,5 @@ void FileSearchQuery::addQuery(FileSearchQuery::Field field, FileSearchQuery::Co
break;
}
FileSearchQuery query;
query.d->field = field;
query.d->compareOp = op;
query.d->value = value;
d->subqueries.append(query);
SearchQuery::addQuery(fieldToString(field), op, valueToString(field, value));
}
void FileSearchQuery::addQuery(const FileSearchQuery &query)
{
d->subqueries.append(query);
}
bool FileSearchQuery::isEmpty() const
{
return d->value.isNull() && d->subqueries.isEmpty();
}
QString FileSearchQuery::serialize() const
{
if (isEmpty()) {
return QString();
}
QString r;
r = QLatin1Char('(');
if (d->subqueries.isEmpty()) {
if (d->compareOp == In) {
r += QStringLiteral("%1 in %2").arg(Private::valueToString(d->field, d->value),
Private::fieldToString(d->field));
} else {
r += Private::fieldToString(d->field) % Private::compareOperatorToString(d->compareOp) % Private::valueToString(d->field, d->value);
}
} else {
QList<FileSearchQuery>::ConstIterator iter, end;
for (iter = d->subqueries.constBegin(), end = d->subqueries.constEnd(); iter != end; ++iter) {
if (iter != d->subqueries.constBegin()) {
r += Private::logicOperatorToString(d->logicOp);
}
r += (*iter).serialize();
}
}
r += QLatin1Char(')');
return r;
}
/*
* Copyright (C) 2014 Daniel Vrátil <dvratil@redhat.com>
* Copyright (C) 2019 David Barchiesi <david@barchie.si>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
......@@ -23,6 +24,8 @@
#include "kgapidrive_export.h"
#include "searchquery.h"
#include <QVariant>
#include <QSharedDataPointer>
......@@ -40,7 +43,7 @@ namespace Drive
*
* @since 2.3
*/
class KGAPIDRIVE_EXPORT FileSearchQuery
class KGAPIDRIVE_EXPORT FileSearchQuery : public SearchQuery
{
public:
enum Field {
......@@ -59,38 +62,14 @@ public:
/*Properties FIXME: Not supported atm */
};
enum CompareOperator {
Contains,
Equals,
NotEquals,
Less,
LessOrEqual,
Greater,
GreaterOrEqual,
In,
Has
};
enum LogicOperator {
And,
Or
};
explicit FileSearchQuery(LogicOperator op = And);
FileSearchQuery(const FileSearchQuery &other);
FileSearchQuery &operator=(const FileSearchQuery &other);
~FileSearchQuery();
using SearchQuery::SearchQuery;
using SearchQuery::addQuery;
void addQuery(Field field, CompareOperator op, const QVariant &value);
void addQuery(const FileSearchQuery &query);
bool isEmpty() const;
QString serialize() const;
private:
class Private;
QSharedDataPointer<Private> d;
private:
QString fieldToString(Field field);
QString valueToString(FileSearchQuery::Field field, const QVariant &var);
};
}
}
......
/*
* Copyright (C) 2014 Daniel Vrátil <dvratil@redhat.com>
* Copyright (C) 2019 David Barchiesi <david@barchie.si>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) version 3, or any
* later version accepted by the membership of KDE e.V. (or its
* successor approved by the membership of KDE e.V.), which shall
* act as a proxy defined in Section 6 of version 3 of the license.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <https://www.gnu.org/licenses/>.
*/
#include "searchquery.h"
#include <QString>
#include <QDateTime>
using namespace KGAPI2;
using namespace KGAPI2::Drive;
class Q_DECL_HIDDEN SearchQuery::Private : public QSharedData
{
public:
Private() = default;
Private(const Private &other) = default;
~Private() = default;
static QString compareOperatorToString(CompareOperator op);
static QString logicOperatorToString(LogicOperator op);
QList<SearchQuery> subqueries;
QString field;
QString value;
CompareOperator compareOp;
LogicOperator logicOp;
};
QString SearchQuery::Private::compareOperatorToString(CompareOperator op)
{
switch (op) {
case Contains:
return QStringLiteral(" contains ");
case Equals:
return QStringLiteral(" = ");
case NotEquals:
return QStringLiteral(" != ");
case Less:
return QStringLiteral(" < ");
case LessOrEqual:
return QStringLiteral(" <= ");
case Greater:
return QStringLiteral(" > ");
case GreaterOrEqual:
return QStringLiteral(" >= ");
case In:
return QStringLiteral(" in ");
case Has:
return QStringLiteral(" has ");
}
Q_ASSERT(false);
return QString();
}
QString SearchQuery::Private::logicOperatorToString(SearchQuery::LogicOperator op)
{
switch (op) {
case And:
return QStringLiteral(" and ");
case Or:
return QStringLiteral(" or ");
}
Q_ASSERT(false);
return QString();
}
SearchQuery::SearchQuery(SearchQuery::LogicOperator op)
: d(new Private)
{
d->logicOp = op;
}
SearchQuery::SearchQuery(const SearchQuery &other) = default;
SearchQuery::~SearchQuery() = default;
SearchQuery &SearchQuery::operator=(const SearchQuery &other) = default;
void SearchQuery::addQuery(const QString &field, SearchQuery::CompareOperator op, const QString &value)
{
SearchQuery query;
query.d->field = field;
query.d->compareOp = op;
query.d->value = value;
d->subqueries.append(query);
}
void SearchQuery::addQuery(const SearchQuery &query)
{
d->subqueries.append(query);
}
bool SearchQuery::isEmpty() const
{
return d->value.isNull() && d->subqueries.isEmpty();
}
QString SearchQuery::serialize() const
{
if (isEmpty()) {
return QString();
}
QString r;
r = QLatin1Char('(');
if (d->subqueries.isEmpty()) {
if (d->compareOp == In) {
r += QStringLiteral("%1 in %2").arg(d->value, d->field);
} else {
r += d->field % Private::compareOperatorToString(d->compareOp) % d->value;
}
} else {
QList<SearchQuery>::ConstIterator iter, end;
for (iter = d->subqueries.constBegin(), end = d->subqueries.constEnd(); iter != end; ++iter) {
if (iter != d->subqueries.constBegin()) {
r += Private::logicOperatorToString(d->logicOp);
}
r += (*iter).serialize();
}
}
r += QLatin1Char(')');
return r;
}
/*
* Copyright (C) 2014 Daniel Vrátil <dvratil@redhat.com>
* Copyright (C) 2019 David Barchiesi <david@barchie.si>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) version 3, or any
* later version accepted by the membership of KDE e.V. (or its
* successor approved by the membership of KDE e.V.), which shall
* act as a proxy defined in Section 6 of version 3 of the license.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef KGAPI2_DRIVE_SEARCHQUERY_H
#define KGAPI2_DRIVE_SEARCHQUERY_H
#include "kgapidrive_export.h"
#include <QVariant>
#include <QSharedDataPointer>
namespace KGAPI2
{
namespace Drive
{
/**
* SearchQuery class should be used as a base class for building file/team search queries.
*/
class KGAPIDRIVE_EXPORT SearchQuery
{
public:
enum CompareOperator {
Contains,
Equals,
NotEquals,
Less,
LessOrEqual,
Greater,
GreaterOrEqual,
In,
Has
};
enum LogicOperator {
And,
Or
};
SearchQuery(LogicOperator op = And);
SearchQuery(const SearchQuery &other);
~SearchQuery();
SearchQuery &operator=(const SearchQuery &other);
void addQuery(const QString &field, CompareOperator op, const QString &value);
void addQuery(const SearchQuery &query);
bool isEmpty() const;
QString serialize() const;
private:
class Private;
QSharedDataPointer<Private> d;
};
}
}
#endif // KGAPI2_DRIVE_SEARCHQUERY_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment