Herunterladen als
root/src/entitymanager.cpp @ 3b5d8beb
81c23b56 | Christian Ehringfeld | /*
|
|
6899f814 | Christian Ehringfeld | * Copyright (C) 2015 Christian Ehringfeld <c.ehringfeld@t-online.de>
|
|
*
|
|||
* This program is free software; you can redistribute it and/or modify it
|
|||
* under the terms of the GNU Lesser General Public License as published by
|
|||
* the Free Software Foundation.
|
|||
*
|
|||
* This program is distributed in the hope that it will be useful, but
|
|||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|||
* for more details.
|
|||
*
|
|||
* You should have received a copy of the GNU Lesser General Public License
|
|||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|||
*/
|
|||
81c23b56 | Christian Ehringfeld | ||
f9cef58f | Christian Ehringfeld | #include <QVariantList>
|
|
81c23b56 | Christian Ehringfeld | #include "entitymanager.h"
|
|
7e233492 | Christian Ehringfeld | #include "enums/databasetype.h"
|
|
1e213c09 | Christian Ehringfeld | #include "databasemigration.h"
|
|
4d58ef6a | Christian Ehringfeld | using namespace CuteEntityManager;
|
|
81c23b56 | Christian Ehringfeld | ||
QStringList EntityManager::connectionNames = QStringList();
|
|||
afde9013 | Christian Ehringfeld | QStringList EntityManager::getConnectionNames() {
|
|
return EntityManager::connectionNames;
|
|||
}
|
|||
1b167b6c | Christian Ehringfeld | QSharedPointer<QueryBuilder> EntityManager::getQueryBuilder() const {
|
|
return this->schema->getQueryBuilder();
|
|||
}
|
|||
bcd2b697 | Christian Ehringfeld | EntityManager::EntityManager(QSqlDatabase database,
|
|
bool logQueries) : QObject() {
|
|||
auto db = new Database(database, true, logQueries);
|
|||
afde9013 | Christian Ehringfeld | this->db = QSharedPointer<Database>(db);
|
|
this->init();
|
|||
}
|
|||
EntityManager::EntityManager(const QString &databaseType, QString databasename ,
|
|||
f5b682e0 | Christian Ehringfeld | QString hostname, QString username, QString password, QString port,
|
|
9971e7d2 | Christian Ehringfeld | bool logQueries, QString databaseOptions) : QObject() {
|
|
afde9013 | Christian Ehringfeld | auto db = new Database(databaseType, this->createConnection(), hostname,
|
|
databasename, username,
|
|||
password,
|
|||
9971e7d2 | Christian Ehringfeld | port.toInt(), true, logQueries, true, databaseOptions);
|
|
afde9013 | Christian Ehringfeld | this->db = QSharedPointer<Database>(db);
|
|
this->init();
|
|||
}
|
|||
7e233492 | Christian Ehringfeld | void EntityManager::init() {
|
|
3820ae33 | Christian Ehringfeld | auto schema = Database::getSchema(Database::getDatabaseType(
|
|
abb9e8c5 | Christian Ehringfeld | this->db->getDatabase().driverName()), this->db);
|
|
7e233492 | Christian Ehringfeld | this->schema = QSharedPointer<Schema>(schema);
|
|
abb9e8c5 | Christian Ehringfeld | this->schema->setTables(this->schema->getTableSchemas());
|
|
2ee5022f | Christian Ehringfeld | this->queryInterpreter = QSharedPointer<QueryInterpreter>(new QueryInterpreter(
|
|
this->schema->getQueryBuilder()));
|
|||
7e233492 | Christian Ehringfeld | }
|
|
afde9013 | Christian Ehringfeld | EntityManager::~EntityManager() {
|
|
EntityManager::removeConnectionName(this->db->getConnectionName());
|
|||
7e233492 | Christian Ehringfeld | }
|
|
9971e7d2 | Christian Ehringfeld | bool EntityManager::startup(QString version, QStringList toInitialize,
|
|
bool createIndices) {
|
|||
66704054 | Christian Ehringfeld | QSharedPointer<Entity> dbm = QSharedPointer<DatabaseMigration>
|
|
(new DatabaseMigration());
|
|||
1e213c09 | Christian Ehringfeld | QHash<QString, QVariant> map = QHash<QString, QVariant>();
|
|
d84c91e7 | Christian Ehringfeld | bool ok = true;
|
|
1e213c09 | Christian Ehringfeld | map.insert("version", version);
|
|
abb9e8c5 | Christian Ehringfeld | if (!this->schema->getTableNames().contains(dbm->getTablename())) {
|
|
42d32a68 | Christian Ehringfeld | this->createTable(dbm, true);
|
|
e86c23a2 | Christian Ehringfeld | }
|
|
d568923d | Christian Ehringfeld | if (this->findAllByAttributes(map, dbm->getTablename()).isEmpty()) {
|
|
9971e7d2 | Christian Ehringfeld | QList<QSharedPointer<Entity>> entities = QList<QSharedPointer<Entity>>();
|
|
d568923d | Christian Ehringfeld | for (int var = 0; var < toInitialize.size(); ++var) {
|
|
d84c91e7 | Christian Ehringfeld | if (ok) {
|
|
QString c = toInitialize.at(var);
|
|||
9971e7d2 | Christian Ehringfeld | auto entity = QSharedPointer<Entity>
|
|
(EntityInstanceFactory::createInstance(c));
|
|||
ok = this->createTable(entity);
|
|||
entities.append(entity);
|
|||
d84c91e7 | Christian Ehringfeld | } else {
|
|
break;
|
|||
}
|
|||
}
|
|||
9971e7d2 | Christian Ehringfeld | if (createIndices) {
|
|
for (int i = 0; i < entities.size(); ++i) {
|
|||
ok = this->schema->getQueryBuilder()->createIndices(entities.at(i));
|
|||
}
|
|||
}
|
|||
d84c91e7 | Christian Ehringfeld | if (ok) {
|
|
42d32a68 | Christian Ehringfeld | auto dbmPtr = dbm.objectCast<DatabaseMigration>();
|
|
dbmPtr->setVersion(version);
|
|||
dbmPtr->setApplyTime(QDateTime::currentDateTime());
|
|||
this->create(dbm);
|
|||
d568923d | Christian Ehringfeld | }
|
|
abb9e8c5 | Christian Ehringfeld | this->schema->setTables(this->schema->getTableSchemas());
|
|
d568923d | Christian Ehringfeld | }
|
|
d84c91e7 | Christian Ehringfeld | return ok;
|
|
1e213c09 | Christian Ehringfeld | }
|
|
e0e1ead8 | Christian Ehringfeld | bool EntityManager::executeQuery(const QString &query) {
|
|
94bf67c7 | Christian Ehringfeld | return this->db->exec(query);
|
|
e0e1ead8 | Christian Ehringfeld | }
|
|
b0bf458e | Christian Ehringfeld | bool EntityManager::checkTable(const QSharedPointer<Entity> &entity) {
|
|
bool rc = true;
|
|||
df1e56bd | Christian Ehringfeld | if (!this->schema->containsTable(entity->getTablename())
|
|
&& this->schema->getQueryBuilder()->createTable(entity)) {
|
|||
this->schema->getTableSchema(entity->getTablename(), true);
|
|||
rc = this->schema->getTables().contains(entity->getTablename());
|
|||
b0bf458e | Christian Ehringfeld | }
|
|
return rc;
|
|||
}
|
|||
9d05e414 | Christian Ehringfeld | ||
7e233492 | Christian Ehringfeld | QSharedPointer<Database> EntityManager::getDb() const {
|
|
return db;
|
|||
}
|
|||
void EntityManager::setDb(const QSharedPointer<Database> &value) {
|
|||
db = value;
|
|||
}
|
|||
QSharedPointer<Schema> EntityManager::getSchema() const {
|
|||
return schema;
|
|||
}
|
|||
4f3b13f3 | Christian Ehringfeld | void EntityManager::refresh(QSharedPointer<Entity> &entity) {
|
|
b5d490c7 | Christian Ehringfeld | entity = this->findById(entity->getProperty(
|
|
abb9e8c5 | Christian Ehringfeld | entity->getPrimaryKey()).toLongLong(),
|
|
e8d1537c | Christian Ehringfeld | EntityHelper::getClassName(entity.data()));
|
|
4f3b13f3 | Christian Ehringfeld | }
|
|
7e233492 | Christian Ehringfeld | void EntityManager::setSchema(const QSharedPointer<Schema> &value) {
|
|
schema = value;
|
|||
}
|
|||
506067a2 | Christian Ehringfeld | QList<QHash<QString, QVariant> > EntityManager::selectByQuery(Query &query) {
|
|
QSqlQuery q = this->queryInterpreter->build(query);
|
|||
return this->convertQueryResult(q);
|
|||
}
|
|||
QList<QHash<QString, QVariant> > EntityManager::selectBySql(
|
|||
const QString &sql) {
|
|||
QSqlQuery q = this->db->select(sql);
|
|||
return this->convertQueryResult(q);
|
|||
}
|
|||
qint8 EntityManager::count(Query &query) {
|
|||
qint8 rc = 0;
|
|||
query.appendSelect("COUNT(*)");
|
|||
QSqlQuery q = this->queryInterpreter->build(query);
|
|||
this->db->select(q);
|
|||
if (q.next()) {
|
|||
rc = q.value(0).toInt();
|
|||
}
|
|||
return rc;
|
|||
}
|
|||
7e233492 | Christian Ehringfeld | QString EntityManager::createConnection() {
|
|
QStringList l = EntityManager::getConnectionNames();
|
|||
QString conName = "";
|
|||
bool ok = false;
|
|||
qint16 i = 0;
|
|||
while (!ok) {
|
|||
if (l.contains("con" + QString::number(i))) {
|
|||
++i;
|
|||
} else {
|
|||
l.append("con" + QString::number(i));
|
|||
ok = true;
|
|||
conName = "con" + QString::number(i);
|
|||
EntityManager::setConnectionNames(l);
|
|||
}
|
|||
}
|
|||
return conName;
|
|||
}
|
|||
void EntityManager::removeConnectionName(const QString &name) {
|
|||
EntityManager::connectionNames.removeOne(name);
|
|||
}
|
|||
f5087482 | Christian Ehringfeld | QSharedPointer<Entity> EntityManager::findById(const qint64 &id,
|
|
66704054 | Christian Ehringfeld | QSharedPointer<Entity> &e,
|
|
const bool refresh) {
|
|||
2d9fab10 | Christian Ehringfeld | QSharedPointer<Entity> r;
|
|
df1e56bd | Christian Ehringfeld | if (!e.isNull() && (refresh
|
|
e8d1537c | Christian Ehringfeld | || !(r = this->cache.get(id, EntityHelper::getClassname(e.data()))))) {
|
|
df1e56bd | Christian Ehringfeld | e->setId(id);
|
|
auto map = this->findByPk(id, e);
|
|||
e8d1537c | Christian Ehringfeld | r = this->convert(map, EntityHelper::getClassname(e.data()), refresh);
|
|
2d9fab10 | Christian Ehringfeld | }
|
|
return r;
|
|||
}
|
|||
e0e1ead8 | Christian Ehringfeld | QSharedPointer<Entity> EntityManager::findById(const qint64 &id,
|
|
66704054 | Christian Ehringfeld | const QString &classname) {
|
|
f5087482 | Christian Ehringfeld | QSharedPointer<Entity> e = QSharedPointer<Entity>
|
|
66704054 | Christian Ehringfeld | (EntityInstanceFactory::createInstance(classname));
|
|
2d9fab10 | Christian Ehringfeld | return this->findById(id, e);
|
|
}
|
|||
e0e1ead8 | Christian Ehringfeld | void EntityManager::manyToOne(const QSharedPointer<Entity> &entity,
|
|
const QVariant &id,
|
|||
4f3b13f3 | Christian Ehringfeld | const QMetaProperty &property, const bool refresh) {
|
|
a06633f7 | Christian Ehringfeld | qint64 convertedId = -1;
|
|
bool ok = false;
|
|||
df1e56bd | Christian Ehringfeld | if ((convertedId = id.toLongLong(&ok)) && ok && convertedId > -1) {
|
|
e0e1ead8 | Christian Ehringfeld | QString className = EntityInstanceFactory::extractEntityType(
|
|
66704054 | Christian Ehringfeld | property.typeName());
|
|
706de2e8 | Christian Ehringfeld | QSharedPointer<Entity> ptr = QSharedPointer<Entity>();
|
|
4f3b13f3 | Christian Ehringfeld | if (refresh || !(this->cache.contains(convertedId, className)
|
|
afde9013 | Christian Ehringfeld | && (ptr = this->cache.get(convertedId, className)) && ptr)) {
|
|
706de2e8 | Christian Ehringfeld | ptr = this->findById(convertedId, className);
|
|
}
|
|||
e8d1537c | Christian Ehringfeld | EntityHelper::setProperty(entity, ptr, property);
|
|
a06633f7 | Christian Ehringfeld | }
|
|
}
|
|||
e0e1ead8 | Christian Ehringfeld | void EntityManager::oneToMany(const QSharedPointer<Entity> &entity,
|
|
const Relation &r,
|
|||
4f3b13f3 | Christian Ehringfeld | const QMetaProperty &property, const bool refresh) {
|
|
abb9e8c5 | Christian Ehringfeld | if (entity.data() && entity->getId() > -1) {
|
|
df1e56bd | Christian Ehringfeld | auto e = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(
|
|
EntityInstanceFactory::extractEntityType(
|
|||
property.typeName())));
|
|||
if (e) {
|
|||
QSqlQuery q = this->schema->getQueryBuilder()->oneToMany(e->getTablename(),
|
|||
66704054 | Christian Ehringfeld | this->schema->getQueryBuilder()->generateColumnNameID(r.getMappedBy()),
|
|
entity->getId());
|
|||
df1e56bd | Christian Ehringfeld | auto listMap = this->convertQueryResult(q);
|
|
66704054 | Christian Ehringfeld | auto entities = this->convert(listMap, EntityHelper::getClassname(e.data()),
|
|
refresh);
|
|||
e8d1537c | Christian Ehringfeld | EntityHelper::setListProperty(entity, entities, property);
|
|
df1e56bd | Christian Ehringfeld | }
|
|
706de2e8 | Christian Ehringfeld | }
|
|
}
|
|||
a06633f7 | Christian Ehringfeld | ||
c599658a | Christian Ehringfeld | ||
e0e1ead8 | Christian Ehringfeld | void EntityManager::oneToOne(const QSharedPointer<Entity> &entity,
|
|
f5b682e0 | Christian Ehringfeld | const Relation &r, const QMetaProperty &property, const bool refresh,
|
|
c599658a | Christian Ehringfeld | const QVariant &id) {
|
|
if (r.getMappedBy().isEmpty()) {
|
|||
this->manyToOne(entity, id, property);
|
|||
} else {
|
|||
df1e56bd | Christian Ehringfeld | auto e = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(
|
|
EntityInstanceFactory::extractEntityType(property.typeName())));
|
|||
if (e) {
|
|||
QSqlQuery q = this->schema->getQueryBuilder()->oneToMany(
|
|||
66704054 | Christian Ehringfeld | e->getTablename(),
|
|
this->schema->getQueryBuilder()->generateColumnNameID(
|
|||
r.getMappedBy()),
|
|||
entity->getProperty(entity->getPrimaryKey()).toLongLong(), 1);
|
|||
df1e56bd | Christian Ehringfeld | auto listMap = this->convertQueryResult(q);
|
|
66704054 | Christian Ehringfeld | auto entities = this->convert(listMap, EntityHelper::getClassname(e.data()),
|
|
refresh);
|
|||
df1e56bd | Christian Ehringfeld | if (!entities.isEmpty()) {
|
|
QSharedPointer<Entity> ptr = entities.at(0);
|
|||
e8d1537c | Christian Ehringfeld | EntityHelper::setProperty(entity, ptr, property);
|
|
df1e56bd | Christian Ehringfeld | }
|
|
c599658a | Christian Ehringfeld | }
|
|
}
|
|||
}
|
|||
98b5b08d | Christian Ehringfeld | bool EntityManager::canPersistRelation(const Relation &relation,
|
|
const RelationType &r, const QVariant &var) const {
|
|||
f6a3fe0a | Christian Ehringfeld | return relation.getType() == r && var.canConvert<QVariantList>();
|
|
4f3b13f3 | Christian Ehringfeld | }
|
|
df1e56bd | Christian Ehringfeld | void EntityManager::savePrePersistedRelations(const QSharedPointer<Entity>
|
|
66704054 | Christian Ehringfeld | &entity) {
|
|
e8d1537c | Christian Ehringfeld | auto relations = EntityHelper::getRelationProperties(entity.data());
|
|
4f3b13f3 | Christian Ehringfeld | auto iterator = relations.constBegin();
|
|
while (iterator != relations.constEnd()) {
|
|||
const Relation r = iterator.key();
|
|||
auto var = iterator.value().read(entity.data());
|
|||
df1e56bd | Christian Ehringfeld | if (!var.isNull()) {
|
|
da565582 | Christian Ehringfeld | if (r.getType() == RelationType::MANY_TO_ONE) {
|
|
df1e56bd | Christian Ehringfeld | auto e = EntityInstanceFactory::castQVariant(var);
|
|
if (this->shouldBeSaved(e, r)) {
|
|||
this->save(e);
|
|||
66704054 | Christian Ehringfeld | auto fkProp = EntityHelper::mappedProperty(r, e);
|
|
if (fkProp.isValid()) {
|
|||
EntityHelper::addEntityToListProperty(e, entity, fkProp);
|
|||
e8d1537c | Christian Ehringfeld | }
|
|
df1e56bd | Christian Ehringfeld | }
|
|
66704054 | Christian Ehringfeld | } else if (r.getType() == RelationType::ONE_TO_ONE
|
|
&& r.getMappedBy().isEmpty()) {
|
|||
e8d1537c | Christian Ehringfeld | auto e = EntityInstanceFactory::castQVariant(var);
|
|
this->save(e);
|
|||
66704054 | Christian Ehringfeld | auto prop = EntityHelper::mappedProperty(r, e);
|
|
if (prop.isValid()) {
|
|||
EntityHelper::setProperty(e, entity, prop);
|
|||
9d62f4aa | Christian Ehringfeld | }
|
|
e24791d7 | Christian Ehringfeld | }
|
|
df1e56bd | Christian Ehringfeld | }
|
|
++iterator;
|
|||
}
|
|||
}
|
|||
void EntityManager::savePostPersistedRelations(const QSharedPointer<Entity>
|
|||
66704054 | Christian Ehringfeld | &entity) {
|
|
e8d1537c | Christian Ehringfeld | auto relations = EntityHelper::getRelationProperties(entity.data());
|
|
df1e56bd | Christian Ehringfeld | auto iterator = relations.constBegin();
|
|
while (iterator != relations.constEnd()) {
|
|||
const Relation r = iterator.key();
|
|||
auto var = iterator.value().read(entity.data());
|
|||
if (!var.isNull()) {
|
|||
da565582 | Christian Ehringfeld | if (this->canPersistRelation(r, RelationType::MANY_TO_MANY, var)) {
|
|
df1e56bd | Christian Ehringfeld | this->persistManyToMany(entity, r, var);
|
|
da565582 | Christian Ehringfeld | } else if (this->canPersistRelation(r, RelationType::ONE_TO_MANY, var)) {
|
|
df1e56bd | Christian Ehringfeld | QList<QSharedPointer<Entity>> list = EntityInstanceFactory::castQVariantList(
|
|
66704054 | Christian Ehringfeld | var);
|
|
if (!list.isEmpty()) {
|
|||
auto fkProp = EntityHelper::mappedProperty(r, list.at(0));
|
|||
42d32a68 | Christian Ehringfeld | for (int var = 0; var < list.size(); ++var) {
|
|
auto e = list.at(var);
|
|||
if (this->shouldBeSaved(e, r)) {
|
|||
this->save(e);
|
|||
66704054 | Christian Ehringfeld | if (fkProp.isValid()) {
|
|
EntityHelper::addEntityToListProperty(e, entity, fkProp);
|
|||
42d32a68 | Christian Ehringfeld | }
|
|
}
|
|||
df1e56bd | Christian Ehringfeld | }
|
|
e8d1537c | Christian Ehringfeld | }
|
|
da565582 | Christian Ehringfeld | } else if (r.getType() == RelationType::ONE_TO_ONE
|
|
&& !r.getMappedBy().isEmpty()) {
|
|||
e8d1537c | Christian Ehringfeld | auto e = EntityInstanceFactory::castQVariant(var);
|
|
this->save(e);
|
|||
66704054 | Christian Ehringfeld | auto fkProp = EntityHelper::mappedProperty(r, e);
|
|
if (fkProp.isValid()) {
|
|||
EntityHelper::addEntityToListProperty(e, entity, fkProp);
|
|||
42d32a68 | Christian Ehringfeld | }
|
|
4f3b13f3 | Christian Ehringfeld | }
|
|
}
|
|||
396a60d7 | Christian Ehringfeld | ++iterator;
|
|
4f3b13f3 | Christian Ehringfeld | }
|
|
91ed1164 | Christian Ehringfeld | }
|
|
void EntityManager::persistMappedByRelation(const QList<QSharedPointer<Entity> >
|
|||
66704054 | Christian Ehringfeld | &list, QSqlQuery &q, const QSharedPointer<Entity> &entity,
|
|
const QSharedPointer<Entity> &ptr, const Relation &r,
|
|||
const QString &tblName) {
|
|||
91ed1164 | Christian Ehringfeld | q.clear();
|
|
66704054 | Christian Ehringfeld | QList<QSharedPointer<Entity>> saved =
|
|
r.getCascadeType().contains(CascadeType::ALL) ||
|
|||
r.getCascadeType().contains(CascadeType::MERGE) ||
|
|||
r.getCascadeType().contains(CascadeType::PERSIST) ?
|
|||
this->saveRelationEntities(list, r) : list;
|
|||
719a0ba1 | Christian Ehringfeld | this->db->startTransaction();
|
|
abb9e8c5 | Christian Ehringfeld | auto builder = this->schema->getQueryBuilder();
|
|
q = builder->manyToManyInsert(tblName,
|
|||
builder->generateManyToManyColumnName(entity),
|
|||
builder->generateManyToManyColumnName(ptr));
|
|||
b5d490c7 | Christian Ehringfeld | q.bindValue(0, entity->getProperty(entity->getPrimaryKey()));
|
|
e8d1537c | Christian Ehringfeld | auto prop = EntityHelper::mappedProperty(r, ptr);
|
|
66704054 | Christian Ehringfeld | bool propertyIsValid = this->isRelationPropertyValid(prop, r, entity, ptr);
|
|
3fd96253 | Christian Ehringfeld | QSharedPointer<Entity> item;
|
|
e24791d7 | Christian Ehringfeld | for (int var = 0; var < saved.size(); ++var) {
|
|
3fd96253 | Christian Ehringfeld | item = list.at(var);
|
|
b5d490c7 | Christian Ehringfeld | if (item->getProperty(item->getPrimaryKey()).toLongLong() > -1) {
|
|
q.bindValue(1, item->getProperty(ptr->getPrimaryKey()));
|
|||
66704054 | Christian Ehringfeld | bool ok = this->db->exec(q);
|
|
if (ok && propertyIsValid) {
|
|||
c9e79b83 | Christian Ehringfeld | EntityHelper::addEntityToListProperty(item, entity, prop);
|
|
e24791d7 | Christian Ehringfeld | }
|
|
91ed1164 | Christian Ehringfeld | }
|
|
}
|
|||
719a0ba1 | Christian Ehringfeld | if (!this->db->commitTransaction()) {
|
|
this->db->rollbackTransaction();
|
|||
}
|
|||
4f3b13f3 | Christian Ehringfeld | }
|
|
a06633f7 | Christian Ehringfeld | ||
66704054 | Christian Ehringfeld | ||
bool EntityManager::isRelationPropertyValid(const QMetaProperty &prop,
|
|||
const Relation &r, const QSharedPointer<Entity> &e,
|
|||
const QSharedPointer<Entity> &relatedEntity) {
|
|||
bool propertyIsValid = prop.isValid() && prop.isReadable() && prop.isWritable();
|
|||
if (!propertyIsValid) {
|
|||
fc71bc10 | Christian Ehringfeld | qWarning() << "Relation is incomplete:" << r.getPropertyName();
|
|
qWarning() << "Involved entities: " << EntityHelper::getClassName(
|
|||
bcd2b697 | Christian Ehringfeld | e.data()) <<
|
|
"(MainEntitiy) and " << EntityHelper::getClassName(relatedEntity.data());
|
|||
66704054 | Christian Ehringfeld | }
|
|
return propertyIsValid;
|
|||
}
|
|||
e24791d7 | Christian Ehringfeld | bool EntityManager::shouldBeSaved(QSharedPointer<Entity> &entity,
|
|
const Relation &r) {
|
|||
da565582 | Christian Ehringfeld | return entity && (r.getCascadeType().contains(CascadeType::ALL)
|
|
b5d490c7 | Christian Ehringfeld | || (entity->getProperty(entity->getPrimaryKey()) > -1
|
|
da565582 | Christian Ehringfeld | && r.getCascadeType().contains(CascadeType::MERGE))
|
|
b5d490c7 | Christian Ehringfeld | || (entity->getProperty(entity->getPrimaryKey()) <= -1
|
|
da565582 | Christian Ehringfeld | && r.getCascadeType().contains(CascadeType::PERSIST)));
|
|
e24791d7 | Christian Ehringfeld | }
|
|
void EntityManager::removeRelations(const QSharedPointer<Entity> &entity) {
|
|||
e8d1537c | Christian Ehringfeld | auto props = EntityHelper::getRelationProperties(entity.data());
|
|
e24791d7 | Christian Ehringfeld | auto iterator = props.constBegin();
|
|
while (iterator != props.constEnd()) {
|
|||
const Relation r = iterator.key();
|
|||
2075db87 | Christian Ehringfeld | auto property = iterator.value();
|
|
auto var = property.read(entity.data());
|
|||
da565582 | Christian Ehringfeld | if (r.getType() == RelationType::MANY_TO_MANY) {
|
|
2075db87 | Christian Ehringfeld | this->removeManyToManyEntityList(entity, r, var);
|
|
da565582 | Christian Ehringfeld | } else if (r.getType() == RelationType::ONE_TO_MANY) {
|
|
if (r.getCascadeType().contains(CascadeType::REMOVE)
|
|||
|| r.getCascadeType().contains(CascadeType::ALL)) {
|
|||
2075db87 | Christian Ehringfeld | this->removeEntityList(var);
|
|
} else {
|
|||
this->setNullOneToManyRelation(var, r);
|
|||
}
|
|||
da565582 | Christian Ehringfeld | } else if (r.getType() == RelationType::MANY_TO_ONE
|
|
|| r.getType() == RelationType::MANY_TO_ONE) {
|
|||
2075db87 | Christian Ehringfeld | this->setNullEntityPropertyRelation(var, r);
|
|
e24791d7 | Christian Ehringfeld | }
|
|
++iterator;
|
|||
}
|
|||
}
|
|||
2075db87 | Christian Ehringfeld | ||
void EntityManager::setNullOneToManyRelation(QVariant &var, const Relation &r) {
|
|||
df1e56bd | Christian Ehringfeld | if (!r.getMappedBy().isEmpty() && !var.isNull()
|
|
&& var.canConvert<QVariantList>()) {
|
|||
auto list = EntityInstanceFactory::castQVariantList(var);
|
|||
if (!list.isEmpty()) {
|
|||
e8d1537c | Christian Ehringfeld | auto metas = EntityHelper::getMetaProperties(list.at(0).data());
|
|
df1e56bd | Christian Ehringfeld | if (metas.contains(r.getMappedBy())) {
|
|
for (int var = 0; var < list.size(); ++var) {
|
|||
auto entity = list.at(var);
|
|||
e8d1537c | Christian Ehringfeld | EntityHelper::setProperty(entity, QSharedPointer<Entity>(),
|
|
42d32a68 | Christian Ehringfeld | metas.value(r.getMappedBy()));
|
|
df1e56bd | Christian Ehringfeld | this->save(entity);
|
|
2075db87 | Christian Ehringfeld | }
|
|
}
|
|||
}
|
|||
}
|
|||
}
|
|||
void EntityManager::setNullEntityPropertyRelation(QVariant &var,
|
|||
66704054 | Christian Ehringfeld | const Relation &r) {
|
|
da565582 | Christian Ehringfeld | if (r.getCascadeType().contains(CascadeType::REMOVE)
|
|
|| r.getCascadeType().contains(CascadeType::ALL)) {
|
|||
2075db87 | Christian Ehringfeld | this->removeEntity(var);
|
|
df1e56bd | Christian Ehringfeld | } else if (!r.getMappedBy().isEmpty() && !var.isNull()) {
|
|
auto e = EntityInstanceFactory::castQVariant(var);
|
|||
e8d1537c | Christian Ehringfeld | auto metas = EntityHelper::getMetaProperties(e.data());
|
|
df1e56bd | Christian Ehringfeld | if (metas.contains(r.getMappedBy())) {
|
|
66704054 | Christian Ehringfeld | EntityHelper::setProperty(e, QSharedPointer<Entity>(),
|
|
metas.value(r.getMappedBy()));
|
|||
df1e56bd | Christian Ehringfeld | this->save(e);
|
|
2075db87 | Christian Ehringfeld | }
|
|
}
|
|||
}
|
|||
void EntityManager::removeEntity(QVariant &var) {
|
|||
df1e56bd | Christian Ehringfeld | if (!var.isNull()) {
|
|
auto e = EntityInstanceFactory::castQVariant(var);
|
|||
2075db87 | Christian Ehringfeld | this->remove(e);
|
|
}
|
|||
}
|
|||
void EntityManager::removeEntityList(QVariant &var) {
|
|||
df1e56bd | Christian Ehringfeld | if (var.canConvert<QVariantList>()) {
|
|
auto list = EntityInstanceFactory::castQVariantList(var);
|
|||
2075db87 | Christian Ehringfeld | for (int var = 0; var < list.size(); ++var) {
|
|
auto entity = list.at(var);
|
|||
this->remove(entity);
|
|||
}
|
|||
}
|
|||
}
|
|||
void EntityManager::removeManyToManyEntityList(const QSharedPointer<Entity> &e,
|
|||
66704054 | Christian Ehringfeld | const Relation &r,
|
|
QVariant &var) {
|
|||
df1e56bd | Christian Ehringfeld | if (!var.isNull() && var.canConvert<QVariantList>()) {
|
|
auto list = EntityInstanceFactory::castQVariantList(var);
|
|||
2075db87 | Christian Ehringfeld | if (!list.isEmpty()) {
|
|
abb9e8c5 | Christian Ehringfeld | auto builder = this->schema->getQueryBuilder();
|
|
2075db87 | Christian Ehringfeld | auto ptr = list.at(0);
|
|
09b2592d | Christian Ehringfeld | QString tblName = builder->generateManyToManyTableName(e, ptr, r);
|
|
abb9e8c5 | Christian Ehringfeld | if (this->schema->getTables().contains(tblName)) {
|
|
QSqlQuery q = builder->manyToManyDelete(
|
|||
66704054 | Christian Ehringfeld | tblName, builder->generateManyToManyColumnName(e),
|
|
e->getProperty(e->getPrimaryKey()).toLongLong());
|
|||
48245e6a | Christian Ehringfeld | if (this->db->exec(q)) {
|
|
bool refresh = r.getCascadeType().contains(CascadeType::REFRESH)
|
|||
|| r.getCascadeType().contains(CascadeType::ALL);
|
|||
bool remove = r.getCascadeType().contains(CascadeType::REMOVE)
|
|||
|| r.getCascadeType().contains(CascadeType::ALL);
|
|||
auto fkProp = EntityHelper::mappedProperty(r, ptr);
|
|||
2075db87 | Christian Ehringfeld | for (int var = 0; var < list.size(); ++var) {
|
|
auto entity = list.at(var);
|
|||
if (remove) {
|
|||
this->remove(entity);
|
|||
} else if (refresh) {
|
|||
48245e6a | Christian Ehringfeld | EntityHelper::removeEntityFromListProperty(entity, e, fkProp);
|
|
2075db87 | Christian Ehringfeld | }
|
|
}
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|
|||
e24791d7 | Christian Ehringfeld | QList<QSharedPointer<Entity>> EntityManager::saveRelationEntities(
|
|
66704054 | Christian Ehringfeld | const QList<QSharedPointer<Entity> > &list, const Relation &r) {
|
|
e24791d7 | Christian Ehringfeld | QList<QSharedPointer<Entity>> saved = QList<QSharedPointer<Entity>>();
|
|
91ed1164 | Christian Ehringfeld | QSharedPointer<Entity> ptr;
|
|
for (int var = 0; var < list.size(); ++var) {
|
|||
ptr = list.at(var);
|
|||
e24791d7 | Christian Ehringfeld | if (this->shouldBeSaved(ptr, r) && this->save(ptr)) {
|
|
saved.append(ptr);
|
|||
91ed1164 | Christian Ehringfeld | }
|
|
}
|
|||
e24791d7 | Christian Ehringfeld | return saved;
|
|
91ed1164 | Christian Ehringfeld | }
|
|
98b5b08d | Christian Ehringfeld | ||
void EntityManager::persistManyToMany(const QSharedPointer<Entity> &entity,
|
|||
abb9e8c5 | Christian Ehringfeld | const Relation &r, QVariant &property) {
|
|
f6a3fe0a | Christian Ehringfeld | auto list = property.value<QList<QVariant>>();
|
|
df1e56bd | Christian Ehringfeld | if (!list.isEmpty() && !(list.at(0).isNull())) {
|
|
auto var = list.at(0);
|
|||
auto ptr = EntityInstanceFactory::castQVariant(var);
|
|||
auto builder = this->schema->getQueryBuilder();
|
|||
09b2592d | Christian Ehringfeld | QString tblName = builder->generateManyToManyTableName(entity, ptr, r);
|
|
df1e56bd | Christian Ehringfeld | if (this->schema->getTables().contains(tblName)) {
|
|
QSqlQuery q = builder->manyToManyDelete(
|
|||
66704054 | Christian Ehringfeld | tblName, builder->generateManyToManyColumnName(entity),
|
|
entity->getProperty(entity->getPrimaryKey()).toLongLong());
|
|||
94bf67c7 | Christian Ehringfeld | if (this->db->exec(q)) {
|
|
df1e56bd | Christian Ehringfeld | auto nList = EntityInstanceFactory::castQVariantList(property);
|
|
this->persistMappedByRelation(nList, q, entity, ptr, r, tblName);
|
|||
98b5b08d | Christian Ehringfeld | }
|
|
df1e56bd | Christian Ehringfeld | } else {
|
|
66704054 | Christian Ehringfeld | this->missingManyToManyTable(tblName, entity, r);
|
|
98b5b08d | Christian Ehringfeld | }
|
|
}
|
|||
}
|
|||
66704054 | Christian Ehringfeld | void EntityManager::missingManyToManyTable(const QString &tblName,
|
|
const QSharedPointer<Entity> &e, const Relation &r) {
|
|||
fc71bc10 | Christian Ehringfeld | qWarning() << "MANY_TO_MANY Table " << tblName << " is missing";
|
|
bcd2b697 | Christian Ehringfeld | qWarning() << "Entity " << EntityHelper::getClassName(e.data()) <<
|
|
" is affected";
|
|||
fc71bc10 | Christian Ehringfeld | qWarning() << "Relation of property: " << r.getPropertyName();
|
|
66704054 | Christian Ehringfeld | /**
|
|
@todo wait for Qt 5.5.1
|
|||
@see https://codereview.qt-project.org/#/c/122232/
|
|||
*/
|
|||
//qDebug() << "RelationType:" << r.getType() << " MappedBy:" << r.getMappedBy();
|
|||
}
|
|||
98b5b08d | Christian Ehringfeld | ||
e0e1ead8 | Christian Ehringfeld | void EntityManager::manyToMany(const QSharedPointer<Entity> &entity,
|
|
09b2592d | Christian Ehringfeld | const QMetaProperty &property, const Relation &relation, const bool refresh) {
|
|
df1e56bd | Christian Ehringfeld | QSharedPointer<Entity> secEntityPtr = QSharedPointer<Entity>
|
|
66704054 | Christian Ehringfeld | (EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(
|
|
QString(property.typeName()))));
|
|||
abb9e8c5 | Christian Ehringfeld | auto builder = this->schema->getQueryBuilder();
|
|
df1e56bd | Christian Ehringfeld | if (secEntityPtr) {
|
|
09b2592d | Christian Ehringfeld | QString tblName = builder->generateManyToManyTableName(entity, secEntityPtr,
|
|
66704054 | Christian Ehringfeld | relation);
|
|
abb9e8c5 | Christian Ehringfeld | if (this->schema->getTables().contains(tblName)) {
|
|
QSqlQuery q = builder->manyToMany(tblName,
|
|||
builder->generateManyToManyColumnName(entity),
|
|||
2ce163c3 | Christian Ehringfeld | entity->getProperty(entity->getPrimaryKey()).toLongLong());
|
|
c599658a | Christian Ehringfeld | auto listMap = this->convertQueryResult(q);
|
|
9d62f4aa | Christian Ehringfeld | QSharedPointer<Entity> e = QSharedPointer<Entity>();
|
|
2ce163c3 | Christian Ehringfeld | for (int var = 0; var < listMap.size(); ++var) {
|
|
auto id = listMap.at(var).value(builder->generateManyToManyColumnName(
|
|||
secEntityPtr));
|
|||
if (!refresh
|
|||
66704054 | Christian Ehringfeld | && this->cache.contains(id.toLongLong(),
|
|
EntityHelper::getClassname(secEntityPtr.data()))) {
|
|||
e = this->cache.get(id.toLongLong(),
|
|||
EntityHelper::getClassname(secEntityPtr.data()));
|
|||
2ce163c3 | Christian Ehringfeld | } else {
|
|
66704054 | Christian Ehringfeld | e = this->findById(id.toLongLong(),
|
|
EntityHelper::getClassname(secEntityPtr.data()));
|
|||
9d62f4aa | Christian Ehringfeld | }
|
|
66704054 | Christian Ehringfeld | if (e) {
|
|
EntityHelper::addEntityToListProperty(entity, e, property);
|
|||
2ce163c3 | Christian Ehringfeld | }
|
|
f9cef58f | Christian Ehringfeld | }
|
|
c599658a | Christian Ehringfeld | } else {
|
|
66704054 | Christian Ehringfeld | this->missingManyToManyTable(tblName, entity, relation);
|
|
c599658a | Christian Ehringfeld | }
|
|
706de2e8 | Christian Ehringfeld | }
|
|
a06633f7 | Christian Ehringfeld | }
|
|
e0e1ead8 | Christian Ehringfeld | QList<QSharedPointer<Entity> > EntityManager::findEntityByAttributes(
|
|
66704054 | Christian Ehringfeld | const QSharedPointer<Entity>
|
|
&entity,
|
|||
bool ignoreID) {
|
|||
1e213c09 | Christian Ehringfeld | auto maps = this->findAllByAttributes(entity, ignoreID);
|
|
e8d1537c | Christian Ehringfeld | return this->convert(maps, EntityHelper::getClassname(entity.data()));
|
|
7e233492 | Christian Ehringfeld | }
|
|
3820ae33 | Christian Ehringfeld | /**
|
|
244c6d53 | Christian Ehringfeld | * @todo should be an insert statement with many values
|
|
3820ae33 | Christian Ehringfeld | * @brief EntityManager::create
|
|
* @param entities
|
|||
* @return
|
|||
*/
|
|||
244c6d53 | Christian Ehringfeld | bool EntityManager::create(QList<QSharedPointer<Entity> > entities,
|
|
const bool persistRelations) {
|
|||
3820ae33 | Christian Ehringfeld | bool ok = true;
|
|
foreach (QSharedPointer<Entity> ent, entities) {
|
|||
244c6d53 | Christian Ehringfeld | ok = this->create(ent, persistRelations);
|
|
3820ae33 | Christian Ehringfeld | if (!ok) {
|
|
break;
|
|||
}
|
|||
}
|
|||
return ok;
|
|||
d84c91e7 | Christian Ehringfeld | }
|
|
244c6d53 | Christian Ehringfeld | bool EntityManager::create(QSharedPointer<Entity> &entity,
|
|
696666eb | Christian Ehringfeld | const bool persistRelations, const bool checkDuplicate) {
|
|
dc6b13b4 | Christian Ehringfeld | bool rc = false;
|
|
4677e852 | Christian Ehringfeld | if (this->checkTable(entity) && !(checkDuplicate && this->count(entity) > 0)) {
|
|
df1e56bd | Christian Ehringfeld | if (persistRelations) {
|
|
this->savePrePersistedRelations(entity);
|
|||
}
|
|||
this->db->startTransaction();
|
|||
abb9e8c5 | Christian Ehringfeld | QList<QSqlQuery> q = this->schema->getQueryBuilder()->create(
|
|
66704054 | Christian Ehringfeld | entity);
|
|
99140fe9 | Christian Ehringfeld | bool first = true;
|
|
94bf67c7 | Christian Ehringfeld | QVariant id = -1;
|
|
99140fe9 | Christian Ehringfeld | for (int var = 0; var < q.size(); ++var) {
|
|
auto query = q.at(var);
|
|||
94bf67c7 | Christian Ehringfeld | if (!first) {
|
|
this->schema->getQueryBuilder()->bindValue(entity->getPrimaryKey(), id, query);
|
|||
}
|
|||
abb9e8c5 | Christian Ehringfeld | rc = this->db->exec(query);
|
|
afde9013 | Christian Ehringfeld | if (!rc) {
|
|
bcd2b697 | Christian Ehringfeld | qWarning() << "class is erroneous:" << EntityHelper::getClassname(
|
|
entity.data());
|
|||
afde9013 | Christian Ehringfeld | break;
|
|
}
|
|||
99140fe9 | Christian Ehringfeld | if (first) {
|
|
94bf67c7 | Christian Ehringfeld | id = query.lastInsertId();
|
|
entity->setProperty(entity->getPrimaryKey(), id);
|
|||
99140fe9 | Christian Ehringfeld | first = false;
|
|
}
|
|||
}
|
|||
afde9013 | Christian Ehringfeld | if (!rc || !this->db->commitTransaction()) {
|
|
99140fe9 | Christian Ehringfeld | this->db->rollbackTransaction();
|
|
abb9e8c5 | Christian Ehringfeld | entity->setId(-1);
|
|
99140fe9 | Christian Ehringfeld | rc = false;
|
|
} else {
|
|||
2ce163c3 | Christian Ehringfeld | this->cache.insert(entity);
|
|
244c6d53 | Christian Ehringfeld | if (persistRelations) {
|
|
df1e56bd | Christian Ehringfeld | this->savePostPersistedRelations(entity);
|
|
244c6d53 | Christian Ehringfeld | }
|
|
99140fe9 | Christian Ehringfeld | rc = true;
|
|
7e233492 | Christian Ehringfeld | }
|
|
}
|
|||
dc6b13b4 | Christian Ehringfeld | return rc;
|
|
7e233492 | Christian Ehringfeld | }
|
|
9d05e414 | Christian Ehringfeld | ||
afde9013 | Christian Ehringfeld | bool EntityManager::merge(QSharedPointer<Entity> &entity, bool withRelations) {
|
|
c9f21778 | Christian Ehringfeld | if (entity->getId() > -1) {
|
|
df1e56bd | Christian Ehringfeld | if (withRelations) {
|
|
this->savePrePersistedRelations(entity);
|
|||
}
|
|||
afde9013 | Christian Ehringfeld | this->db->startTransaction();
|
|
abb9e8c5 | Christian Ehringfeld | QList<QSqlQuery> q = this->schema->getQueryBuilder()->merge(
|
|
66704054 | Christian Ehringfeld | entity);
|
|
bool ok = this->db->exec(q);
|
|||
afde9013 | Christian Ehringfeld | if (!ok || !this->db->commitTransaction()) {
|
|
this->db->rollbackTransaction();
|
|||
return false;
|
|||
} else if (ok && withRelations) {
|
|||
df1e56bd | Christian Ehringfeld | this->savePostPersistedRelations(entity);
|
|
afde9013 | Christian Ehringfeld | }
|
|
}
|
|||
return false;
|
|||
dc6b13b4 | Christian Ehringfeld | }
|
|
b0b8dac3 | Christian Ehringfeld | ||
6d91d381 | Christian Ehringfeld | QHash<QString, QVariant> EntityManager::findByPk(qint64 id,
|
|
66704054 | Christian Ehringfeld | const QSharedPointer<Entity>
|
|
&e) {
|
|||
abb9e8c5 | Christian Ehringfeld | QSqlQuery q = this->schema->getQueryBuilder()->find(id, e, 0,
|
|
66704054 | Christian Ehringfeld | e->getPrimaryKey());
|
|
706de2e8 | Christian Ehringfeld | auto listMap = this->convertQueryResult(q);
|
|
if (!listMap.isEmpty()) {
|
|||
return listMap.at(0);
|
|||
7e233492 | Christian Ehringfeld | }
|
|
706de2e8 | Christian Ehringfeld | return QHash<QString, QVariant>();
|
|
7e233492 | Christian Ehringfeld | }
|
|
f4e3904b | Christian Ehringfeld | ||
e0e1ead8 | Christian Ehringfeld | QList<QHash<QString, QVariant> > EntityManager::findAllByAttributes(
|
|
66704054 | Christian Ehringfeld | const QSharedPointer<Entity>
|
|
&entity,
|
|||
bool ignoreID) {
|
|||
abb9e8c5 | Christian Ehringfeld | QSqlQuery q = this->schema->getQueryBuilder()->findByAttributes(
|
|
66704054 | Christian Ehringfeld | entity, ignoreID);
|
|
dc6b13b4 | Christian Ehringfeld | return this->convertQueryResult(q);
|
|
}
|
|||
e0e1ead8 | Christian Ehringfeld | QList<QHash <QString, QVariant> > EntityManager::findAllByAttributes(
|
|
66704054 | Christian Ehringfeld | const QHash<QString, QVariant> &m, const QString &tblname, bool ignoreID) {
|
|
abb9e8c5 | Christian Ehringfeld | QSqlQuery q = this->schema->getQueryBuilder()->findByAttributes(m,
|
|
66704054 | Christian Ehringfeld | tblname, ignoreID);
|
|
7e233492 | Christian Ehringfeld | return this->convertQueryResult(q);
|
|
}
|
|||
e0e1ead8 | Christian Ehringfeld | QList<QHash<QString, QVariant> > EntityManager::convertQueryResult(
|
|
66704054 | Christian Ehringfeld | QSqlQuery &q) {
|
|
7e233492 | Christian Ehringfeld | QList<QHash <QString, QVariant> > listmap = QList<QHash <QString, QVariant> >();
|
|
this->db->select(q);
|
|||
QSqlRecord rec = q.record();
|
|||
QStringList l = QStringList();
|
|||
qint16 field_count = rec.count();
|
|||
for (int var = 0; var < field_count; ++var) {
|
|||
l.append(rec.fieldName(var));
|
|||
}
|
|||
QHash<QString, QVariant> map = QHash<QString, QVariant>();
|
|||
while (q.next()) {
|
|||
for (int var = 0; var < field_count; ++var) {
|
|||
map.insert(l.at(var), q.value(rec.indexOf(l.at(var))));
|
|||
}
|
|||
listmap.append(map);
|
|||
}
|
|||
return listmap;
|
|||
}
|
|||
f5087482 | Christian Ehringfeld | QList<QHash <QString, QVariant> > EntityManager::findAll(
|
|
66704054 | Christian Ehringfeld | const QSharedPointer<Entity> &e) {
|
|
abb9e8c5 | Christian Ehringfeld | QSqlQuery q = this->schema->getQueryBuilder()->findAll(e);
|
|
7e233492 | Christian Ehringfeld | return this->convertQueryResult(q);
|
|
}
|
|||
b0b8dac3 | Christian Ehringfeld | ||
e0e1ead8 | Christian Ehringfeld | void EntityManager::resolveRelations(const QSharedPointer<Entity> &entity,
|
|
4f3b13f3 | Christian Ehringfeld | const QHash<QString, QVariant> &map, const bool refresh) {
|
|
e8d1537c | Christian Ehringfeld | auto props = EntityHelper::getRelationProperties(entity.data());
|
|
2d9fab10 | Christian Ehringfeld | auto iterator = props.constBegin();
|
|
while (iterator != props.constEnd()) {
|
|||
const Relation r = iterator.key();
|
|||
const QMetaProperty property = iterator.value();
|
|||
df1e56bd | Christian Ehringfeld | QString colName = this->schema->getQueryBuilder()->generateColumnNameID(
|
|
66704054 | Christian Ehringfeld | r.getPropertyName());
|
|
a06633f7 | Christian Ehringfeld | switch (r.getType()) {
|
|
da565582 | Christian Ehringfeld | case RelationType::MANY_TO_ONE:
|
|
df1e56bd | Christian Ehringfeld | if (map.contains(colName)) {
|
|
this->manyToOne(entity, map.value(colName), property, refresh);
|
|||
a06633f7 | Christian Ehringfeld | }
|
|
break;
|
|||
da565582 | Christian Ehringfeld | case RelationType::MANY_TO_MANY:
|
|
09b2592d | Christian Ehringfeld | this->manyToMany(entity, property, r, refresh);
|
|
a06633f7 | Christian Ehringfeld | break;
|
|
da565582 | Christian Ehringfeld | case RelationType::ONE_TO_MANY:
|
|
4f3b13f3 | Christian Ehringfeld | this->oneToMany(entity, r, property, refresh);
|
|
a06633f7 | Christian Ehringfeld | break;
|
|
da565582 | Christian Ehringfeld | case RelationType::ONE_TO_ONE:
|
|
df1e56bd | Christian Ehringfeld | this->oneToOne(entity, r, property, refresh, map.value(colName));
|
|
a06633f7 | Christian Ehringfeld | break;
|
|
}
|
|||
++iterator;
|
|||
}
|
|||
}
|
|||
244c6d53 | Christian Ehringfeld | bool EntityManager::save(QSharedPointer<Entity> &entity,
|
|
const bool persistRelations) {
|
|||
c9f21778 | Christian Ehringfeld | if (entity->getProperty(entity->getPrimaryKey()).toLongLong() > -1) {
|
|
244c6d53 | Christian Ehringfeld | return this->merge(entity, persistRelations);
|
|
7e233492 | Christian Ehringfeld | } else {
|
|
244c6d53 | Christian Ehringfeld | return this->create(entity, persistRelations);
|
|
7e233492 | Christian Ehringfeld | }
|
|
}
|
|||
dc6b13b4 | Christian Ehringfeld | qint64 EntityManager::findId(QSharedPointer<Entity> &entity) {
|
|
qint64 r = -1;
|
|||
abb9e8c5 | Christian Ehringfeld | QSqlQuery q = this->schema->getQueryBuilder()->findId(entity);
|
|
dc6b13b4 | Christian Ehringfeld | this->db->select(q);
|
|
if (q.next()) {
|
|||
r = q.value(0).toLongLong();
|
|||
}
|
|||
return r;
|
|||
}
|
|||
586bb527 | Christian Ehringfeld | bool EntityManager::remove(QSharedPointer<Entity> &entity) {
|
|
7e233492 | Christian Ehringfeld | bool rc = false;
|
|
e24791d7 | Christian Ehringfeld | this->db->startTransaction();
|
|
this->removeRelations(entity);
|
|||
abb9e8c5 | Christian Ehringfeld | auto queries = this->schema->getQueryBuilder()->remove(entity);
|
|
66704054 | Christian Ehringfeld | bool ok = this->db->exec(queries);
|
|
95b60eb2 | Christian Ehringfeld | if (ok && this->db->commitTransaction()) {
|
|
9070a496 | Christian Ehringfeld | this->cache.remove(entity);
|
|
586bb527 | Christian Ehringfeld | entity.clear();
|
|
7e233492 | Christian Ehringfeld | rc = true;
|
|
66704054 | Christian Ehringfeld | } else {
|
|
this->db->rollbackTransaction();
|
|||
7e233492 | Christian Ehringfeld | }
|
|
return rc;
|
|||
}
|
|||
b0b8dac3 | Christian Ehringfeld | ||
8306a974 | Christian Ehringfeld | bool EntityManager::removeAll(QString tblname) {
|
|
abb9e8c5 | Christian Ehringfeld | return this->schema->getQueryBuilder()->removeAll(tblname).exec();
|
|
8306a974 | Christian Ehringfeld | }
|
|
b7446f4c | Christian Ehringfeld | bool EntityManager::createTable(const QSharedPointer<Entity> &entity,
|
|
bool createRelationTables) {
|
|||
abb9e8c5 | Christian Ehringfeld | return this->schema->getQueryBuilder()->createTable(entity,
|
|
66704054 | Christian Ehringfeld | createRelationTables);
|
|
9c2f773f | Christian Ehringfeld | }
|
|
506067a2 | Christian Ehringfeld | quint8 EntityManager::count(const QSharedPointer<Entity> &entity,
|
|
bool ignoreID) {
|
|||
dc6b13b4 | Christian Ehringfeld | qint8 rc = -1;
|
|
abb9e8c5 | Christian Ehringfeld | QSqlQuery q = this->schema->getQueryBuilder()->count(entity,
|
|
66704054 | Christian Ehringfeld | ignoreID);
|
|
dc6b13b4 | Christian Ehringfeld | this->db->select(q);
|
|
if (q.next()) {
|
|||
rc = q.value(0).toInt();
|
|||
}
|
|||
return rc;
|
|||
}
|
|||
506067a2 | Christian Ehringfeld | quint8 EntityManager::count(const QString &tableName) {
|
|
dc6b13b4 | Christian Ehringfeld | qint8 rc = -1;
|
|
abb9e8c5 | Christian Ehringfeld | QSqlQuery q = this->schema->getQueryBuilder()->count(tableName);
|
|
dc6b13b4 | Christian Ehringfeld | this->db->select(q);
|
|
if (q.next()) {
|
|||
rc = q.value(0).toInt();
|
|||
}
|
|||
return rc;
|
|||
}
|
|||
586bb527 | Christian Ehringfeld | void EntityManager::setConnectionNames(QStringList list) {
|
|
EntityManager::connectionNames = list;
|
|||
}
|
|||
e8d1537c | Christian Ehringfeld | ||
QSharedPointer<Entity> EntityManager::convert(const QHash<QString, QVariant>
|
|||
66704054 | Christian Ehringfeld | &map,
|
|
c9f21778 | Christian Ehringfeld | const char *classname, const bool refresh, const bool resolveRelations) {
|
|
e8d1537c | Christian Ehringfeld | auto ptr = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(
|
|
classname, map));
|
|||
this->cache.insert(ptr);
|
|||
c9f21778 | Christian Ehringfeld | if (resolveRelations) {
|
|
this->resolveRelations(ptr, map, refresh);
|
|||
}
|
|||
e8d1537c | Christian Ehringfeld | return ptr;
|
|
}
|
|||
QList<QSharedPointer<Entity> > EntityManager::convert(
|
|||
66704054 | Christian Ehringfeld | QList<QHash<QString, QVariant> > maps,
|
|
c9f21778 | Christian Ehringfeld | const char *classname, const bool refresh, const bool resolveRelations) {
|
|
e8d1537c | Christian Ehringfeld | auto list = QList<QSharedPointer<Entity> >();
|
|
for (int var = 0; var < maps.size(); ++var) {
|
|||
c9f21778 | Christian Ehringfeld | auto ptr = this->convert(maps.at(var), classname, refresh, resolveRelations);
|
|
e8d1537c | Christian Ehringfeld | list.append(ptr);
|
|
this->cache.insert(ptr);
|
|||
}
|
|||
return list;
|
|||
}
|