Herunterladen als
root/src/entitymanager.h @ c599658a
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>
|
|||
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 | ||
426974c6 | Christian Ehringfeld | class EntityManager {
|
|
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();
|
|
586bb527 | Christian Ehringfeld | QString createTableQuery(const QSharedPointer<Entity> &entity);
|
|
81c23b56 | Christian Ehringfeld | 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();
|
|
dc6b13b4 | Christian Ehringfeld | QList<QHash<QString, QVariant> > findAll(QString tblname);
|
|
a06633f7 | Christian Ehringfeld | void resolveRelations(const QSharedPointer<Entity> &entity, const QHash<QString, QVariant> &map);
|
|
2d9fab10 | Christian Ehringfeld | QHash<QString, QVariant> findByPk(qint64 id, QString tblname);
|
|
f4e3904b | Christian Ehringfeld | QSharedPointer<Entity> convert(const QHash<QString, QVariant> &map, const char *classname);
|
|
1e213c09 | Christian Ehringfeld | QList<QSharedPointer<Entity>> convert(QList<QHash<QString, QVariant> > maps, const char *classname);
|
|
a06633f7 | Christian Ehringfeld | void manyToOne(const QSharedPointer<Entity> &entity, const QVariant &id, const QMetaProperty &property);
|
|
void oneToMany(const QSharedPointer<Entity> &entity, const Relation &r, const QMetaProperty &property);
|
|||
void manyToMany(const QSharedPointer<Entity> &entity, const Relation &r, const QMetaProperty &property);
|
|||
void oneToOne(const QSharedPointer<Entity> &entity, const Relation &r, const QMetaProperty &property,
|
|||
const QVariant &id = "");
|
|||
1e213c09 | 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);
|
|
2d9fab10 | Christian Ehringfeld | QSharedPointer<Entity> findById(const qint64 &id, Entity *&e);
|
|
97846191 | Christian Ehringfeld | void setListProperty(const QSharedPointer<Entity> &entity, QList<QSharedPointer<Entity>> &list,
|
|
const QMetaProperty &property) const;
|
|||
c599658a | Christian Ehringfeld | void setProperty(const QSharedPointer<Entity> &entiy, QSharedPointer<Entity> value, const QMetaProperty &property) const;
|
|
81c23b56 | Christian Ehringfeld | ||
426974c6 | Christian Ehringfeld | public:
|
|
81c23b56 | Christian Ehringfeld | EntityManager(QSqlDatabase database);
|
|
426974c6 | Christian Ehringfeld | EntityManager(const QString &databaseType, QString databasename = "" , QString hostname = "", QString username = "",
|
|
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
|
|||
*/
|
|||
bool startup(QString version, QStringList toInitialize);
|
|||
81c23b56 | Christian Ehringfeld | static void removeConnectionName(const QString &name);
|
|
2d9fab10 | Christian Ehringfeld | QSharedPointer<Entity> findById(const qint64 &id, const QString &classname);
|
|
dc6b13b4 | Christian Ehringfeld | QList<QSharedPointer<Entity>> findEntityByAttributes(const QSharedPointer<Entity> &entity, bool ignoreID = false);
|
|
9070a496 | Christian Ehringfeld | bool create(QList<QSharedPointer<Entity>> entities);
|
|
bool create(QSharedPointer<Entity> &entity);
|
|||
bool save(QSharedPointer<Entity> &entity);
|
|||
qint64 findId(QSharedPointer<Entity> &entity);
|
|||
bool merge(QSharedPointer<Entity> &entity, bool withRelations = true);
|
|||
bool remove(QSharedPointer<Entity> &entity);
|
|||
bool removeAll(QString tblname);
|
|||
bool createTable(const QSharedPointer<Entity> &entity);
|
|||
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;
|
|||
void setSchema(const QSharedPointer<Schema> &value);
|
|||
/**
|
|||
*@TODO use conditions
|
|||
*/
|
|||
template<class T> qint8 count(QHash<QString, QString> condition = QHash<QString, QString>()) {
|
|||
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() {
|
|||
Entity *e = EntityInstanceFactory::createInstance<T>();
|
|||
if (e) {
|
|||
auto maps = this->findAll(e->getTablename());
|
|||
const char *className = e->getClassname();
|
|||
delete e;
|
|||
return this->convert(maps, className);
|
|||
}
|
|||
return QList<QSharedPointer<Entity>>();
|
|||
}
|
|||
a205e8a9 | Christian Ehringfeld | template<class T> QSharedPointer<Entity> findById(const qint64 &id) {
|
|
Entity *e = EntityInstanceFactory::createInstance<T>();
|
|||
2d9fab10 | Christian Ehringfeld | return this->findById(id, e);
|
|
a205e8a9 | Christian Ehringfeld | }
|
|
9070a496 | Christian Ehringfeld | ||
11bbe9a6 | Christian Ehringfeld | template<class T> QSharedPointer<Entity> findEntityByAttributes(const QHash<QString, QString> &attributes) {
|
|
auto list = this->findAllEntitiesByAttributes<T>(attributes, 1, 0);
|
|||
if (list.isEmpty()) {
|
|||
return QSharedPointer<Entity>();
|
|||
}
|
|||
return list.at(0);
|
|||
d84c91e7 | Christian Ehringfeld | }
|
|
11bbe9a6 | Christian Ehringfeld | template<class T> QList<QSharedPointer<Entity>> findAllEntitiesByAttributes(const QHash<QString, QString> &attributes =
|
|
QHash<QString, QString>(), quint32 limit = 0, quint32 offset = 0) {
|
|||
auto list = this->findAllEntitiesByAttributes<T>(attributes);
|
|||
return list;
|
|||
}
|
|||
d84c91e7 | Christian Ehringfeld | ||
11bbe9a6 | Christian Ehringfeld | template<class T> QList<QSharedPointer<Entity>> findEntitiesBySql(const QString &sql) {
|
|
Entity *e = EntityInstanceFactory::createInstance<T>();
|
|||
if (e) {
|
|||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->getQuery();
|
|||
q = this->db.data()->select(sql);
|
|||
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);
|
|||
e->setId(id);
|
|||
return this->remove(ptr);
|
|||
}
|
|||
return false;
|
|||
d84c91e7 | Christian Ehringfeld | }
|
|
11bbe9a6 | Christian Ehringfeld | ||
81c23b56 | Christian Ehringfeld | };
|
|
}
|
|||
#endif // ENTITYMANAGER_H
|