Revision a873a3ba
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
| src/cache.cpp | ||
|---|---|---|
|
#include "entityhelper.h"
|
||
|
using namespace CuteEntityManager;
|
||
|
Cache::Cache() {
|
||
|
|
||
|
}
|
||
|
QHash<QString, QWeakPointer<Entity> > Cache::getCache() const {
|
||
|
QHash<QString, QWeakPointer<Entity>> Cache::getCache() const {
|
||
|
return cache;
|
||
|
}
|
||
|
|
||
| ... | ... | |
|
return false;
|
||
|
}
|
||
|
|
||
|
void Cache::clear() {
|
||
|
this->cache.clear();
|
||
|
}
|
||
|
|
||
|
void Cache::insert(QSharedPointer<Entity> &entity) {
|
||
|
if (entity && entity->getId() > -1) {
|
||
|
QString key = this->generateKey(entity->getId(),
|
||
| src/cache.h | ||
|---|---|---|
|
}
|
||
|
return ok;
|
||
|
}
|
||
|
void clear();
|
||
|
void insert(QSharedPointer<Entity> &entity);
|
||
|
void remove(const QSharedPointer<Entity> &entity);
|
||
|
void remove(const qint64 &id, const QString &classname);
|
||
| src/entityinspector.cpp | ||
|---|---|---|
|
*/
|
||
|
#include "entityinspector.h"
|
||
|
#include <QDir>
|
||
|
#include <QDebug>
|
||
|
#include <QDateTime>
|
||
|
using namespace CuteEntityManager;
|
||
|
|
||
| ... | ... | |
|
if (typeName.contains("QSharedPointer") && !relations.contains(i.key())) {
|
||
|
ok = false;
|
||
|
msg += "No relation defined for attribute " + i.key() + "!\n";
|
||
|
|
||
|
} else if (typeName.contains("QPointer")) {
|
||
|
ok = false;
|
||
|
msg += i.key() + " must use QSharedPointer.\n";
|
||
| ... | ... | |
|
&& foreign.getType() != RelationType::ONE_TO_ONE) {
|
||
|
ok = false;
|
||
|
this->logRelationTypeErrorMsg("ONE_TO_ONE", r, foreign);
|
||
|
|
||
|
} else if (r.getType() == RelationType::MANY_TO_MANY
|
||
|
&& foreign.getType() != RelationType::MANY_TO_MANY) {
|
||
|
this->logRelationTypeErrorMsg("MANY_TO_MANY", r, foreign);
|
||
| ... | ... | |
|
" for primary key not exists. Please check your getPrimaryKey() method!\n",
|
||
|
MsgType::CRITICAL);
|
||
|
}
|
||
|
if(metaprops.size() <= 1) {
|
||
|
ok = false;
|
||
|
this->logger->logMsg("Entity has only one attribute. Please add attributes. Otherwise you can run into problems.",
|
||
|
MsgType::CRITICAL);
|
||
|
}
|
||
|
return ok;
|
||
|
}
|
||
|
|
||
| src/entitymanager.cpp | ||
|---|---|---|
|
return this->schema->getQueryBuilder();
|
||
|
}
|
||
|
|
||
|
void EntityManager::clearCache() {
|
||
|
this->cache.clear();
|
||
|
}
|
||
|
|
||
|
bool EntityManager::saveObject(QSharedPointer<Entity> &entity,
|
||
|
QList<Entity *> &mergedObjects, const bool persistRelations,
|
||
|
const bool ignoreHasChanged, const bool validate,
|
||
| ... | ... | |
|
QString className = attr->getRelatedClass()->className();
|
||
|
QSharedPointer<Entity> ptr = QSharedPointer<Entity>();
|
||
|
if (!(this->cache.contains(convertedId, className)
|
||
|
&& (ptr = this->cache.get(convertedId, className)))) {
|
||
|
&& (ptr = this->cache.get(convertedId, className)))) {
|
||
|
ptr = this->findById(convertedId, className);
|
||
|
}
|
||
|
EntityHelper::setProperty(entity, ptr, attr->getMetaProperty());
|
||
| ... | ... | |
|
auto e = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(attr));
|
||
|
if (e) {
|
||
|
Query query = this->schema->getQueryBuilder()->oneToMany(
|
||
|
attr->getRelatedTable(),
|
||
|
attr->getRelatedColumnName(), entity->getId());
|
||
|
attr->getRelatedTable(),
|
||
|
attr->getRelatedColumnName(), entity->getId());
|
||
|
QSqlQuery q = this->queryInterpreter->build(query);
|
||
|
auto listMap = this->convertQueryResult(q);
|
||
|
auto entities = this->convert(listMap, EntityHelper::getClassname(e.data()));
|
||
| ... | ... | |
|
for (int var = 0; var < listMap.size(); ++var) {
|
||
|
auto id = listMap.at(var).value(attr->getRelatedColumnName());
|
||
|
if (!(this->cache.contains(id.toLongLong(), secClassName) &&
|
||
|
(e = this->cache.get(id.toLongLong(), secClassName)))) {
|
||
|
(e = this->cache.get(id.toLongLong(), secClassName)))) {
|
||
|
e = this->findById(id.toLongLong(), secClassName);
|
||
|
}
|
||
|
if (e) {
|
||
| ... | ... | |
|
}
|
||
|
}
|
||
|
|
||
|
QList<QSharedPointer<Entity>> EntityManager::findEntityByAttributes(const QSharedPointer<Entity> &entity,
|
||
|
QList<QSharedPointer<Entity>> EntityManager::findEntityByAttributes(
|
||
|
const QSharedPointer<Entity> &entity,
|
||
|
bool ignoreID, const bool resolveRelations) {
|
||
|
auto maps = this->findAllByAttributes(entity, ignoreID);
|
||
|
return this->convert(maps, EntityHelper::getClassname(entity.data()),resolveRelations);
|
||
|
return this->convert(maps, EntityHelper::getClassname(entity.data()), resolveRelations);
|
||
|
}
|
||
|
|
||
|
QHash<QString, QVariant> EntityManager::findByPk(qint64 id,
|
||
| ... | ... | |
|
const char *classname, const bool resolveRelations) {
|
||
|
auto list = QList<QSharedPointer<Entity>>();
|
||
|
for (int var = 0; var < maps.size(); ++var) {
|
||
|
auto ptr = this->convert(maps.at(var), classname,resolveRelations);
|
||
|
auto ptr = this->convert(maps.at(var), classname, resolveRelations);
|
||
|
list.append(ptr);
|
||
|
}
|
||
|
return list;
|
||
| src/entitymanager.h | ||
|---|---|---|
|
static EntityManager *getDefaultInstance();
|
||
|
static EntityManager *getInstance(QString name);
|
||
|
QSharedPointer<QueryBuilder> getQueryBuilder() const;
|
||
|
|
||
|
/**
|
||
|
* @brief clearCache
|
||
|
* don't use this function. It's only for test purposes.
|
||
|
*/
|
||
|
void clearCache();
|
||
|
template<class T> QList<QSharedPointer<T>> find(Query &q,
|
||
|
const bool joinBaseClasses = false, const bool resolveRelations = true) {
|
||
|
QSharedPointer<Entity> ptr = QSharedPointer<Entity>
|
||
| src/querybuilder.cpp | ||
|---|---|---|
|
q.appendWhere(Expression(this->where(m, this->andKeyword(), ignoreID, "id", false), m));
|
||
|
q.setLimit(limit);
|
||
|
q.setOffset(offset);
|
||
|
// QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
|
||
|
// tableName)) + this->where(m, this->andKeyword(), ignoreID) + this->limit(limit,
|
||
|
// offset));
|
||
|
// this->bindValues(m, q, ignoreID);
|
||
|
return q;
|
||
|
}
|
||
|
|
||
| tests/em/tst_querybuilder.cpp | ||
|---|---|---|
|
}
|
||
|
|
||
|
void QuerybuilderTest::testRefresh() {
|
||
|
e->clearCache();
|
||
|
auto persons = e->findAll<Person>(false);
|
||
|
QCOMPARE(persons.first()->getGroups().size(), 0);
|
||
|
QHash<QString, QVariant> attributes;
|
||
|
attributes["name"] = QString("Group Health");
|
||
|
QSharedPointer<Group> g = this->e->findEntityByAttributes<Group>(attributes, false, false);
|
||
|
QSharedPointer<Group> g = this->e->findEntityByAttributes<Group>(attributes, false,
|
||
|
false);
|
||
|
QCOMPARE(g->getPersons().count(), 0);
|
||
|
auto entity = g.objectCast<Entity>();
|
||
|
e->refresh(entity, true);
|
||
Auch abrufbar als: Unified diff
closes #614