Revision 612083d3
Von Christian Ehringfeld vor etwa 10 Jahren hinzugefügt
| src/entity.cpp | ||
|---|---|---|
|
return r;
|
||
|
}
|
||
|
|
||
|
Entity *Entity::copy() const {
|
||
|
Entity* Entity::copy() const {
|
||
|
return EntityHelper::copyObject(this);
|
||
|
}
|
||
|
|
||
| src/entity.h | ||
|---|---|---|
|
Q_OBJECT
|
||
|
Q_PROPERTY(qint64 id READ getId WRITE setId NOTIFY idChanged)
|
||
|
|
||
|
signals:
|
||
|
signals:
|
||
|
void idChanged();
|
||
|
|
||
|
public:
|
||
|
public:
|
||
|
virtual QString toString() const;
|
||
|
/**
|
||
|
* @brief copy
|
||
| ... | ... | |
|
QString getErrorsAsString() const;
|
||
|
void setErrors(const QList<ErrorMsg> &value);
|
||
|
|
||
|
protected:
|
||
|
protected:
|
||
|
explicit Entity (QObject *parent = 0);
|
||
|
virtual QString slimToString() const;
|
||
|
QList<ErrorMsg> errors;
|
||
| 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>();
|
||
|
auto relations = entity->getRelations();
|
||
|
for (int var = 0; var < entity->metaObject()->propertyCount(); ++var) {
|
||
| ... | ... | |
|
return h;
|
||
|
}
|
||
|
|
||
|
Entity *EntityHelper::copyObject(const Entity *entity)
|
||
|
{
|
||
|
Entity* EntityHelper::copyObject(const Entity *entity) {
|
||
|
auto metaObject = entity->metaObject();
|
||
|
auto newInstance = EntityInstanceFactory::createInstance(metaObject);
|
||
|
if(newInstance) {
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
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());
|
||
|
if (!var.isNull() && var.canConvert<QList<QVariant>>()) {
|
||
|
auto list = EntityInstanceFactory::castQVariantList(var);
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
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);
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
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();
|
||
| src/entityhelper.h | ||
|---|---|---|
|
#include <QString>
|
||
|
#include <QHash>
|
||
|
#include <QMetaProperty>
|
||
|
#include "entity.h"
|
||
|
#include "entityinstancefactory.h"
|
||
|
#include "entity.h"
|
||
|
namespace CuteEntityManager {
|
||
|
|
||
|
class Relation;
|
||
| ... | ... | |
|
const Entity *entity);
|
||
|
static const QHash<Relation, QMetaProperty> getRelationProperties(
|
||
|
const Entity *entity);
|
||
|
static Entity* copyObject(const Entity*entity);
|
||
|
static Entity* copyObject(const Entity *entity);
|
||
|
|
||
|
|
||
|
static const char *getClassname(const Entity *entity);
|
||
| src/entitymanager.h | ||
|---|---|---|
|
class QueryInterpreter;
|
||
|
class EntityManager : public QObject {
|
||
|
Q_OBJECT
|
||
|
public slots:
|
||
|
public slots:
|
||
|
/**
|
||
|
* @brief startup
|
||
|
* @param version must be unique
|
||
| ... | ... | |
|
bool executeQuery(const QString &query);
|
||
|
QSharedPointer<Entity> findById(const qint64 &id, const QString &classname);
|
||
|
QList<QSharedPointer<Entity>> findEntityByAttributes(const
|
||
|
QSharedPointer<Entity> &entity,
|
||
|
bool ignoreID = false);
|
||
|
QSharedPointer<Entity> &entity,
|
||
|
bool ignoreID = false);
|
||
|
qint64 findId(QSharedPointer<Entity> &entity);
|
||
|
/**
|
||
|
* @todo should be an insert statement with many values
|
||
| ... | ... | |
|
*/
|
||
|
bool hasChanged(QSharedPointer<Entity> &entity);
|
||
|
|
||
|
public:
|
||
|
public:
|
||
|
EntityManager(QSqlDatabase database, bool logQueries = false,
|
||
|
const bool inspectEntities = INSPECTENTITIES,
|
||
|
MsgType logActions = DEFAULTMSGTYPE);
|
||
| ... | ... | |
|
QSharedPointer<QueryBuilder> getQueryBuilder() const;
|
||
|
|
||
|
template<class T> QList<QSharedPointer<T>> find(Query &q,
|
||
|
const bool joinBaseClasses = false, const bool resolveRelations = true) {
|
||
|
const bool joinBaseClasses = false, const bool resolveRelations = true) {
|
||
|
QSharedPointer<Entity> ptr = QSharedPointer<Entity>
|
||
|
(EntityInstanceFactory::createInstance<T *>());
|
||
|
(EntityInstanceFactory::createInstance<T *>());
|
||
|
if (ptr) {
|
||
|
if (q.getFrom().isEmpty()) {
|
||
|
q.setFrom(QStringList(ptr->getTablename()));
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
template<class T> QList<QSharedPointer<T>> findAll(const bool resolveRelations =
|
||
|
true) {
|
||
|
true) {
|
||
|
QSharedPointer<Entity> ptr = QSharedPointer<Entity>
|
||
|
(EntityInstanceFactory::createInstance<T *>());
|
||
|
(EntityInstanceFactory::createInstance<T *>());
|
||
|
if (ptr) {
|
||
|
auto maps = this->findAll(ptr);
|
||
|
auto converted = this->convert(maps, EntityHelper::getClassname(ptr.data()),
|
||
| ... | ... | |
|
|
||
|
template<class T>
|
||
|
QSharedPointer<T> findEntityByAttributes(
|
||
|
const QHash<QString, QVariant>
|
||
|
&attributes, const bool joinBaseClasses = false,
|
||
|
const bool resolveRelations = true) {
|
||
|
const QHash<QString, QVariant>
|
||
|
&attributes, const bool joinBaseClasses = false,
|
||
|
const bool resolveRelations = true) {
|
||
|
auto list = this->findAllEntitiesByAttributes<T>(attributes, 1, 0,
|
||
|
joinBaseClasses, resolveRelations);
|
||
|
joinBaseClasses, resolveRelations);
|
||
|
if (list.isEmpty()) {
|
||
|
return QSharedPointer<T>();
|
||
|
}
|
||
| ... | ... | |
|
|
||
|
template<class T> QList<QSharedPointer<T>> findAllEntitiesByAttributes(
|
||
|
const QHash<QString, QVariant> &attributes =
|
||
|
QHash<QString, QString>(), quint64 limit = 0, quint64 offset = 0,
|
||
|
bool joinBaseClasses = false, const bool resolveRelations = true) {
|
||
|
QHash<QString, QString>(), quint64 limit = 0, quint64 offset = 0,
|
||
|
bool joinBaseClasses = false, const bool resolveRelations = true) {
|
||
|
QSharedPointer<Entity> e = QSharedPointer<Entity>
|
||
|
(EntityInstanceFactory::createInstance<T *>());
|
||
|
(EntityInstanceFactory::createInstance<T *>());
|
||
|
if (e) {
|
||
|
Query query = Query(QStringList(e->getTablename()));
|
||
|
if (joinBaseClasses) {
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
template<class T> QList<QSharedPointer<T>> findEntitiesBySql(
|
||
|
const QString &sql) {
|
||
|
const QString &sql) {
|
||
|
QSharedPointer<T> e = EntityInstanceFactory::createInstance<T *>();
|
||
|
if (e) {
|
||
|
QSqlQuery q = this->schema->getDatabase()->getQuery(sql);
|
||
| ... | ... | |
|
return false;
|
||
|
}
|
||
|
|
||
|
template<class T> QList<QSharedPointer<T>> convertList(const
|
||
|
QList<QSharedPointer<Entity>> &list) {
|
||
|
template<class T, class X>
|
||
|
QList<QSharedPointer<T>> convertList(const QList<QSharedPointer<X>> &list) {
|
||
|
QList<QSharedPointer<T>> newList = QList<QSharedPointer<T>>();
|
||
|
for (int i = 0; i < list.size(); ++i) {
|
||
|
newList.append(list.at(i).objectCast<T>());
|
||
|
newList.append(qSharedPointerObjectCast<T>(list.at(i)));
|
||
|
}
|
||
|
return newList;
|
||
|
}
|
||
|
|
||
|
template<class T> QList<QSharedPointer<Entity>> convertToEntityList(
|
||
|
const QList<QSharedPointer<T>> &list) {
|
||
|
QList<QSharedPointer<Entity>> newList = QList<QSharedPointer<Entity>>();
|
||
|
for (int i = 0; i < list.size(); ++i) {
|
||
|
QSharedPointer<T> entity = list.at(i);
|
||
|
newList.append(entity);
|
||
|
}
|
||
|
return newList;
|
||
|
}
|
||
|
|
||
|
protected:
|
||
|
protected:
|
||
|
bool saveObject(QSharedPointer<Entity> &entity, QList<Entity *> &mergedObjects,
|
||
|
const bool persistRelations = true,
|
||
|
const bool ignoreHasChanged = false, const bool validate = true,
|
||
| ... | ... | |
|
QVariant &property, QList<Entity *> &mergedObjects,
|
||
|
const bool ignoreHasChanged = false, const bool newItem = false);
|
||
|
QList<QHash<QString, QVariant> > findAllByAttributes(const
|
||
|
QSharedPointer<Entity> &entity,
|
||
|
bool ignoreID = false);
|
||
|
QSharedPointer<Entity> &entity,
|
||
|
bool ignoreID = false);
|
||
|
QList<QHash<QString, QVariant> > findAllByAttributes(const
|
||
|
QHash<QString, QVariant> &m,
|
||
|
const QString &tblname,
|
||
|
bool ignoreID = false);
|
||
|
QHash<QString, QVariant> &m,
|
||
|
const QString &tblname,
|
||
|
bool ignoreID = false);
|
||
|
QSharedPointer<Entity> findById(const qint64 &id, QSharedPointer<Entity> &e,
|
||
|
const bool refresh = false);
|
||
|
/**
|
||
| ... | ... | |
|
bool newItem = false);
|
||
|
|
||
|
QList<QSharedPointer<Entity>> saveRelationEntities(const
|
||
|
QList<QSharedPointer<Entity>> &list, const Relation &r,
|
||
|
QList<Entity *> &mergedObjects);
|
||
|
QList<QSharedPointer<Entity>> &list, const Relation &r,
|
||
|
QList<Entity *> &mergedObjects);
|
||
|
/**
|
||
|
* @brief EntityManager::persistManyToMany
|
||
|
* @param entity
|
||
| ... | ... | |
|
QString generateObjectName();
|
||
|
void appendToInstanceList();
|
||
|
|
||
|
private:
|
||
|
private:
|
||
|
static QStringList connectionNames;
|
||
|
static QHash<QString, EntityManager *> instances;
|
||
|
QSharedPointer<Logger> logger;
|
||
Auch abrufbar als: Unified diff
more dynamic EntityManager::convertList