Akonadi

entity.cpp
1/***************************************************************************
2 * SPDX-FileCopyrightText: 2006 Andreas Gungl <a.gungl@gmx.de> *
3 * *
4 * SPDX-License-Identifier: LGPL-2.0-or-later *
5 ***************************************************************************/
6
7#include "entity.h"
8#include "countquerybuilder.h"
9#include "datastore.h"
10
11#include <QSqlDatabase>
12#include <QVariant>
13
14using namespace Akonadi::Server;
15
16Entity::Entity()
17 : m_id(-1)
18{
19}
20
21Entity::Entity(qint64 id)
22 : m_id(id)
23{
24}
25
26Entity::~Entity()
27{
28}
29
30DataStore *Entity::dataStore()
31{
32 return DataStore::self();
33}
34
35qint64 Entity::id() const
36{
37 return m_id;
38}
39
40void Entity::setId(qint64 id)
41{
42 m_id = id;
43}
44
45bool Entity::isValid() const
46{
47 return m_id != -1;
48}
49
50int Entity::countImpl(DataStore *store, const QString &tableName, const QString &column, const QVariant &value)
51{
52 QSqlDatabase db = store->database();
53 if (!db.isOpen()) {
54 return -1;
55 }
56
57 CountQueryBuilder builder(store, tableName);
58 builder.addValueCondition(column, Query::Equals, value);
59
60 if (!builder.exec()) {
61 qCWarning(AKONADISERVER_LOG) << "Error counting records in table" << tableName;
62 return -1;
63 }
64
65 return builder.result();
66}
67
68bool Entity::removeImpl(DataStore *store, const QString &tableName, const QString &column, const QVariant &value)
69{
70 QSqlDatabase db = store->database();
71 if (!db.isOpen()) {
72 return false;
73 }
74
75 QueryBuilder builder(store, tableName, QueryBuilder::Delete);
76 builder.addValueCondition(column, Query::Equals, value);
77
78 if (!builder.exec()) {
79 qCWarning(AKONADISERVER_LOG) << "Error during deleting records from table" << tableName;
80 return false;
81 }
82 return true;
83}
84
85bool Entity::relatesToImpl(DataStore *store, const QString &tableName, const QString &leftColumn, const QString &rightColumn, qint64 leftId, qint64 rightId)
86{
87 QSqlDatabase db = store->database();
88 if (!db.isOpen()) {
89 return false;
90 }
91
92 CountQueryBuilder builder(store, tableName);
93 builder.addValueCondition(leftColumn, Query::Equals, leftId);
94 builder.addValueCondition(rightColumn, Query::Equals, rightId);
95
96 if (!builder.exec()) {
97 qCWarning(AKONADISERVER_LOG) << "Error during counting records in table" << tableName;
98 return false;
99 }
100
101 return builder.result() > 0;
102}
103
104bool Entity::addToRelationImpl(DataStore *store, const QString &tableName, const QString &leftColumn, const QString &rightColumn, qint64 leftId, qint64 rightId)
105{
106 QSqlDatabase db = store->database();
107 if (!db.isOpen()) {
108 return false;
109 }
110
111 QueryBuilder qb(store, tableName, QueryBuilder::Insert);
112 qb.setColumnValue(leftColumn, leftId);
113 qb.setColumnValue(rightColumn, rightId);
114 qb.setIdentificationColumn(QString());
115
116 if (!qb.exec()) {
117 qCWarning(AKONADISERVER_LOG) << "Error during adding a record to table" << tableName;
118 return false;
119 }
120
121 return true;
122}
123
124bool Entity::removeFromRelationImpl(DataStore *store,
125 const QString &tableName,
126 const QString &leftColumn,
127 const QString &rightColumn,
128 qint64 leftId,
129 qint64 rightId)
130{
131 QSqlDatabase db = store->database();
132 if (!db.isOpen()) {
133 return false;
134 }
135
136 QueryBuilder builder(store, tableName, QueryBuilder::Delete);
137 builder.addValueCondition(leftColumn, Query::Equals, leftId);
138 builder.addValueCondition(rightColumn, Query::Equals, rightId);
139
140 if (!builder.exec()) {
141 qCWarning(AKONADISERVER_LOG) << "Error during removing a record from relation table" << tableName;
142 return false;
143 }
144
145 return true;
146}
147
148bool Entity::clearRelationImpl(DataStore *store, const QString &tableName, const QString &leftColumn, const QString &rightColumn, qint64 id, RelationSide side)
149{
150 QSqlDatabase db = store->database();
151 if (!db.isOpen()) {
152 return false;
153 }
154
155 QueryBuilder builder(store, tableName, QueryBuilder::Delete);
156 switch (side) {
157 case Left:
158 builder.addValueCondition(leftColumn, Query::Equals, id);
159 break;
160 case Right:
161 builder.addValueCondition(rightColumn, Query::Equals, id);
162 break;
163 default:
164 qFatal("Invalid enum value");
165 }
166 if (!builder.exec()) {
167 qCWarning(AKONADISERVER_LOG) << "Error during clearing relation table" << tableName << "for ID" << id;
168 return false;
169 }
170
171 return true;
172}
Helper class for creating queries to count elements in a database.
This class handles all the database access.
Definition datastore.h:95
static DataStore * self()
Per thread singleton.
QSqlDatabase database()
Returns the QSqlDatabase object.
Helper class to construct arbitrary SQL queries.
bool isOpen() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.