File indexing completed on 2025-01-05 04:47:00

0001 /*
0002     SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QList>
0010 #include <QString>
0011 #include <QVariant>
0012 
0013 namespace Akonadi
0014 {
0015 namespace Server
0016 {
0017 class QueryBuilder;
0018 
0019 /**
0020   Building blocks for SQL queries.
0021   @see QueryBuilder
0022 */
0023 namespace Query
0024 {
0025 /**
0026   Compare operators to be used in query conditions.
0027 */
0028 enum CompareOperator {
0029     Equals,
0030     NotEquals,
0031     Is,
0032     IsNot,
0033     Less,
0034     LessOrEqual,
0035     Greater,
0036     GreaterOrEqual,
0037     In,
0038     NotIn,
0039     Like,
0040 };
0041 
0042 /**
0043   Logic operations used to combine multiple query conditions.
0044 */
0045 enum LogicOperator {
0046     And,
0047     Or,
0048 };
0049 
0050 /**
0051   Sort orders.
0052 */
0053 enum SortOrder {
0054     Ascending,
0055     Descending,
0056 };
0057 
0058 /**
0059   Represents a WHERE condition tree.
0060 */
0061 class Condition
0062 {
0063     friend class Akonadi::Server::QueryBuilder;
0064 
0065 public:
0066     /** A list of conditions. */
0067     using List = QList<Condition>;
0068 
0069     /**
0070       Create an empty condition.
0071       @param op how to combine sub queries.
0072     */
0073     explicit Condition(LogicOperator op = And);
0074 
0075     /**
0076       Add a WHERE condition which compares a column with a given value.
0077       @param column The column that should be compared.
0078       @param op The operator used for comparison
0079       @param value The value @p column is compared to.
0080     */
0081     void addValueCondition(const QString &column, CompareOperator op, const QVariant &value);
0082 
0083     /**
0084       Add a WHERE condition which compares a column with another column.
0085       @param column The column that should be compared.
0086       @param op The operator used for comparison.
0087       @param column2 The column @p column is compared to.
0088     */
0089     void addColumnCondition(const QString &column, CompareOperator op, const QString &column2);
0090 
0091     /**
0092       Add a WHERE condition. Use this method to build hierarchical conditions.
0093     */
0094     void addCondition(const Condition &condition);
0095 
0096     /**
0097       Set how sub-conditions should be combined, default is And.
0098     */
0099     void setSubQueryMode(LogicOperator op);
0100 
0101     /**
0102       Returns if there are sub conditions.
0103     */
0104     bool isEmpty() const;
0105 
0106     /**
0107       Returns the list of sub-conditions.
0108     */
0109     Condition::List subConditions() const;
0110 
0111 private:
0112     Condition::List mSubConditions;
0113     QString mColumn;
0114     QString mComparedColumn;
0115     QVariant mComparedValue;
0116     CompareOperator mCompareOp;
0117     LogicOperator mCombineOp;
0118 
0119 }; // class Condition
0120 
0121 class Case
0122 {
0123     friend class Akonadi::Server::QueryBuilder;
0124 
0125 public:
0126     Case(const Condition &when, const QString &then, const QString &elseBranch = QString());
0127     Case(const QString &column, Query::CompareOperator op, const QVariant &value, const QString &when, const QString &elseBranch = QString());
0128 
0129     void addCondition(const Condition &when, const QString &then);
0130     void addValueCondition(const QString &column, Query::CompareOperator op, const QVariant &value, const QString &then);
0131     void addColumnCondition(const QString &column, Query::CompareOperator op, const QString &column2, const QString &then);
0132 
0133     void setElse(const QString &elseBranch);
0134 
0135 private:
0136     QList<QPair<Condition, QString>> mWhenThen;
0137     QString mElse;
0138 };
0139 
0140 } // namespace Query
0141 } // namespace Server
0142 } // namespace Akonadi
0143 
0144 Q_DECLARE_TYPEINFO(Akonadi::Server::Query::Condition, Q_RELOCATABLE_TYPE);