Revision df1e56bd
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
samples/example/main.cpp | ||
---|---|---|
QSharedPointer<Group> gPtr = QSharedPointer<Group>(g);
|
||
e->createTable(gPtr);
|
||
QSharedPointer<Entity> groupPtr = gPtr.objectCast<Entity>();
|
||
QSharedPointer<Person> mainTeacher = QSharedPointer<Person>(new Person("Max",
|
||
"Mustermann", Person::Gender::MALE));
|
||
gPtr->setMainTeacher(mainTeacher);
|
||
//Persons will also persisted
|
||
e->create(groupPtr, true, true);
|
||
|
||
|
||
/** ---------------------------------
|
||
* FIND
|
||
* ---------------------------------
|
||
... | ... | |
qDebug() << "GroupID:" << groupFindPtr->getId();
|
||
QSharedPointer<Group> grp = groupFindPtr.objectCast<Group>();
|
||
qDebug() << "PersonSize:" << grp->getPersons().size();
|
||
qDebug() << "MainTeacher:" << grp->getMainTeacher()->toString();
|
||
qDebug() << "Duration:" << t.elapsed();
|
||
return 0;
|
||
}
|
src/database.cpp | ||
---|---|---|
}
|
||
|
||
bool Database::rollbackTransaction() {
|
||
qDebug() << "Transaction rolled back!";
|
||
return supportTransactions && this->database.rollback();
|
||
}
|
||
|
src/entity.cpp | ||
---|---|---|
using namespace CuteEntityManager;
|
||
|
||
Entity::Entity(QObject *parent) : QObject(parent) {
|
||
this->id = -1;
|
||
//this->id = -1;
|
||
}
|
||
|
||
QString Entity::toString() const {
|
src/entityinstancefactory.cpp | ||
---|---|---|
while (iterator != attributes.constEnd()) {
|
||
if (metaprops.contains(iterator.key())) {
|
||
QMetaProperty prop = metaprops.value(iterator.key());
|
||
if(prop.isWritable()) {
|
||
if(prop.isEnumType()) {
|
||
prop.write(e,prop.enumerator().key(iterator.value().toInt()));
|
||
if (prop.isWritable()) {
|
||
if (prop.isEnumType()) {
|
||
prop.write(e, prop.enumerator().key(iterator.value().toInt()));
|
||
} else {
|
||
prop.write(e, iterator.value());
|
||
}
|
src/entitymanager.cpp | ||
---|---|---|
|
||
bool EntityManager::checkTable(const QSharedPointer<Entity> &entity) {
|
||
bool rc = true;
|
||
if (!this->schema->containsTable(entity->getTablename())) {
|
||
if (this->schema->getQueryBuilder()->createTable(entity)) {
|
||
this->schema->getTableSchema(entity->getTablename(), true);
|
||
rc = this->schema->getTables().contains(entity->getTablename());
|
||
}
|
||
if (!this->schema->containsTable(entity->getTablename())
|
||
&& this->schema->getQueryBuilder()->createTable(entity)) {
|
||
this->schema->getTableSchema(entity->getTablename(), true);
|
||
rc = this->schema->getTables().contains(entity->getTablename());
|
||
}
|
||
return rc;
|
||
}
|
||
... | ... | |
QSharedPointer<Entity> &e,
|
||
const bool refresh) {
|
||
QSharedPointer<Entity> r;
|
||
if (!e.isNull()) {
|
||
if (refresh || !(r = this->cache.get(id, QString(e->getClassname())))) {
|
||
e->setId(id);
|
||
auto map = this->findByPk(id, e);
|
||
r = this->convert(map, e->getClassname(), refresh);
|
||
}
|
||
if (!e.isNull() && (refresh
|
||
|| !(r = this->cache.get(id, QString(e->getClassname()))))) {
|
||
e->setId(id);
|
||
auto map = this->findByPk(id, e);
|
||
r = this->convert(map, e->getClassname(), refresh);
|
||
}
|
||
return r;
|
||
}
|
||
... | ... | |
const QMetaProperty &property, const bool refresh) {
|
||
qint64 convertedId = -1;
|
||
bool ok = false;
|
||
if ((convertedId == id.toLongLong(&ok)) && ok && convertedId > -1) {
|
||
if ((convertedId = id.toLongLong(&ok)) && ok && convertedId > -1) {
|
||
QString className = EntityInstanceFactory::extractEntityType(
|
||
property.typeName());
|
||
QSharedPointer<Entity> ptr = QSharedPointer<Entity>();
|
||
... | ... | |
const Relation &r,
|
||
const QMetaProperty &property, const bool refresh) {
|
||
if (entity.data() && entity->getId() > -1) {
|
||
Entity *e = EntityInstanceFactory::createInstance(
|
||
EntityInstanceFactory::extractEntityType(
|
||
property.typeName()));
|
||
QSqlQuery q = this->schema->getQueryBuilder()->oneToMany(
|
||
e->getTablename(),
|
||
this->schema->getQueryBuilder()->generateColumnNameID(
|
||
r.getMappedBy()), entity->getId());
|
||
auto listMap = this->convertQueryResult(q);
|
||
auto entities = this->convert(listMap, e->getClassname(), refresh);
|
||
delete e;
|
||
this->setListProperty(entity, entities, property);
|
||
auto e = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(
|
||
EntityInstanceFactory::extractEntityType(
|
||
property.typeName())));
|
||
if (e) {
|
||
QSqlQuery q = this->schema->getQueryBuilder()->oneToMany(e->getTablename(),
|
||
this->schema->getQueryBuilder()->generateColumnNameID(r.getMappedBy()),
|
||
entity->getId());
|
||
auto listMap = this->convertQueryResult(q);
|
||
auto entities = this->convert(listMap, e->getClassname(), refresh);
|
||
this->setListProperty(entity, entities, property);
|
||
}
|
||
}
|
||
}
|
||
|
||
... | ... | |
if (r.getMappedBy().isEmpty()) {
|
||
this->manyToOne(entity, id, property);
|
||
} else {
|
||
Entity *e = EntityInstanceFactory::createInstance(
|
||
EntityInstanceFactory::extractEntityType(
|
||
property.typeName()));
|
||
QSqlQuery q = this->schema->getQueryBuilder()->oneToMany(
|
||
e->getTablename(),
|
||
this->schema->getQueryBuilder()->generateColumnNameID(
|
||
r.getMappedBy()),
|
||
entity->getProperty(entity->getPrimaryKey()).toLongLong(), 1);
|
||
auto listMap = this->convertQueryResult(q);
|
||
auto entities = this->convert(listMap, e->getClassname(), refresh);
|
||
if (!entities.isEmpty()) {
|
||
QSharedPointer<Entity> ptr = entities.at(0);
|
||
this->setProperty(entity, ptr, property);
|
||
auto e = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(
|
||
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);
|
||
auto listMap = this->convertQueryResult(q);
|
||
auto entities = this->convert(listMap, e->getClassname(), refresh);
|
||
if (!entities.isEmpty()) {
|
||
QSharedPointer<Entity> ptr = entities.at(0);
|
||
this->setProperty(entity, ptr, property);
|
||
}
|
||
}
|
||
delete e;
|
||
}
|
||
}
|
||
|
||
... | ... | |
}
|
||
}
|
||
|
||
void EntityManager::setProperty(const QSharedPointer<Entity> &entiy,
|
||
void EntityManager::setProperty(const QSharedPointer<Entity> &entity,
|
||
QSharedPointer<Entity> value,
|
||
const QMetaProperty &property) const {
|
||
if (value && value->getProperty(value->getPrimaryKey()).toLongLong()
|
||
> -1) {
|
||
property.write(entiy.data(), QVariant(value));
|
||
QVariant var;
|
||
var.setValue<QSharedPointer<Entity>>(value);
|
||
property.write(entity.data(), var);
|
||
}
|
||
}
|
||
|
||
void EntityManager::saveRelations(const QSharedPointer<Entity> &entity) {
|
||
void EntityManager::savePrePersistedRelations(const QSharedPointer<Entity>
|
||
&entity) {
|
||
auto relations = entity->getRelationProperties();
|
||
auto iterator = relations.constBegin();
|
||
while (iterator != relations.constEnd()) {
|
||
const Relation r = iterator.key();
|
||
auto var = iterator.value().read(entity.data());
|
||
if (r.getType() == MANY_TO_ONE && !var.isNull()
|
||
&& var.canConvert<QSharedPointer<Entity>>()) {
|
||
auto e = qvariant_cast<QSharedPointer<Entity>>(var);
|
||
if (this->shouldBeSaved(e, r)) {
|
||
this->save(e);
|
||
if (!var.isNull()) {
|
||
if (r.getType() == MANY_TO_ONE) {
|
||
auto e = EntityInstanceFactory::castQVariant(var);
|
||
if (this->shouldBeSaved(e, r)) {
|
||
this->save(e);
|
||
}
|
||
} else if (r.getType() == ONE_TO_ONE && r.getMappedBy().isEmpty()) {
|
||
auto entity = EntityInstanceFactory::castQVariant(var);
|
||
this->save(entity);
|
||
}
|
||
} else if (this->canPersistRelation(r, MANY_TO_MANY, var)) {
|
||
this->persistManyToMany(entity, r, var);
|
||
} 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);
|
||
if (this->shouldBeSaved(entity, r)) {
|
||
this->save(entity);
|
||
}
|
||
++iterator;
|
||
}
|
||
}
|
||
|
||
void EntityManager::savePostPersistedRelations(const QSharedPointer<Entity>
|
||
&entity) {
|
||
auto relations = entity->getRelationProperties();
|
||
auto iterator = relations.constBegin();
|
||
while (iterator != relations.constEnd()) {
|
||
const Relation r = iterator.key();
|
||
auto var = iterator.value().read(entity.data());
|
||
if (!var.isNull()) {
|
||
if (this->canPersistRelation(r, MANY_TO_MANY, var)) {
|
||
this->persistManyToMany(entity, r, var);
|
||
} else if (this->canPersistRelation(r, ONE_TO_MANY, var)) {
|
||
QList<QSharedPointer<Entity>> list = EntityInstanceFactory::castQVariantList(
|
||
var);
|
||
for (int var = 0; var < list.size(); ++var) {
|
||
auto entity = list.at(var);
|
||
if (this->shouldBeSaved(entity, r)) {
|
||
this->save(entity);
|
||
}
|
||
}
|
||
} else if (r.getType() == ONE_TO_ONE && !r.getMappedBy().isEmpty()) {
|
||
auto entity = EntityInstanceFactory::castQVariant(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::persistMappedByRelation(const QList<QSharedPointer<Entity> >
|
||
... | ... | |
const QSharedPointer<Entity> &foreignEntity) const {
|
||
QMetaProperty prop;
|
||
auto props = foreignEntity->getMetaProperties();
|
||
if (!r.getMappedBy().isEmpty()) {
|
||
if (props.contains(r.getMappedBy())) {
|
||
prop = props.value(r.getMappedBy());
|
||
}
|
||
if (!r.getMappedBy().isEmpty() && props.contains(r.getMappedBy())) {
|
||
prop = props.value(r.getMappedBy());
|
||
} else {
|
||
auto relations = foreignEntity->getRelations();
|
||
for (auto iterator = relations.constBegin(); iterator != relations.constEnd();
|
||
... | ... | |
return prop;
|
||
}
|
||
|
||
|
||
bool EntityManager::shouldBeSaved(QSharedPointer<Entity> &entity,
|
||
const Relation &r) {
|
||
return entity && (r.getCascadeType().contains(ALL)
|
||
... | ... | |
|
||
|
||
void EntityManager::setNullOneToManyRelation(QVariant &var, const Relation &r) {
|
||
if (!r.getMappedBy().isEmpty()) {
|
||
if (!var.isNull() && var.canConvert<QList<QSharedPointer<Entity>>>()) {
|
||
auto list = qvariant_cast<QList<QSharedPointer<Entity>>>(var);
|
||
if (!list.isEmpty()) {
|
||
auto metas = list.at(0)->getMetaProperties();
|
||
if (metas.contains(r.getMappedBy())) {
|
||
for (int var = 0; var < list.size(); ++var) {
|
||
auto entity = list.at(var);
|
||
this->setProperty(entity, QSharedPointer<Entity>(),
|
||
metas.value(r.getMappedBy()));
|
||
this->save(entity);
|
||
}
|
||
if (!r.getMappedBy().isEmpty() && !var.isNull()
|
||
&& var.canConvert<QVariantList>()) {
|
||
auto list = EntityInstanceFactory::castQVariantList(var);
|
||
if (!list.isEmpty()) {
|
||
auto metas = list.at(0)->getMetaProperties();
|
||
if (metas.contains(r.getMappedBy())) {
|
||
for (int var = 0; var < list.size(); ++var) {
|
||
auto entity = list.at(var);
|
||
this->setProperty(entity, QSharedPointer<Entity>(),
|
||
metas.value(r.getMappedBy()));
|
||
this->save(entity);
|
||
}
|
||
}
|
||
}
|
||
... | ... | |
const Relation &r) {
|
||
if (r.getCascadeType().contains(REMOVE) || r.getCascadeType().contains(ALL)) {
|
||
this->removeEntity(var);
|
||
} else if (!r.getMappedBy().isEmpty()) {
|
||
if (!var.isNull() && var.canConvert<QSharedPointer<Entity>>()) {
|
||
auto e = qvariant_cast<QSharedPointer<Entity>>(var);
|
||
auto metas = e->getMetaProperties();
|
||
if (metas.contains(r.getMappedBy())) {
|
||
this->setProperty(e, QSharedPointer<Entity>(), metas.value(r.getMappedBy()));
|
||
this->save(e);
|
||
}
|
||
} else if (!r.getMappedBy().isEmpty() && !var.isNull()) {
|
||
auto e = EntityInstanceFactory::castQVariant(var);
|
||
auto metas = e->getMetaProperties();
|
||
if (metas.contains(r.getMappedBy())) {
|
||
this->setProperty(e, QSharedPointer<Entity>(), metas.value(r.getMappedBy()));
|
||
this->save(e);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
void EntityManager::removeEntity(QVariant &var) {
|
||
if (!var.isNull() && var.canConvert<QSharedPointer<Entity>>()) {
|
||
auto e = qvariant_cast<QSharedPointer<Entity>>(var);
|
||
if (!var.isNull()) {
|
||
auto e = EntityInstanceFactory::castQVariant(var);
|
||
this->remove(e);
|
||
}
|
||
}
|
||
|
||
void EntityManager::removeEntityList(QVariant &var) {
|
||
if (var.canConvert<QList<QSharedPointer<Entity>>>()) {
|
||
QList<QSharedPointer<Entity>> list =
|
||
qvariant_cast<QList<QSharedPointer<Entity>>>(var);
|
||
if (var.canConvert<QVariantList>()) {
|
||
auto list = EntityInstanceFactory::castQVariantList(var);
|
||
for (int var = 0; var < list.size(); ++var) {
|
||
auto entity = list.at(var);
|
||
this->remove(entity);
|
||
... | ... | |
void EntityManager::removeManyToManyEntityList(const QSharedPointer<Entity> &e,
|
||
const Relation &r,
|
||
QVariant &var) {
|
||
if (var.canConvert<QList<QSharedPointer<Entity>>>()) {
|
||
QList<QSharedPointer<Entity>> list =
|
||
qvariant_cast<QList<QSharedPointer<Entity>>>(var);
|
||
if (!var.isNull() && var.canConvert<QVariantList>()) {
|
||
auto list = EntityInstanceFactory::castQVariantList(var);
|
||
if (!list.isEmpty()) {
|
||
auto builder = this->schema->getQueryBuilder();
|
||
auto ptr = list.at(0);
|
||
... | ... | |
const Relation &r, QVariant &property) {
|
||
Q_UNUSED(r)
|
||
auto list = property.value<QList<QVariant>>();
|
||
if (!list.isEmpty()) {
|
||
if (!list.at(0).isNull()) {
|
||
auto var = list.at(0);
|
||
auto ptr = EntityInstanceFactory::castQVariant(var);
|
||
auto builder = this->schema->getQueryBuilder();
|
||
QString tblName = builder->generateManyToManyTableName(entity, ptr);
|
||
if (this->schema->getTables().contains(tblName)) {
|
||
QSqlQuery q = builder->manyToManyDelete(
|
||
tblName, builder->generateManyToManyColumnName(entity),
|
||
entity->getProperty(entity->getPrimaryKey()).toLongLong());
|
||
if (this->db->transaction(q)) {
|
||
auto nList = EntityInstanceFactory::castQVariantList(property);
|
||
this->persistMappedByRelation(nList, q, entity, ptr, r, tblName);
|
||
}
|
||
} else {
|
||
qDebug() << "MANY_TO_MANY Table " << tblName << " not exists";
|
||
if (!list.isEmpty() && !(list.at(0).isNull())) {
|
||
auto var = list.at(0);
|
||
auto ptr = EntityInstanceFactory::castQVariant(var);
|
||
auto builder = this->schema->getQueryBuilder();
|
||
QString tblName = builder->generateManyToManyTableName(entity, ptr);
|
||
if (this->schema->getTables().contains(tblName)) {
|
||
QSqlQuery q = builder->manyToManyDelete(
|
||
tblName, builder->generateManyToManyColumnName(entity),
|
||
entity->getProperty(entity->getPrimaryKey()).toLongLong());
|
||
if (this->db->transaction(q)) {
|
||
auto nList = EntityInstanceFactory::castQVariantList(property);
|
||
this->persistMappedByRelation(nList, q, entity, ptr, r, tblName);
|
||
}
|
||
} else {
|
||
qDebug() << "MANY_TO_MANY Table " << tblName << " not exists";
|
||
}
|
||
}
|
||
}
|
||
... | ... | |
|
||
void EntityManager::manyToMany(const QSharedPointer<Entity> &entity,
|
||
const QMetaProperty &property, const bool refresh) {
|
||
Entity *secEntity = EntityInstanceFactory::createInstance(
|
||
EntityInstanceFactory::extractEntityType(
|
||
QString(
|
||
property.typeName())));
|
||
QSharedPointer<Entity> secEntityPtr = QSharedPointer<Entity>
|
||
(EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(
|
||
QString(property.typeName()))));
|
||
auto builder = this->schema->getQueryBuilder();
|
||
if (secEntity) {
|
||
QSharedPointer<Entity> secEntityPtr = QSharedPointer<Entity>(secEntity);
|
||
QString tblName = builder->generateManyToManyTableName(entity,
|
||
secEntityPtr);
|
||
if (secEntityPtr) {
|
||
QString tblName = builder->generateManyToManyTableName(entity, secEntityPtr);
|
||
if (this->schema->getTables().contains(tblName)) {
|
||
QSqlQuery q = builder->manyToMany(tblName,
|
||
builder->generateManyToManyColumnName(entity),
|
||
... | ... | |
const bool persistRelations, const bool checkDuplicate) {
|
||
bool rc = false;
|
||
if (this->checkTable(entity) && !(checkDuplicate && this->count(entity) == 0)) {
|
||
if (persistRelations) {
|
||
this->savePrePersistedRelations(entity);
|
||
}
|
||
this->db->startTransaction();
|
||
QList<QSqlQuery> q = this->schema->getQueryBuilder()->create(
|
||
entity);
|
||
this->db->startTransaction();
|
||
bool first = true;
|
||
for (int var = 0; var < q.size(); ++var) {
|
||
auto query = q.at(var);
|
||
... | ... | |
} else {
|
||
this->cache.insert(entity);
|
||
if (persistRelations) {
|
||
this->saveRelations(entity);
|
||
this->savePostPersistedRelations(entity);
|
||
}
|
||
rc = true;
|
||
}
|
||
... | ... | |
|
||
bool EntityManager::merge(QSharedPointer<Entity> &entity, bool withRelations) {
|
||
if (entity->getId() > -1 && this->count(entity) == 1) {
|
||
if (withRelations) {
|
||
this->savePrePersistedRelations(entity);
|
||
}
|
||
this->db->startTransaction();
|
||
QList<QSqlQuery> q = this->schema->getQueryBuilder()->merge(
|
||
entity);
|
||
... | ... | |
this->db->rollbackTransaction();
|
||
return false;
|
||
} else if (ok && withRelations) {
|
||
this->saveRelations(entity);
|
||
this->savePostPersistedRelations(entity);
|
||
}
|
||
}
|
||
return false;
|
||
... | ... | |
while (iterator != props.constEnd()) {
|
||
const Relation r = iterator.key();
|
||
const QMetaProperty property = iterator.value();
|
||
QString colName = this->schema->getQueryBuilder()->generateColumnNameID(
|
||
r.getPropertyName());
|
||
switch (r.getType()) {
|
||
case MANY_TO_ONE:
|
||
if (map.contains(
|
||
this->schema->getQueryBuilder()->generateColumnNameID(
|
||
r.getPropertyName()))) {
|
||
this->manyToOne(entity, property.read(entity.data()), property, refresh);
|
||
if (map.contains(colName)) {
|
||
this->manyToOne(entity, map.value(colName), property, refresh);
|
||
}
|
||
break;
|
||
case MANY_TO_MANY:
|
||
... | ... | |
this->oneToMany(entity, r, property, refresh);
|
||
break;
|
||
case ONE_TO_ONE:
|
||
this->oneToOne(entity, r, property, refresh, property.read(entity.data()));
|
||
this->oneToOne(entity, r, property, refresh, map.value(colName));
|
||
break;
|
||
}
|
||
++iterator;
|
||
... | ... | |
auto q = queries.at(var);
|
||
if (!q.exec()) {
|
||
this->db->rollbackTransaction();
|
||
qDebug() << "Remove transaction rolled back";
|
||
ok = false;
|
||
break;
|
||
}
|
||
... | ... | |
bool createRelationTables) {
|
||
return this->schema->getQueryBuilder()->createTable(entity,
|
||
createRelationTables);
|
||
|
||
}
|
||
|
||
qint8 EntityManager::count(const QSharedPointer<Entity> &entity,
|
src/entitymanager.h | ||
---|---|---|
void setListProperty(const QSharedPointer<Entity> &entity,
|
||
QList<QSharedPointer<Entity>> &list,
|
||
const QMetaProperty &property) const;
|
||
void setProperty(const QSharedPointer<Entity> &entiy,
|
||
void setProperty(const QSharedPointer<Entity> &entity,
|
||
QSharedPointer<Entity> value,
|
||
const QMetaProperty &property) const;
|
||
void saveRelations(const QSharedPointer<Entity> &entity);
|
||
void savePrePersistedRelations(const QSharedPointer<Entity> &entity);
|
||
void savePostPersistedRelations(const QSharedPointer<Entity> &entity);
|
||
|
||
QList<QSharedPointer<Entity>> saveRelationEntities(const
|
||
QList<QSharedPointer<Entity>> &list, const Relation &r);
|
||
void persistMappedByRelation(const QList<QSharedPointer<Entity>> &list,
|
||
QSqlQuery &q, const QSharedPointer<Entity> &entity, const QSharedPointer<Entity> &ptr, const Relation &r,
|
||
QSqlQuery &q, const QSharedPointer<Entity> &entity,
|
||
const QSharedPointer<Entity> &ptr, const Relation &r,
|
||
const QString &tblName);
|
||
bool shouldBeSaved(QSharedPointer<Entity> &entity , const Relation &r);
|
||
void removeRelations(const QSharedPointer<Entity> &entity);
|
||
... | ... | |
void removeEntity(QVariant &var);
|
||
void setNullOneToManyRelation(QVariant &var, const Relation &r);
|
||
void setNullEntityPropertyRelation(QVariant &var, const Relation &r);
|
||
QMetaProperty mappedProperty(const Relation &r,const QSharedPointer<Entity> &foreignEntity) const;
|
||
QMetaProperty mappedProperty(const Relation &r,
|
||
const QSharedPointer<Entity> &foreignEntity) const;
|
||
|
||
public:
|
||
EntityManager(QSqlDatabase database);
|
||
... | ... | |
* @param toInitialize list of entity classnames which database tables should be created
|
||
* @return
|
||
*/
|
||
public slots:
|
||
public slots:
|
||
bool startup(QString version, QStringList toInitialize);
|
||
bool executeQuery(const QString &query);
|
||
static void removeConnectionName(const QString &name);
|
||
... | ... | |
bool merge(QSharedPointer<Entity> &entity, bool withRelations = true);
|
||
bool remove(QSharedPointer<Entity> &entity);
|
||
bool removeAll(QString tblname);
|
||
bool createTable(const QSharedPointer<Entity> &entity, bool createRelationTables=true);
|
||
bool createTable(const QSharedPointer<Entity> &entity,
|
||
bool createRelationTables = true);
|
||
qint8 count(const QSharedPointer<Entity> &entity, bool ignoreID = true);
|
||
qint8 count(const QString &tableName);
|
||
QSharedPointer<Database> getDb() const;
|
||
... | ... | |
/**
|
||
*@TODO use conditions
|
||
*/
|
||
public:
|
||
public:
|
||
template<class T> qint8 count(QHash<QString, QString> condition =
|
||
QHash<QString, QString>()) {
|
||
Entity *e = EntityInstanceFactory::createInstance<T>();
|
src/querybuilder.cpp | ||
---|---|---|
|
||
QList<QSqlQuery> QueryBuilder::createOrMerge(const QSharedPointer<Entity>
|
||
&entity, bool insert) const {
|
||
auto attrs = this->inheritedAttributes(entity);
|
||
const QList<ClassAttributes> attrs = this->inheritedAttributes(entity);
|
||
auto queries = QList<QSqlQuery>();
|
||
for (int var = 0; var < attrs.size(); ++var) {
|
||
auto attr = attrs.at(var);
|
||
... | ... | |
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(entity.data());
|
||
if (v.canConvert<Entity *>()) {
|
||
this->insertRelationId(qvariant_cast<Entity *>(v), map, i.key());
|
||
} else if (v.canConvert<QSharedPointer<Entity>>()) {
|
||
this->insertRelationId(qvariant_cast<QSharedPointer<Entity>>(v).data(), map,
|
||
i.key());
|
||
} else if (v.canConvert<QPointer<Entity>>()) {
|
||
this->insertRelationId(qvariant_cast<QPointer<Entity>>(v).data(), map, i.key());
|
||
QSharedPointer<Entity> e = EntityInstanceFactory::castQVariant(v);
|
||
if (e) {
|
||
this->insertRelationId(e.data(), map, i.key());
|
||
}
|
||
}
|
||
++i;
|
Auch abrufbar als: Unified diff
many-to-one