File indexing completed on 2025-01-05 04:46:58

0001 /***************************************************************************
0002  *   SPDX-FileCopyrightText: 2006 Andreas Gungl <a.gungl@gmx.de>           *
0003  *                                                                         *
0004  *   SPDX-License-Identifier: LGPL-2.0-or-later                            *
0005  ***************************************************************************/
0006 
0007 #include "entity.h"
0008 #include "countquerybuilder.h"
0009 #include "datastore.h"
0010 
0011 #include <QSqlDatabase>
0012 #include <QVariant>
0013 
0014 using namespace Akonadi::Server;
0015 
0016 Entity::Entity()
0017     : m_id(-1)
0018 {
0019 }
0020 
0021 Entity::Entity(qint64 id)
0022     : m_id(id)
0023 {
0024 }
0025 
0026 Entity::~Entity()
0027 {
0028 }
0029 
0030 DataStore *Entity::dataStore()
0031 {
0032     return DataStore::self();
0033 }
0034 
0035 qint64 Entity::id() const
0036 {
0037     return m_id;
0038 }
0039 
0040 void Entity::setId(qint64 id)
0041 {
0042     m_id = id;
0043 }
0044 
0045 bool Entity::isValid() const
0046 {
0047     return m_id != -1;
0048 }
0049 
0050 int Entity::countImpl(DataStore *store, const QString &tableName, const QString &column, const QVariant &value)
0051 {
0052     QSqlDatabase db = store->database();
0053     if (!db.isOpen()) {
0054         return -1;
0055     }
0056 
0057     CountQueryBuilder builder(store, tableName);
0058     builder.addValueCondition(column, Query::Equals, value);
0059 
0060     if (!builder.exec()) {
0061         qCWarning(AKONADISERVER_LOG) << "Error counting records in table" << tableName;
0062         return -1;
0063     }
0064 
0065     return builder.result();
0066 }
0067 
0068 bool Entity::removeImpl(DataStore *store, const QString &tableName, const QString &column, const QVariant &value)
0069 {
0070     QSqlDatabase db = store->database();
0071     if (!db.isOpen()) {
0072         return false;
0073     }
0074 
0075     QueryBuilder builder(store, tableName, QueryBuilder::Delete);
0076     builder.addValueCondition(column, Query::Equals, value);
0077 
0078     if (!builder.exec()) {
0079         qCWarning(AKONADISERVER_LOG) << "Error during deleting records from table" << tableName;
0080         return false;
0081     }
0082     return true;
0083 }
0084 
0085 bool Entity::relatesToImpl(DataStore *store, const QString &tableName, const QString &leftColumn, const QString &rightColumn, qint64 leftId, qint64 rightId)
0086 {
0087     QSqlDatabase db = store->database();
0088     if (!db.isOpen()) {
0089         return false;
0090     }
0091 
0092     CountQueryBuilder builder(store, tableName);
0093     builder.addValueCondition(leftColumn, Query::Equals, leftId);
0094     builder.addValueCondition(rightColumn, Query::Equals, rightId);
0095 
0096     if (!builder.exec()) {
0097         qCWarning(AKONADISERVER_LOG) << "Error during counting records in table" << tableName;
0098         return false;
0099     }
0100 
0101     return builder.result() > 0;
0102 }
0103 
0104 bool Entity::addToRelationImpl(DataStore *store, const QString &tableName, const QString &leftColumn, const QString &rightColumn, qint64 leftId, qint64 rightId)
0105 {
0106     QSqlDatabase db = store->database();
0107     if (!db.isOpen()) {
0108         return false;
0109     }
0110 
0111     QueryBuilder qb(store, tableName, QueryBuilder::Insert);
0112     qb.setColumnValue(leftColumn, leftId);
0113     qb.setColumnValue(rightColumn, rightId);
0114     qb.setIdentificationColumn(QString());
0115 
0116     if (!qb.exec()) {
0117         qCWarning(AKONADISERVER_LOG) << "Error during adding a record to table" << tableName;
0118         return false;
0119     }
0120 
0121     return true;
0122 }
0123 
0124 bool Entity::removeFromRelationImpl(DataStore *store,
0125                                     const QString &tableName,
0126                                     const QString &leftColumn,
0127                                     const QString &rightColumn,
0128                                     qint64 leftId,
0129                                     qint64 rightId)
0130 {
0131     QSqlDatabase db = store->database();
0132     if (!db.isOpen()) {
0133         return false;
0134     }
0135 
0136     QueryBuilder builder(store, tableName, QueryBuilder::Delete);
0137     builder.addValueCondition(leftColumn, Query::Equals, leftId);
0138     builder.addValueCondition(rightColumn, Query::Equals, rightId);
0139 
0140     if (!builder.exec()) {
0141         qCWarning(AKONADISERVER_LOG) << "Error during removing a record from relation table" << tableName;
0142         return false;
0143     }
0144 
0145     return true;
0146 }
0147 
0148 bool Entity::clearRelationImpl(DataStore *store, const QString &tableName, const QString &leftColumn, const QString &rightColumn, qint64 id, RelationSide side)
0149 {
0150     QSqlDatabase db = store->database();
0151     if (!db.isOpen()) {
0152         return false;
0153     }
0154 
0155     QueryBuilder builder(store, tableName, QueryBuilder::Delete);
0156     switch (side) {
0157     case Left:
0158         builder.addValueCondition(leftColumn, Query::Equals, id);
0159         break;
0160     case Right:
0161         builder.addValueCondition(rightColumn, Query::Equals, id);
0162         break;
0163     default:
0164         qFatal("Invalid enum value");
0165     }
0166     if (!builder.exec()) {
0167         qCWarning(AKONADISERVER_LOG) << "Error during clearing relation table" << tableName << "for ID" << id;
0168         return false;
0169     }
0170 
0171     return true;
0172 }