Revision 66704054
Von Christian Ehringfeld vor mehr als 10 Jahren hinzugefügt
| src/entitymanager.cpp | ||
|---|---|---|
| EntityManager::EntityManager(const QString &databaseType, QString databasename ,
 | ||
|                              QString hostname,
 | ||
|                              QString username,
 | ||
|                              QString password, QString port) : QObject() {
 | ||
|     bool logQueries = false;
 | ||
| #ifdef QT_DEBUG
 | ||
|     logQueries = true;
 | ||
| #endif
 | ||
|                              QString password, QString port, bool logQueries) : QObject() {
 | ||
|     auto db = new Database(databaseType, this->createConnection(), hostname,
 | ||
|                            databasename, username,
 | ||
|                            password,
 | ||
|                            port.toInt(),true,logQueries);
 | ||
|                            port.toInt(), true, logQueries);
 | ||
|     this->db = QSharedPointer<Database>(db);
 | ||
|     this->init();
 | ||
| }
 | ||
| ... | ... | |
| }
 | ||
|  | ||
| bool EntityManager::startup(QString version, QStringList toInitialize) {
 | ||
|     QSharedPointer<Entity> dbm = QSharedPointer<DatabaseMigration>(new DatabaseMigration());
 | ||
|     QSharedPointer<Entity> dbm = QSharedPointer<DatabaseMigration>
 | ||
|                                  (new DatabaseMigration());
 | ||
|     QHash<QString, QVariant> map = QHash<QString, QVariant>();
 | ||
|     bool ok = true;
 | ||
|     map.insert("version", version);
 | ||
| ... | ... | |
| }
 | ||
|  | ||
| QSharedPointer<Entity> EntityManager::findById(const qint64 &id,
 | ||
|                                                QSharedPointer<Entity> &e,
 | ||
|                                                const bool refresh) {
 | ||
|         QSharedPointer<Entity> &e,
 | ||
|         const bool refresh) {
 | ||
|     QSharedPointer<Entity> r;
 | ||
|     if (!e.isNull() && (refresh
 | ||
|                         || !(r = this->cache.get(id, EntityHelper::getClassname(e.data()))))) {
 | ||
| ... | ... | |
| }
 | ||
|  | ||
| QSharedPointer<Entity> EntityManager::findById(const qint64 &id,
 | ||
|                                                const QString &classname) {
 | ||
|         const QString &classname) {
 | ||
|     QSharedPointer<Entity> e = QSharedPointer<Entity>
 | ||
|             (EntityInstanceFactory::createInstance(classname));
 | ||
|                                (EntityInstanceFactory::createInstance(classname));
 | ||
|     return this->findById(id, e);
 | ||
| }
 | ||
|  | ||
| ... | ... | |
|     bool ok = false;
 | ||
|     if ((convertedId = id.toLongLong(&ok)) && ok && convertedId > -1) {
 | ||
|         QString className = EntityInstanceFactory::extractEntityType(
 | ||
|                     property.typeName());
 | ||
|                                 property.typeName());
 | ||
|         QSharedPointer<Entity> ptr = QSharedPointer<Entity>();
 | ||
|         if (refresh || !(this->cache.contains(convertedId, className)
 | ||
|                          && (ptr = this->cache.get(convertedId, className)) && ptr)) {
 | ||
| ... | ... | |
|                                                 property.typeName())));
 | ||
|         if (e) {
 | ||
|             QSqlQuery q = this->schema->getQueryBuilder()->oneToMany(e->getTablename(),
 | ||
|                                                                      this->schema->getQueryBuilder()->generateColumnNameID(r.getMappedBy()),
 | ||
|                                                                      entity->getId());
 | ||
|                           this->schema->getQueryBuilder()->generateColumnNameID(r.getMappedBy()),
 | ||
|                           entity->getId());
 | ||
|             auto listMap = this->convertQueryResult(q);
 | ||
|             auto entities = this->convert(listMap, EntityHelper::getClassname(e.data()), refresh);
 | ||
|             auto entities = this->convert(listMap, EntityHelper::getClassname(e.data()),
 | ||
|                                           refresh);
 | ||
|             EntityHelper::setListProperty(entity, entities, property);
 | ||
|         }
 | ||
|     }
 | ||
| ... | ... | |
|                                             EntityInstanceFactory::extractEntityType(property.typeName())));
 | ||
|         if (e) {
 | ||
|             QSqlQuery q = this->schema->getQueryBuilder()->oneToMany(
 | ||
|                         e->getTablename(),
 | ||
|                         this->schema->getQueryBuilder()->generateColumnNameID(
 | ||
|                             r.getMappedBy()),
 | ||
|                         entity->getProperty(entity->getPrimaryKey()).toLongLong(), 1);
 | ||
|                               e->getTablename(),
 | ||
|                               this->schema->getQueryBuilder()->generateColumnNameID(
 | ||
|                                   r.getMappedBy()),
 | ||
|                               entity->getProperty(entity->getPrimaryKey()).toLongLong(), 1);
 | ||
|             auto listMap = this->convertQueryResult(q);
 | ||
|             auto entities = this->convert(listMap, EntityHelper::getClassname(e.data()), refresh);
 | ||
|             auto entities = this->convert(listMap, EntityHelper::getClassname(e.data()),
 | ||
|                                           refresh);
 | ||
|             if (!entities.isEmpty()) {
 | ||
|                 QSharedPointer<Entity> ptr = entities.at(0);
 | ||
|                 EntityHelper::setProperty(entity, ptr, property);
 | ||
| ... | ... | |
| }
 | ||
|  | ||
| void EntityManager::savePrePersistedRelations(const QSharedPointer<Entity>
 | ||
|                                               &entity) {
 | ||
|         &entity) {
 | ||
|     auto relations = EntityHelper::getRelationProperties(entity.data());
 | ||
|     auto iterator = relations.constBegin();
 | ||
|     while (iterator != relations.constEnd()) {
 | ||
| ... | ... | |
|                 auto e = EntityInstanceFactory::castQVariant(var);
 | ||
|                 if (this->shouldBeSaved(e, r)) {
 | ||
|                     this->save(e);
 | ||
|                     auto fkProp = EntityHelper::mappedProperty(r,e);
 | ||
|                     if(fkProp.isValid()) {
 | ||
|                         EntityHelper::addEntityToListProperty(e,entity,fkProp);
 | ||
|                     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()) {
 | ||
|             } else if (r.getType() == RelationType::ONE_TO_ONE
 | ||
|                        && r.getMappedBy().isEmpty()) {
 | ||
|                 auto e =  EntityInstanceFactory::castQVariant(var);
 | ||
|                 this->save(e);
 | ||
|                 auto prop = EntityHelper::mappedProperty(r,e);
 | ||
|                 if(prop.isValid()) {
 | ||
|                     EntityHelper::setProperty(e,entity,prop);
 | ||
|                 auto prop = EntityHelper::mappedProperty(r, e);
 | ||
|                 if (prop.isValid()) {
 | ||
|                     EntityHelper::setProperty(e, entity, prop);
 | ||
|                 }
 | ||
|             }
 | ||
|         }
 | ||
| ... | ... | |
| }
 | ||
|  | ||
| void EntityManager::savePostPersistedRelations(const QSharedPointer<Entity>
 | ||
|                                                &entity) {
 | ||
|         &entity) {
 | ||
|     auto relations = EntityHelper::getRelationProperties(entity.data());
 | ||
|     auto iterator = relations.constBegin();
 | ||
|     while (iterator != relations.constEnd()) {
 | ||
| ... | ... | |
|                 this->persistManyToMany(entity, r, var);
 | ||
|             } else if (this->canPersistRelation(r, RelationType::ONE_TO_MANY, var)) {
 | ||
|                 QList<QSharedPointer<Entity>> list = EntityInstanceFactory::castQVariantList(
 | ||
|                             var);
 | ||
|                 if(!list.isEmpty()) {
 | ||
|                     auto fkProp = EntityHelper::mappedProperty(r,list.at(0));
 | ||
|                         var);
 | ||
|                 if (!list.isEmpty()) {
 | ||
|                     auto fkProp = EntityHelper::mappedProperty(r, list.at(0));
 | ||
|                     for (int var = 0; var < list.size(); ++var) {
 | ||
|                         auto e = list.at(var);
 | ||
|                         if (this->shouldBeSaved(e, r)) {
 | ||
|                             this->save(e);
 | ||
|                             if(fkProp.isValid()) {
 | ||
|                                 EntityHelper::addEntityToListProperty(e,entity,fkProp);
 | ||
|                             if (fkProp.isValid()) {
 | ||
|                                 EntityHelper::addEntityToListProperty(e, entity, fkProp);
 | ||
|                             }
 | ||
|                         }
 | ||
|                     }
 | ||
| ... | ... | |
|                        && !r.getMappedBy().isEmpty()) {
 | ||
|                 auto e =  EntityInstanceFactory::castQVariant(var);
 | ||
|                 this->save(e);
 | ||
|                 auto fkProp = EntityHelper::mappedProperty(r,e);
 | ||
|                 if(fkProp.isValid()) {
 | ||
|                     EntityHelper::addEntityToListProperty(e,entity,fkProp);
 | ||
|                 auto fkProp = EntityHelper::mappedProperty(r, e);
 | ||
|                 if (fkProp.isValid()) {
 | ||
|                     EntityHelper::addEntityToListProperty(e, entity, fkProp);
 | ||
|                 }
 | ||
|             }
 | ||
|         }
 | ||
| ... | ... | |
| }
 | ||
|  | ||
| void EntityManager::persistMappedByRelation(const QList<QSharedPointer<Entity> >
 | ||
|                                             &list, QSqlQuery &q, const QSharedPointer<Entity> &entity,
 | ||
|                                             const QSharedPointer<Entity> &ptr, const Relation &r,
 | ||
|                                             const QString &tblName) {
 | ||
|         &list, QSqlQuery &q, const QSharedPointer<Entity> &entity,
 | ||
|         const QSharedPointer<Entity> &ptr, const Relation &r,
 | ||
|         const QString &tblName) {
 | ||
|     q.clear();
 | ||
|     QList<QSharedPointer<Entity>> saved = r.getCascadeType().contains(
 | ||
|                 CascadeType::ALL)
 | ||
|             || r.getCascadeType().contains(CascadeType::MERGE)
 | ||
|             || r.getCascadeType().contains(CascadeType::PERSIST) ?
 | ||
|                 this->saveRelationEntities(list,
 | ||
|                                            r) : list;
 | ||
|     QList<QSharedPointer<Entity>> saved =
 | ||
|                                    r.getCascadeType().contains(CascadeType::ALL) ||
 | ||
|                                    r.getCascadeType().contains(CascadeType::MERGE) ||
 | ||
|                                    r.getCascadeType().contains(CascadeType::PERSIST) ?
 | ||
|                                    this->saveRelationEntities(list, r) : list;
 | ||
|     this->db->startTransaction();
 | ||
|     auto builder = this->schema->getQueryBuilder();
 | ||
|     q = builder->manyToManyInsert(tblName,
 | ||
| ... | ... | |
|                                   builder->generateManyToManyColumnName(ptr));
 | ||
|     q.bindValue(0, entity->getProperty(entity->getPrimaryKey()));
 | ||
|     auto prop = EntityHelper::mappedProperty(r, ptr);
 | ||
|     bool propertyIsValid = this->isRelationPropertyValid(prop, r, entity, ptr);
 | ||
|     QSharedPointer<Entity> item;
 | ||
|     for (int var = 0; var < saved.size(); ++var) {
 | ||
|         item = list.at(var);
 | ||
|         if (item->getProperty(item->getPrimaryKey()).toLongLong() > -1) {
 | ||
|             q.bindValue(1, item->getProperty(ptr->getPrimaryKey()));
 | ||
|             this->schema->getDatabase()->exec(q);
 | ||
|             if (prop.isReadable()) {
 | ||
|             bool ok = this->db->exec(q);
 | ||
|             if (ok && propertyIsValid) {
 | ||
|                 EntityHelper::addEntityToListProperty(item, entity, prop);
 | ||
|             } else {
 | ||
|                 qDebug() << "Query exec for many to many relation failed." <<
 | ||
|                             q.lastError().text();
 | ||
|                 qDebug() << "Involved entities: " << EntityHelper::getClassName(entity.data()) <<
 | ||
|                             "(MainEntitiy) and "  << EntityHelper::getClassName(ptr.data());
 | ||
|                 qDebug() << "Relation:" << r.getPropertyName();
 | ||
|             }
 | ||
|         }
 | ||
|     }
 | ||
| ... | ... | |
|     }
 | ||
| }
 | ||
|  | ||
|  | ||
| bool EntityManager::isRelationPropertyValid(const QMetaProperty &prop,
 | ||
|         const Relation &r, const QSharedPointer<Entity> &e,
 | ||
|         const QSharedPointer<Entity> &relatedEntity) {
 | ||
|     bool propertyIsValid = prop.isValid() && prop.isReadable() && prop.isWritable();
 | ||
|     if (!propertyIsValid) {
 | ||
|         qDebug() << "Relation is incomplete:" << r.getPropertyName();
 | ||
|         qDebug() << "Involved entities: " << EntityHelper::getClassName(
 | ||
|                      e.data()) <<
 | ||
|                  "(MainEntitiy) and "  << EntityHelper::getClassName(relatedEntity.data());
 | ||
|     }
 | ||
|     return propertyIsValid;
 | ||
| }
 | ||
|  | ||
| bool EntityManager::shouldBeSaved(QSharedPointer<Entity> &entity,
 | ||
|                                   const Relation &r) {
 | ||
|     return entity && (r.getCascadeType().contains(CascadeType::ALL)
 | ||
| ... | ... | |
| }
 | ||
|  | ||
| void EntityManager::setNullEntityPropertyRelation(QVariant &var,
 | ||
|                                                   const Relation &r) {
 | ||
|         const Relation &r) {
 | ||
|     if (r.getCascadeType().contains(CascadeType::REMOVE)
 | ||
|             || r.getCascadeType().contains(CascadeType::ALL)) {
 | ||
|         this->removeEntity(var);
 | ||
| ... | ... | |
|         auto e = EntityInstanceFactory::castQVariant(var);
 | ||
|         auto metas = EntityHelper::getMetaProperties(e.data());
 | ||
|         if (metas.contains(r.getMappedBy())) {
 | ||
|             EntityHelper::setProperty(e, QSharedPointer<Entity>(), metas.value(r.getMappedBy()));
 | ||
|             EntityHelper::setProperty(e, QSharedPointer<Entity>(),
 | ||
|                                       metas.value(r.getMappedBy()));
 | ||
|             this->save(e);
 | ||
|         }
 | ||
|     }
 | ||
| ... | ... | |
| }
 | ||
|  | ||
| void EntityManager::removeManyToManyEntityList(const QSharedPointer<Entity> &e,
 | ||
|                                                const Relation &r,
 | ||
|                                                QVariant &var) {
 | ||
|         const Relation &r,
 | ||
|         QVariant &var) {
 | ||
|     if (!var.isNull() && var.canConvert<QVariantList>()) {
 | ||
|         auto list = EntityInstanceFactory::castQVariantList(var);
 | ||
|         if (!list.isEmpty()) {
 | ||
| ... | ... | |
|             QString tblName = builder->generateManyToManyTableName(e, ptr, r);
 | ||
|             if (this->schema->getTables().contains(tblName)) {
 | ||
|                 QSqlQuery q = builder->manyToManyDelete(
 | ||
|                             tblName, builder->generateManyToManyColumnName(e),
 | ||
|                             e->getProperty(e->getPrimaryKey()).toLongLong());
 | ||
|                                   tblName, builder->generateManyToManyColumnName(e),
 | ||
|                                   e->getProperty(e->getPrimaryKey()).toLongLong());
 | ||
|                 bool refresh = r.getCascadeType().contains(CascadeType::REFRESH)
 | ||
|                         || r.getCascadeType().contains(CascadeType::ALL);
 | ||
|                                || r.getCascadeType().contains(CascadeType::ALL);
 | ||
|                 bool remove = r.getCascadeType().contains(CascadeType::REMOVE)
 | ||
|                         || r.getCascadeType().contains(CascadeType::ALL);
 | ||
|                               || r.getCascadeType().contains(CascadeType::ALL);
 | ||
|                 if (this->schema->getDatabase()->exec(q)) {
 | ||
|                     for (int var = 0; var < list.size(); ++var) {
 | ||
|                         auto entity = list.at(var);
 | ||
| ... | ... | |
| }
 | ||
|  | ||
| QList<QSharedPointer<Entity>> EntityManager::saveRelationEntities(
 | ||
|         const QList<QSharedPointer<Entity> > &list, const Relation &r) {
 | ||
| const QList<QSharedPointer<Entity> > &list, const Relation &r) {
 | ||
|     QList<QSharedPointer<Entity>> saved = QList<QSharedPointer<Entity>>();
 | ||
|     QSharedPointer<Entity> ptr;
 | ||
|     for (int var = 0; var < list.size(); ++var) {
 | ||
| ... | ... | |
|         QString tblName = builder->generateManyToManyTableName(entity, ptr, r);
 | ||
|         if (this->schema->getTables().contains(tblName)) {
 | ||
|             QSqlQuery q = builder->manyToManyDelete(
 | ||
|                         tblName, builder->generateManyToManyColumnName(entity),
 | ||
|                         entity->getProperty(entity->getPrimaryKey()).toLongLong());
 | ||
|                               tblName, builder->generateManyToManyColumnName(entity),
 | ||
|                               entity->getProperty(entity->getPrimaryKey()).toLongLong());
 | ||
|             if (this->db->exec(q)) {
 | ||
|                 auto nList = EntityInstanceFactory::castQVariantList(property);
 | ||
|                 this->persistMappedByRelation(nList, q, entity, ptr, r, tblName);
 | ||
|             }
 | ||
|         } else {
 | ||
|             qDebug() << "MANY_TO_MANY Table " << tblName << " not exists";
 | ||
|             this->missingManyToManyTable(tblName, entity, r);
 | ||
|         }
 | ||
|     }
 | ||
| }
 | ||
|  | ||
|  | ||
| void EntityManager::missingManyToManyTable(const QString &tblName,
 | ||
|         const QSharedPointer<Entity> &e, const Relation &r) {
 | ||
|     qDebug() << "MANY_TO_MANY Table " << tblName << " is missing";
 | ||
|     qDebug() << "Entity " << EntityHelper::getClassName(e.data()) << " is affected";
 | ||
|     qDebug() << "Relation of property: " << r.getPropertyName();
 | ||
|     /**
 | ||
|       @todo wait for Qt 5.5.1
 | ||
|       @see https://codereview.qt-project.org/#/c/122232/
 | ||
|       */
 | ||
|     //qDebug() << "RelationType:" << r.getType() << " MappedBy:" << r.getMappedBy();
 | ||
| }
 | ||
|  | ||
| void EntityManager::manyToMany(const QSharedPointer<Entity> &entity,
 | ||
|                                const QMetaProperty &property, const Relation &relation, const bool refresh) {
 | ||
|     QSharedPointer<Entity> secEntityPtr = QSharedPointer<Entity>
 | ||
|             (EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(
 | ||
|                                                        QString(property.typeName()))));
 | ||
|                                           (EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(
 | ||
|                                                   QString(property.typeName()))));
 | ||
|     auto builder = this->schema->getQueryBuilder();
 | ||
|     if (secEntityPtr) {
 | ||
|         QString tblName = builder->generateManyToManyTableName(entity, secEntityPtr,
 | ||
|                                                                relation);
 | ||
|                           relation);
 | ||
|         if (this->schema->getTables().contains(tblName)) {
 | ||
|             QSqlQuery q = builder->manyToMany(tblName,
 | ||
|                                               builder->generateManyToManyColumnName(entity),
 | ||
| ... | ... | |
|                 auto id = listMap.at(var).value(builder->generateManyToManyColumnName(
 | ||
|                                                     secEntityPtr));
 | ||
|                 if (!refresh
 | ||
|                         && this->cache.contains(id.toLongLong(), EntityHelper::getClassname(secEntityPtr.data()))) {
 | ||
|                     e = this->cache.get(id.toLongLong(), EntityHelper::getClassname(secEntityPtr.data()));
 | ||
|                         && this->cache.contains(id.toLongLong(),
 | ||
|                                                 EntityHelper::getClassname(secEntityPtr.data()))) {
 | ||
|                     e = this->cache.get(id.toLongLong(),
 | ||
|                                         EntityHelper::getClassname(secEntityPtr.data()));
 | ||
|                 } else {
 | ||
|                     e = this->findById(id.toLongLong(), EntityHelper::getClassname(secEntityPtr.data()));
 | ||
|                     e = this->findById(id.toLongLong(),
 | ||
|                                        EntityHelper::getClassname(secEntityPtr.data()));
 | ||
|                 }
 | ||
|                 if(e) {
 | ||
|                     EntityHelper::addEntityToListProperty(entity,e,property);
 | ||
|                 if (e) {
 | ||
|                     EntityHelper::addEntityToListProperty(entity, e, property);
 | ||
|                 }
 | ||
|             }
 | ||
|         } else {
 | ||
|             qDebug() << "MANY_TO_MANY Table " << tblName << " not exists";
 | ||
|             this->missingManyToManyTable(tblName, entity, relation);
 | ||
|         }
 | ||
|     }
 | ||
| }
 | ||
|  | ||
| QList<QSharedPointer<Entity> > EntityManager::findEntityByAttributes(
 | ||
|         const QSharedPointer<Entity>
 | ||
|         &entity,
 | ||
|         bool ignoreID) {
 | ||
|     const QSharedPointer<Entity>
 | ||
|     &entity,
 | ||
|     bool ignoreID) {
 | ||
|     auto maps = this->findAllByAttributes(entity, ignoreID);
 | ||
|     return this->convert(maps, EntityHelper::getClassname(entity.data()));
 | ||
| }
 | ||
| ... | ... | |
|         }
 | ||
|         this->db->startTransaction();
 | ||
|         QList<QSqlQuery> q = this->schema->getQueryBuilder()->create(
 | ||
|                     entity);
 | ||
|                                  entity);
 | ||
|         bool first = true;
 | ||
|         QVariant id = -1;
 | ||
|         for (int var = 0; var < q.size(); ++var) {
 | ||
| ... | ... | |
|             }
 | ||
|             rc = this->db->exec(query);
 | ||
|             if (!rc) {
 | ||
|                 qDebug() << "Query failed:" << query.lastError().text() << " of class " <<
 | ||
|                             EntityHelper::getClassname(entity.data());
 | ||
|                 qDebug() << "class is erroneous:" <<  EntityHelper::getClassname(entity.data());
 | ||
|                 break;
 | ||
|             }
 | ||
|             if (first) {
 | ||
| ... | ... | |
|         }
 | ||
|         this->db->startTransaction();
 | ||
|         QList<QSqlQuery> q = this->schema->getQueryBuilder()->merge(
 | ||
|                     entity);
 | ||
|         bool ok = true;
 | ||
|         for (int var = 0; var < q.size(); ++var) {
 | ||
|             auto query = q.at(var);
 | ||
|             ok = this->db->exec(query);
 | ||
|             if (!ok) {
 | ||
|                 qDebug() << query.lastError().text();
 | ||
|                 break;
 | ||
|             }
 | ||
|         }
 | ||
|                                  entity);
 | ||
|         bool ok = this->db->exec(q);
 | ||
|         if (!ok || !this->db->commitTransaction()) {
 | ||
|             this->db->rollbackTransaction();
 | ||
|             return false;
 | ||
| ... | ... | |
| }
 | ||
|  | ||
| QHash<QString, QVariant> EntityManager::findByPk(qint64 id,
 | ||
|                                                  const QSharedPointer<Entity>
 | ||
|                                                  &e) {
 | ||
|         const QSharedPointer<Entity>
 | ||
|         &e) {
 | ||
|     QSqlQuery q = this->schema->getQueryBuilder()->find(id, e, 0,
 | ||
|                                                         e->getPrimaryKey());
 | ||
|                   e->getPrimaryKey());
 | ||
|     auto listMap  = this->convertQueryResult(q);
 | ||
|     if (!listMap.isEmpty()) {
 | ||
|         return listMap.at(0);
 | ||
| ... | ... | |
|  | ||
|  | ||
| QList<QHash<QString, QVariant> > EntityManager::findAllByAttributes(
 | ||
|         const QSharedPointer<Entity>
 | ||
|         &entity,
 | ||
|         bool ignoreID) {
 | ||
|     const QSharedPointer<Entity>
 | ||
|     &entity,
 | ||
|     bool ignoreID) {
 | ||
|     QSqlQuery q = this->schema->getQueryBuilder()->findByAttributes(
 | ||
|                 entity, ignoreID);
 | ||
|                       entity, ignoreID);
 | ||
|     return this->convertQueryResult(q);
 | ||
| }
 | ||
|  | ||
| QList<QHash <QString, QVariant> > EntityManager::findAllByAttributes(
 | ||
|         const QHash<QString, QVariant> &m, const QString &tblname, bool ignoreID) {
 | ||
|     const QHash<QString, QVariant> &m, const QString &tblname, bool ignoreID) {
 | ||
|     QSqlQuery q = this->schema->getQueryBuilder()->findByAttributes(m,
 | ||
|                                                                     tblname, ignoreID);
 | ||
|                   tblname, ignoreID);
 | ||
|     return this->convertQueryResult(q);
 | ||
| }
 | ||
|  | ||
| QList<QHash<QString, QVariant> > EntityManager::convertQueryResult(
 | ||
|         QSqlQuery &q) {
 | ||
|     QSqlQuery &q) {
 | ||
|     QList<QHash <QString, QVariant> > listmap = QList<QHash <QString, QVariant> >();
 | ||
|     this->db->select(q);
 | ||
|     QSqlRecord rec = q.record();
 | ||
| ... | ... | |
| }
 | ||
|  | ||
| QList<QHash <QString, QVariant> > EntityManager::findAll(
 | ||
|         const QSharedPointer<Entity> &e) {
 | ||
|     const QSharedPointer<Entity> &e) {
 | ||
|     QSqlQuery q = this->schema->getQueryBuilder()->findAll(e);
 | ||
|     return this->convertQueryResult(q);
 | ||
| }
 | ||
| ... | ... | |
|         const Relation r = iterator.key();
 | ||
|         const QMetaProperty property = iterator.value();
 | ||
|         QString colName = this->schema->getQueryBuilder()->generateColumnNameID(
 | ||
|                     r.getPropertyName());
 | ||
|                               r.getPropertyName());
 | ||
|         switch (r.getType()) {
 | ||
|         case RelationType::MANY_TO_ONE:
 | ||
|             if (map.contains(colName)) {
 | ||
| ... | ... | |
|     this->db->startTransaction();
 | ||
|     this->removeRelations(entity);
 | ||
|     auto queries = this->schema->getQueryBuilder()->remove(entity);
 | ||
|     bool ok = true;
 | ||
|     for (int var = 0; var < queries.size(); ++var) {
 | ||
|         auto q = queries.at(var);
 | ||
|         if (!q.exec()) {
 | ||
|             this->db->rollbackTransaction();
 | ||
|             ok = false;
 | ||
|             break;
 | ||
|         }
 | ||
|     }
 | ||
|     bool ok = this->db->exec(queries);
 | ||
|     if (ok && this->db->commitTransaction()) {
 | ||
|         this->cache.remove(entity);
 | ||
|         entity.clear();
 | ||
|         rc = true;
 | ||
|     } else {
 | ||
|         this->db->rollbackTransaction();
 | ||
|     }
 | ||
|     return rc;
 | ||
| }
 | ||
| ... | ... | |
| bool EntityManager::createTable(const QSharedPointer<Entity> &entity,
 | ||
|                                 bool createRelationTables) {
 | ||
|     return this->schema->getQueryBuilder()->createTable(entity,
 | ||
|                                                         createRelationTables);
 | ||
|             createRelationTables);
 | ||
| }
 | ||
|  | ||
| qint8 EntityManager::count(const QSharedPointer<Entity> &entity,
 | ||
|                            bool ignoreID) {
 | ||
|     qint8 rc = -1;
 | ||
|     QSqlQuery q = this->schema->getQueryBuilder()->count(entity,
 | ||
|                                                          ignoreID);
 | ||
|                   ignoreID);
 | ||
|     this->db->select(q);
 | ||
|     if (q.next()) {
 | ||
|         rc = q.value(0).toInt();
 | ||
| ... | ... | |
| }
 | ||
|  | ||
| QSharedPointer<Entity> EntityManager::convert(const QHash<QString, QVariant>
 | ||
|                                               &map,
 | ||
|                                               const char *classname,const bool refresh) {
 | ||
|         &map,
 | ||
|         const char *classname, const bool refresh) {
 | ||
|     auto ptr = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(
 | ||
|                                           classname, map));
 | ||
|     this->cache.insert(ptr);
 | ||
| ... | ... | |
| }
 | ||
|  | ||
| QList<QSharedPointer<Entity> > EntityManager::convert(
 | ||
|         QList<QHash<QString, QVariant> > maps,
 | ||
|         const char *classname,const bool refresh) {
 | ||
|     QList<QHash<QString, QVariant> > maps,
 | ||
|     const char *classname, const bool refresh) {
 | ||
|     auto list = QList<QSharedPointer<Entity> >();
 | ||
|     for (int var = 0; var < maps.size(); ++var) {
 | ||
|         auto ptr = this->convert(maps.at(var), classname, refresh);
 | ||
Auch abrufbar als: Unified diff
improvements