Revision 4f3b13f3
Von Christian Ehringfeld vor mehr als 10 Jahren hinzugefügt
| src/entitymanager.cpp | ||
|---|---|---|
|
return schema;
|
||
|
}
|
||
|
|
||
|
void EntityManager::refresh(QSharedPointer<Entity> &entity) {
|
||
|
entity = this->findById(entity.data()->getId(),
|
||
|
QString(entity.data()->getClassname()));
|
||
|
}
|
||
|
|
||
|
void EntityManager::setSchema(const QSharedPointer<Schema> &value) {
|
||
|
schema = value;
|
||
|
}
|
||
| ... | ... | |
|
EntityManager::connectionNames.removeOne(name);
|
||
|
}
|
||
|
|
||
|
QSharedPointer<Entity> EntityManager::findById(const qint64 &id, Entity *&e) {
|
||
|
QSharedPointer<Entity> EntityManager::findById(const qint64 &id, Entity *&e,
|
||
|
const bool refresh) {
|
||
|
QSharedPointer<Entity> r;
|
||
|
if (e) {
|
||
|
if ((r = this->cache.get(id, QString(e->getClassname()))) && !r.data()) {
|
||
|
if (refresh || ((r = this->cache.get(id, QString(e->getClassname())))
|
||
|
&& !r.data())) {
|
||
|
auto map = this->findByPk(id, e->getTablename());
|
||
|
r = this->convert(map, e->getClassname());
|
||
|
r = this->convert(map, e->getClassname(), refresh);
|
||
|
}
|
||
|
delete e;
|
||
|
}
|
||
| ... | ... | |
|
const QString &classname) {
|
||
|
Entity *e = EntityInstanceFactory::createInstance(classname);
|
||
|
return this->findById(id, e);
|
||
|
|
||
|
}
|
||
|
|
||
|
QSharedPointer<Entity> EntityManager::convert(const QHash<QString, QVariant>
|
||
|
&map,
|
||
|
const char *classname) {
|
||
|
const char *classname, const bool refresh) {
|
||
|
auto ptr = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(
|
||
|
classname, map));
|
||
|
this->resolveRelations(ptr, map);
|
||
|
this->resolveRelations(ptr, map, refresh);
|
||
|
this->cache.insert(ptr);
|
||
|
return ptr;
|
||
|
}
|
||
|
|
||
|
QList<QSharedPointer<Entity> > EntityManager::convert(
|
||
|
QList<QHash<QString, QVariant> > maps,
|
||
|
const char *classname) {
|
||
|
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);
|
||
|
auto ptr = this->convert(maps.at(var), classname, refresh);
|
||
|
list.append(ptr);
|
||
|
this->cache.insert(ptr);
|
||
|
}
|
||
| ... | ... | |
|
|
||
|
void EntityManager::manyToOne(const QSharedPointer<Entity> &entity,
|
||
|
const QVariant &id,
|
||
|
const QMetaProperty &property) {
|
||
|
const QMetaProperty &property, const bool refresh) {
|
||
|
qint64 convertedId = -1;
|
||
|
bool ok = false;
|
||
|
if ((convertedId == id.toLongLong(&ok)) && ok && convertedId > -1) {
|
||
|
QString className = EntityInstanceFactory::extractEntityType(
|
||
|
property.typeName());
|
||
|
QSharedPointer<Entity> ptr = QSharedPointer<Entity>();
|
||
|
if (!(this->cache.contains(convertedId, className)
|
||
|
&& (ptr = this->cache.get(convertedId, className)) && ptr.data())) {
|
||
|
if (refresh || !(this->cache.contains(convertedId, className)
|
||
|
&& (ptr = this->cache.get(convertedId, className)) && ptr.data())) {
|
||
|
ptr = this->findById(convertedId, className);
|
||
|
}
|
||
|
this->setProperty(entity, ptr, property);
|
||
| ... | ... | |
|
|
||
|
void EntityManager::oneToMany(const QSharedPointer<Entity> &entity,
|
||
|
const Relation &r,
|
||
|
const QMetaProperty &property) {
|
||
|
const QMetaProperty &property, const bool refresh) {
|
||
|
if (entity.data() && entity.data()->getId() > -1) {
|
||
|
Entity *e = EntityInstanceFactory::createInstance(
|
||
|
EntityInstanceFactory::extractEntityType(
|
||
| ... | ... | |
|
|
||
|
void EntityManager::oneToOne(const QSharedPointer<Entity> &entity,
|
||
|
const Relation &r,
|
||
|
const QMetaProperty &property,
|
||
|
const QMetaProperty &property, const bool refresh,
|
||
|
const QVariant &id) {
|
||
|
if (r.getMappedBy().isEmpty()) {
|
||
|
this->manyToOne(entity, id, property);
|
||
| ... | ... | |
|
}
|
||
|
}
|
||
|
|
||
|
const bool EntityManager::canPersistRelation(const Relation &relation,
|
||
|
const RelationType &r, const QVariant &var) const {
|
||
|
return relation.getType() == r
|
||
|
&& var.canConvert<QList<QSharedPointer<Entity>>>();
|
||
|
}
|
||
|
|
||
|
|
||
|
void EntityManager::setListProperty(const QSharedPointer<Entity> &entity,
|
||
|
QList<QSharedPointer<Entity> > &list,
|
||
| ... | ... | |
|
}
|
||
|
}
|
||
|
|
||
|
void EntityManager::saveRelations(const QSharedPointer<Entity> &entity) {
|
||
|
auto relations = entity.data()->getRelationProperties();
|
||
|
auto iterator = relations.constBegin();
|
||
|
while (iterator != relations.constEnd()) {
|
||
|
const Relation r = iterator.key();
|
||
|
/**
|
||
|
* @TODO cascade types
|
||
|
*/
|
||
|
auto var = iterator.value().read(entity.data());
|
||
|
if (this->canPersistRelation(r, MANY_TO_MANY, var)) {
|
||
|
/**
|
||
|
@TODO
|
||
|
*/
|
||
|
} else if (this->canPersistRelation(r, ONE_TO_MANY, var)) {
|
||
|
QList<QSharedPointer<Entity>> list =
|
||
|
qvariant_cast<QList<QSharedPointer<Entity>>>(var);
|
||
|
for (int var = 0; var < list.size(); ++var) {
|
||
|
auto entity = list.at(var);
|
||
|
this->save(entity);
|
||
|
}
|
||
|
} else if (r.getType() == ONE_TO_ONE && !r.getMappedBy().isEmpty()
|
||
|
&& var.canConvert<QSharedPointer<Entity>>()) {
|
||
|
auto entity = qvariant_cast<QSharedPointer<Entity>>(var);
|
||
|
this->save(entity);
|
||
|
}
|
||
|
++iterator;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void EntityManager::manyToMany(const QSharedPointer<Entity> &entity,
|
||
|
const Relation &r,
|
||
|
const QMetaProperty &property) {
|
||
|
const QMetaProperty &property, const bool refresh) {
|
||
|
Entity *secEntity = EntityInstanceFactory::createInstance(
|
||
|
EntityInstanceFactory::extractEntityType(
|
||
|
QString(
|
||
| ... | ... | |
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
QList<QHash<QString, QVariant> > EntityManager::convertQueryResult(
|
||
|
QSqlQuery &q) {
|
||
|
QList<QHash <QString, QVariant> > listmap = QList<QHash <QString, QVariant> >();
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
void EntityManager::resolveRelations(const QSharedPointer<Entity> &entity,
|
||
|
const QHash<QString, QVariant> &map) {
|
||
|
const QHash<QString, QVariant> &map, const bool refresh) {
|
||
|
auto props = entity.data()->getRelationProperties();
|
||
|
auto iterator = props.constBegin();
|
||
|
while (iterator != props.constEnd()) {
|
||
| ... | ... | |
|
switch (r.getType()) {
|
||
|
case MANY_TO_ONE:
|
||
|
if (map.contains(r.getPropertyName()) + "_id") {
|
||
|
this->manyToOne(entity, property.read(entity.data()), property);
|
||
|
this->manyToOne(entity, property.read(entity.data()), property, refresh);
|
||
|
}
|
||
|
break;
|
||
|
case MANY_TO_MANY:
|
||
|
this->manyToMany(entity, r, property);
|
||
|
this->manyToMany(entity, r, property, refresh);
|
||
|
break;
|
||
|
case ONE_TO_MANY:
|
||
|
this->oneToMany(entity, r, property);
|
||
|
this->oneToMany(entity, r, property, refresh);
|
||
|
break;
|
||
|
case ONE_TO_ONE:
|
||
|
this->oneToOne(entity, r, property, property.read(entity.data()));
|
||
|
this->oneToOne(entity, r, property, refresh, property.read(entity.data()));
|
||
|
break;
|
||
|
}
|
||
|
++iterator;
|
||
| src/entitymanager.h | ||
|---|---|---|
|
void init();
|
||
|
QList<QHash<QString, QVariant> > findAll(QString tblname);
|
||
|
void resolveRelations(const QSharedPointer<Entity> &entity,
|
||
|
const QHash<QString, QVariant> &map);
|
||
|
const QHash<QString, QVariant> &map, const bool refresh = false);
|
||
|
QHash<QString, QVariant> findByPk(qint64 id, QString tblname);
|
||
|
QSharedPointer<Entity> convert(const QHash<QString, QVariant> &map,
|
||
|
const char *classname);
|
||
|
const char *classname, const bool refresh = false);
|
||
|
QList<QSharedPointer<Entity>> convert(QList<QHash<QString, QVariant> > maps,
|
||
|
const char *classname);
|
||
|
const char *classname, const bool refresh = false);
|
||
|
void manyToOne(const QSharedPointer<Entity> &entity, const QVariant &id,
|
||
|
const QMetaProperty &property);
|
||
|
const QMetaProperty &property, const bool refresh = false);
|
||
|
void oneToMany(const QSharedPointer<Entity> &entity, const Relation &r,
|
||
|
const QMetaProperty &property);
|
||
|
const QMetaProperty &property,const bool refresh = false);
|
||
|
void manyToMany(const QSharedPointer<Entity> &entity, const Relation &r,
|
||
|
const QMetaProperty &property);
|
||
|
const QMetaProperty &property,const bool refresh = false);
|
||
|
void oneToOne(const QSharedPointer<Entity> &entity, const Relation &r,
|
||
|
const QMetaProperty &property,
|
||
|
const QMetaProperty &property, const bool refresh = false,
|
||
|
const QVariant &id = "");
|
||
|
const bool canPersistRelation(const Relation &relation, const RelationType &r, const QVariant &var) const;
|
||
|
QList<QHash<QString, QVariant> > findAllByAttributes(const
|
||
|
QSharedPointer<Entity> &entity,
|
||
|
bool ignoreID = false);
|
||
| ... | ... | |
|
QHash<QString, QVariant> &m,
|
||
|
const QString &tblname,
|
||
|
bool ignoreID = false);
|
||
|
QSharedPointer<Entity> findById(const qint64 &id, Entity *&e);
|
||
|
QSharedPointer<Entity> findById(const qint64 &id, Entity *&e, const bool refresh=false);
|
||
|
void setListProperty(const QSharedPointer<Entity> &entity,
|
||
|
QList<QSharedPointer<Entity>> &list,
|
||
|
const QMetaProperty &property) const;
|
||
|
void setProperty(const QSharedPointer<Entity> &entiy,
|
||
|
QSharedPointer<Entity> value,
|
||
|
const QMetaProperty &property) const;
|
||
|
void saveRelations(const QSharedPointer<Entity> &entity);
|
||
|
|
||
|
public:
|
||
|
EntityManager(QSqlDatabase database);
|
||
| ... | ... | |
|
QSharedPointer<Database> getDb() const;
|
||
|
void setDb(const QSharedPointer<Database> &value);
|
||
|
QSharedPointer<Schema> getSchema() const;
|
||
|
void refresh(QSharedPointer<Entity> &entity);
|
||
|
void setSchema(const QSharedPointer<Schema> &value);
|
||
|
|
||
|
/**
|
||
| src/querybuilder.cpp | ||
|---|---|---|
|
auto i = relations.constBegin();
|
||
|
while (i != relations.constEnd()) {
|
||
|
Relation r = i.value();
|
||
|
if (r.getType() == MANY_TO_ONE && props.contains(i.key())) {
|
||
|
if (r.getType() == MANY_TO_ONE && props.contains(i.key())
|
||
|
|| (r.getType() == ONE_TO_ONE && r.getMappedBy().isEmpty())) {
|
||
|
auto v = props.value(i.key()).read(e);
|
||
|
if (v.canConvert<Entity *>()) {
|
||
|
this->insertRelationId(qvariant_cast<Entity *>(v), map, i.key());
|
||
Auch abrufbar als: Unified diff
wip persiting relations