Revision ba800d6d
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
src/entity.cpp | ||
---|---|---|
}
|
||
|
||
Entity::~Entity() {
|
||
|
||
}
|
||
|
||
QString Entity::getTablename() {
|
||
... | ... | |
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")) {
|
||
if (m.name() != QString("objectName") && m.isValid()) {
|
||
h.insert(m.name(), m);
|
||
}
|
||
}
|
src/entity.h | ||
---|---|---|
namespace CuteEntityManager {
|
||
|
||
/**
|
||
* You should name any persisted property objectName, because its pre used by Qt and will be ignored by Entity Manager
|
||
* You mustn't name any persisted property objectName, because its pre used by Qt and will be ignored by Entity Manager
|
||
* @brief The Entity class
|
||
*/
|
||
class Entity : public QObject {
|
||
... | ... | |
/**
|
||
* The hashmap keys must be equal to the ones which are defined in the hashmap of getRelations()
|
||
* The EntityManager will only introspect Entity Objects, non-Entity inherited relations will be processed in a other way
|
||
* You must use this method, if you have a n-n Relation with Entity Objects.
|
||
* You must use this method, if you have a n-m Relation with Entity Objects.
|
||
* @brief getRelationObjects
|
||
* @return
|
||
*/
|
src/querybuilder.cpp | ||
---|---|---|
map.insert(m.name(), this->schema.data()->getTypeMap().data()->value(this->schema.data()->TYPE_INTEGER));
|
||
} else if (relations.contains(m.name())) {
|
||
Relation r = relations.value(m.name());
|
||
if (r.getType() == RelationType::BELONGS_TO) {
|
||
if (r.getType() == RelationType::MANY_TO_ONE) {
|
||
map.insert(QString(m.name()) + "_id", this->schema.data()->TYPE_BIGINT);
|
||
}
|
||
} else if (entity.data()->getBLOBColumns().contains(m.name())) {
|
||
... | ... | |
return map;
|
||
}
|
||
|
||
QList<QHash<QString, QString>> QueryBuilder::generateRelationTables(const QSharedPointer<Entity> &entity) const {
|
||
QList<QHash<QString, QString>> relations = QList<QHash<QString, QString>>();
|
||
QString QueryBuilder::generateManyToManyTableName(const QSharedPointer<Entity> &firstEntity,
|
||
const QSharedPointer<Entity> &secondEntity) const {
|
||
return QString(firstEntity.data()->metaObject()->className()).toLower() + "_" + QString(
|
||
secondEntity.data()->metaObject()->className()).toLower();
|
||
}
|
||
|
||
QHash<QString, QHash<QString, QString>> QueryBuilder::generateRelationTables(const QSharedPointer<Entity> &entity)
|
||
const {
|
||
auto relations = QHash<QString, QHash<QString, QString>>();
|
||
QHash<QString, Relation> m = entity.data()->getRelations();
|
||
QHash<QString, QSharedPointer<Entity>> os = entity.data()->getRelationObjects();
|
||
for(auto i = m.begin(); i != m.end(); ++i) {
|
||
for (auto i = m.begin(); i != m.end(); ++i) {
|
||
Relation r = i.value();
|
||
if(r.getType() == HAS_MANY) {
|
||
if (r.getType() == MANY_TO_MANY && r.getMappedBy().isEmpty()) {
|
||
QHash<QString, QString> h = QHash<QString, QString>();
|
||
h.insert("id",this->schema.data()->TYPE_BIGPK);
|
||
h.insert(QString(entity.data()->metaObject()->className())+ QString("_id"), this->schema.data()->TYPE_BIGINT);
|
||
if(os.contains(i.key())) {
|
||
h.insert(QString(os.value(i.key()).data()->metaObject()->className())+ QString("_id"),this->schema.data()->TYPE_BIGINT);
|
||
relations.append(h);
|
||
h.insert("id", this->schema.data()->TYPE_BIGPK);
|
||
h.insert(QString(entity.data()->metaObject()->className()) + QString("_id"), this->schema.data()->TYPE_BIGINT);
|
||
if (os.contains(i.key())) {
|
||
h.insert(QString(os.value(i.key()).data()->metaObject()->className()) + QString("_id"),
|
||
this->schema.data()->TYPE_BIGINT);
|
||
if (r.getTableName().isEmpty()) {
|
||
relations.insert(this->generateManyToManyTableName(entity, os.value(i.key())), h);
|
||
} else {
|
||
relations.insert(r.getTableName(), h);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
... | ... | |
return type;
|
||
}
|
||
|
||
QHash<QString, QVariant> QueryBuilder::getEntityAttributes(const QSharedPointer<Entity> &entity) {
|
||
QHash<QString, QVariant> QueryBuilder::getEntityAttributes(const QHash<QString, QMetaProperty> &props,
|
||
const QSharedPointer<Entity> &entity) {
|
||
Entity *e = entity.data();
|
||
auto map = QHash<QString, QVariant>();
|
||
auto metaObject = e->metaObject();
|
||
auto transientAttrs = e->getTransientAttributes();
|
||
for (int var = 0; var < metaObject->propertyCount(); ++var) {
|
||
auto p = metaObject->property(var);
|
||
QString name = QString(p.name());
|
||
if (p.isValid() && !transientAttrs.contains(name) && name != QString("objectName")) {
|
||
QVariant v = p.read(e);
|
||
//Relation
|
||
auto relations = e->getRelations();
|
||
auto i = props.constBegin();
|
||
while (i != props.constEnd()) {
|
||
if (!transientAttrs.contains(i.key()) && !relations.contains(i.key())) {
|
||
map.insert(i.key(), i.value().read(e));
|
||
}
|
||
++i;
|
||
}
|
||
return map;
|
||
}
|
||
|
||
QHash<QString, QVariant> QueryBuilder::getManyToOneAttributes(const QHash<QString, QMetaProperty> &props,
|
||
const QSharedPointer<Entity> &entity) {
|
||
Entity *e = entity.data();
|
||
auto map = QHash<QString, QVariant>();
|
||
auto relations = e->getRelations();
|
||
auto i = relations.constBegin();
|
||
while (i != relations.constEnd()) {
|
||
Relation r = i.value();
|
||
if (r.getType() == MANY_TO_ONE && props.contains(i.key())) {
|
||
auto v = props.value(i.key()).read(e);
|
||
if (v.canConvert<Entity *>()) {
|
||
this->insertRelationId(qvariant_cast<Entity *>(v), map, name);
|
||
this->insertRelationId(qvariant_cast<Entity *>(v), map, i.key());
|
||
} else if (v.canConvert<QSharedPointer<Entity>>()) {
|
||
this->insertRelationId(qvariant_cast<QSharedPointer<Entity>>(v).data(), map, name);
|
||
this->insertRelationId(qvariant_cast<QSharedPointer<Entity>>(v).data(), map, i.key());
|
||
} else if (v.canConvert<QPointer<Entity>>()) {
|
||
this->insertRelationId(qvariant_cast<QPointer<Entity>>(v).data(), map, name);
|
||
} else if (QString(p.typeName()).contains("QList")) {
|
||
/**
|
||
@TODO
|
||
//List and/or ManyToManyRelation
|
||
*/
|
||
auto n = static_cast<QList<CuteEntityManager::Entity *>*>(v.data());
|
||
for (int var = 0; var < n->size(); ++var) {
|
||
CuteEntityManager::Entity *entity = n->at(var);
|
||
qDebug() << entity->toString();
|
||
}
|
||
} else {
|
||
map.insert(name, v);
|
||
this->insertRelationId(qvariant_cast<QPointer<Entity>>(v).data(), map, i.key());
|
||
}
|
||
++i;
|
||
}
|
||
}
|
||
return map;
|
||
}
|
||
|
||
|
||
void QueryBuilder::insertRelationId(const Entity *e, QHash<QString, QVariant> &map, QString relName) {
|
||
if (e && e->getId() > -1) {
|
||
map.insert(relName + "_id", e->getId());
|
||
... | ... | |
return r;
|
||
}
|
||
|
||
|
||
|
||
QSharedPointer<Schema> QueryBuilder::getSchema() const {
|
||
return schema;
|
||
}
|
||
... | ... | |
void QueryBuilder::setSchema(const QSharedPointer<Schema> &value) {
|
||
schema = value;
|
||
}
|
||
|
src/querybuilder.h | ||
---|---|---|
virtual QString dropForeignKey(QString name, QString tableName) const;
|
||
virtual QString createIndex(QString name, QString tableName, QStringList columns, bool unique)const;
|
||
virtual QString dropIndex(QString name, QString tableName)const;
|
||
QHash<QString, QVariant> getEntityAttributes(const QSharedPointer<Entity> &entity);
|
||
QHash<QString, QVariant> getEntityAttributes(const QHash<QString, QMetaProperty> &props,
|
||
const QSharedPointer<Entity> &entity);
|
||
|
||
QSharedPointer<Schema> getSchema() const;
|
||
void setSchema(const QSharedPointer<Schema> &value);
|
||
|
||
QSharedPointer<Database> getDatabase() const;
|
||
void setDatabase(const QSharedPointer<Database> &value);
|
||
QList<QHash<QString, QString>> generateRelationTables(const QSharedPointer<Entity> &entity) const;
|
||
QHash<QString, QHash<QString, QString>> generateRelationTables(const QSharedPointer<Entity> &entity) const;
|
||
QHash<QString, QString> generateTableDefinition(const QSharedPointer<Entity> &entity) const;
|
||
QString generateManyToManyTableName(const QSharedPointer<Entity> &firstEntity,
|
||
const QSharedPointer<Entity> &secondEntity) const;
|
||
|
||
QString transformTypeToAbstractDbType(QString typeName) const;
|
||
QString transformAbstractTypeToRealDbType(QString typeName) const;
|
||
... | ... | |
protected:
|
||
void insertRelationId(const Entity *e, QHash<QString, QVariant> &map, QString relName);
|
||
QString buildColumns(const QStringList &columns) const;
|
||
QHash<QString, QVariant> getManyToOneAttributes(const QHash<QString, QMetaProperty> &props,
|
||
const QSharedPointer<Entity> &entity);
|
||
|
||
QSharedPointer<Schema> schema;
|
||
QSharedPointer<Database> database;
|
src/relation.cpp | ||
---|---|---|
this->propertyName = propertyName;
|
||
this->type = type;
|
||
this->optional = optional;
|
||
this->tableName = "";
|
||
this->cascadeType = ALL;
|
||
}
|
||
|
||
Relation::Relation(QString propertyName, RelationType type, QString mappedBy, QString tableName,
|
||
CascadeType cascadeType) {
|
||
this->propertyName = propertyName;
|
||
this->type = type;
|
||
this->mappedBy = mappedBy;
|
||
this->optional = true;
|
||
this->tableName = tableName;
|
||
this->cascadeType = cascadeType;
|
||
|
||
}
|
||
|
||
Relation::~Relation() {
|
||
... | ... | |
void Relation::setOptional(bool value) {
|
||
optional = value;
|
||
}
|
||
QString Relation::getMappedBy() const {
|
||
return mappedBy;
|
||
}
|
||
|
||
void Relation::setMappedBy(const QString &value) {
|
||
mappedBy = value;
|
||
}
|
||
QString Relation::getTableName() const {
|
||
return tableName;
|
||
}
|
||
|
||
void Relation::setTableName(const QString &value) {
|
||
tableName = value;
|
||
}
|
||
CascadeType Relation::getCascadeType() const {
|
||
return cascadeType;
|
||
}
|
||
|
||
void Relation::setCascadeType(const CascadeType &value) {
|
||
cascadeType = value;
|
||
}
|
||
|
||
|
||
|
src/relation.h | ||
---|---|---|
#include <QString>
|
||
namespace CuteEntityManager {
|
||
enum RelationType {
|
||
BELONGS_TO,
|
||
HAS_MANY,
|
||
HAS_ONE,
|
||
MANY_MANY,
|
||
ONE_TO_ONE, //e.g. specialization
|
||
ONE_TO_MANY, //@OneToMany(cascade=ALL, mappedBy="customer")
|
||
MANY_TO_ONE, //1-n Entity foreign key in same table
|
||
MANY_TO_MANY,
|
||
};
|
||
|
||
enum CascadeType {
|
||
ALL,
|
||
MERGE,
|
||
PERSIST,
|
||
REFRESH,
|
||
REMOVE,
|
||
};
|
||
|
||
class Relation {
|
||
public:
|
||
Relation();
|
||
Relation(QString propertyName, RelationType type, bool optional = true);
|
||
/**
|
||
* @brief Relation
|
||
* @param propertyName
|
||
* @param type
|
||
* @param mappedBy Q_PROPERTY in foreign Entity
|
||
*/
|
||
Relation(QString propertyName, RelationType type, QString mappedBy, QString tableName = "",
|
||
CascadeType cascadeType = ALL);
|
||
~Relation();
|
||
RelationType getType() const;
|
||
void setType(const RelationType &value);
|
||
... | ... | |
bool getOptional() const;
|
||
void setOptional(bool value);
|
||
|
||
QString getMappedBy() const;
|
||
void setMappedBy(const QString &value);
|
||
|
||
QString getTableName() const;
|
||
void setTableName(const QString &value);
|
||
|
||
CascadeType getCascadeType() const;
|
||
void setCascadeType(const CascadeType &value);
|
||
|
||
protected:
|
||
QString propertyName;
|
||
QString mappedBy;
|
||
QString tableName;
|
||
RelationType type;
|
||
CascadeType cascadeType;
|
||
bool optional;
|
||
|
||
};
|
src/schema.h | ||
---|---|---|
QSharedPointer<QHash<QString, QString> > getAbstractTypeMap() const;
|
||
void setAbstractTypeMap(const QSharedPointer<QHash<QString, QString> > &value);
|
||
|
||
protected:
|
||
protected:
|
||
virtual QStringList findTableNames(QString schema = "") = 0;
|
||
virtual QHash<QString, QStringList> findUniqueIndexes(const QSharedPointer<TableSchema> &table) = 0;
|
||
virtual void findConstraints(const QSharedPointer<TableSchema> &ts) = 0;
|
Auch abrufbar als: Unified diff
wip