Projekt

Allgemein

Profil

Herunterladen als
Herunterladen (9,81 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
#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>
9d05e414 Christian Ehringfeld
#include "schema.h"
81c23b56 Christian Ehringfeld
#include <QtSql/QSqlError>
a205e8a9 Christian Ehringfeld
#include <QMetaType>
81c23b56 Christian Ehringfeld
#include "entity.h"
#include "database.h"
a205e8a9 Christian Ehringfeld
#include "entityinstancefactory.h"
3820ae33 Christian Ehringfeld
#include "cache.h"
81c23b56 Christian Ehringfeld
6899f814 Christian Ehringfeld
namespace CuteEntityManager {
81c23b56 Christian Ehringfeld
ac8aede7 Christian Ehringfeld
class EntityManager : public QObject {
Q_OBJECT
24c5480c Christian Ehringfeld
signals:
void actionFinished(qint64 id);

426974c6 Christian Ehringfeld
private:
81c23b56 Christian Ehringfeld
static QStringList connectionNames;
9d05e414 Christian Ehringfeld
QSharedPointer<Schema> schema;
81c23b56 Christian Ehringfeld
static void setConnectionNames(QStringList list);
9d05e414 Christian Ehringfeld
QSharedPointer<Database> db;
3820ae33 Christian Ehringfeld
Cache cache;
81c23b56 Christian Ehringfeld
QString createConnection();
QList<QHash<QString, QVariant> > convertQueryResult(QSqlQuery &q);
586bb527 Christian Ehringfeld
bool checkTable(const QSharedPointer<Entity> &entity);
81c23b56 Christian Ehringfeld
426974c6 Christian Ehringfeld
protected:
7e233492 Christian Ehringfeld
void init();
f5087482 Christian Ehringfeld
QList<QHash<QString, QVariant> > findAll(const QSharedPointer<Entity> &e);
e0e1ead8 Christian Ehringfeld
void resolveRelations(const QSharedPointer<Entity> &entity,
4f3b13f3 Christian Ehringfeld
const QHash<QString, QVariant> &map, const bool refresh = false);
6d91d381 Christian Ehringfeld
QHash<QString, QVariant> findByPk(qint64 id, const QSharedPointer<Entity> &e);
e0e1ead8 Christian Ehringfeld
QSharedPointer<Entity> convert(const QHash<QString, QVariant> &map,
4f3b13f3 Christian Ehringfeld
const char *classname, const bool refresh = false);
e0e1ead8 Christian Ehringfeld
QList<QSharedPointer<Entity>> convert(QList<QHash<QString, QVariant> > maps,
4f3b13f3 Christian Ehringfeld
const char *classname, const bool refresh = false);
e0e1ead8 Christian Ehringfeld
void manyToOne(const QSharedPointer<Entity> &entity, const QVariant &id,
4f3b13f3 Christian Ehringfeld
const QMetaProperty &property, const bool refresh = false);
e0e1ead8 Christian Ehringfeld
void oneToMany(const QSharedPointer<Entity> &entity, const Relation &r,
244c6d53 Christian Ehringfeld
const QMetaProperty &property, const bool refresh = false);
afde9013 Christian Ehringfeld
void manyToMany(const QSharedPointer<Entity> &entity,
244c6d53 Christian Ehringfeld
const QMetaProperty &property, const bool refresh = false);
e0e1ead8 Christian Ehringfeld
void oneToOne(const QSharedPointer<Entity> &entity, const Relation &r,
4f3b13f3 Christian Ehringfeld
const QMetaProperty &property, const bool refresh = false,
a06633f7 Christian Ehringfeld
const QVariant &id = "");
98b5b08d Christian Ehringfeld
bool canPersistRelation(const Relation &relation, const RelationType &r,
const QVariant &var) const;
void persistManyToMany(const QSharedPointer<Entity> &entity, const Relation &r,
abb9e8c5 Christian Ehringfeld
QVariant &property);
e0e1ead8 Christian Ehringfeld
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,
dc6b13b4 Christian Ehringfeld
bool ignoreID = false);
f5087482 Christian Ehringfeld
QSharedPointer<Entity> findById(const qint64 &id, QSharedPointer<Entity> &e,
244c6d53 Christian Ehringfeld
const bool refresh = false);
91ed1164 Christian Ehringfeld
void addEntityToListProperty(const QSharedPointer<Entity> &entity,
QSharedPointer<Entity> add, const QMetaProperty &property);
e0e1ead8 Christian Ehringfeld
void setListProperty(const QSharedPointer<Entity> &entity,
QList<QSharedPointer<Entity>> &list,
97846191 Christian Ehringfeld
const QMetaProperty &property) const;
e0e1ead8 Christian Ehringfeld
void setProperty(const QSharedPointer<Entity> &entiy,
QSharedPointer<Entity> value,
const QMetaProperty &property) const;
e24791d7 Christian Ehringfeld
void saveRelations(const QSharedPointer<Entity> &entity);
QList<QSharedPointer<Entity>> saveRelationEntities(const
91ed1164 Christian Ehringfeld
QList<QSharedPointer<Entity>> &list, const Relation &r);
void persistMappedByRelation(const QList<QSharedPointer<Entity>> &list,
3fd96253 Christian Ehringfeld
QSqlQuery &q, const QSharedPointer<Entity> &entity, const QSharedPointer<Entity> &ptr, const Relation &r,
91ed1164 Christian Ehringfeld
const QString &tblName);
e24791d7 Christian Ehringfeld
bool shouldBeSaved(QSharedPointer<Entity> &entity , const Relation &r);
void removeRelations(const QSharedPointer<Entity> &entity);
2075db87 Christian Ehringfeld
void removeEntityList(QVariant &var);
47f9301a Christian Ehringfeld
void removeManyToManyEntityList(const QSharedPointer<Entity> &e,
const Relation &r, QVariant &var);
2075db87 Christian Ehringfeld
void removeEntity(QVariant &var);
void setNullOneToManyRelation(QVariant &var, const Relation &r);
void setNullEntityPropertyRelation(QVariant &var, const Relation &r);
d7727319 Christian Ehringfeld
QMetaProperty mappedProperty(const Relation &r,const QSharedPointer<Entity> &foreignEntity) const;
81c23b56 Christian Ehringfeld
426974c6 Christian Ehringfeld
public:
81c23b56 Christian Ehringfeld
EntityManager(QSqlDatabase database);
e0e1ead8 Christian Ehringfeld
EntityManager(const QString &databaseType, QString databasename = "" ,
QString hostname = "",
QString username = "",
426974c6 Christian Ehringfeld
QString password = "", QString port = "");
81c23b56 Christian Ehringfeld
~EntityManager();
static QStringList getConnectionNames();
1e213c09 Christian Ehringfeld
/**
* @brief startup
* @param version must be unique
* @param toInitialize list of entity classnames which database tables should be created
* @return
*/
8306a974 Christian Ehringfeld
public slots:
1e213c09 Christian Ehringfeld
bool startup(QString version, QStringList toInitialize);
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);
e86c23a2 Christian Ehringfeld
bool createTable(const QSharedPointer<Entity> &entity, bool createRelationTables=true);
9070a496 Christian Ehringfeld
qint8 count(const QSharedPointer<Entity> &entity, bool ignoreID = true);
qint8 count(const QString &tableName);
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);
244c6d53 Christian Ehringfeld
/**
9070a496 Christian Ehringfeld
*@TODO use conditions
*/
8306a974 Christian Ehringfeld
public:
e0e1ead8 Christian Ehringfeld
template<class T> qint8 count(QHash<QString, QString> condition =
QHash<QString, QString>()) {
9070a496 Christian Ehringfeld
Entity *e = EntityInstanceFactory::createInstance<T>();
qint8 rc = 0;
if (e) {
rc = this->count(e->getTablename());
delete e;
}
return rc;
}

template<class T> QList<QSharedPointer<Entity>> findAll() {
6d91d381 Christian Ehringfeld
QSharedPointer<Entity> ptr = QSharedPointer<Entity>
(EntityInstanceFactory::createInstance<T>());
if (ptr) {
f5087482 Christian Ehringfeld
auto maps = this->findAll(ptr);
const char *className = ptr.data()->getClassname();
9070a496 Christian Ehringfeld
return this->convert(maps, className);
}
return QList<QSharedPointer<Entity>>();
}

a205e8a9 Christian Ehringfeld
template<class T> QSharedPointer<Entity> findById(const qint64 &id) {
f5087482 Christian Ehringfeld
auto e = EntityInstanceFactory::createInstance<T>();
QSharedPointer<Entity> ptr = QSharedPointer<Entity>(e);
return this->findById(id, ptr);
a205e8a9 Christian Ehringfeld
}
9070a496 Christian Ehringfeld
e0e1ead8 Christian Ehringfeld
template<class T> QSharedPointer<Entity> findEntityByAttributes(
const QHash<QString, QString>
&attributes) {
11bbe9a6 Christian Ehringfeld
auto list = this->findAllEntitiesByAttributes<T>(attributes, 1, 0);
if (list.isEmpty()) {
return QSharedPointer<Entity>();
}
return list.at(0);
d84c91e7 Christian Ehringfeld
}

e0e1ead8 Christian Ehringfeld
template<class T> QList<QSharedPointer<Entity>> findAllEntitiesByAttributes(
const QHash<QString, QString> &attributes =
11bbe9a6 Christian Ehringfeld
QHash<QString, QString>(), quint32 limit = 0, quint32 offset = 0) {
auto list = this->findAllEntitiesByAttributes<T>(attributes);
return list;
}
d84c91e7 Christian Ehringfeld
e0e1ead8 Christian Ehringfeld
template<class T> QList<QSharedPointer<Entity>> findEntitiesBySql(
const QString &sql) {
11bbe9a6 Christian Ehringfeld
Entity *e = EntityInstanceFactory::createInstance<T>();
if (e) {
ac8aede7 Christian Ehringfeld
QSqlQuery q = this->db.data()->select(sql);
11bbe9a6 Christian Ehringfeld
auto result = this->convertQueryResult(q);
auto ret = this->convert(result, e->getClassname());
delete e;
return ret;
}
return QList<QSharedPointer<Entity>>();
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) {
if (!this->remove<T>(var)) {
ok = false;
break;
}
}
return ok;
}
d84c91e7 Christian Ehringfeld
11bbe9a6 Christian Ehringfeld
template<class T> bool remove(qint64 id) {
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
81c23b56 Christian Ehringfeld
};
}
abb9e8c5 Christian Ehringfeld
Q_DECLARE_METATYPE(QSharedPointer<CuteEntityManager::Entity>)
81c23b56 Christian Ehringfeld
#endif // ENTITYMANAGER_H