Revision 47d95679
Von Christian Ehringfeld vor mehr als 7 Jahren hinzugefügt
src/entity.cpp | ||
---|---|---|
this->errors = value;
|
||
}
|
||
|
||
void Entity::setProperty(const QSharedPointer<Entity> &e, QSharedPointer<Entity> value, const QMetaProperty &property) {
|
||
property.write(e.data(), QVariant::fromValue(value));
|
||
}
|
||
|
||
Entity::~Entity() {
|
||
}
|
||
|
src/entity.h | ||
---|---|---|
#define EM_MACRO(type) \
|
||
virtual void setListProperty(const QSharedPointer<Entity> &e,QList<QSharedPointer<Entity>> &entList, const QMetaProperty &property) override { \
|
||
QList<QSharedPointer<type>> list = *reinterpret_cast<QList<QSharedPointer<type>>*>(&entList); \
|
||
QVariant var; \
|
||
var.setValue<QList<QSharedPointer<type>>>(list); \
|
||
property.write(e.data(), var); \
|
||
} \
|
||
virtual void setProperty(const QSharedPointer<Entity> &e,QSharedPointer<Entity> &value, const QMetaProperty &property) override { \
|
||
QSharedPointer<type> en = *reinterpret_cast<QSharedPointer<type>*>(&value); \
|
||
QVariant var; \
|
||
var.setValue<QSharedPointer<type>>(en); \
|
||
property.write(e.data(), var); \
|
||
property.write(e.data(), QVariant::fromValue(list)); \
|
||
}
|
||
|
||
public:
|
||
... | ... | |
void setErrors(const QList<ErrorMsg> &value);
|
||
virtual void setListProperty(const QSharedPointer<Entity> &e, QList<QSharedPointer<Entity>> &entList,
|
||
const QMetaProperty &property) = 0;
|
||
virtual void setProperty(const QSharedPointer<Entity> &e, QSharedPointer<Entity> &value, const QMetaProperty &property) = 0;
|
||
virtual void setProperty(const QSharedPointer<Entity> &e, QSharedPointer<Entity> value, const QMetaProperty &property);
|
||
QString internalRelationName(const QString &name);
|
||
|
||
|
||
protected:
|
||
explicit Entity (QObject *parent = 0);
|
src/entityhelper.cpp | ||
---|---|---|
}
|
||
|
||
const QHash<QString, Relation> EntityHelper::getNonInheritedRelations(
|
||
const Entity *entity) {
|
||
const Entity *entity) {
|
||
auto relations = entity->getRelations();
|
||
auto superObject = EntityInstanceFactory::newSuperClassInstance(entity);
|
||
if (superObject) {
|
||
... | ... | |
}
|
||
|
||
const QList<const QMetaObject *> EntityHelper::superClasses(
|
||
const Entity *entity, bool
|
||
stopAtSingleTableInheritance) {
|
||
const Entity *entity, bool
|
||
stopAtSingleTableInheritance) {
|
||
QList<const QMetaObject *> classes = QList<const QMetaObject *>();
|
||
auto superMetaObject = entity->metaObject()->superClass();
|
||
if (entity->getInheritanceStrategy() == InheritanceStrategy::JOINED_TABLE) {
|
||
Entity *e = nullptr;
|
||
while (superMetaObject && QString(superMetaObject->className()) !=
|
||
QString("CuteEntityManager::Entity")) {
|
||
QString("CuteEntityManager::Entity")) {
|
||
e = EntityInstanceFactory::createInstance(superMetaObject->className());
|
||
if (e) {
|
||
classes.append(superMetaObject);
|
||
... | ... | |
}
|
||
|
||
const QHash<QString, QMetaProperty> EntityHelper::getMetaProperties(
|
||
const Entity *entity) {
|
||
const Entity *entity) {
|
||
return EntityHelper::getMetaProperties(entity->metaObject());
|
||
}
|
||
|
||
const QHash<QString, QMetaProperty> EntityHelper::getSuperMetaProperties(
|
||
const Entity *entity) {
|
||
const Entity *entity) {
|
||
auto superMetaObjectPropertyMap = QHash<QString, QMetaProperty>();
|
||
auto superMeta = entity->metaObject()->superClass();
|
||
if (QString(superMeta->className()) != QString("CuteEntityManager::Entity")
|
||
... | ... | |
}
|
||
|
||
const QHash<QString, QMetaProperty> EntityHelper::getMetaProperties(
|
||
const QMetaObject *object) {
|
||
const QMetaObject *object) {
|
||
auto h = QHash<QString, QMetaProperty>();
|
||
for (int var = 0; var < object->propertyCount(); ++var) {
|
||
QMetaProperty m = object->property(var);
|
||
... | ... | |
}
|
||
|
||
const QHash<QString, QMetaProperty> EntityHelper::getNonInheritedMetaProperties(
|
||
const Entity *entity) {
|
||
const Entity *entity) {
|
||
auto props = EntityHelper::getMetaProperties(entity);
|
||
auto superObject = EntityInstanceFactory::newSuperClassInstance(entity);
|
||
if (superObject) {
|
||
auto superProps = EntityHelper::getMetaProperties(superObject);
|
||
for (auto iterator = superProps.constBegin(); iterator != superProps.constEnd();
|
||
++iterator) {
|
||
++iterator) {
|
||
if (props.contains(iterator.key())) {
|
||
props.remove(iterator.key());
|
||
}
|
||
... | ... | |
}
|
||
|
||
const QHash<QString, QMetaProperty> EntityHelper::getInheritedMetaProperties(
|
||
const Entity *entity) {
|
||
const Entity *entity) {
|
||
auto props = EntityHelper::getMetaProperties(entity);
|
||
auto superObject = EntityInstanceFactory::newSuperClassInstance(entity);
|
||
auto wholeProperties = QHash<QString, QMetaProperty>();
|
||
... | ... | |
}
|
||
|
||
const QHash<Relation, QMetaProperty> EntityHelper::getRelationProperties(
|
||
const Entity *entity) {
|
||
const Entity *entity) {
|
||
auto h = QHash<Relation, QMetaProperty>();
|
||
if(entity) {
|
||
auto relations = entity->getRelations();
|
||
... | ... | |
}
|
||
|
||
Entity *EntityHelper::getBaseClassObject(const QSharedPointer<Entity> &entity,
|
||
QString attributeName) {
|
||
QString attributeName) {
|
||
auto superObject = EntityInstanceFactory::createInstance(entity->metaObject());
|
||
auto objectBefore = superObject;
|
||
bool first = true;
|
||
... | ... | |
}
|
||
|
||
void EntityHelper::addEntityToListProperty(const QSharedPointer<Entity>
|
||
&entity, QSharedPointer<Entity> add, const QMetaProperty &property) {
|
||
&entity, QSharedPointer<Entity> add, const QMetaProperty &property) {
|
||
QVariant var = property.read(entity.data());
|
||
QList<QSharedPointer<Entity>> list = (!var.isNull() && var.data() &&
|
||
var.canConvert<QList<QVariant>>() ? EntityInstanceFactory::castQVariantList(
|
||
var) : QList<QSharedPointer<Entity>>());
|
||
var) : QList<QSharedPointer<Entity>>());
|
||
if (!list.contains(add)) {
|
||
list.append(add);
|
||
EntityHelper::setListProperty(entity, list, property);
|
||
... | ... | |
}
|
||
|
||
void EntityHelper::removeEntityFromListProperty(const QSharedPointer<Entity>
|
||
&entity, QSharedPointer<Entity> remove, const QMetaProperty &property) {
|
||
&entity, QSharedPointer<Entity> remove, const QMetaProperty &property) {
|
||
QVariant var = property.read(entity.data());
|
||
if (!var.isNull() && var.canConvert<QList<QVariant>>()) {
|
||
auto list = EntityInstanceFactory::castQVariantList(var);
|
||
... | ... | |
}
|
||
|
||
void EntityHelper::clearEntityListProperty(const QSharedPointer<Entity> &entity,
|
||
const QMetaProperty &property) {
|
||
const QMetaProperty &property) {
|
||
QVariant var = property.read(entity.data());
|
||
if (var.canConvert<QList<QVariant>>()) {
|
||
auto list = EntityInstanceFactory::castQVariantList(var);
|
||
... | ... | |
> -1) {
|
||
auto i = EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(property.typeName()));
|
||
if(i) {
|
||
i->setProperty(entity, value, property);
|
||
delete i;
|
||
entity->setProperty(property.name() + EntityHelper::internalString(), value->getId());
|
||
i->setProperty(entity, value, property);
|
||
delete i;
|
||
}
|
||
}
|
||
}
|
||
... | ... | |
QList<QSharedPointer<Entity>> &value, const QMetaProperty &property) {
|
||
auto i = EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(property.typeName()));
|
||
if(i) {
|
||
i->setListProperty(entity, value, property);
|
||
delete i;
|
||
entity->setProperty(property.name()+EntityHelper::internalString(),EntityHelper::getIdsAsVariant(value));
|
||
i->setListProperty(entity, value, property);
|
||
delete i;
|
||
}
|
||
}
|
||
|
||
|
||
QMetaProperty EntityHelper::mappedProperty(const Relation &r,
|
||
const QSharedPointer<Entity> &foreignEntity) {
|
||
const QSharedPointer<Entity> &foreignEntity) {
|
||
QMetaProperty prop;
|
||
auto props = EntityHelper::getMetaProperties(foreignEntity.data());
|
||
if (!r.getMappedBy().isEmpty() && props.contains(r.getMappedBy())) {
|
||
... | ... | |
} else {
|
||
auto relations = foreignEntity->getRelations();
|
||
for (auto iterator = relations.constBegin(); iterator != relations.constEnd();
|
||
++iterator) {
|
||
++iterator) {
|
||
auto rel = iterator.value();
|
||
if (rel.getMappedBy() == r.getPropertyName()) {
|
||
prop = props.value(rel.getPropertyName());
|
||
... | ... | |
}
|
||
|
||
QHash<QString, QVariant> EntityHelper::getEntityAttributes(
|
||
const QHash<QString, QMetaProperty>
|
||
&props,
|
||
const QSharedPointer<Entity> &entity) {
|
||
const QHash<QString, QMetaProperty>
|
||
&props,
|
||
const QSharedPointer<Entity> &entity) {
|
||
auto map = QHash<QString, QVariant>();
|
||
auto transientAttrs = entity->getTransientAttributes();
|
||
auto relations = entity->getRelations();
|
||
... | ... | |
}
|
||
return map;
|
||
}
|
||
|
||
const QString EntityHelper::internalString() {
|
||
return QString("_em_internal");
|
||
}
|
||
|
||
const QList<qint64> EntityHelper::getIds(QList<QSharedPointer<Entity> > entities) {
|
||
QList<qint64> l = QList<qint64>();
|
||
for(int i=0; i<entities.size(); ++i) {
|
||
auto e = entities.at(i);
|
||
l.append(e->getId());
|
||
}
|
||
return l;
|
||
}
|
||
|
||
const QVariant EntityHelper::getIdsAsVariant(QList<QSharedPointer<Entity> > entities) {
|
||
QVariant var;
|
||
var.setValue<QList<qint64>>(EntityHelper::getIds(entities));
|
||
return var;
|
||
}
|
src/entityhelper.h | ||
---|---|---|
static QHash<QString, QVariant> getEntityAttributes(const
|
||
QHash<QString, QMetaProperty> &props,
|
||
const QSharedPointer<Entity> &entity);
|
||
static const QString internalString();
|
||
static const QList<qint64> getIds(QList<QSharedPointer<Entity>> entities);
|
||
static const QVariant getIdsAsVariant(QList<QSharedPointer<Entity>> entities);
|
||
|
||
};
|
||
}
|
||
|
src/entityinspector.cpp | ||
---|---|---|
|
||
bool EntityInspector::checkRegisteredEntities() {
|
||
QStringList classes = EntityInstanceFactory::getRegisteredClasses();
|
||
classes.sort();
|
||
QString msg = QDateTime::currentDateTime().toString(Qt::ISODate) +
|
||
" - Start checking entities\n";
|
||
this->logger->logMsg(msg, MsgType::INFO);
|
src/entityinstancefactory.cpp | ||
---|---|---|
|
||
QHash<QByteArray, EntityInstanceFactory::Constructor>
|
||
EntityInstanceFactory::instance =
|
||
QHash<QByteArray, EntityInstanceFactory::Constructor>();
|
||
QHash<QByteArray, EntityInstanceFactory::Constructor>();
|
||
|
||
EntityInstanceFactory::EntityInstanceFactory() {
|
||
}
|
||
... | ... | |
s.append("*");
|
||
}
|
||
auto ptr = EntityInstanceFactory::createInstance(QMetaType::type(
|
||
s.toUtf8().constData()));
|
||
s.toUtf8().constData()));
|
||
if (!ptr) {
|
||
s.remove("*");
|
||
ptr = EntityInstanceFactory::createObject(s.toUtf8());
|
||
... | ... | |
if(!e) {
|
||
e = qobject_cast<Entity *>(metaObject->newInstance());
|
||
qDebug() << "Backup method for dynamic object creation was called. Maybe the class " +
|
||
QString(metaObject->className()) + " isn't registered?";
|
||
QString(metaObject->className()) + " isn't registered?";
|
||
}
|
||
} else {
|
||
void *newObj = QMetaType::create(metaTypeId);
|
||
... | ... | |
}
|
||
|
||
Entity *EntityInstanceFactory::createInstance(const char *className,
|
||
const QHash<QString, QVariant> &attributes) {
|
||
const QHash<QString, QVariant> &attributes) {
|
||
Entity *e = EntityInstanceFactory::createInstance(className);
|
||
EntityInstanceFactory::setAttributes(e, attributes);
|
||
return e;
|
||
}
|
||
|
||
void EntityInstanceFactory::setAttributes(Entity *&e,
|
||
const QHash<QString, QVariant> &attributes,
|
||
QHash<QString, QMetaProperty> metaprops) {
|
||
const QHash<QString, QVariant> &attributes,
|
||
QHash<QString, QMetaProperty> metaprops) {
|
||
if (e) {
|
||
auto iterator = attributes.constBegin();
|
||
while (iterator != attributes.constEnd()) {
|
||
... | ... | |
}
|
||
} else {
|
||
qWarning() << prop.name() << "on Entity" << EntityHelper::getClassname(
|
||
e) << "not writeable!";
|
||
e) << "not writeable!";
|
||
}
|
||
} else {
|
||
e->setProperty(iterator.key().toLatin1().data(), iterator.value());
|
||
... | ... | |
}
|
||
|
||
void EntityInstanceFactory::setAttributes(Entity *&e,
|
||
const QHash<QString, QVariant> &attributes) {
|
||
const QHash<QString, QVariant> &attributes) {
|
||
if (!attributes.isEmpty()) {
|
||
auto metaprops = EntityHelper::getMetaProperties(e);
|
||
EntityInstanceFactory::setAttributes(e, attributes, metaprops);
|
||
... | ... | |
}
|
||
|
||
QList<QSharedPointer<Entity>> EntityInstanceFactory::castQVariantList(
|
||
QVariant &list) {
|
||
QVariant &list) {
|
||
return *reinterpret_cast<QList<QSharedPointer<Entity>>*>(list.data());
|
||
}
|
||
|
||
QSharedPointer<Entity> EntityInstanceFactory::castQVariant(
|
||
QVariant &entity) {
|
||
auto e = entity.value<QSharedPointer<Entity>>();
|
||
if(!e) {
|
||
auto ne = *static_cast<QSharedPointer<QObject>*>(entity.data());
|
||
auto entityPtr = ne.objectCast<Entity>();
|
||
if(entityPtr) {
|
||
e = entityPtr;
|
||
}
|
||
}
|
||
return e;
|
||
const QSharedPointer<Entity> EntityInstanceFactory::castQVariant(
|
||
QVariant &entity) {
|
||
|
||
return *static_cast<const QSharedPointer<Entity>*>(entity.constData());
|
||
}
|
||
|
||
QStringList EntityInstanceFactory::getRegisteredClasses() {
|
||
QStringList registered = QStringList();
|
||
for (auto i = EntityInstanceFactory::instance.constBegin();
|
||
i != EntityInstanceFactory::instance.constEnd(); ++i) {
|
||
i != EntityInstanceFactory::instance.constEnd(); ++i) {
|
||
registered.append(i.key());
|
||
}
|
||
return registered;
|
||
... | ... | |
|
||
Entity *EntityInstanceFactory::createInstance(Attribute *&attr) {
|
||
return EntityInstanceFactory::createInstance(
|
||
EntityInstanceFactory::extractEntityType(
|
||
attr->getMetaProperty().typeName()));
|
||
EntityInstanceFactory::extractEntityType(
|
||
attr->getMetaProperty().typeName()));
|
||
}
|
src/entityinstancefactory.h | ||
---|---|---|
static Entity *newSuperClassInstance(const Entity *e);
|
||
static Entity *createInstance(const QMetaObject *object);
|
||
static QList<QSharedPointer<Entity>> castQVariantList(QVariant &list);
|
||
static QSharedPointer<Entity> castQVariant(QVariant &entity);
|
||
static const QSharedPointer<Entity> castQVariant(QVariant &entity);
|
||
static QStringList getRegisteredClasses();
|
||
|
||
template<typename T>
|
src/entitymanager.cpp | ||
---|---|---|
auto iterator = relations.constBegin();
|
||
while (iterator != relations.constEnd()) {
|
||
const Relation r = iterator.key();
|
||
auto var = iterator.value().read(entity.data());
|
||
if(!var.isNull() && var.data()) {
|
||
QVariant var = iterator.value().read(entity.data());
|
||
if(!var.isNull() && var.data() != NULL) {
|
||
if (r.getType() == RelationType::MANY_TO_ONE) {
|
||
auto e = EntityInstanceFactory::castQVariant(var);
|
||
if (e && this->shouldBeSaved(e, r)) {
|
||
this->saveObject(e, mergedObjects, true, ignoreHasChanged);
|
||
entity->setProperty(r.getPropertyName()+EntityHelper::internalString(),e->getId());
|
||
auto fkProp = EntityHelper::mappedProperty(r, e);
|
||
if (fkProp.isValid()) {
|
||
EntityHelper::addEntityToListProperty(e, entity, fkProp);
|
||
... | ... | |
auto e = EntityInstanceFactory::castQVariant(var);
|
||
if(e) {
|
||
this->saveObject(e, mergedObjects, true, ignoreHasChanged);
|
||
entity->setProperty(r.getPropertyName()+EntityHelper::internalString(),e->getId());
|
||
auto prop = EntityHelper::mappedProperty(r, e);
|
||
EntityHelper::setProperty(e, entity, prop);
|
||
}
|
||
... | ... | |
auto relations = EntityHelper::getRelationProperties(entity.data());
|
||
for (auto i = relations.constBegin(); i != relations.constEnd(); ++i) {
|
||
const Relation r = i.key();
|
||
auto var = i.value().read(entity.data());
|
||
QVariant var = i.value().read(entity.data());
|
||
if (r.getType() == RelationType::MANY_TO_MANY) {
|
||
this->persistManyToMany(entity, r, var, mergedObjects, ignoreHasChanged,
|
||
newItem);
|
||
... | ... | |
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);
|
||
for (int x = 0; x < list.size(); ++x) {
|
||
auto e = list.at(x);
|
||
if (e && this->shouldBeSaved(e, r)) {
|
||
EntityHelper::setProperty(e, entity, fkProp);
|
||
this->saveObject(e, mergedObjects, true, ignoreHasChanged);
|
||
... | ... | |
if (ok && !list.isEmpty() && list.at(0).data()) {
|
||
auto nList = EntityInstanceFactory::castQVariantList(property);
|
||
this->persistMappedByRelation(nList, q, entity, ptr, r, tblName, mergedObjects);
|
||
entity->setProperty(r.getPropertyName()+EntityHelper::internalString(),EntityHelper::getIdsAsVariant(nList));
|
||
}
|
||
} else {
|
||
this->missingManyToManyTable(tblName, entity, r);
|
src/entitymanager.h | ||
---|---|---|
#include <QStringList>
|
||
#include <QObject>
|
||
#include <QSharedPointer>
|
||
#include <QDebug>
|
||
#include <QtSql/QSqlError>
|
||
#include <QMetaType>
|
||
#include "schema.h"
|
||
... | ... | |
#include "attribute.h"
|
||
namespace CuteEntityManager {
|
||
#ifdef QT_DEBUG
|
||
#include <QDebug>
|
||
#define DEFAULTMSGTYPE MsgType::DEBUG
|
||
#define INSPECTENTITIES true
|
||
#else
|
tests/em/tst_em.cpp | ||
---|---|---|
QVERIFY(containsColumn);
|
||
if(containsColumn) {
|
||
auto column = columns.value(name);
|
||
qDebug() << name << column->type() << type;
|
||
if(type != QVariant::UserType) {
|
||
QCOMPARE(column->type(), type);
|
||
}
|
||
... | ... | |
p3->setGroups(groups);
|
||
QVERIFY(this->e->save(pEnt, true, true));
|
||
this->e->refresh(g);
|
||
qDebug() << g->getPersons().size();
|
||
QVERIFY(g->getPersons().size() == 2);
|
||
auto firstPerson = g->getPersons().first();
|
||
g->removePerson(firstPerson);
|
Auch abrufbar als: Unified diff
bugfix