Akonadi

entity.h
1 /***************************************************************************
2  * SPDX-FileCopyrightText: 2006 Andreas Gungl <[email protected]> *
3  * *
4  * SPDX-License-Identifier: LGPL-2.0-or-later *
5  ***************************************************************************/
6 
7 #ifndef ENTITY_H
8 #define ENTITY_H
9 
10 #include <QString>
11 #include <QStringList>
12 
13 class QVariant;
14 class QSqlDatabase;
15 
16 namespace Akonadi
17 {
18 namespace Server
19 {
20 
25 class Entity
26 {
27 public:
28  typedef qint64 Id;
29 
30 protected:
31  qint64 id() const;
32  void setId(qint64 id);
33 
34  bool isValid() const;
35 
36 public:
37  template <typename T> static QString joinByName(const QVector<T> &list, const QString &sep)
38  {
39  QStringList tmp;
40  tmp.reserve(list.count());
41  for (const T &t : list) {
42  tmp << t.name();
43  }
44  return tmp.join(sep);
45  }
46 
52  template <typename T> inline static int count(const QString &column, const QVariant &value)
53  {
54  return Entity::countImpl(T::tableName(), column, value);
55  }
56 
60  template <typename T> inline static bool remove(const QString &column, const QVariant &value)
61  {
62  return Entity::removeImpl(T::tableName(), column, value);
63  }
64 
70  template <typename T> inline static bool relatesTo(qint64 leftId, qint64 rightId)
71  {
72  return Entity::relatesToImpl(T::tableName(), T::leftColumn(), T::rightColumn(), leftId, rightId);
73  }
74 
80  template <typename T> inline static bool addToRelation(qint64 leftId, qint64 rightId)
81  {
82  return Entity::addToRelationImpl(T::tableName(), T::leftColumn(), T::rightColumn(), leftId, rightId);
83  }
84 
90  template <typename T> inline static bool removeFromRelation(qint64 leftId, qint64 rightId)
91  {
92  return Entity::removeFromRelationImpl(T::tableName(), T::leftColumn(), T::rightColumn(), leftId, rightId);
93  }
94 
95  enum RelationSide {
96  Left,
97  Right
98  };
99 
105  template <typename T> inline static bool clearRelation(qint64 id, RelationSide side = Left)
106  {
107  return Entity::clearRelationImpl(T::tableName(), T::leftColumn(), T::rightColumn(), id, side);
108  }
109 
110 protected:
111  Entity();
112  explicit Entity(qint64 id);
113  ~Entity();
114 
115 private:
116  static int countImpl(const QString &tableName, const QString &column, const QVariant &value);
117  static bool removeImpl(const QString &tableName, const QString &column, const QVariant &value);
118  static bool relatesToImpl(const QString &tableName, const QString &leftColumn, const QString &rightColumn, qint64 leftId, qint64 rightId);
119  static bool addToRelationImpl(const QString &tableName, const QString &leftColumn, const QString &rightColumn, qint64 leftId, qint64 rightId);
120  static bool removeFromRelationImpl(const QString &tableName, const QString &leftColumn, const QString &rightColumn, qint64 leftId, qint64 rightId);
121  static bool clearRelationImpl(const QString &tableName, const QString &leftColumn, const QString &rightColumn, qint64 id, RelationSide side);
122 
123 private:
124  static QSqlDatabase database();
125  qint64 m_id;
126 };
127 
128 namespace _detail
129 {
130 
148 template <template <typename U> class Op>
149 struct ById {
150  typedef bool result_type;
151  bool operator()(Entity::Id lhs, Entity::Id rhs) const
152  {
153  return Op<Entity::Id>()(lhs, rhs);
154  }
155  template <typename E>
156  bool operator()(const E &lhs, const E &rhs) const
157  {
158  return this->operator()(lhs.id(), rhs.id());
159  }
160  template <typename E>
161  bool operator()(const E &lhs, Entity::Id rhs) const
162  {
163  return this->operator()(lhs.id(), rhs);
164  }
165  template <typename E>
166  bool operator()(Entity::Id lhs, const E &rhs) const
167  {
168  return this->operator()(lhs, rhs.id());
169  }
170 };
171 }
172 
173 } // namespace Server
174 } // namespace Akonadi
175 
176 #endif
void reserve(int alloc)
Left
QString join(const QString &separator) const const
Definition: item.h:31
Helper integration between Akonadi and Qt.
int count(const T &value) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Jul 12 2020 23:16:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.