Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 4e75d5a1

Von Christian Ehringfeld vor etwa 9 Jahren hinzugefügt

  • ID 4e75d5a1eccaf269829248be40dbe3f2e0160e01
  • Vorgänger 01fe6db3
  • Nachfolger 46d2de48

many-to-many table name resolve

Unterschiede anzeigen:

src/attribute.cpp
#include "attribute.h"
using namespace CuteEntityManager;
Attribute::Attribute()
{
}
src/attribute.h
#ifndef ATTRIBUTE_H
#define ATTRIBUTE_H
#include <QString>
namespace CuteEntityManager {
class Attribute {
public:
Attribute();
private:
QString name;
QString className;
QString relatedClass;
QString relatedTable;
};
}
#endif // ATTRIBUTE_H
src/entityhelper.cpp
return newInstance;
}
Entity *EntityHelper::getBaseClassObject(const QSharedPointer<Entity> &entity,
QString attributeName) {
auto superObject = EntityInstanceFactory::createInstance(entity->metaObject());
auto objectBefore = superObject;
bool first = true;
while(superObject) {
auto props = EntityHelper::getMetaProperties(superObject);
if(!props.contains(attributeName) ||
superObject->getInheritanceStrategy() == InheritanceStrategy::PER_CLASS_TABLE) {
break;
} else if(!first){
delete objectBefore;
objectBefore = nullptr;
} else {
first = false;
}
objectBefore = superObject;
superObject = EntityInstanceFactory::newSuperClassInstance(superObject);
}
return objectBefore;
}
const char *EntityHelper::getClassname(const Entity *entity) {
return entity->metaObject()->className();
}
src/entityhelper.h
const Entity *entity);
static const QHash<QString, QMetaProperty> getMetaProperties(
const QMetaObject *object);
static const QHash<QString, QMetaProperty> getNonInheritedMetaProperties(const Entity *entity);
static const QHash<QString, QMetaProperty> getNonInheritedMetaProperties(
const Entity *entity);
static const QHash<QString, QMetaProperty> getInheritedMetaProperties(
const Entity *entity);
static const QHash<Relation, QMetaProperty> getRelationProperties(
const Entity *entity);
static Entity* copyObject(const Entity *entity);
static Entity* getBaseClassObject(const QSharedPointer<Entity> &entity, QString attributeName);
static const char *getClassname(const Entity *entity);
static const QString getClassName(const Entity *entity);
......
QSharedPointer<Entity> add, const QMetaProperty &property);
static void removeEntityFromListProperty(const QSharedPointer<Entity> &entity,
QSharedPointer<Entity> remove, const QMetaProperty &property);
static void clearEntityListProperty(const QSharedPointer<Entity> &entity, const QMetaProperty &property);
static void clearEntityListProperty(const QSharedPointer<Entity> &entity,
const QMetaProperty &property);
static void setListProperty(const QSharedPointer<Entity> &entity,
QList<QSharedPointer<Entity>> &list,
const QMetaProperty &property);
......
static QMetaProperty mappedProperty(const Relation &r,
const QSharedPointer<Entity> &foreignEntity);
static QHash<QString, QVariant> getEntityAttributes(const
QHash<QString, QMetaProperty>
&props,
QHash<QString, QMetaProperty> &props,
const QSharedPointer<Entity> &entity);
};
}
src/entitymanager.cpp
this->db->startTransaction();
auto builder = this->schema->getQueryBuilder();
q = builder->manyToManyInsert(tblName,
builder->generateManyToManyColumnName(entity),
builder->generateManyToManyColumnName(ptr));
builder->generateManyToManyColumnName(entity,r.getPropertyName()),
builder->generateManyToManyColumnName(ptr,r.getMappedBy()));
q.bindValue(0, entity->getProperty(entity->getPrimaryKey()));
auto prop = EntityHelper::mappedProperty(r, ptr);
QSharedPointer<Entity> item;
......
QString tblName = builder->generateManyToManyTableName(e, ptr, r);
if (this->schema->getTables().contains(tblName)) {
QSqlQuery q = builder->manyToManyDelete(
tblName, builder->generateManyToManyColumnName(e),
tblName, builder->generateManyToManyColumnName(e,r.getPropertyName()),
e->getProperty(e->getPrimaryKey()).toLongLong());
if (this->db->exec(q)) {
bool refresh = r.getCascadeType().contains(CascadeType::REFRESH)
......
* @todo diff and remove entity from relational object when association is deleted
*/
q = builder->manyToManyDelete(
tblName, builder->generateManyToManyColumnName(entity),
tblName, builder->generateManyToManyColumnName(entity,r.getPropertyName()),
entity->getProperty(entity->getPrimaryKey()).toLongLong());
ok = this->db->exec(q);
} else {
......
relation);
if (this->schema->getTables().contains(tblName)) {
QSqlQuery q = builder->manyToMany(tblName,
builder->generateManyToManyColumnName(entity),
builder->generateManyToManyColumnName(entity,relation.getPropertyName()),
entity->getProperty(entity->getPrimaryKey()).toLongLong());
auto listMap = this->convertQueryResult(q);
auto secClassName = EntityHelper::getClassName(secEntityPtr.data());
QSharedPointer<Entity> e;
for (int var = 0; var < listMap.size(); ++var) {
auto id = listMap.at(var).value(builder->generateManyToManyColumnName(secEntityPtr));
auto id = listMap.at(var).value(builder->generateManyToManyColumnName(secEntityPtr,relation.getMappedBy()));
if (refresh || !(this->cache.contains(id.toLongLong(), secClassName) &&
(e = this->cache.get(id.toLongLong(), secClassName)))) {
e = this->findById(id.toLongLong(), secClassName);
src/querybuilder.cpp
QString QueryBuilder::createForeignKeyManyToMany(const QString &tableName,
const QSharedPointer<Entity> &entity, const QString &update,
const QString &remove) const {
QString fkColumn = this->generateManyToManyColumnName(entity);
QString fkColumn = this->generateManyToManyColumnName(entity, "id");
QString indexName = this->generateIndexName(fkColumn,
tableName, fkColumn,
entity->getTablename(), true);
......
QString QueryBuilder::generateManyToManyTableName(const QSharedPointer<Entity>
&firstEntity,
const QSharedPointer<Entity> &secondEntity, const Relation &r) const {
if (r.getMappedBy().isEmpty()) {
return firstEntity->getTablename() + "_" + r.getPropertyName();
} else {
return secondEntity->getTablename() + "_" + r.getMappedBy();
auto ptr = firstEntity;
QString attributeName = r.getPropertyName();
if (!r.getMappedBy().isEmpty()) {
ptr = secondEntity;
attributeName = r.getMappedBy();
}
auto obj = EntityHelper::getBaseClassObject(ptr, attributeName);
QString tblName = obj->getTablename() + "_" + attributeName;
delete obj;
return tblName;
}
QHash<QString, QHash<QString, QString>> QueryBuilder::generateRelationTables(
......
if (r.getType() == RelationType::MANY_TO_MANY && r.getMappedBy().isEmpty()) {
QHash<QString, QString> h = QHash<QString, QString>();
h.insert(entity->getPrimaryKey(), this->schema->TYPE_BIGPK);
h.insert(this->generateManyToManyColumnName(entity),
h.insert(this->generateManyToManyColumnName(entity, r.getPropertyName()),
this->schema->TYPE_BIGINT);
auto meta = props.value(r.getPropertyName());
QSharedPointer<Entity> ptr = QSharedPointer<Entity>
(EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(
QMetaType::typeName(meta.userType()))));
h.insert(this->generateManyToManyColumnName(ptr),
h.insert(this->generateManyToManyColumnName(ptr, r.getMappedBy()),
this->schema->TYPE_BIGINT);
relations.insert(this->generateManyToManyTableName(entity, ptr, r), h);
}
......
}
QString QueryBuilder::generateManyToManyColumnName(const QSharedPointer<Entity>
&entity) const {
&entity, QString attribute) const {
if (entity) {
return this->generateColumnNameID(entity->getTablename());
auto obj = EntityHelper::getBaseClassObject(entity, attribute);
QString tblName = obj->getTablename();
delete obj;
return this->generateColumnNameID(tblName);
}
this->database->getLogger()->logMsg("Entity is empty!", MsgType::WARNING);
return "";
src/querybuilder.h
const QSharedPointer<Entity> &entity) const;
QString generateManyToManyTableName(const QSharedPointer<Entity> &firstEntity,
const QSharedPointer<Entity> &secondEntity, const Relation &r) const;
QString generateManyToManyColumnName(const QSharedPointer<Entity> &entity)
const;
QString generateManyToManyColumnName(const QSharedPointer<Entity> &entity, QString attribute) const;
QString buildCreateQuery(QHash<QString, QVariant>::const_iterator i,
QHash<QString, QVariant>::const_iterator end,
QString &p1, QString &p2) const;
src/sqlitebackupprocessor.cpp
sqlite3_backup *pBackup; /* Backup object used to copy data */
sqlite3 *pTo; /* Database to copy to (pFile or pInMemory) */
sqlite3 *pFrom; /* Database to copy from (pFile or pInMemory) */
/* Open the database file identified by zFilename. Exit early if this fails
** for any reason. */
rc = sqlite3_open( zFilename, &pFile );
src/src.pro
validators/lengthvalidator.h \
schema/mysqlquerybuilder.h \
entityinspector.h \
sqlitebackupprocessor.h
sqlitebackupprocessor.h \
attribute.h
SOURCES += \
entity.cpp \
......
validators/lengthvalidator.cpp \
schema/mysqlquerybuilder.cpp \
entityinspector.cpp \
sqlitebackupprocessor.cpp
sqlitebackupprocessor.cpp \
attribute.cpp
win32:!system-sqlite:!contains(LIBS, .*sqlite3.*) {
include($$[QT_INSTALL_PREFIX]/../Src/qtbase/src/3rdparty/sqlite.pri)

Auch abrufbar als: Unified diff