Herunterladen als
root/src/entitymanager.cpp @ 97846191
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();
|
|||
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 | }
|
|
EntityManager::EntityManager(QSqlDatabase database) {
|
|||
auto db = new Database(database);
|
|||
this->db = QSharedPointer<Database>(db);
|
|||
this->init();
|
|||
}
|
|||
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);
|
|||
ok = this->createTable(QSharedPointer<Entity>(EntityInstanceFactory::createInstance(c)));
|
|||
} 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 | }
|
|
7e233492 | Christian Ehringfeld | EntityManager::EntityManager(const QString &databaseType, QString databasename , QString hostname, QString username,
|
|
QString password, QString port) {
|
|||
auto db = new Database(databaseType, this->createConnection(), hostname, databasename, username, password,
|
|||
port.toInt());
|
|||
this->db = QSharedPointer<Database>(db);
|
|||
this->init();
|
|||
}
|
|||
81c23b56 | Christian Ehringfeld | ||
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;
|
|||
}
|
|||
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);
|
|||
}
|
|||
2d9fab10 | Christian Ehringfeld | QSharedPointer<Entity> EntityManager::findById(const qint64 &id, Entity *&e) {
|
|
QSharedPointer<Entity> r;
|
|||
if (e) {
|
|||
if ((r = this->cache.get(id, QString(e->getClassname()))) && !r.data()) {
|
|||
auto map = this->findByPk(id, e->getTablename());
|
|||
r = this->convert(map, e->getClassname());
|
|||
}
|
|||
delete e;
|
|||
}
|
|||
return r;
|
|||
}
|
|||
QSharedPointer<Entity> EntityManager::findById(const qint64 &id, const QString &classname) {
|
|||
Entity *e = EntityInstanceFactory::createInstance(classname);
|
|||
return this->findById(id, e);
|
|||
}
|
|||
f4e3904b | Christian Ehringfeld | QSharedPointer<Entity> EntityManager::convert(const QHash<QString, QVariant> &map, const char *classname) {
|
|
9070a496 | Christian Ehringfeld | auto ptr = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(classname, map));
|
|
this->cache.insert(ptr);
|
|||
return ptr;
|
|||
f4e3904b | Christian Ehringfeld | }
|
|
QList<QSharedPointer<Entity> > EntityManager::convert(QList<QHash<QString, QVariant> > maps, const char *classname) {
|
|||
auto list = QList<QSharedPointer<Entity> >();
|
|||
for (int var = 0; var < maps.size(); ++var) {
|
|||
9070a496 | Christian Ehringfeld | auto ptr = this->convert(maps.at(var), classname);
|
|
list.append(ptr);
|
|||
this->cache.insert(ptr);
|
|||
f4e3904b | Christian Ehringfeld | }
|
|
return list;
|
|||
586bb527 | Christian Ehringfeld | }
|
|
a06633f7 | Christian Ehringfeld | void EntityManager::manyToOne(const QSharedPointer<Entity> &entity, const QVariant &id, const QMetaProperty &property) {
|
|
qint64 convertedId = -1;
|
|||
bool ok = false;
|
|||
2d9fab10 | Christian Ehringfeld | if ((convertedId == id.toLongLong(&ok)) && ok && convertedId > -1) {
|
|
706de2e8 | Christian Ehringfeld | QString className = EntityInstanceFactory::extractEntityType(property.typeName());
|
|
QSharedPointer<Entity> ptr = QSharedPointer<Entity>();
|
|||
if (this->cache.contains(convertedId, className) && (ptr = this->cache.get(convertedId, className)) && ptr.data()) {
|
|||
} else {
|
|||
ptr = this->findById(convertedId, className);
|
|||
}
|
|||
2d9fab10 | Christian Ehringfeld | if (ptr.data() && ptr.data()->getId() > -1) {
|
|
property.write(entity.data(), QVariant(ptr));
|
|||
}
|
|||
a06633f7 | Christian Ehringfeld | }
|
|
}
|
|||
void EntityManager::oneToMany(const QSharedPointer<Entity> &entity, const Relation &r, const QMetaProperty &property) {
|
|||
706de2e8 | Christian Ehringfeld | if (entity.data() && entity.data()->getId() > -1) {
|
|
Entity *e = EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(property.typeName()));
|
|||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->oneToMany(e->getTablename(),
|
|||
r.getMappedBy() + "_id", entity.data()->getId());
|
|||
auto listMap = this->convertQueryResult(q);
|
|||
auto entities = this->convert(listMap, e->getClassname());
|
|||
delete e;
|
|||
this->setListProperty(entity, entities, property);
|
|||
}
|
|||
}
|
|||
a06633f7 | Christian Ehringfeld | ||
706de2e8 | Christian Ehringfeld | void EntityManager::setListProperty(const QSharedPointer<Entity> &entity, QList<QSharedPointer<Entity> > &list,
|
|
const QMetaProperty &property) const {
|
|||
QVariant var;
|
|||
var.setValue<QList<QSharedPointer<Entity>>>(list);
|
|||
property.write(entity.data(), var);
|
|||
a06633f7 | Christian Ehringfeld | }
|
|
706de2e8 | Christian Ehringfeld | void EntityManager::manyToMany(const QSharedPointer<Entity> &entity, const Relation &r, const QMetaProperty &property) {
|
|
Entity *secEntity = EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(QString(
|
|||
property.typeName())));
|
|||
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);
|
|||
}
|
|||
97846191 | Christian Ehringfeld | /**
|
|
* maybe it would be better, to fetch first the ids, look up cache and then request missing entities
|
|||
*/
|
|||
706de2e8 | Christian Ehringfeld | QSqlQuery q = this->schema.data()->getQueryBuilder().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());
|
|||
this->setListProperty(entity, entities, property);
|
|||
}
|
|||
a06633f7 | Christian Ehringfeld | }
|
|
void EntityManager::oneToOne(const QSharedPointer<Entity> &entity, const Relation &r, const QMetaProperty &property,
|
|||
const QVariant &id) {
|
|||
}
|
|||
dc6b13b4 | 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 | /**
|
|
* @todo should be an insert statement with much values
|
|||
* not really usefull atm
|
|||
* @brief EntityManager::create
|
|||
* @param entities
|
|||
* @return
|
|||
*/
|
|||
d84c91e7 | Christian Ehringfeld | bool EntityManager::create(QList<QSharedPointer<Entity> > entities) {
|
|
3820ae33 | Christian Ehringfeld | bool ok = true;
|
|
foreach (QSharedPointer<Entity> ent, entities) {
|
|||
ok = this->create(ent);
|
|||
if (!ok) {
|
|||
break;
|
|||
}
|
|||
}
|
|||
return ok;
|
|||
d84c91e7 | Christian Ehringfeld | }
|
|
dc6b13b4 | Christian Ehringfeld | /**
|
|
* @TODO insert Relations
|
|||
* @brief EntityManager::create
|
|||
* @param entity
|
|||
* @return
|
|||
*/
|
|||
bool EntityManager::create(QSharedPointer<Entity> &entity) {
|
|||
bool rc = false;
|
|||
if (this->checkTable(entity) && this->count(entity) == 0) {
|
|||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->create(entity);
|
|||
rc = this->db->transaction(q);
|
|||
if (rc) {
|
|||
entity.data()->setId(this->schema.data()->getLastInsertID().toLongLong(&rc));
|
|||
3820ae33 | Christian Ehringfeld | this->cache.insert(entity);
|
|
7e233492 | Christian Ehringfeld | }
|
|
}
|
|||
dc6b13b4 | Christian Ehringfeld | return rc;
|
|
7e233492 | Christian Ehringfeld | }
|
|
9d05e414 | Christian Ehringfeld | ||
dc6b13b4 | Christian Ehringfeld | EntityManager::~EntityManager() {
|
|
EntityManager::removeConnectionName(this->db->getConnectionName());
|
|||
7e233492 | Christian Ehringfeld | }
|
|
b0b8dac3 | Christian Ehringfeld | ||
dc6b13b4 | Christian Ehringfeld | QStringList EntityManager::getConnectionNames() {
|
|
return EntityManager::connectionNames;
|
|||
}
|
|||
b0b8dac3 | Christian Ehringfeld | ||
2d9fab10 | Christian Ehringfeld | QHash<QString, QVariant> EntityManager::findByPk(qint64 id, QString tblname) {
|
|
dc6b13b4 | Christian Ehringfeld | QSqlQuery q = this->schema.data()->getQueryBuilder().data()->find(id, tblname);
|
|
706de2e8 | Christian Ehringfeld | auto listMap = this->convertQueryResult(q);
|
|
if (!listMap.isEmpty()) {
|
|||
return listMap.at(0);
|
|||
7e233492 | Christian Ehringfeld | }
|
|
706de2e8 | Christian Ehringfeld | // this->db->select(q);
|
|
// QSqlRecord rec = q.record();
|
|||
// QHash<QString, QVariant> map = QHash<QString, QVariant>();
|
|||
// if (q.next()) {
|
|||
// for (int var = 0; var < rec.count(); ++var) {
|
|||
// QVariant variant = rec.value(var);
|
|||
// if (variant.isValid()) {
|
|||
// map.insert(rec.fieldName(var), variant);
|
|||
// }
|
|||
// }
|
|||
// }
|
|||
// return map;
|
|||
return QHash<QString, QVariant>();
|
|||
7e233492 | Christian Ehringfeld | }
|
|
f4e3904b | Christian Ehringfeld | ||
1e213c09 | Christian Ehringfeld | QList<QHash<QString, QVariant> > EntityManager::findAllByAttributes(const QSharedPointer<Entity> &entity,
|
|
bool ignoreID) {
|
|||
dc6b13b4 | Christian Ehringfeld | QSqlQuery q = this->schema.data()->getQueryBuilder().data()->findByAttributes(entity, ignoreID);
|
|
return this->convertQueryResult(q);
|
|||
}
|
|||
1e213c09 | Christian Ehringfeld | QList<QHash <QString, QVariant> > EntityManager::findAllByAttributes(const QHash<QString, QVariant> &m,
|
|
586bb527 | Christian Ehringfeld | const QString &tblname,
|
|
7e233492 | Christian Ehringfeld | bool ignoreID) {
|
|
dc6b13b4 | Christian Ehringfeld | QSqlQuery q = this->schema.data()->getQueryBuilder().data()->findByAttributes(m,
|
|
tblname, ignoreID);
|
|||
7e233492 | Christian Ehringfeld | return this->convertQueryResult(q);
|
|
}
|
|||
dc6b13b4 | Christian Ehringfeld | /**
|
|
* @TODO insert Relations
|
|||
* @brief EntityManager::merge
|
|||
* @param entity
|
|||
* @param withRelations
|
|||
* @return
|
|||
*/
|
|||
bool EntityManager::merge(QSharedPointer<Entity> &entity, bool withRelations) {
|
|||
if (this->count(entity) == 0 && entity->getId() != -1) {
|
|||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->merge(entity);
|
|||
return this->db->transaction(q);
|
|||
} else {
|
|||
return false;
|
|||
}
|
|||
586bb527 | Christian Ehringfeld | }
|
|
dc6b13b4 | Christian Ehringfeld | ||
7e233492 | Christian Ehringfeld | QList<QHash<QString, QVariant> > EntityManager::convertQueryResult(QSqlQuery &q) {
|
|
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;
|
|||
}
|
|||
QList<QHash <QString, QVariant> > EntityManager::findAll(QString tblname) {
|
|||
dc6b13b4 | Christian Ehringfeld | QSqlQuery q = this->schema.data()->getQueryBuilder().data()->findAll(tblname);
|
|
7e233492 | Christian Ehringfeld | return this->convertQueryResult(q);
|
|
}
|
|||
b0b8dac3 | Christian Ehringfeld | ||
a06633f7 | Christian Ehringfeld | void EntityManager::resolveRelations(const QSharedPointer<Entity> &entity, const QHash<QString, QVariant> &map) {
|
|
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:
|
|||
if (map.contains(r.getPropertyName()) + "_id") {
|
|||
this->manyToOne(entity, property.read(entity.data()), property);
|
|||
}
|
|||
break;
|
|||
case MANY_TO_MANY:
|
|||
this->manyToMany(entity, r, property);
|
|||
break;
|
|||
case ONE_TO_MANY:
|
|||
this->oneToMany(entity, r, property);
|
|||
break;
|
|||
case ONE_TO_ONE:
|
|||
this->oneToOne(entity, r, property, property.read(entity.data()));
|
|||
break;
|
|||
}
|
|||
++iterator;
|
|||
}
|
|||
}
|
|||
586bb527 | Christian Ehringfeld | bool EntityManager::save(QSharedPointer<Entity> &entity) {
|
|
if (entity.data()->getId() > -1) {
|
|||
7e233492 | Christian Ehringfeld | return this->merge(entity);
|
|
} else {
|
|||
return this->create(entity);
|
|||
}
|
|||
}
|
|||
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;
|
|||
}
|
|||
7e233492 | Christian Ehringfeld | ||
586bb527 | Christian Ehringfeld | bool EntityManager::remove(QSharedPointer<Entity> &entity) {
|
|
7e233492 | Christian Ehringfeld | bool rc = false;
|
|
dc6b13b4 | Christian Ehringfeld | QSqlQuery q = this->schema.data()->getQueryBuilder().data()->remove(entity);
|
|
7e233492 | Christian Ehringfeld | if (this->db->transaction(q)) {
|
|
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);
|
|||
}
|
|||
dc6b13b4 | Christian Ehringfeld | qint8 EntityManager::count(const QSharedPointer<Entity> &entity, bool ignoreID) {
|
|
qint8 rc = -1;
|
|||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->count(entity, ignoreID);
|
|||
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;
|
|||
}
|