Akonadi

entity.h
1/***************************************************************************
2 * SPDX-FileCopyrightText: 2006 Andreas Gungl <a.gungl@gmx.de> *
3 * *
4 * SPDX-License-Identifier: LGPL-2.0-or-later *
5 ***************************************************************************/
6
7#pragma once
8
9#include <QString>
10#include <QStringList>
11
12class QVariant;
13
14namespace Akonadi
15{
16namespace Server
17{
18class DataStore;
19/**
20 Base class for classes representing database records. It also contains
21 low-level data access and manipulation template methods.
22*/
23class Entity
24{
25public:
26 using Id = qint64;
27
28protected:
29 qint64 id() const;
30 void setId(qint64 id);
31
32 bool isValid() const;
33
34public:
35 template<typename T>
36 static QString joinByName(const QList<T> &list, const QString &sep)
37 {
38 QStringList tmp;
39 tmp.reserve(list.count());
40 for (const T &t : list) {
41 tmp << t.name();
42 }
43 return tmp.join(sep);
44 }
45
46 /**
47 Returns the number of records having @p value in @p column.
48 @param column The name of the key column.
49 @param value The value used to identify the record.
50 */
51 template<typename T>
52 inline static int count(const QString &column, const QVariant &value)
53 {
54 return count<T>(dataStore(), column, value);
55 }
56
57 template<typename T>
58 inline static int count(DataStore *store, const QString &column, const QVariant &value)
59 {
60 return Entity::countImpl(store, T::tableName(), column, value);
61 }
62
63 /**
64 Deletes all records having @p value in @p column.
65 */
66 template<typename T>
67 inline static bool remove(const QString &column, const QVariant &value)
68 {
69 return remove<T>(dataStore(), column, value);
70 }
71
72 template<typename T>
73 inline static bool remove(DataStore *store, const QString &column, const QVariant &value)
74 {
75 return Entity::removeImpl(store, T::tableName(), column, value);
76 }
77
78 /**
79 Checks whether an entry in a n:m relation table exists.
80 @param leftId Identifier of the left part of the relation.
81 @param rightId Identifier of the right part of the relation.
82 */
83 template<typename T>
84 inline static bool relatesTo(qint64 leftId, qint64 rightId)
85 {
86 return relatesTo<T>(dataStore(), leftId, rightId);
87 }
88
89 template<typename T>
90 inline static bool relatesTo(DataStore *store, qint64 leftId, qint64 rightId)
91 {
92 return Entity::relatesToImpl(store, T::tableName(), T::leftColumn(), T::rightColumn(), leftId, rightId);
93 }
94
95 /**
96 Adds an entry to a n:m relation table (specified by the template parameter).
97 @param leftId Identifier of the left part of the relation.
98 @param rightId Identifier of the right part of the relation.
99 */
100 template<typename T>
101 inline static bool addToRelation(qint64 leftId, qint64 rightId)
102 {
103 return addToRelation<T>(dataStore(), leftId, rightId);
104 }
105
106 template<typename T>
107 inline static bool addToRelation(DataStore *store, qint64 leftId, qint64 rightId)
108 {
109 return Entity::addToRelationImpl(store, T::tableName(), T::leftColumn(), T::rightColumn(), leftId, rightId);
110 }
111
112 /**
113 Removes an entry from a n:m relation table (specified by the template parameter).
114 @param leftId Identifier of the left part of the relation.
115 @param rightId Identifier of the right part of the relation.
116 */
117 template<typename T>
118 inline static bool removeFromRelation(qint64 leftId, qint64 rightId)
119 {
120 return removeFromRelation<T>(dataStore(), leftId, rightId);
121 }
122
123 template<typename T>
124 inline static bool removeFromRelation(DataStore *store, qint64 leftId, qint64 rightId)
125 {
126 return Entity::removeFromRelationImpl(store, T::tableName(), T::leftColumn(), T::rightColumn(), leftId, rightId);
127 }
128
129 enum RelationSide {
130 Left,
131 Right,
132 };
133
134 /**
135 Clears all entries from a n:m relation table (specified by the given template parameter).
136 @param id Identifier on the relation side.
137 @param side The side of the relation.
138 */
139 template<typename T>
140 inline static bool clearRelation(qint64 id, RelationSide side = Left)
141 {
142 return clearRelation<T>(dataStore(), id, side);
143 }
144 template<typename T>
145 inline static bool clearRelation(DataStore *store, qint64 id, RelationSide side = Left)
146 {
147 return Entity::clearRelationImpl(store, T::tableName(), T::leftColumn(), T::rightColumn(), id, side);
148 }
149
150protected:
151 Entity();
152 explicit Entity(qint64 id);
153 ~Entity();
154
155private:
156 static DataStore *dataStore();
157
158 static int countImpl(DataStore *store, const QString &tableName, const QString &column, const QVariant &value);
159 static bool removeImpl(DataStore *store, const QString &tableName, const QString &column, const QVariant &value);
160 static bool relatesToImpl(DataStore *store, const QString &tableName, const QString &leftColumn, const QString &rightColumn, qint64 leftId, qint64 rightId);
161 static bool
162 addToRelationImpl(DataStore *store, const QString &tableName, const QString &leftColumn, const QString &rightColumn, qint64 leftId, qint64 rightId);
163 static bool
164 removeFromRelationImpl(DataStore *store, const QString &tableName, const QString &leftColumn, const QString &rightColumn, qint64 leftId, qint64 rightId);
165 static bool
166 clearRelationImpl(DataStore *store, const QString &tableName, const QString &leftColumn, const QString &rightColumn, qint64 id, RelationSide side);
167
168private:
169 qint64 m_id;
170};
171
172namespace _detail
173{
174/*!
175 Binary predicate to sort collections of Entity subclasses by
176 their id.
177
178 Example for sorting:
179 \code
180 std::sort( coll.begin(), coll.end(), _detail::ById<std::less>() );
181 \endcode
182
183 Example for finding by id:
184 \code
185 // linear:
186 std::find_if( coll.begin(), coll.end(), bind( _detail::ById<std::equal_to>(), _1, myId ) );
187 // binary:
188 std::lower_bound( coll.begin(), coll.end(), myId, _detail::ById<std::less>() );
189 \endcode
190*/
191template<template<typename U> class Op>
192struct ById {
193 using result_type = bool;
194 bool operator()(Entity::Id lhs, Entity::Id rhs) const
195 {
196 return Op<Entity::Id>()(lhs, rhs);
197 }
198 template<typename E>
199 bool operator()(const E &lhs, const E &rhs) const
200 {
201 return this->operator()(lhs.id(), rhs.id());
202 }
203 template<typename E>
204 bool operator()(const E &lhs, Entity::Id rhs) const
205 {
206 return this->operator()(lhs.id(), rhs);
207 }
208 template<typename E>
209 bool operator()(Entity::Id lhs, const E &rhs) const
210 {
211 return this->operator()(lhs, rhs.id());
212 }
213};
214}
215
216} // namespace Server
217} // namespace Akonadi
This class handles all the database access.
Definition datastore.h:95
Base class for classes representing database records.
Definition entity.h:24
static bool remove(const QString &column, const QVariant &value)
Deletes all records having value in column.
Definition entity.h:67
static bool clearRelation(qint64 id, RelationSide side=Left)
Clears all entries from a n:m relation table (specified by the given template parameter).
Definition entity.h:140
static bool relatesTo(qint64 leftId, qint64 rightId)
Checks whether an entry in a n:m relation table exists.
Definition entity.h:84
static bool addToRelation(qint64 leftId, qint64 rightId)
Adds an entry to a n:m relation table (specified by the template parameter).
Definition entity.h:101
static bool removeFromRelation(qint64 leftId, qint64 rightId)
Removes an entry from a n:m relation table (specified by the template parameter).
Definition entity.h:118
static int count(const QString &column, const QVariant &value)
Returns the number of records having value in column.
Definition entity.h:52
Helper integration between Akonadi and Qt.
qsizetype count() const const
void reserve(qsizetype size)
QString join(QChar separator) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:31:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.