Herunterladen als
root/src/entitymanager.h @ 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 | ||
#ifndef ENTITYMANAGER_H
|
|||
#define ENTITYMANAGER_H
|
|||
#include <QtSql/QSqlDatabase>
|
|||
#include <QtSql/QSqlRecord>
|
|||
#include <QtSql/QSqlField>
|
|||
#include <QString>
|
|||
#include <QStringList>
|
|||
ac8aede7 | Christian Ehringfeld | #include <QObject>
|
|
586bb527 | Christian Ehringfeld | #include <QSharedPointer>
|
|
81c23b56 | Christian Ehringfeld | #include <QDebug>
|
|
#include <QtSql/QSqlError>
|
|||
a205e8a9 | Christian Ehringfeld | #include <QMetaType>
|
|
82442988 | Christian Ehringfeld | #include "schema.h"
|
|
81c23b56 | Christian Ehringfeld | #include "entity.h"
|
|
#include "database.h"
|
|||
a205e8a9 | Christian Ehringfeld | #include "entityinstancefactory.h"
|
|
506067a2 | Christian Ehringfeld | #include "queryinterpreter.h"
|
|
82442988 | Christian Ehringfeld | #include "cache.h"
|
|
#include "querybuilder.h"
|
|||
6899f814 | Christian Ehringfeld | namespace CuteEntityManager {
|
|
81c23b56 | Christian Ehringfeld | ||
45135a14 | Christian Ehringfeld | class Logger;
|
|
2ee5022f | Christian Ehringfeld | class QueryInterpreter;
|
|
ac8aede7 | Christian Ehringfeld | class EntityManager : public QObject {
|
|
Q_OBJECT
|
|||
df1e56bd | Christian Ehringfeld | public slots:
|
|
1708f3a8 | Christian Ehringfeld | /**
|
|
* @brief startup
|
|||
* @param version must be unique
|
|||
* @param toInitialize list of entity classnames which database tables should be created
|
|||
* @return
|
|||
*/
|
|||
9971e7d2 | Christian Ehringfeld | bool startup(QString version, QStringList toInitialize, bool createIndices = false);
|
|
e0e1ead8 | Christian Ehringfeld | bool executeQuery(const QString &query);
|
|
81c23b56 | Christian Ehringfeld | static void removeConnectionName(const QString &name);
|
|
2d9fab10 | Christian Ehringfeld | QSharedPointer<Entity> findById(const qint64 &id, const QString &classname);
|
|
e0e1ead8 | Christian Ehringfeld | QList<QSharedPointer<Entity>> findEntityByAttributes(const
|
|
QSharedPointer<Entity> &entity,
|
|||
bool ignoreID = false);
|
|||
244c6d53 | Christian Ehringfeld | bool create(QList<QSharedPointer<Entity>> entities,
|
|
const bool persistRelations = true);
|
|||
696666eb | Christian Ehringfeld | bool create(QSharedPointer<Entity> &entity, const bool persistRelations = true,
|
|
const bool checkDuplicate = false);
|
|||
244c6d53 | Christian Ehringfeld | bool save(QSharedPointer<Entity> &entity, const bool persistRelations = true);
|
|
9070a496 | Christian Ehringfeld | qint64 findId(QSharedPointer<Entity> &entity);
|
|
bool merge(QSharedPointer<Entity> &entity, bool withRelations = true);
|
|||
bool remove(QSharedPointer<Entity> &entity);
|
|||
bool removeAll(QString tblname);
|
|||
df1e56bd | Christian Ehringfeld | bool createTable(const QSharedPointer<Entity> &entity,
|
|
bool createRelationTables = true);
|
|||
506067a2 | Christian Ehringfeld | quint8 count(const QSharedPointer<Entity> &entity, bool ignoreID = true);
|
|
quint8 count(const QString &tableName);
|
|||
9070a496 | Christian Ehringfeld | QSharedPointer<Database> getDb() const;
|
|
void setDb(const QSharedPointer<Database> &value);
|
|||
QSharedPointer<Schema> getSchema() const;
|
|||
4f3b13f3 | Christian Ehringfeld | void refresh(QSharedPointer<Entity> &entity);
|
|
9070a496 | Christian Ehringfeld | void setSchema(const QSharedPointer<Schema> &value);
|
|
506067a2 | Christian Ehringfeld | QList<QHash<QString, QVariant> > selectByQuery(Query &query);
|
|
QList<QHash<QString, QVariant> > selectBySql(const QString &sql);
|
|||
qint8 count(Query &query);
|
|||
dc618bda | Christian Ehringfeld | public:
|
|
bcd2b697 | Christian Ehringfeld | EntityManager(QSqlDatabase database, bool logQueries = false);
|
|
dc618bda | Christian Ehringfeld | EntityManager(const QString &databaseType, QString databasename = "" ,
|
|
QString hostname = "",
|
|||
QString username = "",
|
|||
9971e7d2 | Christian Ehringfeld | QString password = "", QString port = "", bool logQueries = false, QString databaseOptions = "");
|
|
dc618bda | Christian Ehringfeld | ~EntityManager();
|
|
static QStringList getConnectionNames();
|
|||
QSharedPointer<QueryBuilder> getQueryBuilder() const;
|
|||
1b167b6c | Christian Ehringfeld | ||
c9f21778 | Christian Ehringfeld | template<class T> QList<QSharedPointer<T>> find(Query &q,
|
|
const bool joinBaseClasses = false, const bool resolveRelations = true) {
|
|||
1b167b6c | Christian Ehringfeld | QSharedPointer<Entity> ptr = QSharedPointer<Entity>
|
|
(EntityInstanceFactory::createInstance<T *>());
|
|||
if (ptr) {
|
|||
if (q.getFrom().isEmpty()) {
|
|||
q.setFrom(QStringList(ptr->getTablename()));
|
|||
}
|
|||
c9f21778 | Christian Ehringfeld | if (joinBaseClasses) {
|
|
q.appendJoins(this->schema->getQueryBuilder()->joinBaseClasses(ptr));
|
|||
}
|
|||
1b167b6c | Christian Ehringfeld | QSqlQuery query = this->queryInterpreter->build(q);
|
|
auto maps = this->convertQueryResult(query);
|
|||
c9f21778 | Christian Ehringfeld | auto converted = this->convert(maps, EntityHelper::getClassname(ptr.data()),
|
|
resolveRelations);
|
|||
1b167b6c | Christian Ehringfeld | return this->convertList<T>(converted);
|
|
}
|
|||
return QList<QSharedPointer<T>>();
|
|||
}
|
|||
c9f21778 | Christian Ehringfeld | template<class T> QList<QSharedPointer<T>> findAll(const bool resolveRelations =
|
|
true) {
|
|||
6d91d381 | Christian Ehringfeld | QSharedPointer<Entity> ptr = QSharedPointer<Entity>
|
|
506067a2 | Christian Ehringfeld | (EntityInstanceFactory::createInstance<T *>());
|
|
6d91d381 | Christian Ehringfeld | if (ptr) {
|
|
f5087482 | Christian Ehringfeld | auto maps = this->findAll(ptr);
|
|
c9f21778 | Christian Ehringfeld | auto converted = this->convert(maps, EntityHelper::getClassname(ptr.data()),
|
|
resolveRelations);
|
|||
506067a2 | Christian Ehringfeld | return this->convertList<T>(converted);
|
|
9070a496 | Christian Ehringfeld | }
|
|
506067a2 | Christian Ehringfeld | return QList<QSharedPointer<T>>();
|
|
9070a496 | Christian Ehringfeld | }
|
|
506067a2 | Christian Ehringfeld | template<class T> QSharedPointer<T> findById(const qint64 &id) {
|
|
auto e = EntityInstanceFactory::createInstance<T *>();
|
|||
f5087482 | Christian Ehringfeld | QSharedPointer<Entity> ptr = QSharedPointer<Entity>(e);
|
|
506067a2 | Christian Ehringfeld | return this->findById(id, ptr).objectCast<T>();
|
|
a205e8a9 | Christian Ehringfeld | }
|
|
9070a496 | Christian Ehringfeld | ||
c9f21778 | Christian Ehringfeld | template<class T>
|
|
QSharedPointer<T> findEntityByAttributes(
|
|||
ac5cbdcf | Christian Ehringfeld | const QHash<QString, QVariant>
|
|
c9f21778 | Christian Ehringfeld | &attributes, const bool joinBaseClasses = false,
|
|
const bool resolveRelations = true) {
|
|||
auto list = this->findAllEntitiesByAttributes<T>(attributes, 1, 0,
|
|||
joinBaseClasses, resolveRelations);
|
|||
11bbe9a6 | Christian Ehringfeld | if (list.isEmpty()) {
|
|
506067a2 | Christian Ehringfeld | return QSharedPointer<T>();
|
|
11bbe9a6 | Christian Ehringfeld | }
|
|
506067a2 | Christian Ehringfeld | QSharedPointer<Entity> obj = list.at(0);
|
|
return obj.objectCast<T>();
|
|||
d84c91e7 | Christian Ehringfeld | }
|
|
506067a2 | Christian Ehringfeld | template<class T> QList<QSharedPointer<T>> findAllEntitiesByAttributes(
|
|
const QHash<QString, QVariant> &attributes =
|
|||
c9f21778 | Christian Ehringfeld | QHash<QString, QString>(), quint64 limit = 0, quint64 offset = 0,
|
|
bool joinBaseClasses = false, const bool resolveRelations = true) {
|
|||
9e44a59b | Christian Ehringfeld | QSharedPointer<Entity> e = QSharedPointer<Entity>
|
|
c9f21778 | Christian Ehringfeld | (EntityInstanceFactory::createInstance<T *>());
|
|
ac5cbdcf | Christian Ehringfeld | if (e) {
|
|
9e44a59b | Christian Ehringfeld | Query query = Query(QStringList(e->getTablename()));
|
|
c9f21778 | Christian Ehringfeld | if (joinBaseClasses) {
|
|
query.appendJoins(this->schema->getQueryBuilder()->joinBaseClasses(e));
|
|||
}
|
|||
9e44a59b | Christian Ehringfeld | query.appendWhere(this->schema->getQueryBuilder()->where(attributes));
|
|
506067a2 | Christian Ehringfeld | query.setLimit(limit);
|
|
query.setOffset(offset);
|
|||
QSqlQuery q = this->queryInterpreter->build(query);
|
|||
auto results = this->convertQueryResult(q);
|
|||
c9f21778 | Christian Ehringfeld | auto list = this->convert(results, EntityHelper::getClassname(e.data()),
|
|
resolveRelations);
|
|||
506067a2 | Christian Ehringfeld | return this->convertList<T>(list);
|
|
ac5cbdcf | Christian Ehringfeld | }
|
|
9e44a59b | Christian Ehringfeld | return QList<QSharedPointer<T>>();
|
|
11bbe9a6 | Christian Ehringfeld | }
|
|
d84c91e7 | Christian Ehringfeld | ||
506067a2 | Christian Ehringfeld | template<class T> QList<QSharedPointer<T>> findEntitiesBySql(
|
|
e0e1ead8 | Christian Ehringfeld | const QString &sql) {
|
|
506067a2 | Christian Ehringfeld | QSharedPointer<T> e = EntityInstanceFactory::createInstance<T *>();
|
|
11bbe9a6 | Christian Ehringfeld | if (e) {
|
|
dc618bda | Christian Ehringfeld | QSqlQuery q = this->schema->getDatabase()->getQuery(sql);
|
|
11bbe9a6 | Christian Ehringfeld | auto result = this->convertQueryResult(q);
|
|
506067a2 | Christian Ehringfeld | auto converted = this->convert(result, EntityHelper::getClassname(e));
|
|
return this->convertList<T>(converted);
|
|||
11bbe9a6 | Christian Ehringfeld | }
|
|
506067a2 | Christian Ehringfeld | return QList<QSharedPointer<T>>();
|
|
d84c91e7 | Christian Ehringfeld | }
|
|
dc6b13b4 | Christian Ehringfeld | ||
11bbe9a6 | Christian Ehringfeld | template<class T> bool remove(const QList<qint64> &ids) {
|
|
bool ok = true;
|
|||
foreach (qint64 var, ids) {
|
|||
506067a2 | Christian Ehringfeld | if (!this->remove<T *>(var)) {
|
|
11bbe9a6 | Christian Ehringfeld | ok = false;
|
|
break;
|
|||
}
|
|||
}
|
|||
return ok;
|
|||
}
|
|||
d84c91e7 | Christian Ehringfeld | ||
11bbe9a6 | Christian Ehringfeld | template<class T> bool remove(qint64 id) {
|
|
506067a2 | Christian Ehringfeld | Entity *e = EntityInstanceFactory::createInstance<T *>();
|
|
9070a496 | Christian Ehringfeld | if (e) {
|
|
QSharedPointer<Entity> ptr = QSharedPointer<Entity>(e);
|
|||
abb9e8c5 | Christian Ehringfeld | ptr->setId(id);
|
|
9070a496 | Christian Ehringfeld | return this->remove(ptr);
|
|
}
|
|||
return false;
|
|||
d84c91e7 | Christian Ehringfeld | }
|
|
11bbe9a6 | Christian Ehringfeld | ||
a82e4cea | Christian Ehringfeld | protected:
|
|
506067a2 | Christian Ehringfeld | template<class T> QList<QSharedPointer<T>> convertList(const
|
|
QList<QSharedPointer<Entity>> &list) {
|
|||
QList<QSharedPointer<T>> newList = QList<QSharedPointer<T>>();
|
|||
for (int i = 0; i < list.size(); ++i) {
|
|||
newList.append(list.at(i).objectCast<T>());
|
|||
}
|
|||
return newList;
|
|||
}
|
|||
a82e4cea | Christian Ehringfeld | void init();
|
|
QList<QHash<QString, QVariant> > findAll(const QSharedPointer<Entity> &e);
|
|||
void resolveRelations(const QSharedPointer<Entity> &entity,
|
|||
const QHash<QString, QVariant> &map, const bool refresh = false);
|
|||
QHash<QString, QVariant> findByPk(qint64 id, const QSharedPointer<Entity> &e);
|
|||
void manyToOne(const QSharedPointer<Entity> &entity, const QVariant &id,
|
|||
const QMetaProperty &property, const bool refresh = false);
|
|||
void oneToMany(const QSharedPointer<Entity> &entity, const Relation &r,
|
|||
const QMetaProperty &property, const bool refresh = false);
|
|||
void manyToMany(const QSharedPointer<Entity> &entity,
|
|||
66704054 | Christian Ehringfeld | const QMetaProperty &property, const Relation &relation,
|
|
const bool refresh = false);
|
|||
a82e4cea | Christian Ehringfeld | void oneToOne(const QSharedPointer<Entity> &entity, const Relation &r,
|
|
const QMetaProperty &property, const bool refresh = false,
|
|||
const QVariant &id = "");
|
|||
bool canPersistRelation(const Relation &relation, const RelationType &r,
|
|||
const QVariant &var) const;
|
|||
void persistManyToMany(const QSharedPointer<Entity> &entity, const Relation &r,
|
|||
QVariant &property);
|
|||
QList<QHash<QString, QVariant> > findAllByAttributes(const
|
|||
QSharedPointer<Entity> &entity,
|
|||
bool ignoreID = false);
|
|||
QList<QHash<QString, QVariant> > findAllByAttributes(const
|
|||
QHash<QString, QVariant> &m,
|
|||
const QString &tblname,
|
|||
bool ignoreID = false);
|
|||
QSharedPointer<Entity> findById(const qint64 &id, QSharedPointer<Entity> &e,
|
|||
const bool refresh = false);
|
|||
void savePrePersistedRelations(const QSharedPointer<Entity> &entity);
|
|||
void savePostPersistedRelations(const QSharedPointer<Entity> &entity);
|
|||
QList<QSharedPointer<Entity>> saveRelationEntities(const
|
|||
QList<QSharedPointer<Entity>> &list, const Relation &r);
|
|||
void persistMappedByRelation(const QList<QSharedPointer<Entity>> &list,
|
|||
QSqlQuery &q, const QSharedPointer<Entity> &entity,
|
|||
const QSharedPointer<Entity> &ptr, const Relation &r,
|
|||
const QString &tblName);
|
|||
bool shouldBeSaved(QSharedPointer<Entity> &entity , const Relation &r);
|
|||
void removeRelations(const QSharedPointer<Entity> &entity);
|
|||
void removeEntityList(QVariant &var);
|
|||
void removeManyToManyEntityList(const QSharedPointer<Entity> &e,
|
|||
const Relation &r, QVariant &var);
|
|||
void removeEntity(QVariant &var);
|
|||
void setNullOneToManyRelation(QVariant &var, const Relation &r);
|
|||
void setNullEntityPropertyRelation(QVariant &var, const Relation &r);
|
|||
e8d1537c | Christian Ehringfeld | QSharedPointer<Entity> convert(const QHash<QString, QVariant> &map,
|
|
c9f21778 | Christian Ehringfeld | const char *classname, const bool refresh = false,
|
|
const bool resolveRelations = true);
|
|||
e8d1537c | Christian Ehringfeld | QList<QSharedPointer<Entity>> convert(QList<QHash<QString, QVariant> > maps,
|
|
c9f21778 | Christian Ehringfeld | const char *classname, const bool refresh = false,
|
|
const bool resolveRelations = true);
|
|||
66704054 | Christian Ehringfeld | void missingManyToManyTable(const QString &tblName,
|
|
const QSharedPointer<Entity> &e, const Relation &r);
|
|||
bool isRelationPropertyValid(const QMetaProperty &prop, const Relation &r,
|
|||
const QSharedPointer<Entity> &e, const QSharedPointer<Entity> &relatedEntity);
|
|||
a82e4cea | Christian Ehringfeld | ||
private:
|
|||
static QStringList connectionNames;
|
|||
QSharedPointer<Schema> schema;
|
|||
static void setConnectionNames(QStringList list);
|
|||
QSharedPointer<Database> db;
|
|||
Cache cache;
|
|||
QString createConnection();
|
|||
QList<QHash<QString, QVariant> > convertQueryResult(QSqlQuery &q);
|
|||
bool checkTable(const QSharedPointer<Entity> &entity);
|
|||
2ee5022f | Christian Ehringfeld | QSharedPointer<QueryInterpreter> queryInterpreter;
|
|
a82e4cea | Christian Ehringfeld | ||
81c23b56 | Christian Ehringfeld | };
|
|
}
|
|||
abb9e8c5 | Christian Ehringfeld | Q_DECLARE_METATYPE(QSharedPointer<CuteEntityManager::Entity>)
|
|
81c23b56 | Christian Ehringfeld | #endif // ENTITYMANAGER_H
|