8#include "countquerybuilder.h"
11#include <QSqlDatabase>
14using namespace Akonadi::Server;
21Entity::Entity(qint64
id)
35qint64 Entity::id()
const
40void Entity::setId(qint64
id)
45bool Entity::isValid()
const
50int Entity::countImpl(
DataStore *store,
const QString &tableName,
const QString &column,
const QVariant &value)
57 CountQueryBuilder builder(store, tableName);
58 builder.addValueCondition(column, Query::Equals, value);
60 if (!builder.exec()) {
61 qCWarning(AKONADISERVER_LOG) <<
"Error counting records in table" << tableName;
65 return builder.result();
68bool Entity::removeImpl(
DataStore *store,
const QString &tableName,
const QString &column,
const QVariant &value)
75 QueryBuilder builder(store, tableName, QueryBuilder::Delete);
76 builder.addValueCondition(column, Query::Equals, value);
78 if (!builder.exec()) {
79 qCWarning(AKONADISERVER_LOG) <<
"Error during deleting records from table" << tableName;
85bool Entity::relatesToImpl(
DataStore *store,
const QString &tableName,
const QString &leftColumn,
const QString &rightColumn, qint64 leftId, qint64 rightId)
92 CountQueryBuilder builder(store, tableName);
93 builder.addValueCondition(leftColumn, Query::Equals, leftId);
94 builder.addValueCondition(rightColumn, Query::Equals, rightId);
96 if (!builder.exec()) {
97 qCWarning(AKONADISERVER_LOG) <<
"Error during counting records in table" << tableName;
101 return builder.result() > 0;
104bool Entity::addToRelationImpl(
DataStore *store,
const QString &tableName,
const QString &leftColumn,
const QString &rightColumn, qint64 leftId, qint64 rightId)
106 QSqlDatabase db = store->
database();
111 QueryBuilder qb(store, tableName, QueryBuilder::Insert);
112 qb.setColumnValue(leftColumn, leftId);
113 qb.setColumnValue(rightColumn, rightId);
114 qb.setIdentificationColumn(QString());
117 qCWarning(AKONADISERVER_LOG) <<
"Error during adding a record to table" << tableName;
124bool Entity::removeFromRelationImpl(
DataStore *store,
125 const QString &tableName,
126 const QString &leftColumn,
127 const QString &rightColumn,
131 QSqlDatabase db = store->
database();
136 QueryBuilder builder(store, tableName, QueryBuilder::Delete);
137 builder.addValueCondition(leftColumn, Query::Equals, leftId);
138 builder.addValueCondition(rightColumn, Query::Equals, rightId);
140 if (!builder.exec()) {
141 qCWarning(AKONADISERVER_LOG) <<
"Error during removing a record from relation table" << tableName;
148bool Entity::clearRelationImpl(
DataStore *store,
const QString &tableName,
const QString &leftColumn,
const QString &rightColumn, qint64
id, RelationSide side)
150 QSqlDatabase db = store->
database();
155 QueryBuilder builder(store, tableName, QueryBuilder::Delete);
158 builder.addValueCondition(leftColumn, Query::Equals,
id);
161 builder.addValueCondition(rightColumn, Query::Equals,
id);
164 qFatal(
"Invalid enum value");
166 if (!builder.exec()) {
167 qCWarning(AKONADISERVER_LOG) <<
"Error during clearing relation table" << tableName <<
"for ID" << id;
This class handles all the database access.
static DataStore * self()
Per thread singleton.
QSqlDatabase database()
Returns the QSqlDatabase object.
bool isOpen() const const