Projekt

Allgemein

Profil

Herunterladen als
Herunterladen (29,5 KB) Statistiken
| Zweig: | Revision:
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
#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;
}

EntityManager::EntityManager(QSqlDatabase database) : QObject() {
auto db = new Database(database);
this->db = QSharedPointer<Database>(db);
this->init();
}

EntityManager::EntityManager(const QString &databaseType, QString databasename ,
QString hostname,
QString username,
QString password, QString port) : QObject() {
auto db = new Database(databaseType, this->createConnection(), hostname,
databasename, username,
password,
port.toInt());
this->db = QSharedPointer<Database>(db);
this->init();
}

7e233492 Christian Ehringfeld
void EntityManager::init() {
3820ae33 Christian Ehringfeld
auto schema = Database::getSchema(Database::getDatabaseType(
9070a496 Christian Ehringfeld
this->db.data()->getDatabase().driverName()), this->db);
7e233492 Christian Ehringfeld
this->schema = QSharedPointer<Schema>(schema);
b0bf458e Christian Ehringfeld
this->schema.data()->setTables(this->schema.data()->getTableSchemas());
7e233492 Christian Ehringfeld
}

afde9013 Christian Ehringfeld
EntityManager::~EntityManager() {
EntityManager::removeConnectionName(this->db->getConnectionName());
7e233492 Christian Ehringfeld
}

1e213c09 Christian Ehringfeld
bool EntityManager::startup(QString version, QStringList toInitialize) {
d568923d Christian Ehringfeld
DatabaseMigration *dbm = new DatabaseMigration();
QSharedPointer<Entity> ptrDbm = QSharedPointer<Entity>(dbm);
1e213c09 Christian Ehringfeld
QHash<QString, QVariant> map = QHash<QString, QVariant>();
d84c91e7 Christian Ehringfeld
bool ok = true;
1e213c09 Christian Ehringfeld
map.insert("version", version);
d568923d Christian Ehringfeld
if (this->findAllByAttributes(map, dbm->getTablename()).isEmpty()) {
for (int var = 0; var < toInitialize.size(); ++var) {
d84c91e7 Christian Ehringfeld
if (ok) {
QString c = toInitialize.at(var);
e0e1ead8 Christian Ehringfeld
ok = this->createTable(QSharedPointer<Entity>
(EntityInstanceFactory::createInstance(c)));
d84c91e7 Christian Ehringfeld
} else {
break;
}
}
if (ok) {
dbm->setVersion(version);
dbm->setApplyTime(QDateTime::currentDateTime());
this->create(ptrDbm);
d568923d Christian Ehringfeld
}
}
delete dbm;
d84c91e7 Christian Ehringfeld
return ok;
1e213c09 Christian Ehringfeld
}

e0e1ead8 Christian Ehringfeld
bool EntityManager::executeQuery(const QString &query) {
return this->db.data()->transaction(query);
}

b0bf458e Christian Ehringfeld
bool EntityManager::checkTable(const QSharedPointer<Entity> &entity) {
bool rc = true;
if (!this->schema.data()->containsTable(entity.data()->getTablename())) {
if (this->schema.data()->getQueryBuilder().data()->createTable(entity)) {
this->schema.data()->getTableSchema(entity.data()->getTablename(), true);
rc = this->schema.data()->getTables().contains(entity.data()->getTablename());
}
}
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) {
afde9013 Christian Ehringfeld
entity = this->findById(entity.data()->property(
entity.data()->getPrimaryKey()).toLongLong(),
4f3b13f3 Christian Ehringfeld
QString(entity.data()->getClassname()));
}

7e233492 Christian Ehringfeld
void EntityManager::setSchema(const QSharedPointer<Schema> &value) {
schema = value;
}

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,
QSharedPointer<Entity> &e,
4f3b13f3 Christian Ehringfeld
const bool refresh) {
2d9fab10 Christian Ehringfeld
QSharedPointer<Entity> r;
f5087482 Christian Ehringfeld
if (!e.isNull()) {
6d91d381 Christian Ehringfeld
if (refresh || !(r = this->cache.get(id, QString(e.data()->getClassname())))) {
e.data()->setId(id);
auto map = this->findByPk(id, e);
4f3b13f3 Christian Ehringfeld
r = this->convert(map, e->getClassname(), refresh);
2d9fab10 Christian Ehringfeld
}
}
return r;
}

e0e1ead8 Christian Ehringfeld
QSharedPointer<Entity> EntityManager::findById(const qint64 &id,
const QString &classname) {
f5087482 Christian Ehringfeld
QSharedPointer<Entity> e = QSharedPointer<Entity>
(EntityInstanceFactory::createInstance(classname));
2d9fab10 Christian Ehringfeld
return this->findById(id, e);
}

e0e1ead8 Christian Ehringfeld
QSharedPointer<Entity> EntityManager::convert(const QHash<QString, QVariant>
&map,
4f3b13f3 Christian Ehringfeld
const char *classname, const bool refresh) {
e0e1ead8 Christian Ehringfeld
auto ptr = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(
classname, map));
4f3b13f3 Christian Ehringfeld
this->resolveRelations(ptr, map, refresh);
9070a496 Christian Ehringfeld
this->cache.insert(ptr);
return ptr;
f4e3904b Christian Ehringfeld
}

e0e1ead8 Christian Ehringfeld
QList<QSharedPointer<Entity> > EntityManager::convert(
QList<QHash<QString, QVariant> > maps,
4f3b13f3 Christian Ehringfeld
const char *classname, const bool refresh) {
f4e3904b Christian Ehringfeld
auto list = QList<QSharedPointer<Entity> >();
for (int var = 0; var < maps.size(); ++var) {
4f3b13f3 Christian Ehringfeld
auto ptr = this->convert(maps.at(var), classname, refresh);
9070a496 Christian Ehringfeld
list.append(ptr);
this->cache.insert(ptr);
f4e3904b Christian Ehringfeld
}
return list;
586bb527 Christian Ehringfeld
}

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;
2d9fab10 Christian Ehringfeld
if ((convertedId == id.toLongLong(&ok)) && ok && convertedId > -1) {
e0e1ead8 Christian Ehringfeld
QString className = EntityInstanceFactory::extractEntityType(
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);
}
c599658a Christian Ehringfeld
this->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) {
706de2e8 Christian Ehringfeld
if (entity.data() && entity.data()->getId() > -1) {
e0e1ead8 Christian Ehringfeld
Entity *e = EntityInstanceFactory::createInstance(
EntityInstanceFactory::extractEntityType(
property.typeName()));
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->oneToMany(
e->getTablename(),
afde9013 Christian Ehringfeld
this->schema.data()->getQueryBuilder().data()->generateColumnNameID(
r.getMappedBy()), entity.data()->getId());
706de2e8 Christian Ehringfeld
auto listMap = this->convertQueryResult(q);
98b5b08d Christian Ehringfeld
auto entities = this->convert(listMap, e->getClassname(), refresh);
706de2e8 Christian Ehringfeld
delete e;
this->setListProperty(entity, entities, property);
}
}
a06633f7 Christian Ehringfeld
c599658a Christian Ehringfeld
e0e1ead8 Christian Ehringfeld
void EntityManager::oneToOne(const QSharedPointer<Entity> &entity,
const Relation &r,
4f3b13f3 Christian Ehringfeld
const QMetaProperty &property, const bool refresh,
c599658a Christian Ehringfeld
const QVariant &id) {
if (r.getMappedBy().isEmpty()) {
this->manyToOne(entity, id, property);
} else {
e0e1ead8 Christian Ehringfeld
Entity *e = EntityInstanceFactory::createInstance(
EntityInstanceFactory::extractEntityType(
property.typeName()));
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->oneToMany(
e->getTablename(),
afde9013 Christian Ehringfeld
this->schema.data()->getQueryBuilder().data()->generateColumnNameID(
r.getMappedBy()),
entity.data()->property(entity.data()->getPrimaryKey()).toLongLong(), 1);
c599658a Christian Ehringfeld
auto listMap = this->convertQueryResult(q);
98b5b08d Christian Ehringfeld
auto entities = this->convert(listMap, e->getClassname(), refresh);
c599658a Christian Ehringfeld
if (!entities.isEmpty()) {
QSharedPointer<Entity> ptr = entities.at(0);
this->setProperty(entity, ptr, property);
}
delete e;
}
}

98b5b08d Christian Ehringfeld
bool EntityManager::canPersistRelation(const Relation &relation,
const RelationType &r, const QVariant &var) const {
4f3b13f3 Christian Ehringfeld
return relation.getType() == r
&& var.canConvert<QList<QSharedPointer<Entity>>>();
}

e0e1ead8 Christian Ehringfeld
void EntityManager::setListProperty(const QSharedPointer<Entity> &entity,
QList<QSharedPointer<Entity> > &list,
706de2e8 Christian Ehringfeld
const QMetaProperty &property) const {
QVariant var;
var.setValue<QList<QSharedPointer<Entity>>>(list);
property.write(entity.data(), var);
a06633f7 Christian Ehringfeld
}

91ed1164 Christian Ehringfeld
void EntityManager::addEntityToListProperty(const QSharedPointer<Entity>
&entity, QSharedPointer<Entity> add, const QMetaProperty &property) {
QVariant var = property.read(entity.data());
if (!var.isNull() && var.canConvert<QList<QSharedPointer<Entity>>>()) {
QList<QSharedPointer<Entity>> list = var.value<QList<QSharedPointer<Entity>>>();
if (!list.contains(add)) {
list.append(add);
var.setValue<QList<QSharedPointer<Entity>>>(list);
property.write(entity.data(), var);
}
}
}

e0e1ead8 Christian Ehringfeld
void EntityManager::setProperty(const QSharedPointer<Entity> &entiy,
QSharedPointer<Entity> value,
c599658a Christian Ehringfeld
const QMetaProperty &property) const {
99140fe9 Christian Ehringfeld
if (value && value.data()->property(value.data()->getPrimaryKey()).toLongLong()
> -1) {
c599658a Christian Ehringfeld
property.write(entiy.data(), QVariant(value));
}
}

e24791d7 Christian Ehringfeld
void EntityManager::saveRelations(const QSharedPointer<Entity> &entity) {
4f3b13f3 Christian Ehringfeld
auto relations = entity.data()->getRelationProperties();
auto iterator = relations.constBegin();
while (iterator != relations.constEnd()) {
const Relation r = iterator.key();
auto var = iterator.value().read(entity.data());
e24791d7 Christian Ehringfeld
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);
}
} else if (this->canPersistRelation(r, MANY_TO_MANY, var)) {
98b5b08d Christian Ehringfeld
this->persistManyToMany(entity, r, var);
4f3b13f3 Christian Ehringfeld
} 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);
e24791d7 Christian Ehringfeld
if (this->shouldBeSaved(entity, r)) {
this->save(entity);
}
4f3b13f3 Christian Ehringfeld
}
} else if (r.getType() == ONE_TO_ONE && !r.getMappedBy().isEmpty()
&& var.canConvert<QSharedPointer<Entity>>()) {
auto entity = qvariant_cast<QSharedPointer<Entity>>(var);
this->save(entity);
}
396a60d7 Christian Ehringfeld
++iterator;
4f3b13f3 Christian Ehringfeld
}
91ed1164 Christian Ehringfeld
}

void EntityManager::persistMappedByRelation(const QList<QSharedPointer<Entity> >
&list, QSqlQuery &q, const QSharedPointer<Entity> &entity, const Relation &r,
const QString &tblName) {
q.clear();
QSharedPointer<Entity> ptr;
e24791d7 Christian Ehringfeld
QList<QSharedPointer<Entity>> saved = r.getCascadeType().contains(ALL)
|| r.getCascadeType().contains(MERGE)
|| r.getCascadeType().contains(PERSIST) ? this->saveRelationEntities(list,
r) : list;
91ed1164 Christian Ehringfeld
auto builder = this->schema.data()->getQueryBuilder();
q = builder.data()->manyToManyInsert(tblName,
builder.data()->generateManyToManyColumnName(entity),
builder.data()->generateManyToManyColumnName(ptr));
q.bindValue(0, entity.data()->getId());
QMetaProperty prop;
bool first = true;
e24791d7 Christian Ehringfeld
for (int var = 0; var < saved.size(); ++var) {
91ed1164 Christian Ehringfeld
ptr = list.at(var);
if (first && !r.getMappedBy().isEmpty()) {
auto props = ptr.data()->getMetaProperties();
if (props.contains(r.getMappedBy())) {
prop = props.value(r.getMappedBy());
}
}
afde9013 Christian Ehringfeld
if (ptr.data()->property(ptr.data()->getPrimaryKey()).toLongLong() > -1) {
q.bindValue(1, ptr.data()->property(ptr.data()->getPrimaryKey()));
e24791d7 Christian Ehringfeld
q.exec();
if (prop.isValid()) {
this->addEntityToListProperty(entity, ptr, prop);
}
91ed1164 Christian Ehringfeld
}
}
4f3b13f3 Christian Ehringfeld
}
a06633f7 Christian Ehringfeld
e24791d7 Christian Ehringfeld
bool EntityManager::shouldBeSaved(QSharedPointer<Entity> &entity,
const Relation &r) {
afde9013 Christian Ehringfeld
return entity && (r.getCascadeType().contains(ALL)
|| (entity.data()->property(entity.data()->getPrimaryKey()) > -1
&& r.getCascadeType().contains(MERGE))
|| (entity.data()->property(entity.data()->getPrimaryKey()) <= -1
&& r.getCascadeType().contains(PERSIST)));
e24791d7 Christian Ehringfeld
}

void EntityManager::removeRelations(const QSharedPointer<Entity> &entity) {
auto props = entity.data()->getRelationProperties();
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());
if (r.getType() == MANY_TO_MANY) {
this->removeManyToManyEntityList(entity, r, var);
} else if (r.getType() == ONE_TO_MANY) {
if (r.getCascadeType().contains(REMOVE) || r.getCascadeType().contains(ALL)) {
this->removeEntityList(var);
} else {
this->setNullOneToManyRelation(var, r);
}
} else if (r.getType() == MANY_TO_ONE || r.getType() == MANY_TO_ONE) {
this->setNullEntityPropertyRelation(var, r);
e24791d7 Christian Ehringfeld
}
++iterator;
}
}

2075db87 Christian Ehringfeld
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).data()->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);
}
}
}
}
}
}

void EntityManager::setNullEntityPropertyRelation(QVariant &var,
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.data()->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);
this->remove(e);
}
}

void EntityManager::removeEntityList(QVariant &var) {
if (var.canConvert<QList<QSharedPointer<Entity>>>()) {
QList<QSharedPointer<Entity>> list =
qvariant_cast<QList<QSharedPointer<Entity>>>(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 (!list.isEmpty()) {
auto builder = this->schema.data()->getQueryBuilder();
auto ptr = list.at(0);
QString tblName = builder.data()->generateManyToManyTableName(e, ptr);
if (this->schema.data()->getTables().contains(tblName)) {
QSqlQuery q = builder.data()->manyToManyDelete(
tblName, builder.data()->generateManyToManyColumnName(e),
afde9013 Christian Ehringfeld
e.data()->property(e.data()->getPrimaryKey()).toLongLong());
2075db87 Christian Ehringfeld
bool refresh = r.getCascadeType().contains(REFRESH)
|| r.getCascadeType().contains(ALL);
bool remove = r.getCascadeType().contains(REMOVE)
|| r.getCascadeType().contains(ALL);
if (q.exec()) {
for (int var = 0; var < list.size(); ++var) {
auto entity = list.at(var);
if (remove) {
this->remove(entity);
} else if (refresh) {
/**
not really with good performance, alternatively iterate over relation attribute and delete pointer from list
**/
this->refresh(entity);
}
}
}
}
}
}
}

e24791d7 Christian Ehringfeld
QList<QSharedPointer<Entity>> EntityManager::saveRelationEntities(
91ed1164 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,
const Relation &r, const QVariant &property) {
QList<QSharedPointer<Entity>> list =
qvariant_cast<QList<QSharedPointer<Entity>>>(property);
if (!list.isEmpty()) {
auto ptr = list.at(0);
5c53ac99 Christian Ehringfeld
auto builder = this->schema.data()->getQueryBuilder();
98b5b08d Christian Ehringfeld
QString tblName = builder.data()->generateManyToManyTableName(entity, ptr);
if (this->schema.data()->getTables().contains(tblName)) {
5c53ac99 Christian Ehringfeld
QSqlQuery q = builder.data()->manyToManyDelete(
98b5b08d Christian Ehringfeld
tblName, builder.data()->generateManyToManyColumnName(entity),
afde9013 Christian Ehringfeld
entity.data()->property(entity.data()->getPrimaryKey()).toLongLong());
98b5b08d Christian Ehringfeld
if (this->db.data()->transaction(q)) {
91ed1164 Christian Ehringfeld
this->persistMappedByRelation(list, q, entity, r, tblName);
98b5b08d Christian Ehringfeld
}
} else {
qDebug() << "MANY_TO_MANY Table " << tblName << " not exists";
}
}
}



e0e1ead8 Christian Ehringfeld
void EntityManager::manyToMany(const QSharedPointer<Entity> &entity,
4f3b13f3 Christian Ehringfeld
const QMetaProperty &property, const bool refresh) {
e0e1ead8 Christian Ehringfeld
Entity *secEntity = EntityInstanceFactory::createInstance(
EntityInstanceFactory::extractEntityType(
QString(
property.typeName())));
706de2e8 Christian Ehringfeld
auto builder = this->schema.data()->getQueryBuilder();
if (secEntity) {
QSharedPointer<Entity> secEntityPtr = QSharedPointer<Entity>(secEntity);
98b5b08d Christian Ehringfeld
QString tblName = builder.data()->generateManyToManyTableName(entity,
secEntityPtr);
97846191 Christian Ehringfeld
/**
* maybe it would be better, to fetch first the ids, look up cache and then request missing entities
2075db87 Christian Ehringfeld
* with this it would be also possible to respect cascade type
97846191 Christian Ehringfeld
*/
c599658a Christian Ehringfeld
if (this->schema.data()->getTables().contains(tblName)) {
98b5b08d Christian Ehringfeld
QSqlQuery q = builder.data()->manyToMany(tblName,
afde9013 Christian Ehringfeld
builder.data()->generateManyToManyColumnName(entity),
entity.data()->property(entity.data()->getPrimaryKey()).toLongLong(),
c599658a Christian Ehringfeld
builder.data()->generateManyToManyColumnName(secEntityPtr),
secEntityPtr.data()->getTablename());
auto listMap = this->convertQueryResult(q);
98b5b08d Christian Ehringfeld
auto entities = this->convert(listMap, entity.data()->getClassname(), refresh);
c599658a Christian Ehringfeld
this->setListProperty(entity, entities, property);
} else {
qDebug() << "MANY_TO_MANY Table " << tblName << " not exists";
}
706de2e8 Christian Ehringfeld
}
a06633f7 Christian Ehringfeld
}

e0e1ead8 Christian Ehringfeld
QList<QSharedPointer<Entity> > EntityManager::findEntityByAttributes(
const QSharedPointer<Entity>
&entity,
bool ignoreID) {
1e213c09 Christian Ehringfeld
auto maps = this->findAllByAttributes(entity, ignoreID);
f4e3904b Christian Ehringfeld
return this->convert(maps, entity.data()->getClassname());
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;
696666eb Christian Ehringfeld
if (this->checkTable(entity) && !(checkDuplicate && this->count(entity) == 0)) {
99140fe9 Christian Ehringfeld
QList<QSqlQuery> q = this->schema.data()->getQueryBuilder().data()->create(
entity);
this->db->startTransaction();
bool first = true;
for (int var = 0; var < q.size(); ++var) {
auto query = q.at(var);
afde9013 Christian Ehringfeld
rc = query.exec();
if (!rc) {
qDebug() << "Query failed:" << query.lastError().text();
break;
}
99140fe9 Christian Ehringfeld
if (first) {
entity.data()->setProperty(entity.data()->getPrimaryKey(),
query.lastInsertId().toLongLong(&rc));
first = false;
}
}
afde9013 Christian Ehringfeld
if (!rc || !this->db->commitTransaction()) {
99140fe9 Christian Ehringfeld
this->db->rollbackTransaction();
entity.data()->setId(-1);
rc = false;
} else {
244c6d53 Christian Ehringfeld
if (persistRelations) {
e24791d7 Christian Ehringfeld
this->saveRelations(entity);
244c6d53 Christian Ehringfeld
}
3820ae33 Christian Ehringfeld
this->cache.insert(entity);
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) {
if (entity->getId() > -1 && this->count(entity) == 1) {
this->db->startTransaction();
QList<QSqlQuery> q = this->schema.data()->getQueryBuilder().data()->merge(
entity);
bool ok = true;
for (int var = 0; var < q.size(); ++var) {
auto query = q.at(var);
ok = query.exec();
if (!ok) {
qDebug() << query.lastError().text();
break;
}
}
if (!ok || !this->db->commitTransaction()) {
this->db->rollbackTransaction();
return false;
} else if (ok && withRelations) {
this->saveRelations(entity);
}
}
return false;
dc6b13b4 Christian Ehringfeld
}
b0b8dac3 Christian Ehringfeld
6d91d381 Christian Ehringfeld
QHash<QString, QVariant> EntityManager::findByPk(qint64 id,
const QSharedPointer<Entity>
f5087482 Christian Ehringfeld
&e) {
afde9013 Christian Ehringfeld
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->find(id, e, 0,
e.data()->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(
const QSharedPointer<Entity>
&entity,
bool ignoreID) {
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->findByAttributes(
entity, ignoreID);
dc6b13b4 Christian Ehringfeld
return this->convertQueryResult(q);
}

e0e1ead8 Christian Ehringfeld
QList<QHash <QString, QVariant> > EntityManager::findAllByAttributes(
e24791d7 Christian Ehringfeld
const QHash<QString, QVariant> &m, const QString &tblname, bool ignoreID) {
dc6b13b4 Christian Ehringfeld
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->findByAttributes(m,
tblname, ignoreID);
7e233492 Christian Ehringfeld
return this->convertQueryResult(q);
}

e0e1ead8 Christian Ehringfeld
QList<QHash<QString, QVariant> > EntityManager::convertQueryResult(
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(
const QSharedPointer<Entity> &e) {
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->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) {
a06633f7 Christian Ehringfeld
auto props = entity.data()->getRelationProperties();
2d9fab10 Christian Ehringfeld
auto iterator = props.constBegin();
while (iterator != props.constEnd()) {
const Relation r = iterator.key();
const QMetaProperty property = iterator.value();
a06633f7 Christian Ehringfeld
switch (r.getType()) {
case MANY_TO_ONE:
afde9013 Christian Ehringfeld
if (map.contains(
this->schema.data()->getQueryBuilder().data()->generateColumnNameID(
r.getPropertyName()))) {
4f3b13f3 Christian Ehringfeld
this->manyToOne(entity, property.read(entity.data()), property, refresh);
a06633f7 Christian Ehringfeld
}
break;
case MANY_TO_MANY:
afde9013 Christian Ehringfeld
this->manyToMany(entity, property, refresh);
a06633f7 Christian Ehringfeld
break;
case ONE_TO_MANY:
4f3b13f3 Christian Ehringfeld
this->oneToMany(entity, r, property, refresh);
a06633f7 Christian Ehringfeld
break;
case ONE_TO_ONE:
4f3b13f3 Christian Ehringfeld
this->oneToOne(entity, r, property, refresh, property.read(entity.data()));
a06633f7 Christian Ehringfeld
break;
}
++iterator;
}
}

244c6d53 Christian Ehringfeld
bool EntityManager::save(QSharedPointer<Entity> &entity,
const bool persistRelations) {
afde9013 Christian Ehringfeld
if (entity.data()->property(entity.data()->getPrimaryKey()) > -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;
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->findId(entity);
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);
95b60eb2 Christian Ehringfeld
auto queries = this->schema.data()->getQueryBuilder().data()->remove(entity);
bool ok = true;
for (int var = 0; var < queries.size(); ++var) {
auto q = queries.at(var);
if (!q.exec()) {
this->db->rollbackTransaction();
qDebug() << "Remove transaction rolled back";
ok = false;
break;
}
}
if (ok && this->db->commitTransaction()) {
9070a496 Christian Ehringfeld
this->cache.remove(entity);
586bb527 Christian Ehringfeld
entity.clear();
7e233492 Christian Ehringfeld
rc = true;
}
return rc;
}
b0b8dac3 Christian Ehringfeld
9c2f773f Christian Ehringfeld
bool EntityManager::createTable(const QSharedPointer<Entity> &entity) {
return this->schema.data()->getQueryBuilder().data()->createTable(entity);
}

e0e1ead8 Christian Ehringfeld
qint8 EntityManager::count(const QSharedPointer<Entity> &entity,
bool ignoreID) {
dc6b13b4 Christian Ehringfeld
qint8 rc = -1;
e0e1ead8 Christian Ehringfeld
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->count(entity,
ignoreID);
dc6b13b4 Christian Ehringfeld
this->db->select(q);
if (q.next()) {
rc = q.value(0).toInt();
}
return rc;
}

qint8 EntityManager::count(const QString &tableName) {
qint8 rc = -1;
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->count(tableName);
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;
}