Revision 98b5b08d
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
src/entitymanager.cpp | ||
---|---|---|
e->getTablename(),
|
||
r.getMappedBy() + "_id", entity.data()->getId());
|
||
auto listMap = this->convertQueryResult(q);
|
||
auto entities = this->convert(listMap, e->getClassname());
|
||
auto entities = this->convert(listMap, e->getClassname(), refresh);
|
||
delete e;
|
||
this->setListProperty(entity, entities, property);
|
||
}
|
||
... | ... | |
e->getTablename(),
|
||
r.getMappedBy() + "_id", entity.data()->getId(), 1);
|
||
auto listMap = this->convertQueryResult(q);
|
||
auto entities = this->convert(listMap, e->getClassname());
|
||
auto entities = this->convert(listMap, e->getClassname(), refresh);
|
||
if (!entities.isEmpty()) {
|
||
QSharedPointer<Entity> ptr = entities.at(0);
|
||
this->setProperty(entity, ptr, property);
|
||
... | ... | |
}
|
||
}
|
||
|
||
const bool EntityManager::canPersistRelation(const Relation &relation,
|
||
const RelationType &r, const QVariant &var) 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,
|
||
const QMetaProperty &property) const {
|
||
... | ... | |
*/
|
||
auto var = iterator.value().read(entity.data());
|
||
if (this->canPersistRelation(r, MANY_TO_MANY, var)) {
|
||
/**
|
||
@TODO
|
||
*/
|
||
this->persistManyToMany(entity, r, var);
|
||
} else if (this->canPersistRelation(r, ONE_TO_MANY, var)) {
|
||
QList<QSharedPointer<Entity>> list =
|
||
qvariant_cast<QList<QSharedPointer<Entity>>>(var);
|
||
... | ... | |
}
|
||
}
|
||
|
||
|
||
void EntityManager::persistManyToMany(const QSharedPointer<Entity> &entity,
|
||
const Relation &r, const QVariant &property) {
|
||
QList<QSharedPointer<Entity>> list =
|
||
qvariant_cast<QList<QSharedPointer<Entity>>>(property);
|
||
if (!list.isEmpty()) {
|
||
auto ptr = list.at(0);
|
||
QString tblName = builder.data()->generateManyToManyTableName(entity, ptr);
|
||
if (this->schema.data()->getTables().contains(tblName)) {
|
||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->manyToManyDelete(
|
||
tblName, builder.data()->generateManyToManyColumnName(entity),
|
||
entity.data()->getId());
|
||
if (this->db.data()->transaction(q)) {
|
||
q.clear();
|
||
QList<QSharedPointer<Entity>> persisted = QList<QSharedPointer<Entity>>();
|
||
q = this->schema.data()->getQueryBuilder().data()->manyToManyInsert(tblName,
|
||
builder.data()->generateManyToManyColumnName(entity),
|
||
builder.data()->generateManyToManyColumnName(ptr));
|
||
q.bindValue(0, entity.data()->getId());
|
||
for (int var = 0; var < list.size(); ++var) {
|
||
ptr = list.at(var);
|
||
if (ptr.data() && ((ptr.data()->getId() > -1 && r.getCascadeType() == MERGE)
|
||
|| r.getCascadeType() == ALL || r.getCascadeType() == PERSIST)) {
|
||
if (this->save(list.at(var))) {
|
||
persisted.append(ptr);
|
||
}
|
||
}
|
||
}
|
||
//maybe use transaction
|
||
//this->db.data()->startTransaction();
|
||
for (int var = 0; var < persisted.size(); ++var) {
|
||
ptr = list.at(var);
|
||
q.bindValue(1, ptr.data()->getId());
|
||
q.exec();
|
||
/**
|
||
* @TODO append to attribute list
|
||
* */
|
||
}
|
||
//this->db.data()->commitTransaction();
|
||
/**
|
||
* cascade types remove/refresh will be ignored atm
|
||
* */
|
||
}
|
||
} else {
|
||
qDebug() << "MANY_TO_MANY Table " << tblName << " not exists";
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
void EntityManager::manyToMany(const QSharedPointer<Entity> &entity,
|
||
const Relation &r,
|
||
const QMetaProperty &property, const bool refresh) {
|
||
... | ... | |
auto builder = this->schema.data()->getQueryBuilder();
|
||
if (secEntity) {
|
||
QSharedPointer<Entity> secEntityPtr = QSharedPointer<Entity>(secEntity);
|
||
QString tblName = "";
|
||
if (r.getMappedBy().isEmpty()) {
|
||
tblName = builder.data()->generateManyToManyTableName(entity, secEntityPtr);
|
||
} else {
|
||
tblName = builder.data()->generateManyToManyTableName(secEntityPtr, entity);
|
||
}
|
||
QString tblName = builder.data()->generateManyToManyTableName(entity,
|
||
secEntityPtr);
|
||
/**
|
||
* maybe it would be better, to fetch first the ids, look up cache and then request missing entities
|
||
*/
|
||
if (this->schema.data()->getTables().contains(tblName)) {
|
||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->manyToMany(tblName,
|
||
QSqlQuery q = builder.data()->manyToMany(tblName,
|
||
builder.data()->generateManyToManyColumnName(entity), entity.data()->getId(),
|
||
builder.data()->generateManyToManyColumnName(secEntityPtr),
|
||
secEntityPtr.data()->getTablename());
|
||
auto listMap = this->convertQueryResult(q);
|
||
auto entities = this->convert(listMap, entity.data()->getClassname());
|
||
auto entities = this->convert(listMap, entity.data()->getClassname(), refresh);
|
||
this->setListProperty(entity, entities, property);
|
||
} else {
|
||
qDebug() << "MANY_TO_MANY Table " << tblName << " not exists";
|
src/entitymanager.h | ||
---|---|---|
void oneToOne(const QSharedPointer<Entity> &entity, const Relation &r,
|
||
const QMetaProperty &property, const bool refresh = false,
|
||
const QVariant &id = "");
|
||
const bool canPersistRelation(const Relation &relation, const RelationType &r,
|
||
const QVariant &var) const;
|
||
bool canPersistRelation(const Relation &relation, const RelationType &r,
|
||
const QVariant &var) const;
|
||
void persistManyToMany(const QSharedPointer<Entity> &entity, const Relation &r,
|
||
const QVariant &property);
|
||
QList<QHash<QString, QVariant> > findAllByAttributes(const
|
||
QSharedPointer<Entity> &entity,
|
||
bool ignoreID = false);
|
||
... | ... | |
void setSchema(const QSharedPointer<Schema> &value);
|
||
/**
|
||
*@TODO create indexes
|
||
* /
|
||
/**
|
||
*@TODO use conditions
|
||
*/
|
||
template<class T> qint8 count(QHash<QString, QString> condition =
|
src/querybuilder.cpp | ||
---|---|---|
secondEntity.data()->metaObject()->className()).toLower();
|
||
}
|
||
|
||
QString QueryBuilder::manyToManyTableName(const QSharedPointer<Entity>
|
||
&firstEntity, const QSharedPointer<Entity> &secondEntity,
|
||
const Relation &r) const {
|
||
QString table = "";
|
||
if (r.getMappedBy().isEmpty()) {
|
||
table = this->generateManyToManyTableName(firstEntity, secondEntity);
|
||
} else {
|
||
table = this->generateManyToManyTableName(secondEntity, firstEntity);
|
||
}
|
||
return table;
|
||
}
|
||
|
||
QHash<QString, QHash<QString, QString>> QueryBuilder::generateRelationTables(
|
||
const QSharedPointer<Entity> &entity)
|
||
const {
|
||
... | ... | |
const qint64 &id,
|
||
const QString &foreignKey, const QString &foreignTable) {
|
||
QSqlQuery q = this->database.data()->getQuery();
|
||
QString sql = "SELECT " + this->schema.data()->quoteTableName(
|
||
QString sql = "SELECT " +
|
||
this->schema.data()->quoteTableName(
|
||
foreignTable) + ".* FROM " +
|
||
this->schema.data()->quoteTableName(tableName) + " " + this->leftJoin(
|
||
foreignTable, tableName,
|
||
... | ... | |
return q;
|
||
}
|
||
|
||
QSqlQuery QueryBuilder::manyToManyDelete(const QString &tableName,
|
||
const QString &attribute, const qint64 &id) {
|
||
QSqlQuery q = this->database.data()->getQuery();
|
||
QString sql = "DELETE FROM " + this->schema.data()->quoteTableName(
|
||
tableName) + " WHERE " + this->schema.data()->quoteColumnName(
|
||
attribute) + "=:id";
|
||
q.prepare(sql);
|
||
q.bindValue(":id", id);
|
||
return q;
|
||
}
|
||
|
||
QSqlQuery QueryBuilder::manyToManyInsert(const QString &tableName,
|
||
const QString &col1, const QString &col2) const {
|
||
QSqlQuery q = this->database.data()->getQuery();
|
||
QString sql = "INSERT INTO " + this->schema.data()->quoteTableName(
|
||
tableName) + "(" + col1 + "," + col2 + ")"
|
||
+ "VALUES(?, ?);";
|
||
q.prepare(sql);
|
||
return q;
|
||
}
|
||
|
||
QString QueryBuilder::leftJoin(const QString &foreignTable,
|
||
const QString &tableName,
|
src/querybuilder.h | ||
---|---|---|
const;
|
||
QString generateManyToManyTableName(const QSharedPointer<Entity> &firstEntity,
|
||
const QSharedPointer<Entity> &secondEntity) const;
|
||
QString manyToManyTableName(const QSharedPointer<Entity> &firstEntity,
|
||
const QSharedPointer<Entity> &secondEntity, const Relation &r) const;
|
||
|
||
QString transformTypeToAbstractDbType(QString typeName) const;
|
||
QString transformAbstractTypeToRealDbType(QString typeName) const;
|
||
... | ... | |
const qint64 &id,
|
||
const QString &foreignKey,
|
||
const QString &foreignTable);
|
||
QSqlQuery manyToManyDelete(const QString &tableName, const QString &attribute,
|
||
const qint64 &id);
|
||
QSqlQuery manyToManyInsert(const QString &tableName, const QString &col1,
|
||
const QString &col2) const;
|
||
|
||
virtual QString limit(const qint64 &limit, const qint64 &offset) const;
|
||
QString generateManyToManyColumnName(const QSharedPointer<Entity> &entity)
|
||
const;
|
Auch abrufbar als: Unified diff
more many to many wip