Debian Codegraph @

maz-db/src/MazDBQuery.h

raw symbols SCIP
1 /* Copyright (C) 2017 Dan Chapman <[email protected]>
2
3 This file is part of Dekko email client for Ubuntu devices
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License or (at your option) version 3
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef MAZDBQUERY_H
19 #define MAZDBQUERY_H
20
21 #include <QObject>
22 #include <QQmlEngine>
23 #include <QJSEngine>
24 #include <QJSValue>
25 #include <QQmlListProperty>
26 #include "MazDB.h"
27
28 class QueryBase : public QObject
29 {
30 Q_OBJECT
31 Q_PROPERTY(QString property READ prop WRITE setProp NOTIFY queryChanged)
32 Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY queryChanged)
33 Q_PROPERTY(MatchTypes matchRule READ matchType WRITE setMatchType NOTIFY queryChanged)
34 Q_PROPERTY(QJSValue comparitor READ comparitor WRITE setComparitor NOTIFY queryChanged)
35 Q_ENUMS(MatchTypes)
36 public:
37 explicit QueryBase(QObject *parent = 0);
38
39 enum MatchTypes {
40 Equals,
41 NotEquals,
42 LessThan,
43 LessThanOrEqual,
44 GreaterThan,
45 GreaterThanOrEqual
46 };
47
48 QString prop() const;
49 QVariant value() const;
50 MatchTypes matchType() const;
51
52 bool hasMatch(const QVariant &value);
53
54 QJSValue comparitor() const;
55
56 signals:
57 void queryChanged();
58
59 public slots:
60 void setProp(QString property);
61 void setValue(QVariant value);
62 void setMatchType(MatchTypes matchRule);
63 void setComparitor(QJSValue comparitor);
64
65 protected:
66 bool compare(const QVariant &value);
67 private:
68 QString m_property;
69 QVariant m_value;
70 MatchTypes m_matchRule;
71 QJSValue m_comparitor;
72 };
73
74 #define QUERY_CLASS(name) \
75 class name : public QueryBase { \
76 Q_OBJECT \
77 public: \
78 explicit name (QObject *parent = 0) : QueryBase(parent) {}; \
79 };
80
81 QUERY_CLASS(WhereQuery)
82 QUERY_CLASS(OrQuery)
83 QUERY_CLASS(AndQuery)
84
85 class Range : public QObject
86 {
87 Q_OBJECT
88 Q_PROPERTY(QString start READ start WRITE setStart NOTIFY startChanged)
89 Q_PROPERTY(QString end READ end WRITE setEnd NOTIFY endChanged)
90 public:
91 explicit Range(QObject *parent = 0);
92 bool hasRange() const;
93 QString start() const;
94 QString end() const;
95
96 public slots:
97 void setStart(QString start);
98 void setEnd(QString end);
99
100 signals:
101 void startChanged(QString start);
102 void endChanged(QString end);
103
104 private:
105 Q_DISABLE_COPY(Range)
106 QString m_start;
107 QString m_end;
108 };
109
110 class MazDBQuery : public QObject
111 {
112 Q_OBJECT
113 Q_PROPERTY(Range *range READ range)
114 Q_PROPERTY(int limit READ limit WRITE setLimit NOTIFY limitChanged)
115 Q_PROPERTY(QQmlListProperty<QueryBase> definition READ query)
116 public:
117 explicit MazDBQuery(QObject *parent = 0);
118
119 Range *range();
120 int limit() const;
121 QQmlListProperty<QueryBase> query();
122 bool runQuery(MazDB *db, MazCallBack callback);
123
124 public slots:
125 void setLimit(int limit);
126
127 signals:
128 void limitChanged(int limit);
129
130 protected:
131 Range m_range;
132 int m_limit;
133 WhereQuery *getWhereQuery();
134 OrQuery *getOrQuery();
135 QList<AndQuery *> getAndQueries();
136
137 private:
138 Q_DISABLE_COPY(MazDBQuery)
139 QList<QueryBase *> m_queries;
140 };
141
142
143
144 #endif // MAZDBQUERY_H