Herunterladen als
root/src/entitymanager.cpp @ 7f1090e5
81c23b56 | Christian Ehringfeld | /*
|
|
6899f814 | Christian Ehringfeld | * Copyright (C) 2015 Christian Ehringfeld <c.ehringfeld@t-online.de>
|
|
*
|
|||
* This program is free software; you can redistribute it and/or modify it
|
|||
* under the terms of the GNU Lesser General Public License as published by
|
|||
* the Free Software Foundation.
|
|||
*
|
|||
* This program is distributed in the hope that it will be useful, but
|
|||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|||
* for more details.
|
|||
*
|
|||
* You should have received a copy of the GNU Lesser General Public License
|
|||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|||
*/
|
|||
81c23b56 | Christian Ehringfeld | ||
f9cef58f | Christian Ehringfeld | #include <QVariantList>
|
|
3938a37e | Christian Ehringfeld | #include <QHash>
|
|
#include <QDir>
|
|||
81c23b56 | Christian Ehringfeld | #include "entitymanager.h"
|
|
7e233492 | Christian Ehringfeld | #include "enums/databasetype.h"
|
|
1e213c09 | Christian Ehringfeld | #include "databasemigration.h"
|
|
19082efe | Christian Ehringfeld | #include "attributeresolver.h"
|
|
fc14f551 | Christian Ehringfeld | #include "validators/validatorfactory.h"
|
|
#include "validators/validator.h"
|
|||
#include "validators/validatorrule.h"
|
|||
e332a521 | Christian Ehringfeld | #include "entityinspector.h"
|
|
ae497991 | Christian Ehringfeld | ||
4d58ef6a | Christian Ehringfeld | using namespace CuteEntityManager;
|
|
81c23b56 | Christian Ehringfeld | ||
QStringList EntityManager::connectionNames = QStringList();
|
|||
28d2f01a | Christian Ehringfeld | QHash<QString, EntityManager *> EntityManager::instances =
|
|
1ac14ffb | Christian Ehringfeld | QHash<QString, EntityManager *>();
|
|
28d2f01a | Christian Ehringfeld | ||
afde9013 | Christian Ehringfeld | QStringList EntityManager::getConnectionNames() {
|
|
return EntityManager::connectionNames;
|
|||
}
|
|||
3938a37e | Christian Ehringfeld | EntityManager::EntityManager(QSqlDatabase database, bool logQueries,
|
|
const bool inspectEntities,
|
|||
MsgType logActions) : QObject() {
|
|||
57d6da31 | Christian Ehringfeld | this->db = QSharedPointer<Database>(new Database(database, true, logQueries, logActions));
|
|
3938a37e | Christian Ehringfeld | this->init(inspectEntities, logActions);
|
|
}
|
|||
void EntityManager::init(bool inspect, const MsgType msgType) {
|
|||
1701766b | Christian Ehringfeld | EntityInstanceFactory::registerClass<DatabaseMigration>();
|
|
57d6da31 | Christian Ehringfeld | this->schema = QSharedPointer<Schema>(Database::getSchema(Database::getDatabaseType(
|
|
1ac14ffb | Christian Ehringfeld | this->db->getDatabase().driverName()), this->db));
|
|
3938a37e | Christian Ehringfeld | this->schema->setTables(this->schema->getTableSchemas());
|
|
dbd41a3a | Christian Ehringfeld | this->ar = QSharedPointer<AttributeResolver>(new AttributeResolver(
|
|
1ac14ffb | Christian Ehringfeld | this->schema->getQueryBuilder()));
|
|
dbd41a3a | Christian Ehringfeld | this->queryInterpreter = QSharedPointer<QueryInterpreter>(new QueryInterpreter(
|
|
1ac14ffb | Christian Ehringfeld | this->ar));
|
|
3938a37e | Christian Ehringfeld | this->appendToInstanceList();
|
|
if (inspect) {
|
|||
EntityInspector inspector = EntityInspector(msgType);
|
|||
inspector.checkRegisteredEntities();
|
|||
}
|
|||
2f40d160 | Christian Ehringfeld | QString loggerFile = this->objectName() + ".log";
|
|
3938a37e | Christian Ehringfeld | loggerFile.replace("[", "");
|
|
loggerFile.replace("]", "");
|
|||
this->logger = QSharedPointer<Logger>(new Logger(QDir::currentPath() + "/" +
|
|||
1ac14ffb | Christian Ehringfeld | loggerFile, msgType));
|
|
3938a37e | Christian Ehringfeld | }
|
|
EntityManager::EntityManager(const QString &databaseType, QString databasename,
|
|||
QString hostname, QString username, QString password, QString port,
|
|||
bool logQueries, QString databaseOptions, const bool inspectEntities,
|
|||
CuteEntityManager::MsgType logActions) : QObject() {
|
|||
6be60ddf | Christian Ehringfeld | this->db = QSharedPointer<Database>(new Database(databaseType, this->createConnection(),
|
|
1ac14ffb | Christian Ehringfeld | hostname,
|
|
databasename, username,
|
|||
password,
|
|||
port.toInt(), true, logQueries, databaseOptions, logActions));
|
|||
3938a37e | Christian Ehringfeld | this->init(inspectEntities, logActions);
|
|
}
|
|||
EntityManager::~EntityManager() {
|
|||
EntityManager::removeConnectionName(this->db->getConnectionName());
|
|||
EntityManager::instances.remove(this->objectName());
|
|||
19082efe | Christian Ehringfeld | this->ar = QSharedPointer<AttributeResolver>(nullptr);
|
|
3938a37e | Christian Ehringfeld | }
|
|
1b167b6c | Christian Ehringfeld | QSharedPointer<QueryBuilder> EntityManager::getQueryBuilder() const {
|
|
return this->schema->getQueryBuilder();
|
|||
}
|
|||
a873a3ba | Christian Ehringfeld | void EntityManager::clearCache() {
|
|
this->cache.clear();
|
|||
}
|
|||
0d155b40 | Christian Ehringfeld | bool EntityManager::saveObject(QSharedPointer<Entity> &entity,
|
|
QList<Entity *> &mergedObjects, const bool persistRelations,
|
|||
d2bebf1b | Christian Ehringfeld | const bool ignoreHasChanged, const bool validate,
|
|
const bool relationsIgnoreHasChanged) {
|
|||
0d155b40 | Christian Ehringfeld | bool merged = mergedObjects.contains(entity.data());
|
|
23337ecc | Christian Ehringfeld | if (entity && !merged && (ignoreHasChanged || this->hasChanged(entity))) {
|
|
0d155b40 | Christian Ehringfeld | if (entity->getProperty(entity->getPrimaryKey()).toLongLong() > -1) {
|
|
d2bebf1b | Christian Ehringfeld | return this->mergeObject(entity, mergedObjects, persistRelations, validate,
|
|
relationsIgnoreHasChanged);
|
|||
0d155b40 | Christian Ehringfeld | } else {
|
|
return this->createObject(entity, mergedObjects, persistRelations, false,
|
|||
d2bebf1b | Christian Ehringfeld | validate, relationsIgnoreHasChanged);
|
|
0d155b40 | Christian Ehringfeld | }
|
|
}
|
|||
return merged ? true : false;
|
|||
}
|
|||
bool EntityManager::mergeObject(QSharedPointer<Entity> &entity,
|
|||
d2bebf1b | Christian Ehringfeld | QList<Entity *> &mergedObjects, bool withRelations, const bool validate,
|
|
const bool relationsIgnoreHasChanged) {
|
|||
0d155b40 | Christian Ehringfeld | bool ok = true;
|
|
23337ecc | Christian Ehringfeld | if (entity && !mergedObjects.contains(entity.data())) {
|
|
0d155b40 | Christian Ehringfeld | mergedObjects.append(entity.data());
|
|
ok = false;
|
|||
d2bebf1b | Christian Ehringfeld | if (entity->getId() > -1 && (!validate || this->validate(entity))) {
|
|
0d155b40 | Christian Ehringfeld | if (withRelations) {
|
|
d2bebf1b | Christian Ehringfeld | this->savePrePersistedRelations(entity, mergedObjects,
|
|
relationsIgnoreHasChanged);
|
|||
0d155b40 | Christian Ehringfeld | }
|
|
this->db->startTransaction();
|
|||
QList<QSqlQuery> q = this->schema->getQueryBuilder()->merge(
|
|||
1ac14ffb | Christian Ehringfeld | entity);
|
|
0d155b40 | Christian Ehringfeld | ok = this->db->exec(q);
|
|
if (!ok || !this->db->commitTransaction()) {
|
|||
this->db->rollbackTransaction();
|
|||
ok = false;
|
|||
} else if (ok && withRelations) {
|
|||
d2bebf1b | Christian Ehringfeld | this->savePostPersistedRelations(entity, mergedObjects,
|
|
relationsIgnoreHasChanged);
|
|||
0d155b40 | Christian Ehringfeld | }
|
|
}
|
|||
}
|
|||
return ok;
|
|||
}
|
|||
bool EntityManager::createObject(QSharedPointer<Entity> &entity,
|
|||
QList<Entity *> &mergedObjects, const bool persistRelations,
|
|||
d2bebf1b | Christian Ehringfeld | const bool checkDuplicate, const bool validate,
|
|
const bool relationsIgnoreHasChanged) {
|
|||
0d155b40 | Christian Ehringfeld | bool rc = true;
|
|
23337ecc | Christian Ehringfeld | if (entity && !mergedObjects.contains(entity.data())) {
|
|
0d155b40 | Christian Ehringfeld | mergedObjects.append(entity.data());
|
|
rc = false;
|
|||
d2bebf1b | Christian Ehringfeld | if (this->checkTable(entity) && (!validate || this->validate(entity))
|
|
e5a0d313 | Christian Ehringfeld | && (!checkDuplicate || this->count(entity) <= 0)) {
|
|
0d155b40 | Christian Ehringfeld | if (persistRelations) {
|
|
d2bebf1b | Christian Ehringfeld | this->savePrePersistedRelations(entity, mergedObjects,
|
|
relationsIgnoreHasChanged);
|
|||
0d155b40 | Christian Ehringfeld | }
|
|
this->db->startTransaction();
|
|||
QList<QSqlQuery> q = this->schema->getQueryBuilder()->create(
|
|||
1ac14ffb | Christian Ehringfeld | entity);
|
|
0d155b40 | Christian Ehringfeld | bool first = true;
|
|
QVariant id = -1;
|
|||
for (int var = 0; var < q.size(); ++var) {
|
|||
auto query = q.at(var);
|
|||
if (!first) {
|
|||
this->schema->getQueryBuilder()->bindValue(entity->getPrimaryKey(), id, query);
|
|||
}
|
|||
rc = this->db->exec(query);
|
|||
if (!rc) {
|
|||
3938a37e | Christian Ehringfeld | this->logger->logMsg("class is erroneous:" + EntityHelper::getClassName(
|
|
entity.data()), MsgType::WARNING);
|
|||
0d155b40 | Christian Ehringfeld | break;
|
|
}
|
|||
if (first) {
|
|||
id = query.lastInsertId();
|
|||
entity->setProperty(entity->getPrimaryKey(), id);
|
|||
first = false;
|
|||
}
|
|||
}
|
|||
if (!rc || !this->db->commitTransaction()) {
|
|||
this->db->rollbackTransaction();
|
|||
entity->setId(-1);
|
|||
rc = false;
|
|||
} else {
|
|||
this->cache.insert(entity);
|
|||
if (persistRelations) {
|
|||
d2bebf1b | Christian Ehringfeld | this->savePostPersistedRelations(entity, mergedObjects,
|
|
87739ae0 | Christian Ehringfeld | relationsIgnoreHasChanged, true);
|
|
0d155b40 | Christian Ehringfeld | }
|
|
rc = true;
|
|||
}
|
|||
}
|
|||
entity->idChanged();
|
|||
}
|
|||
return rc;
|
|||
}
|
|||
6be60ddf | Christian Ehringfeld | bool EntityManager::create(QList<QSharedPointer<Entity>> &entities,
|
|
e5a0d313 | Christian Ehringfeld | const bool persistRelations, const bool checkDuplicate, const bool validate,
|
|
const bool relationsIgnoreHasChanged) {
|
|||
87739ae0 | Christian Ehringfeld | bool ok = true;
|
|
auto merged = QList<Entity *>();
|
|||
foreach (QSharedPointer<Entity> ent, entities) {
|
|||
e5a0d313 | Christian Ehringfeld | this->createObject(ent, merged, persistRelations,
|
|
checkDuplicate, validate, relationsIgnoreHasChanged);
|
|||
87739ae0 | Christian Ehringfeld | }
|
|
return ok;
|
|||
}
|
|||
6be60ddf | Christian Ehringfeld | bool EntityManager::save(QList<QSharedPointer<Entity>> &entities,
|
|
87739ae0 | Christian Ehringfeld | const bool persistRelations, const bool ignoreHasChanged, const bool validate,
|
|
const bool relationsIgnoreHasChanged) {
|
|||
bool ok = true;
|
|||
139a9b5e | Christian Ehringfeld | auto merged = QList<Entity *>();
|
|
87739ae0 | Christian Ehringfeld | foreach (QSharedPointer<Entity> entity, entities) {
|
|
ok = this->saveObject(entity, merged, persistRelations,
|
|||
ignoreHasChanged, validate, relationsIgnoreHasChanged);
|
|||
if (!ok) {
|
|||
break;
|
|||
6be60ddf | Christian Ehringfeld | /**
|
|
* @todo error handling
|
|||
*/
|
|||
87739ae0 | Christian Ehringfeld | }
|
|
}
|
|||
return ok;
|
|||
}
|
|||
9971e7d2 | Christian Ehringfeld | bool EntityManager::startup(QString version, QStringList toInitialize,
|
|
bool createIndices) {
|
|||
66704054 | Christian Ehringfeld | QSharedPointer<Entity> dbm = QSharedPointer<DatabaseMigration>
|
|
1ac14ffb | Christian Ehringfeld | (new DatabaseMigration());
|
|
1e213c09 | Christian Ehringfeld | QHash<QString, QVariant> map = QHash<QString, QVariant>();
|
|
d84c91e7 | Christian Ehringfeld | bool ok = true;
|
|
1e213c09 | Christian Ehringfeld | map.insert("version", version);
|
|
abb9e8c5 | Christian Ehringfeld | if (!this->schema->getTableNames().contains(dbm->getTablename())) {
|
|
42d32a68 | Christian Ehringfeld | this->createTable(dbm, true);
|
|
e86c23a2 | Christian Ehringfeld | }
|
|
d568923d | Christian Ehringfeld | if (this->findAllByAttributes(map, dbm->getTablename()).isEmpty()) {
|
|
9971e7d2 | Christian Ehringfeld | QList<QSharedPointer<Entity>> entities = QList<QSharedPointer<Entity>>();
|
|
d568923d | Christian Ehringfeld | for (int var = 0; var < toInitialize.size(); ++var) {
|
|
d84c91e7 | Christian Ehringfeld | if (ok) {
|
|
QString c = toInitialize.at(var);
|
|||
9971e7d2 | Christian Ehringfeld | auto entity = QSharedPointer<Entity>
|
|
1ac14ffb | Christian Ehringfeld | (EntityInstanceFactory::createInstance(c));
|
|
2fedfe76 | Christian Ehringfeld | ok = this->createTable(entity, false);
|
|
9971e7d2 | Christian Ehringfeld | entities.append(entity);
|
|
d84c91e7 | Christian Ehringfeld | } else {
|
|
3938a37e | Christian Ehringfeld | this->logger->logMsg("startup of version " + version + " failed",
|
|
MsgType::CRITICAL);
|
|||
this->logger->logMsg( "erroneous entity:" + (var == 0 ?
|
|||
1ac14ffb | Christian Ehringfeld | "null, this should not happen!" : toInitialize.at(var - 1)), MsgType::CRITICAL);
|
|
d84c91e7 | Christian Ehringfeld | break;
|
|
}
|
|||
}
|
|||
2fedfe76 | Christian Ehringfeld | if (ok) {
|
|
for (int i = 0; i < entities.size(); ++i) {
|
|||
ok = this->schema->getQueryBuilder()->createRelationTables(entities.at(i));
|
|||
if (!ok) {
|
|||
break;
|
|||
}
|
|||
}
|
|||
}
|
|||
if (ok && createIndices) {
|
|||
9971e7d2 | Christian Ehringfeld | for (int i = 0; i < entities.size(); ++i) {
|
|
ok = this->schema->getQueryBuilder()->createIndices(entities.at(i));
|
|||
2fedfe76 | Christian Ehringfeld | if (!ok) {
|
|
break;
|
|||
}
|
|||
9971e7d2 | Christian Ehringfeld | }
|
|
}
|
|||
d84c91e7 | Christian Ehringfeld | if (ok) {
|
|
42d32a68 | Christian Ehringfeld | auto dbmPtr = dbm.objectCast<DatabaseMigration>();
|
|
dbmPtr->setVersion(version);
|
|||
dbmPtr->setApplyTime(QDateTime::currentDateTime());
|
|||
this->create(dbm);
|
|||
d568923d | Christian Ehringfeld | }
|
|
abb9e8c5 | Christian Ehringfeld | this->schema->setTables(this->schema->getTableSchemas());
|
|
d568923d | Christian Ehringfeld | }
|
|
d84c91e7 | Christian Ehringfeld | return ok;
|
|
1e213c09 | Christian Ehringfeld | }
|
|
e0e1ead8 | Christian Ehringfeld | bool EntityManager::executeQuery(const QString &query) {
|
|
94bf67c7 | Christian Ehringfeld | return this->db->exec(query);
|
|
e0e1ead8 | Christian Ehringfeld | }
|
|
b0bf458e | Christian Ehringfeld | bool EntityManager::checkTable(const QSharedPointer<Entity> &entity) {
|
|
bool rc = true;
|
|||
df1e56bd | Christian Ehringfeld | if (!this->schema->containsTable(entity->getTablename())
|
|
&& this->schema->getQueryBuilder()->createTable(entity)) {
|
|||
this->schema->getTableSchema(entity->getTablename(), true);
|
|||
rc = this->schema->getTables().contains(entity->getTablename());
|
|||
b0bf458e | Christian Ehringfeld | }
|
|
return rc;
|
|||
}
|
|||
9d05e414 | Christian Ehringfeld | ||
7e233492 | Christian Ehringfeld | QSharedPointer<Database> EntityManager::getDb() const {
|
|
return db;
|
|||
}
|
|||
void EntityManager::setDb(const QSharedPointer<Database> &value) {
|
|||
db = value;
|
|||
}
|
|||
QSharedPointer<Schema> EntityManager::getSchema() const {
|
|||
return schema;
|
|||
}
|
|||
6be60ddf | Christian Ehringfeld | QList<QHash<QString, QVariant>> EntityManager::selectByQuery(Query &query) {
|
|
506067a2 | Christian Ehringfeld | QSqlQuery q = this->queryInterpreter->build(query);
|
|
return this->convertQueryResult(q);
|
|||
}
|
|||
6be60ddf | Christian Ehringfeld | QList<QHash<QString, QVariant>> EntityManager::selectBySql(
|
|
1ac14ffb | Christian Ehringfeld | const QString &sql) {
|
|
506067a2 | Christian Ehringfeld | QSqlQuery q = this->db->select(sql);
|
|
return this->convertQueryResult(q);
|
|||
}
|
|||
fc14f551 | Christian Ehringfeld | bool EntityManager::validate(QSharedPointer<Entity> &entity) {
|
|
QList<ValidationRule> rules = entity->validationRules();
|
|||
QList<ErrorMsg> list = QList<ErrorMsg>();
|
|||
for (int i = 0; i < rules.size(); ++i) {
|
|||
ValidationRule rule = rules.at(i);
|
|||
QSharedPointer<Validator> validator = ValidatorFactory::getValidatorObject(
|
|||
1ac14ffb | Christian Ehringfeld | rule.getValidatorName());
|
|
fc14f551 | Christian Ehringfeld | if (validator) {
|
|
for (int var = 0; var < rule.getAttributes().size(); ++var) {
|
|||
QString attr = rule.getAttributes().at(var);
|
|||
e80feccc | Christian Ehringfeld | QList<ErrorMsg> msgs = validator->validate(entity->getProperty(attr),
|
|
1ac14ffb | Christian Ehringfeld | rule.getParams());
|
|
e80feccc | Christian Ehringfeld | for (int i = 0; i < msgs.size(); ++i) {
|
|
QString emsg = msgs.at(i).getErrorMsg().replace("<property>", attr);
|
|||
ErrorMsg m = msgs.at(i);
|
|||
m.setErrorMsg(emsg);
|
|||
list.append(m);
|
|||
}
|
|||
fc14f551 | Christian Ehringfeld | }
|
|
}
|
|||
}
|
|||
entity->setErrors(list);
|
|||
return !entity->hasErrors();
|
|||
}
|
|||
554f7bc0 | Christian Ehringfeld | bool EntityManager::hasChanged(QSharedPointer<Entity> &entity) {
|
|
bool changed = true;
|
|||
8588ecff | Christian Ehringfeld | if (entity && entity->getId() > -1) {
|
|
554f7bc0 | Christian Ehringfeld | changed = false;
|
|
auto listmap = this->findByPk(entity->getId(), entity);
|
|||
auto relations = entity->getRelations();
|
|||
QStringList rels = QStringList();
|
|||
for (auto i = relations.constBegin(); i != relations.constEnd(); ++i) {
|
|||
if (i.value().getType() == RelationType::MANY_TO_ONE
|
|||
|| (i.value().getType() == RelationType::ONE_TO_ONE
|
|||
&& i.value().getMappedBy().isEmpty())) {
|
|||
rels.append(this->schema->getQueryBuilder()->generateColumnNameID(i.key()));
|
|||
}
|
|||
}
|
|||
for (auto i = listmap.constBegin(); i != listmap.constEnd(); ++i) {
|
|||
if (rels.contains(i.key())) {
|
|||
QString appendix =
|
|||
1ac14ffb | Christian Ehringfeld | this->schema->getQueryBuilder()->columnNameIDAppendix();
|
|
554f7bc0 | Christian Ehringfeld | QString relKey = i.key();
|
|
QVariant v = entity->getProperty(relKey.remove(relKey.size() - appendix.size(),
|
|||
1ac14ffb | Christian Ehringfeld | appendix.size()));
|
|
6c14baaf | Christian Ehringfeld | if (!v.isNull() && v.data()) {
|
|
8588ecff | Christian Ehringfeld | auto e = EntityInstanceFactory::castQVariant(v);
|
|
if (e && e->getProperty(e->getPrimaryKey()) != i.value()) {
|
|||
554f7bc0 | Christian Ehringfeld | changed = true;
|
|
break;
|
|||
}
|
|||
}
|
|||
} else {
|
|||
QVariant property = entity->getProperty(i.key());
|
|||
6c14baaf | Christian Ehringfeld | changed = i.value() != property;
|
|
554f7bc0 | Christian Ehringfeld | if (changed) {
|
|
break;
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|
|||
return changed;
|
|||
}
|
|||
7e233492 | Christian Ehringfeld | QString EntityManager::createConnection() {
|
|
QStringList l = EntityManager::getConnectionNames();
|
|||
QString conName = "";
|
|||
bool ok = false;
|
|||
qint16 i = 0;
|
|||
while (!ok) {
|
|||
if (l.contains("con" + QString::number(i))) {
|
|||
++i;
|
|||
} else {
|
|||
l.append("con" + QString::number(i));
|
|||
ok = true;
|
|||
conName = "con" + QString::number(i);
|
|||
EntityManager::setConnectionNames(l);
|
|||
}
|
|||
}
|
|||
return conName;
|
|||
}
|
|||
void EntityManager::removeConnectionName(const QString &name) {
|
|||
EntityManager::connectionNames.removeOne(name);
|
|||
}
|
|||
f5087482 | Christian Ehringfeld | QSharedPointer<Entity> EntityManager::findById(const qint64 &id,
|
|
1ac14ffb | Christian Ehringfeld | QSharedPointer<Entity> &e,
|
|
const bool refresh) {
|
|||
2d9fab10 | Christian Ehringfeld | QSharedPointer<Entity> r;
|
|
3214951e | Christian Ehringfeld | if (e && (refresh || !(r = this->cache.get(id, EntityHelper::getClassname(e.data()))))) {
|
|
df1e56bd | Christian Ehringfeld | auto map = this->findByPk(id, e);
|
|
70e3d0c6 | Christian Ehringfeld | r = this->convert(map, EntityHelper::getClassname(e.data()), refresh);
|
|
2d9fab10 | Christian Ehringfeld | }
|
|
return r;
|
|||
}
|
|||
e0e1ead8 | Christian Ehringfeld | QSharedPointer<Entity> EntityManager::findById(const qint64 &id,
|
|
1ac14ffb | Christian Ehringfeld | const QString &classname, const bool refresh) {
|
|
f5087482 | Christian Ehringfeld | QSharedPointer<Entity> e = QSharedPointer<Entity>
|
|
1ac14ffb | Christian Ehringfeld | (EntityInstanceFactory::createInstance(classname));
|
|
373a84e2 | Christian Ehringfeld | return this->findById(id, e, refresh);
|
|
2d9fab10 | Christian Ehringfeld | }
|
|
e0e1ead8 | Christian Ehringfeld | void EntityManager::manyToOne(const QSharedPointer<Entity> &entity,
|
|
const QVariant &id,
|
|||
fd67a7a7 | Christian Ehringfeld | Attribute *&attr) {
|
|
a06633f7 | Christian Ehringfeld | qint64 convertedId = -1;
|
|
bool ok = false;
|
|||
df1e56bd | Christian Ehringfeld | if ((convertedId = id.toLongLong(&ok)) && ok && convertedId > -1) {
|
|
be2e9500 | Christian Ehringfeld | QString className = attr->getRelatedClass()->className();
|
|
706de2e8 | Christian Ehringfeld | QSharedPointer<Entity> ptr = QSharedPointer<Entity>();
|
|
fd67a7a7 | Christian Ehringfeld | if (!(this->cache.contains(convertedId, className)
|
|
1ac14ffb | Christian Ehringfeld | && (ptr = this->cache.get(convertedId, className)))) {
|
|
706de2e8 | Christian Ehringfeld | ptr = this->findById(convertedId, className);
|
|
}
|
|||
51f88a34 | Christian Ehringfeld | EntityHelper::setProperty(entity,ptr,attr->getMetaProperty());
|
|
a06633f7 | Christian Ehringfeld | }
|
|
}
|
|||
e0e1ead8 | Christian Ehringfeld | void EntityManager::oneToMany(const QSharedPointer<Entity> &entity,
|
|
fd67a7a7 | Christian Ehringfeld | Attribute *&attr) {
|
|
abb9e8c5 | Christian Ehringfeld | if (entity.data() && entity->getId() > -1) {
|
|
be2e9500 | Christian Ehringfeld | auto e = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(attr));
|
|
df1e56bd | Christian Ehringfeld | if (e) {
|
|
373a84e2 | Christian Ehringfeld | Query query = this->schema->getQueryBuilder()->oneToMany(
|
|
1ac14ffb | Christian Ehringfeld | attr->getRelatedTable(),
|
|
attr->getRelatedColumnName(), entity->getId());
|
|||
373a84e2 | Christian Ehringfeld | QSqlQuery q = this->queryInterpreter->build(query);
|
|
df1e56bd | Christian Ehringfeld | auto listMap = this->convertQueryResult(q);
|
|
51f88a34 | Christian Ehringfeld | auto relationalClass = EntityHelper::getClassName(e.data());
|
|
auto entities = this->convert(listMap, relationalClass.toLatin1());
|
|||
EntityHelper::setListProperty(entity,entities,attr->getMetaProperty());
|
|||
df1e56bd | Christian Ehringfeld | }
|
|
706de2e8 | Christian Ehringfeld | }
|
|
}
|
|||
a06633f7 | Christian Ehringfeld | ||
c599658a | Christian Ehringfeld | ||
e0e1ead8 | Christian Ehringfeld | void EntityManager::oneToOne(const QSharedPointer<Entity> &entity,
|
|
fd67a7a7 | Christian Ehringfeld | Attribute *&attr, const QVariant &id) {
|
|
be2e9500 | Christian Ehringfeld | if (attr->getRelation().getMappedBy().isEmpty()) {
|
|
this->manyToOne(entity, id, attr);
|
|||
c599658a | Christian Ehringfeld | } else {
|
|
be2e9500 | Christian Ehringfeld | auto e = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(attr));
|
|
df1e56bd | Christian Ehringfeld | if (e) {
|
|
373a84e2 | Christian Ehringfeld | Query query = this->schema->getQueryBuilder()->oneToMany(
|
|
1ac14ffb | Christian Ehringfeld | e->getTablename(),
|
|
this->schema->getQueryBuilder()->generateColumnNameID(
|
|||
attr->getRelation().getMappedBy()),
|
|||
entity->getProperty(entity->getPrimaryKey()).toLongLong(), 1);
|
|||
373a84e2 | Christian Ehringfeld | QSqlQuery q = this->queryInterpreter->build(query);
|
|
df1e56bd | Christian Ehringfeld | auto listMap = this->convertQueryResult(q);
|
|
fd67a7a7 | Christian Ehringfeld | auto entities = this->convert(listMap, EntityHelper::getClassname(e.data()));
|
|
df1e56bd | Christian Ehringfeld | if (!entities.isEmpty()) {
|
|
QSharedPointer<Entity> ptr = entities.at(0);
|
|||
be2e9500 | Christian Ehringfeld | EntityHelper::setProperty(entity, ptr, attr->getMetaProperty());
|
|
df1e56bd | Christian Ehringfeld | }
|
|
c599658a | Christian Ehringfeld | }
|
|
}
|
|||
}
|
|||
df1e56bd | Christian Ehringfeld | void EntityManager::savePrePersistedRelations(const QSharedPointer<Entity>
|
|
1ac14ffb | Christian Ehringfeld | &entity, QList<Entity *> &mergedObjects, bool ignoreHasChanged) {
|
|
e8d1537c | Christian Ehringfeld | auto relations = EntityHelper::getRelationProperties(entity.data());
|
|
4f3b13f3 | Christian Ehringfeld | auto iterator = relations.constBegin();
|
|
while (iterator != relations.constEnd()) {
|
|||
const Relation r = iterator.key();
|
|||
auto var = iterator.value().read(entity.data());
|
|||
5757762e | Christian Ehringfeld | if(!var.isNull() && var.data()) {
|
|
if (r.getType() == RelationType::MANY_TO_ONE) {
|
|||
auto e = EntityInstanceFactory::castQVariant(var);
|
|||
if (e && this->shouldBeSaved(e, r)) {
|
|||
this->saveObject(e, mergedObjects, true, ignoreHasChanged);
|
|||
auto fkProp = EntityHelper::mappedProperty(r, e);
|
|||
if (fkProp.isValid()) {
|
|||
EntityHelper::addEntityToListProperty(e, entity, fkProp);
|
|||
}
|
|||
}
|
|||
} else if (r.getType() == RelationType::ONE_TO_ONE
|
|||
&& r.getMappedBy().isEmpty()) {
|
|||
auto e = EntityInstanceFactory::castQVariant(var);
|
|||
if(e) {
|
|||
this->saveObject(e, mergedObjects, true, ignoreHasChanged);
|
|||
auto prop = EntityHelper::mappedProperty(r, e);
|
|||
EntityHelper::setProperty(e, entity, prop);
|
|||
23337ecc | Christian Ehringfeld | }
|
|
e24791d7 | Christian Ehringfeld | }
|
|
5757762e | Christian Ehringfeld | ++iterator;
|
|
df1e56bd | Christian Ehringfeld | }
|
|
}
|
|||
}
|
|||
void EntityManager::savePostPersistedRelations(const QSharedPointer<Entity>
|
|||
1ac14ffb | Christian Ehringfeld | &entity, QList<Entity *> &mergedObjects, bool ignoreHasChanged, bool newItem) {
|
|
e8d1537c | Christian Ehringfeld | auto relations = EntityHelper::getRelationProperties(entity.data());
|
|
f27ac316 | Christian Ehringfeld | for (auto i = relations.constBegin(); i != relations.constEnd(); ++i) {
|
|
const Relation r = i.key();
|
|||
auto var = i.value().read(entity.data());
|
|||
23337ecc | Christian Ehringfeld | if (r.getType() == RelationType::MANY_TO_MANY) {
|
|
this->persistManyToMany(entity, r, var, mergedObjects, ignoreHasChanged,
|
|||
newItem);
|
|||
} else if (r.getType() == RelationType::ONE_TO_MANY) {
|
|||
QList<QSharedPointer<Entity>> list = EntityInstanceFactory::castQVariantList(
|
|||
1ac14ffb | Christian Ehringfeld | var);
|
|
23337ecc | Christian Ehringfeld | if (!list.isEmpty()) {
|
|
auto fkProp = EntityHelper::mappedProperty(r, list.at(0));
|
|||
for (int var = 0; var < list.size(); ++var) {
|
|||
auto e = list.at(var);
|
|||
5757762e | Christian Ehringfeld | if (e && this->shouldBeSaved(e, r)) {
|
|
23337ecc | Christian Ehringfeld | EntityHelper::setProperty(e, entity, fkProp);
|
|
this->saveObject(e, mergedObjects, true, ignoreHasChanged);
|
|||
df1e56bd | Christian Ehringfeld | }
|
|
e8d1537c | Christian Ehringfeld | }
|
|
23337ecc | Christian Ehringfeld | }
|
|
} else if (r.getType() == RelationType::ONE_TO_ONE
|
|||
&& !r.getMappedBy().isEmpty()) {
|
|||
auto e = EntityInstanceFactory::castQVariant(var);
|
|||
if(e) {
|
|||
auto fkProp = EntityHelper::mappedProperty(r, e);
|
|||
EntityHelper::setProperty(e, entity, fkProp);
|
|||
this->saveObject(e, mergedObjects, true, ignoreHasChanged);
|
|||
4f3b13f3 | Christian Ehringfeld | }
|
|
}
|
|||
}
|
|||
91ed1164 | Christian Ehringfeld | }
|
|
6be60ddf | Christian Ehringfeld | void EntityManager::persistMappedByRelation(const QList<QSharedPointer<Entity>>
|
|
1ac14ffb | Christian Ehringfeld | &list, QSqlQuery &q, const QSharedPointer<Entity> &entity,
|
|
const QSharedPointer<Entity> &ptr, const Relation &r,
|
|||
const QString &tblName, QList<Entity *> &mergedObjects) {
|
|||
91ed1164 | Christian Ehringfeld | q.clear();
|
|
66704054 | Christian Ehringfeld | QList<QSharedPointer<Entity>> saved =
|
|
1ac14ffb | Christian Ehringfeld | r.getCascadeType().contains(CascadeType::ALL) ||
|
|
r.getCascadeType().contains(CascadeType::MERGE) ||
|
|||
r.getCascadeType().contains(CascadeType::PERSIST) ?
|
|||
this->saveRelationEntities(list, r, mergedObjects) : list;
|
|||
719a0ba1 | Christian Ehringfeld | this->db->startTransaction();
|
|
abb9e8c5 | Christian Ehringfeld | auto builder = this->schema->getQueryBuilder();
|
|
q = builder->manyToManyInsert(tblName,
|
|||
d27d606d | Christian Ehringfeld | builder->generateManyToManyColumnName(entity, r.getPropertyName()),
|
|
builder->generateManyToManyColumnName(ptr, r.getMappedBy()));
|
|||
b5d490c7 | Christian Ehringfeld | q.bindValue(0, entity->getProperty(entity->getPrimaryKey()));
|
|
e8d1537c | Christian Ehringfeld | auto prop = EntityHelper::mappedProperty(r, ptr);
|
|
3fd96253 | Christian Ehringfeld | QSharedPointer<Entity> item;
|
|
e24791d7 | Christian Ehringfeld | for (int var = 0; var < saved.size(); ++var) {
|
|
3fd96253 | Christian Ehringfeld | item = list.at(var);
|
|
b5d490c7 | Christian Ehringfeld | if (item->getProperty(item->getPrimaryKey()).toLongLong() > -1) {
|
|
q.bindValue(1, item->getProperty(ptr->getPrimaryKey()));
|
|||
66704054 | Christian Ehringfeld | bool ok = this->db->exec(q);
|
|
e332a521 | Christian Ehringfeld | if (ok) {
|
|
c9e79b83 | Christian Ehringfeld | EntityHelper::addEntityToListProperty(item, entity, prop);
|
|
e24791d7 | Christian Ehringfeld | }
|
|
91ed1164 | Christian Ehringfeld | }
|
|
}
|
|||
719a0ba1 | Christian Ehringfeld | if (!this->db->commitTransaction()) {
|
|
this->db->rollbackTransaction();
|
|||
}
|
|||
4f3b13f3 | Christian Ehringfeld | }
|
|
a06633f7 | Christian Ehringfeld | ||
28d2f01a | Christian Ehringfeld | QString EntityManager::generateObjectName() {
|
|
int i = 0;
|
|||
QString name = "em[";
|
|||
while (true) {
|
|||
if (!EntityManager::instances.contains(name + QString::number(i) + "]")) {
|
|||
name += QString::number(i) + "]";
|
|||
break;
|
|||
}
|
|||
}
|
|||
return name;
|
|||
}
|
|||
void EntityManager::appendToInstanceList() {
|
|||
this->setObjectName(this->generateObjectName());
|
|||
EntityManager::instances.insert(this->objectName(), this);
|
|||
}
|
|||
0d155b40 | Christian Ehringfeld | ||
28d2f01a | Christian Ehringfeld | QHash<QString, EntityManager *> EntityManager::getInstances() {
|
|
return instances;
|
|||
}
|
|||
EntityManager *EntityManager::getDefaultInstance() {
|
|||
6be60ddf | Christian Ehringfeld | auto keys = EntityManager::instances.uniqueKeys();
|
|
if(!keys.isEmpty()) {
|
|||
return EntityManager::instances.value(keys.first());
|
|||
28d2f01a | Christian Ehringfeld | }
|
|
return nullptr;
|
|||
}
|
|||
EntityManager *EntityManager::getInstance(QString name) {
|
|||
if (EntityManager::instances.contains(name)) {
|
|||
return EntityManager::instances.value(name);
|
|||
}
|
|||
return nullptr;
|
|||
}
|
|||
e24791d7 | Christian Ehringfeld | bool EntityManager::shouldBeSaved(QSharedPointer<Entity> &entity,
|
|
const Relation &r) {
|
|||
5757762e | Christian Ehringfeld | return entity && (r.getCascadeType().contains(CascadeType::ALL)
|
|
3214951e | Christian Ehringfeld | || (entity->getProperty(entity->getPrimaryKey()) > -1
|
|
&& r.getCascadeType().contains(CascadeType::MERGE))
|
|||
|| (entity->getProperty(entity->getPrimaryKey()) <= -1
|
|||
&& r.getCascadeType().contains(CascadeType::PERSIST)));
|
|||
e24791d7 | Christian Ehringfeld | }
|
|
void EntityManager::removeRelations(const QSharedPointer<Entity> &entity) {
|
|||
e8d1537c | Christian Ehringfeld | auto props = EntityHelper::getRelationProperties(entity.data());
|
|
f27ac316 | Christian Ehringfeld | for (auto i = props.constBegin(); i != props.constEnd(); ++i) {
|
|
const Relation r = i.key();
|
|||
auto property = i.value();
|
|||
2075db87 | Christian Ehringfeld | auto var = property.read(entity.data());
|
|
da565582 | Christian Ehringfeld | if (r.getType() == RelationType::MANY_TO_MANY) {
|
|
2075db87 | Christian Ehringfeld | this->removeManyToManyEntityList(entity, r, var);
|
|
da565582 | Christian Ehringfeld | } else if (r.getType() == RelationType::ONE_TO_MANY) {
|
|
if (r.getCascadeType().contains(CascadeType::REMOVE)
|
|||
|| r.getCascadeType().contains(CascadeType::ALL)) {
|
|||
2075db87 | Christian Ehringfeld | this->removeEntityList(var);
|
|
} else {
|
|||
this->setNullOneToManyRelation(var, r);
|
|||
}
|
|||
da565582 | Christian Ehringfeld | } else if (r.getType() == RelationType::MANY_TO_ONE
|
|
5ca225ed | Christian Ehringfeld | || r.getType() == RelationType::ONE_TO_ONE) {
|
|
2075db87 | Christian Ehringfeld | this->setNullEntityPropertyRelation(var, r);
|
|
e24791d7 | Christian Ehringfeld | }
|
|
}
|
|||
}
|
|||
2075db87 | Christian Ehringfeld | ||
void EntityManager::setNullOneToManyRelation(QVariant &var, const Relation &r) {
|
|||
df1e56bd | Christian Ehringfeld | if (!r.getMappedBy().isEmpty() && !var.isNull()
|
|
&& var.canConvert<QVariantList>()) {
|
|||
auto list = EntityInstanceFactory::castQVariantList(var);
|
|||
if (!list.isEmpty()) {
|
|||
e8d1537c | Christian Ehringfeld | auto metas = EntityHelper::getMetaProperties(list.at(0).data());
|
|
df1e56bd | Christian Ehringfeld | if (metas.contains(r.getMappedBy())) {
|
|
for (int var = 0; var < list.size(); ++var) {
|
|||
auto entity = list.at(var);
|
|||
e8d1537c | Christian Ehringfeld | EntityHelper::setProperty(entity, QSharedPointer<Entity>(),
|
|
42d32a68 | Christian Ehringfeld | metas.value(r.getMappedBy()));
|
|
df1e56bd | Christian Ehringfeld | this->save(entity);
|
|
2075db87 | Christian Ehringfeld | }
|
|
}
|
|||
}
|
|||
}
|
|||
}
|
|||
void EntityManager::setNullEntityPropertyRelation(QVariant &var,
|
|||
1ac14ffb | Christian Ehringfeld | const Relation &r) {
|
|
da565582 | Christian Ehringfeld | if (r.getCascadeType().contains(CascadeType::REMOVE)
|
|
|| r.getCascadeType().contains(CascadeType::ALL)) {
|
|||
2075db87 | Christian Ehringfeld | this->removeEntity(var);
|
|
6c14baaf | Christian Ehringfeld | } else if (!r.getMappedBy().isEmpty() && var.data()) {
|
|
df1e56bd | Christian Ehringfeld | auto e = EntityInstanceFactory::castQVariant(var);
|
|
e8d1537c | Christian Ehringfeld | auto metas = EntityHelper::getMetaProperties(e.data());
|
|
df1e56bd | Christian Ehringfeld | if (metas.contains(r.getMappedBy())) {
|
|
66704054 | Christian Ehringfeld | EntityHelper::setProperty(e, QSharedPointer<Entity>(),
|
|
metas.value(r.getMappedBy()));
|
|||
df1e56bd | Christian Ehringfeld | this->save(e);
|
|
2075db87 | Christian Ehringfeld | }
|
|
}
|
|||
}
|
|||
void EntityManager::removeEntity(QVariant &var) {
|
|||
6c14baaf | Christian Ehringfeld | if (var.data()) {
|
|
df1e56bd | Christian Ehringfeld | auto e = EntityInstanceFactory::castQVariant(var);
|
|
6c14baaf | Christian Ehringfeld | if(e) {
|
|
this->remove(e);
|
|||
}
|
|||
2075db87 | Christian Ehringfeld | }
|
|
}
|
|||
void EntityManager::removeEntityList(QVariant &var) {
|
|||
df1e56bd | Christian Ehringfeld | if (var.canConvert<QVariantList>()) {
|
|
auto list = EntityInstanceFactory::castQVariantList(var);
|
|||
2075db87 | Christian Ehringfeld | for (int var = 0; var < list.size(); ++var) {
|
|
auto entity = list.at(var);
|
|||
this->remove(entity);
|
|||
}
|
|||
}
|
|||
}
|
|||
void EntityManager::removeManyToManyEntityList(const QSharedPointer<Entity> &e,
|
|||
1ac14ffb | Christian Ehringfeld | const Relation &r,
|
|
QVariant &var) {
|
|||
df1e56bd | Christian Ehringfeld | if (!var.isNull() && var.canConvert<QVariantList>()) {
|
|
auto list = EntityInstanceFactory::castQVariantList(var);
|
|||
2075db87 | Christian Ehringfeld | if (!list.isEmpty()) {
|
|
abb9e8c5 | Christian Ehringfeld | auto builder = this->schema->getQueryBuilder();
|
|
2075db87 | Christian Ehringfeld | auto ptr = list.at(0);
|
|
09b2592d | Christian Ehringfeld | QString tblName = builder->generateManyToManyTableName(e, ptr, r);
|
|
abb9e8c5 | Christian Ehringfeld | if (this->schema->getTables().contains(tblName)) {
|
|
QSqlQuery q = builder->manyToManyDelete(
|
|||
1ac14ffb | Christian Ehringfeld | tblName, builder->generateManyToManyColumnName(e, r.getPropertyName()),
|
|
e->getProperty(e->getPrimaryKey()).toLongLong());
|
|||
48245e6a | Christian Ehringfeld | if (this->db->exec(q)) {
|
|
bool refresh = r.getCascadeType().contains(CascadeType::REFRESH)
|
|||
1ac14ffb | Christian Ehringfeld | || r.getCascadeType().contains(CascadeType::ALL);
|
|
48245e6a | Christian Ehringfeld | bool remove = r.getCascadeType().contains(CascadeType::REMOVE)
|
|
1ac14ffb | Christian Ehringfeld | || r.getCascadeType().contains(CascadeType::ALL);
|
|
48245e6a | Christian Ehringfeld | auto fkProp = EntityHelper::mappedProperty(r, ptr);
|
|
2075db87 | Christian Ehringfeld | for (int var = 0; var < list.size(); ++var) {
|
|
auto entity = list.at(var);
|
|||
if (remove) {
|
|||
this->remove(entity);
|
|||
} else if (refresh) {
|
|||
48245e6a | Christian Ehringfeld | EntityHelper::removeEntityFromListProperty(entity, e, fkProp);
|
|
2075db87 | Christian Ehringfeld | }
|
|
}
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|
|||
e24791d7 | Christian Ehringfeld | QList<QSharedPointer<Entity>> EntityManager::saveRelationEntities(
|
|
1ac14ffb | Christian Ehringfeld | const QList<QSharedPointer<Entity>> &list, const Relation &r,
|
|
QList<Entity *> &mergedObjects) {
|
|||
e24791d7 | Christian Ehringfeld | QList<QSharedPointer<Entity>> saved = QList<QSharedPointer<Entity>>();
|
|
91ed1164 | Christian Ehringfeld | QSharedPointer<Entity> ptr;
|
|
for (int var = 0; var < list.size(); ++var) {
|
|||
ptr = list.at(var);
|
|||
0d155b40 | Christian Ehringfeld | if ((this->shouldBeSaved(ptr, r) && this->saveObject(ptr, mergedObjects))
|
|
a473cd61 | Christian Ehringfeld | || ptr->getProperty(ptr->getPrimaryKey()).toLongLong() > -1) {
|
|
e24791d7 | Christian Ehringfeld | saved.append(ptr);
|
|
91ed1164 | Christian Ehringfeld | }
|
|
}
|
|||
e24791d7 | Christian Ehringfeld | return saved;
|
|
91ed1164 | Christian Ehringfeld | }
|
|
98b5b08d | Christian Ehringfeld | ||
void EntityManager::persistManyToMany(const QSharedPointer<Entity> &entity,
|
|||
d2bebf1b | Christian Ehringfeld | const Relation &r, QVariant &property, QList<Entity *> &mergedObjects,
|
|
87739ae0 | Christian Ehringfeld | const bool ignoreHasChanged, const bool newItem) {
|
|
f6a3fe0a | Christian Ehringfeld | auto list = property.value<QList<QVariant>>();
|
|
23337ecc | Christian Ehringfeld | auto ptr = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(
|
|
EntityInstanceFactory::extractEntityType(QString(property.typeName()))));
|
|||
auto builder = this->schema->getQueryBuilder();
|
|||
QString tblName = builder->generateManyToManyTableName(entity, ptr, r);
|
|||
if (this->schema->containsTable(tblName)) {
|
|||
bool ok = newItem;
|
|||
QSqlQuery q;
|
|||
if (!newItem) {
|
|||
/**
|
|||
* @todo diff and remove entity from relational object when association is deleted
|
|||
*/
|
|||
q = builder->manyToManyDelete(
|
|||
1ac14ffb | Christian Ehringfeld | tblName, builder->generateManyToManyColumnName(entity, r.getPropertyName()),
|
|
entity->getProperty(entity->getPrimaryKey()).toLongLong());
|
|||
23337ecc | Christian Ehringfeld | ok = this->db->exec(q);
|
|
df1e56bd | Christian Ehringfeld | } else {
|
|
23337ecc | Christian Ehringfeld | q = builder->getQuery();
|
|
}
|
|||
if (ok && !list.isEmpty() && list.at(0).data()) {
|
|||
auto nList = EntityInstanceFactory::castQVariantList(property);
|
|||
this->persistMappedByRelation(nList, q, entity, ptr, r, tblName, mergedObjects);
|
|||
98b5b08d | Christian Ehringfeld | }
|
|
23337ecc | Christian Ehringfeld | } else {
|
|
this->missingManyToManyTable(tblName, entity, r);
|
|||
98b5b08d | Christian Ehringfeld | }
|
|
}
|
|||
66704054 | Christian Ehringfeld | void EntityManager::missingManyToManyTable(const QString &tblName,
|
|
1ac14ffb | Christian Ehringfeld | const QSharedPointer<Entity> &e, const Relation &r) {
|
|
b3c9a8bf | Christian Ehringfeld | QString text = "MANY_TO_MANY Table " + tblName + " is missing.\n" +
|
|
1ac14ffb | Christian Ehringfeld | "Entity " + EntityHelper::getClassName(e.data()) +
|
|
" is affected.\n" + "Relation of property: " + r.getPropertyName();
|
|||
b3c9a8bf | Christian Ehringfeld | #ifdef QT_DEBUG
|
|
throw QString(text);
|
|||
#else
|
|||
this->logger->logMsg(text, MsgType::CRITICAL);
|
|||
#endif
|
|||
66704054 | Christian Ehringfeld | }
|
|
98b5b08d | Christian Ehringfeld | ||
e0e1ead8 | Christian Ehringfeld | void EntityManager::manyToMany(const QSharedPointer<Entity> &entity,
|
|
fd67a7a7 | Christian Ehringfeld | Attribute *&attr) {
|
|
df1e56bd | Christian Ehringfeld | QSharedPointer<Entity> secEntityPtr = QSharedPointer<Entity>
|
|
1ac14ffb | Christian Ehringfeld | (EntityInstanceFactory::createInstance(attr->getRelatedClass()->className()));
|
|
abb9e8c5 | Christian Ehringfeld | auto builder = this->schema->getQueryBuilder();
|
|
95a2ba7e | Christian Ehringfeld | EntityHelper::clearEntityListProperty(entity, attr->getMetaProperty());
|
|
df1e56bd | Christian Ehringfeld | if (secEntityPtr) {
|
|
95a2ba7e | Christian Ehringfeld | if (this->schema->getTables().contains(attr->getTableName())) {
|
|
QSqlQuery q = builder->manyToMany(attr->getConjunctedTable(),
|
|||
attr->getColumnName(),
|
|||
2ce163c3 | Christian Ehringfeld | entity->getProperty(entity->getPrimaryKey()).toLongLong());
|
|
c599658a | Christian Ehringfeld | auto listMap = this->convertQueryResult(q);
|
|
95a2ba7e | Christian Ehringfeld | auto secClassName = attr->getRelatedClass()->className();
|
|
70e3d0c6 | Christian Ehringfeld | QSharedPointer<Entity> e;
|
|
2ce163c3 | Christian Ehringfeld | for (int var = 0; var < listMap.size(); ++var) {
|
|
95a2ba7e | Christian Ehringfeld | auto id = listMap.at(var).value(attr->getRelatedColumnName());
|
|
fd67a7a7 | Christian Ehringfeld | if (!(this->cache.contains(id.toLongLong(), secClassName) &&
|
|
1ac14ffb | Christian Ehringfeld | (e = this->cache.get(id.toLongLong(), secClassName)))) {
|
|
70e3d0c6 | Christian Ehringfeld | e = this->findById(id.toLongLong(), secClassName);
|
|
9d62f4aa | Christian Ehringfeld | }
|
|
66704054 | Christian Ehringfeld | if (e) {
|
|
95a2ba7e | Christian Ehringfeld | EntityHelper::addEntityToListProperty(entity, e, attr->getMetaProperty());
|
|
70e3d0c6 | Christian Ehringfeld | e = QSharedPointer<Entity>();
|
|
2ce163c3 | Christian Ehringfeld | }
|
|
f9cef58f | Christian Ehringfeld | }
|
|
c599658a | Christian Ehringfeld | } else {
|
|
be2e9500 | Christian Ehringfeld | this->missingManyToManyTable(attr->getConjunctedTable(), entity, attr->getRelation());
|
|
c599658a | Christian Ehringfeld | }
|
|
706de2e8 | Christian Ehringfeld | }
|
|
a06633f7 | Christian Ehringfeld | }
|
|
a873a3ba | Christian Ehringfeld | QList<QSharedPointer<Entity>> EntityManager::findEntityByAttributes(
|
|
1ac14ffb | Christian Ehringfeld | const QSharedPointer<Entity> &entity,
|
|
bool ignoreID, const bool resolveRelations) {
|
|||
1e213c09 | Christian Ehringfeld | auto maps = this->findAllByAttributes(entity, ignoreID);
|
|
a873a3ba | Christian Ehringfeld | return this->convert(maps, EntityHelper::getClassname(entity.data()), resolveRelations);
|
|
7e233492 | Christian Ehringfeld | }
|
|
6d91d381 | Christian Ehringfeld | QHash<QString, QVariant> EntityManager::findByPk(qint64 id,
|
|
1ac14ffb | Christian Ehringfeld | const QSharedPointer<Entity>
|
|
&e) {
|
|||
abb9e8c5 | Christian Ehringfeld | QSqlQuery q = this->schema->getQueryBuilder()->find(id, e, 0,
|
|
1ac14ffb | Christian Ehringfeld | e->getPrimaryKey());
|
|
706de2e8 | Christian Ehringfeld | auto listMap = this->convertQueryResult(q);
|
|
if (!listMap.isEmpty()) {
|
|||
return listMap.at(0);
|
|||
7e233492 | Christian Ehringfeld | }
|
|
706de2e8 | Christian Ehringfeld | return QHash<QString, QVariant>();
|
|
7e233492 | Christian Ehringfeld | }
|
|
f4e3904b | Christian Ehringfeld | ||
6be60ddf | Christian Ehringfeld | QList<QHash<QString, QVariant>> EntityManager::findAllByAttributes(
|
|
1ac14ffb | Christian Ehringfeld | const QSharedPointer<Entity>
|
|
&entity,
|
|||
bool ignoreID) {
|
|||
373a84e2 | Christian Ehringfeld | Query query = this->schema->getQueryBuilder()->findByAttributes(
|
|
1ac14ffb | Christian Ehringfeld | entity, ignoreID);
|
|
373a84e2 | Christian Ehringfeld | QSqlQuery q = this->queryInterpreter->build(query);
|
|
dc6b13b4 | Christian Ehringfeld | return this->convertQueryResult(q);
|
|
}
|
|||
6be60ddf | Christian Ehringfeld | QList<QHash <QString, QVariant>> EntityManager::findAllByAttributes(
|
|
1ac14ffb | Christian Ehringfeld | const QHash<QString, QVariant> &m, const QString &tblname, bool ignoreID) {
|
|
373a84e2 | Christian Ehringfeld | Query query = this->schema->getQueryBuilder()->findByAttributes(m,
|
|
1ac14ffb | Christian Ehringfeld | tblname, ignoreID);
|
|
373a84e2 | Christian Ehringfeld | QSqlQuery q = this->queryInterpreter->build(query);
|
|
7e233492 | Christian Ehringfeld | return this->convertQueryResult(q);
|
|
}
|
|||
6be60ddf | Christian Ehringfeld | QList<QHash<QString, QVariant>> EntityManager::convertQueryResult(
|
|
1ac14ffb | Christian Ehringfeld | QSqlQuery &q) {
|
|
6be60ddf | Christian Ehringfeld | QList<QHash <QString, QVariant>> listmap = QList<QHash <QString, QVariant>>();
|
|
7e233492 | Christian Ehringfeld | this->db->select(q);
|
|
QSqlRecord rec = q.record();
|
|||
QStringList l = QStringList();
|
|||
qint16 field_count = rec.count();
|
|||
for (int var = 0; var < field_count; ++var) {
|
|||
l.append(rec.fieldName(var));
|
|||
}
|
|||
QHash<QString, QVariant> map = QHash<QString, QVariant>();
|
|||
while (q.next()) {
|
|||
for (int var = 0; var < field_count; ++var) {
|
|||
map.insert(l.at(var), q.value(rec.indexOf(l.at(var))));
|
|||
}
|
|||
listmap.append(map);
|
|||
}
|
|||
return listmap;
|
|||
}
|
|||
6be60ddf | Christian Ehringfeld | QList<QHash <QString, QVariant>> EntityManager::findAll(
|
|
1ac14ffb | Christian Ehringfeld | const QSharedPointer<Entity> &e) {
|
|
abb9e8c5 | Christian Ehringfeld | QSqlQuery q = this->schema->getQueryBuilder()->findAll(e);
|
|
7e233492 | Christian Ehringfeld | return this->convertQueryResult(q);
|
|
}
|
|||
b0b8dac3 | Christian Ehringfeld | ||
e0e1ead8 | Christian Ehringfeld | void EntityManager::resolveRelations(const QSharedPointer<Entity> &entity,
|
|
fd67a7a7 | Christian Ehringfeld | const QHash<QString, QVariant> &map) {
|
|
e8d1537c | Christian Ehringfeld | auto props = EntityHelper::getRelationProperties(entity.data());
|
|
70e3d0c6 | Christian Ehringfeld | for (auto iterator = props.constBegin(); iterator != props.constEnd(); ++iterator) {
|
|
2d9fab10 | Christian Ehringfeld | const Relation r = iterator.key();
|
|
const QMetaProperty property = iterator.value();
|
|||
95a2ba7e | Christian Ehringfeld | auto attr = this->ar->resolveAttribute(entity->getClassname(), r.getPropertyName(),
|
|
EntityInstanceFactory::extractEntityType(property.typeName()));
|
|||
QString colName = attr->getColumnName();
|
|||
a06633f7 | Christian Ehringfeld | switch (r.getType()) {
|
|
da565582 | Christian Ehringfeld | case RelationType::MANY_TO_ONE:
|
|
df1e56bd | Christian Ehringfeld | if (map.contains(colName)) {
|
|
fd67a7a7 | Christian Ehringfeld | this->manyToOne(entity, map.value(colName), attr);
|
|
a06633f7 | Christian Ehringfeld | }
|
|
break;
|
|||
da565582 | Christian Ehringfeld | case RelationType::MANY_TO_MANY:
|
|
fd67a7a7 | Christian Ehringfeld | this->manyToMany(entity, attr);
|
|
a06633f7 | Christian Ehringfeld | break;
|
|
da565582 | Christian Ehringfeld | case RelationType::ONE_TO_MANY:
|
|
fd67a7a7 | Christian Ehringfeld | this->oneToMany(entity, attr);
|
|
a06633f7 | Christian Ehringfeld | break;
|
|
da565582 | Christian Ehringfeld | case RelationType::ONE_TO_ONE:
|
|
fd67a7a7 | Christian Ehringfeld | this->oneToOne(entity, attr, map.value(colName));
|
|
a06633f7 | Christian Ehringfeld | break;
|
|
}
|
|||
}
|
|||
}
|
|||
dc6b13b4 | Christian Ehringfeld | qint64 EntityManager::findId(QSharedPointer<Entity> &entity) {
|
|
qint64 r = -1;
|
|||
abb9e8c5 | Christian Ehringfeld | QSqlQuery q = this->schema->getQueryBuilder()->findId(entity);
|
|
dc6b13b4 | Christian Ehringfeld | this->db->select(q);
|
|
if (q.next()) {
|
|||
r = q.value(0).toLongLong();
|
|||
}
|
|||
return r;
|
|||
}
|
|||
8306a974 | Christian Ehringfeld | bool EntityManager::removeAll(QString tblname) {
|
|
abb9e8c5 | Christian Ehringfeld | return this->schema->getQueryBuilder()->removeAll(tblname).exec();
|
|
8306a974 | Christian Ehringfeld | }
|
|
b7446f4c | Christian Ehringfeld | bool EntityManager::createTable(const QSharedPointer<Entity> &entity,
|
|
bool createRelationTables) {
|
|||
abb9e8c5 | Christian Ehringfeld | return this->schema->getQueryBuilder()->createTable(entity,
|
|
1ac14ffb | Christian Ehringfeld | createRelationTables);
|
|
9c2f773f | Christian Ehringfeld | }
|
|
0ec1cbfb | Christian Ehringfeld | bool EntityManager::createTable(QString className, bool createRelationTables) {
|
|
QSharedPointer<Entity> e = QSharedPointer<Entity>
|
|||
1ac14ffb | Christian Ehringfeld | (EntityInstanceFactory::createInstance(className));
|
|
0ec1cbfb | Christian Ehringfeld | return this->schema->getQueryBuilder()->createTable(e, createRelationTables);
|
|
}
|
|||
23337ecc | Christian Ehringfeld | bool EntityManager::removeTable(QString className) {
|
|
return this->executeQuery(this->schema->getQueryBuilder()->dropTable(className));
|
|||
}
|
|||
95d9cf46 | Christian Ehringfeld | quint32 EntityManager::count(const QSharedPointer<Entity> &entity, bool ignoreID,
|
|
977d8d5f | Christian Ehringfeld | bool joinBaseClasses) {
|
|
87739ae0 | Christian Ehringfeld | Query q = Query();
|
|
auto qb = this->schema->getQueryBuilder();
|
|||
QHash<QString, QVariant> values;
|
|||
977d8d5f | Christian Ehringfeld | if (joinBaseClasses) {
|
|
87739ae0 | Christian Ehringfeld | q.appendJoins(qb->joinBaseClasses(entity));
|
|
values = EntityHelper::getEntityAttributes(EntityHelper::getMetaProperties(
|
|||
1ac14ffb | Christian Ehringfeld | entity.data()), entity);
|
|
87739ae0 | Christian Ehringfeld | } else {
|
|
values = EntityHelper::getEntityAttributes(
|
|||
1ac14ffb | Christian Ehringfeld | EntityHelper::getNonInheritedMetaProperties(entity.data()), entity);
|
|
87739ae0 | Christian Ehringfeld | }
|
|
//maybe filter MANY_TO_MANY and ONE_TO_MANY relations
|
|||
q.appendFrom(entity->getTablename());
|
|||
q.appendWhere(qb->where(values, qb->andKeyword(), ignoreID,
|
|||
entity->getPrimaryKey(), false));
|
|||
q.appendParams(values);
|
|||
return this->count(q);
|
|||
}
|
|||
95d9cf46 | Christian Ehringfeld | quint32 EntityManager::count(const QString &tableName) {
|
|
dc6b13b4 | Christian Ehringfeld | qint8 rc = -1;
|
|
87739ae0 | Christian Ehringfeld | QSqlQuery q = this->schema->getQueryBuilder()->count(tableName);
|
|
dc6b13b4 | Christian Ehringfeld | this->db->select(q);
|
|
if (q.next()) {
|
|||
rc = q.value(0).toInt();
|
|||
}
|
|||
return rc;
|
|||
}
|
|||
95d9cf46 | Christian Ehringfeld | quint32 EntityManager::count(Query &query) {
|
|
87739ae0 | Christian Ehringfeld | qint8 rc = 0;
|
|
query.appendSelect("COUNT(*)");
|
|||
QSqlQuery q = this->queryInterpreter->build(query);
|
|||
dc6b13b4 | Christian Ehringfeld | this->db->select(q);
|
|
if (q.next()) {
|
|||
rc = q.value(0).toInt();
|
|||
}
|
|||
return rc;
|
|||
}
|
|||
586bb527 | Christian Ehringfeld | void EntityManager::setConnectionNames(QStringList list) {
|
|
EntityManager::connectionNames = list;
|
|||
}
|
|||
e8d1537c | Christian Ehringfeld | ||
QSharedPointer<Entity> EntityManager::convert(const QHash<QString, QVariant>
|
|||
1ac14ffb | Christian Ehringfeld | &map,
|
|
const char *classname, const bool resolveRelations) {
|
|||
auto ptr = QSharedPointer<Entity>(map.isEmpty() ? nullptr : EntityInstanceFactory::createInstance(
|
|||
classname, map));
|
|||
e8d1537c | Christian Ehringfeld | this->cache.insert(ptr);
|
|
c9f21778 | Christian Ehringfeld | if (resolveRelations) {
|
|
fd67a7a7 | Christian Ehringfeld | this->resolveRelations(ptr, map);
|
|
c9f21778 | Christian Ehringfeld | }
|
|
e8d1537c | Christian Ehringfeld | return ptr;
|
|
}
|
|||
23337ecc | Christian Ehringfeld | void EntityManager::convert(const QHash<QString, QVariant> &map,
|
|
fd67a7a7 | Christian Ehringfeld | QSharedPointer<Entity> &entity, const bool resolveRelations) {
|
|
23337ecc | Christian Ehringfeld | this->cache.insert(entity);
|
|
auto data = entity.data();
|
|||
EntityInstanceFactory::setAttributes(data, map);
|
|||
if (resolveRelations) {
|
|||
fd67a7a7 | Christian Ehringfeld | this->resolveRelations(entity, map);
|
|
23337ecc | Christian Ehringfeld | }
|
|
}
|
|||
6be60ddf | Christian Ehringfeld | QList<QSharedPointer<Entity>> EntityManager::convert(
|
|
1ac14ffb | Christian Ehringfeld | QList<QHash<QString, QVariant>> maps,
|
|
const char *classname, const bool resolveRelations) {
|
|||
6be60ddf | Christian Ehringfeld | auto list = QList<QSharedPointer<Entity>>();
|
|
e8d1537c | Christian Ehringfeld | for (int var = 0; var < maps.size(); ++var) {
|
|
a873a3ba | Christian Ehringfeld | auto ptr = this->convert(maps.at(var), classname, resolveRelations);
|
|
e8d1537c | Christian Ehringfeld | list.append(ptr);
|
|
}
|
|||
return list;
|
|||
}
|