Revision f4e3904b
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
EntityManager.pro | ||
---|---|---|
src/databasemigration.h \
|
||
src/querybuilder.h \
|
||
src/schema/sqlitequerybuilder.h \
|
||
src/relation.h
|
||
src/relation.h \
|
||
src/entityinstancefactory.h
|
||
|
||
SOURCES += \
|
||
src/entity.cpp \
|
||
... | ... | |
src/databasemigration.cpp \
|
||
src/querybuilder.cpp \
|
||
src/schema/sqlitequerybuilder.cpp \
|
||
src/relation.cpp
|
||
src/relation.cpp \
|
||
src/entityinstancefactory.cpp
|
||
|
||
unix {
|
||
target.path = /usr/lib
|
src/entity.cpp | ||
---|---|---|
return "id";
|
||
}
|
||
|
||
QHash<QString, QMetaProperty> Entity::getMetaProperties() const {
|
||
QHash<QString, QMetaProperty> h = QHash<QString, QMetaProperty>();
|
||
for (int var = 0; var < this->metaObject()->propertyCount(); ++var) {
|
||
QMetaProperty m = this->metaObject()->property(var);
|
||
if (m.name() != QString("objectName") && m.isValid()) {
|
||
h.insert(m.name(), m);
|
||
}
|
||
}
|
||
return h;
|
||
}
|
||
|
||
const char *Entity::getClassname() const {
|
||
return this->metaObject()->className();
|
||
}
|
||
|
||
qint64 Entity::getId() const {
|
||
return id;
|
||
}
|
src/entity.h | ||
---|---|---|
virtual QStringList getBLOBColumns();
|
||
//return value must be the exact name defined in Q_PROPERTY
|
||
virtual QString getPrimaryKey();
|
||
QHash<QString, QMetaProperty> getMetaProperties() const;
|
||
const char* getClassname() const;
|
||
|
||
qint64 getId() const;
|
||
void setId(const qint64 &value);
|
src/entityinstancefactory.cpp | ||
---|---|---|
#include "entityinstancefactory.h"
|
||
#include "entity.h"
|
||
#include <QMetaType>
|
||
using namespace CuteEntityManager;
|
||
EntityInstanceFactory::EntityInstanceFactory() {
|
||
|
||
}
|
||
|
||
Entity *EntityInstanceFactory::createInstance(const char *className) {
|
||
return EntityInstanceFactory::createInstance(QMetaType::type(className));
|
||
}
|
||
|
||
Entity *EntityInstanceFactory::createInstance(int id) {
|
||
Entity *e = 0;
|
||
if (id != -1) {
|
||
e = static_cast<Entity *>(QMetaType::create(id));
|
||
}
|
||
return e;
|
||
}
|
||
|
||
Entity *EntityInstanceFactory::createInstance(const char *className, const QHash<QString, QVariant> &attributes) {
|
||
Entity *e = EntityInstanceFactory::createInstance(className);
|
||
EntityInstanceFactory::setAttributes(e, attributes);
|
||
return e;
|
||
}
|
||
|
||
Entity *EntityInstanceFactory::setAttributes(Entity *e, const QHash<QString, QVariant> &attributes,
|
||
QHash<QString, QMetaProperty> metaprops) {
|
||
if (e) {
|
||
auto iterator = attributes.constBegin();
|
||
while (iterator != attributes.constEnd()) {
|
||
if (metaprops.contains(iterator.key())) {
|
||
QMetaProperty prop = metaprops.value(iterator.key());
|
||
if (!prop.isWritable() && !prop.write(e, iterator.value())) {
|
||
qDebug() << prop.name() << "on Entity" << e->getClassname() << "not writeable!";
|
||
}
|
||
}
|
||
++iterator;
|
||
}
|
||
}
|
||
return e;
|
||
}
|
||
|
||
Entity *EntityInstanceFactory::setAttributes(Entity *e, const QHash<QString, QVariant> &attributes) {
|
||
auto metaprops = e->getMetaProperties();
|
||
return EntityInstanceFactory::setAttributes(e, attributes, metaprops);
|
||
}
|
src/entityinstancefactory.h | ||
---|---|---|
#ifndef ENTITYINSTANCEFACTORY_H
|
||
#define ENTITYINSTANCEFACTORY_H
|
||
#include <QString>
|
||
#include <QVariant>
|
||
#include <QHash>
|
||
|
||
namespace CuteEntityManager {
|
||
class Entity;
|
||
class EntityInstanceFactory {
|
||
public:
|
||
static Entity *createInstance(const char *className);
|
||
static Entity *createInstance(int id);
|
||
static Entity *createInstance(const char *className, const QHash<QString, QVariant> &attributes);
|
||
static Entity *setAttributes(Entity *e, const QHash<QString, QVariant> &attributes, QHash<QString, QMetaProperty> metaprops);
|
||
static Entity *setAttributes(Entity *e, const QHash<QString, QVariant> &attributes);
|
||
protected:
|
||
EntityInstanceFactory();
|
||
};
|
||
}
|
||
#endif // ENTITYINSTANCEFACTORY_H
|
src/entitymanager.cpp | ||
---|---|---|
|
||
#include "entitymanager.h"
|
||
#include "enums/databasetype.h"
|
||
#include "entityinstancefactory.h"
|
||
using namespace CuteEntityManager;
|
||
/**
|
||
* Relationen fehlen noch
|
||
* Fehlermeldungen erstellen am besten eine Exception Klasse diesbzgl. erstellen
|
||
*/
|
||
|
||
QStringList EntityManager::connectionNames = QStringList();
|
||
|
||
... | ... | |
EntityManager::connectionNames.removeOne(name);
|
||
}
|
||
|
||
QList<QSharedPointer<Entity> > EntityManager::findAllEntities(QSharedPointer<Entity> entity) {
|
||
QList<QSharedPointer<Entity>> EntityManager::findAllEntities(QSharedPointer<Entity> entity) {
|
||
auto maps = this->findAll(entity.data()->getTablename());
|
||
return this->convert(maps, entity.data()->getClassname());
|
||
}
|
||
|
||
|
||
QSharedPointer<Entity> EntityManager::convert(const QHash<QString, QVariant> &map, const char *classname) {
|
||
return QSharedPointer<Entity>(EntityInstanceFactory::createInstance(classname, map));
|
||
}
|
||
|
||
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) {
|
||
list.append(this->convert(maps.at(var), classname));
|
||
}
|
||
return list;
|
||
}
|
||
|
||
QSharedPointer<Entity> EntityManager::findEntity(QSharedPointer<Entity> entity) {
|
||
|
||
QSharedPointer<Entity> EntityManager::findEntity(QSharedPointer<Entity> entity) {
|
||
auto map = this->find(entity);
|
||
return this->convert(map, entity.data()->getClassname());
|
||
}
|
||
|
||
QList<QSharedPointer<Entity> > EntityManager::findEntityByAttributes(const QSharedPointer<Entity> &entity,
|
||
bool ignoreID) {
|
||
|
||
auto maps = this->findByAttributes(entity, ignoreID);
|
||
return this->convert(maps, entity.data()->getClassname());
|
||
}
|
||
|
||
/**
|
||
... | ... | |
return map;
|
||
}
|
||
|
||
|
||
QList<QHash<QString, QVariant> > EntityManager::findByAttributes(const QSharedPointer<Entity> &entity, bool ignoreID) {
|
||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->findByAttributes(entity, ignoreID);
|
||
return this->convertQueryResult(q);
|
src/entitymanager.h | ||
---|---|---|
QList<QHash<QString, QVariant> > findAll(QString tblname);
|
||
QHash<QString, QVariant> find(QSharedPointer<Entity> entity);
|
||
QHash<QString, QVariant> find(qint64 id, QString tblname);
|
||
QSharedPointer<Entity> convert(const QHash<QString, QVariant> &map, const char *classname);
|
||
QList<QSharedPointer<Entity>> convert(QList<QHash<QString, QVariant> > maps,const char *classname);
|
||
|
||
QList<QHash<QString, QVariant> > findByAttributes(const QSharedPointer<Entity> &entity, bool ignoreID = false);
|
||
QList<QHash<QString, QVariant> > findByAttributes(const QHash<QString, QVariant> &m, const QString &tblname,
|
||
bool ignoreID = false);
|
src/querybuilder.cpp | ||
---|---|---|
bool first = true;
|
||
QString s = "CREATE TABLE ";
|
||
s.append(this->schema.data()->quoteTableName(tableName).append(" (\n"));
|
||
QHash<QString, QString>::const_iterator i = tableDefinition.constBegin();
|
||
auto i = tableDefinition.constBegin();
|
||
while (i != tableDefinition.constEnd()) {
|
||
if (first) {
|
||
first = false;
|
||
... | ... | |
database = value;
|
||
}
|
||
|
||
|
||
QHash<QString, QMetaProperty> QueryBuilder::getMetaProperties(const QSharedPointer<Entity> &entity) const {
|
||
QHash<QString, QMetaProperty> h = QHash<QString, QMetaProperty>();
|
||
for (int var = 0; var < entity.data()->metaObject()->propertyCount(); ++var) {
|
||
QMetaProperty m = entity.data()->metaObject()->property(var);
|
||
if (m.name() != QString("objectName") && m.isValid()) {
|
||
h.insert(m.name(), m);
|
||
}
|
||
}
|
||
return h;
|
||
}
|
||
|
||
QString QueryBuilder::buildCreateQuery(QHash<QString, QVariant>::const_iterator i,
|
||
QHash<QString, QVariant>::const_iterator end,
|
||
QString &p1, QString &p2) const {
|
||
... | ... | |
const {
|
||
auto relations = QHash<QString, QHash<QString, QString>>();
|
||
QHash<QString, Relation> m = entity.data()->getRelations();
|
||
auto props = this->getMetaProperties(entity);
|
||
auto props = entity.data()->getMetaProperties();
|
||
for (auto i = m.begin(); i != m.end(); ++i) {
|
||
Relation r = i.value();
|
||
if (r.getType() == MANY_TO_MANY && r.getMappedBy().isEmpty()) {
|
||
... | ... | |
}
|
||
|
||
QSqlQuery QueryBuilder::findByAttributes(const QSharedPointer<Entity> &e, bool ignoreID) {
|
||
QHash<QString, QVariant> values = this->getEntityAttributes(this->getMetaProperties(e), e);
|
||
QHash<QString, QVariant> values = this->getEntityAttributes(e.data()->getMetaProperties(), e);
|
||
return this->findByAttributes(values, e.data()->getTablename(), ignoreID);
|
||
}
|
||
|
||
... | ... | |
}
|
||
|
||
QSqlQuery QueryBuilder::findId(const QSharedPointer<Entity> &entity) const {
|
||
QHash<QString, QVariant> values = this->getEntityAttributes(this->getMetaProperties(entity), entity);
|
||
QHash<QString, QVariant> values = this->getEntityAttributes(entity.data()->getMetaProperties(), entity);
|
||
QSqlQuery q = this->database.data()->getQuery("SELECT " + this->schema.data()->quoteColumnName("id") + " FROM " +
|
||
this->schema.data()->quoteTableName(
|
||
entity.data()->getTablename()) + this->where(values,
|
||
... | ... | |
}
|
||
|
||
QSqlQuery QueryBuilder::count(const QSharedPointer<Entity> &entity, bool ignoreID) const {
|
||
QHash<QString, QVariant> values = this->getEntityAttributes(this->getMetaProperties(entity), entity);
|
||
QHash<QString, QVariant> values = this->getEntityAttributes(entity.data()->getMetaProperties(), entity);
|
||
QSqlQuery q = this->database.data()->getQuery("SELECT COUNT(*) FROM " + this->schema.data()->quoteTableName(
|
||
entity.data()->getTablename()) + this->where(
|
||
values, "AND", ignoreID));
|
||
... | ... | |
}
|
||
|
||
QHash<QString, QVariant> QueryBuilder::saveAttributes(const QSharedPointer<Entity> &entity) const {
|
||
auto props = this->getMetaProperties(entity);
|
||
auto props = entity.data()->getMetaProperties();
|
||
auto values = this->getEntityAttributes(props, entity);
|
||
auto relValues = this->getManyToOneAttributes(props, entity);
|
||
auto iterator = relValues.constBegin();
|
||
... | ... | |
return values;
|
||
}
|
||
|
||
Entity *QueryBuilder::createInstance(const char *className) const {
|
||
return this->createInstance(QMetaType::type(className));
|
||
}
|
||
|
||
Entity *QueryBuilder::createInstance(int id) const {
|
||
Entity *e = 0;
|
||
if (id != -1) {
|
||
e = static_cast<Entity *>(QMetaType::create(id));
|
||
}
|
||
return e;
|
||
}
|
||
|
||
QHash<QString, QVariant> QueryBuilder::getEntityAttributes(const QHash<QString, QMetaProperty> &props,
|
||
const QSharedPointer<Entity> &entity) const {
|
||
Entity *e = entity.data();
|
||
... | ... | |
}
|
||
|
||
QString QueryBuilder::where(const QSharedPointer<Entity> &entity, QString conjunction, bool ignoreID) const {
|
||
return this->where(this->getEntityAttributes(this->getMetaProperties(entity), entity), conjunction, ignoreID);
|
||
return this->where(this->getEntityAttributes(entity.data()->getMetaProperties(), entity), conjunction, ignoreID);
|
||
}
|
||
|
||
QString QueryBuilder::where(const QHash<QString, QVariant> &m, const QString &conjunction, bool ignoreID) const {
|
src/relation.cpp | ||
---|---|---|
this->mappedBy = mappedBy;
|
||
this->optional = true;
|
||
this->tableName = tableName;
|
||
this->cascadeType = cascadeType;
|
||
if (this->type == MANY_TO_ONE) {
|
||
this->cascadeType = ALL;
|
||
} else {
|
||
this->cascadeType = cascadeType;
|
||
}
|
||
|
||
|
||
}
|
||
|
Auch abrufbar als: Unified diff
could(!) work, relation loading still not implemented