Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 706de2e8

Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt

  • ID 706de2e85e52b7141184f90a6901a4af8313f9bc
  • Vorgänger 2d9fab10
  • Nachfolger 97846191

some relation stuff

Unterschiede anzeigen:

src/entity.h
qint64 id;
};
}
Q_DECLARE_METATYPE(QList<QSharedPointer<CuteEntityManager::Entity>>)
#endif // ENTITY_H
src/entityinstancefactory.cpp
if (s.contains(sptr)) {
const int index = s.indexOf(sptr) + sptr.count() +1;
r = s.mid(index,s.indexOf(">",index)-index);
} else {
r = s;
}
return r;
}
src/entitymanager.cpp
qint64 convertedId = -1;
bool ok = false;
if ((convertedId == id.toLongLong(&ok)) && ok && convertedId > -1) {
QSharedPointer<Entity> ptr = this->findById(convertedId, EntityInstanceFactory::extractEntityType(property.typeName()));
QString className = EntityInstanceFactory::extractEntityType(property.typeName());
QSharedPointer<Entity> ptr = QSharedPointer<Entity>();
if (this->cache.contains(convertedId, className) && (ptr = this->cache.get(convertedId, className)) && ptr.data()) {
} else {
ptr = this->findById(convertedId, className);
}
if (ptr.data() && ptr.data()->getId() > -1) {
property.write(entity.data(), QVariant(ptr));
}
......
}
void EntityManager::oneToMany(const QSharedPointer<Entity> &entity, const Relation &r, const QMetaProperty &property) {
if (entity.data() && entity.data()->getId() > -1) {
Entity *e = EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(property.typeName()));
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->oneToMany(e->getTablename(),
r.getMappedBy() + "_id", entity.data()->getId());
auto listMap = this->convertQueryResult(q);
auto entities = this->convert(listMap, e->getClassname());
delete e;
this->setListProperty(entity, entities, property);
}
}
void EntityManager::setListProperty(const QSharedPointer<Entity> &entity, QList<QSharedPointer<Entity> > &list,
const QMetaProperty &property) const {
QVariant var;
var.setValue<QList<QSharedPointer<Entity>>>(list);
property.write(entity.data(), var);
}
void EntityManager::manyToMany(const QSharedPointer<Entity> &entity, const Relation &r, const QMetaProperty &property) {
void EntityManager::manyToMany(const QSharedPointer<Entity> &entity, const Relation &r, const QMetaProperty &property) {
Entity *secEntity = EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(QString(
property.typeName())));
auto builder = this->schema.data()->getQueryBuilder();
if (secEntity) {
QSharedPointer<Entity> secEntityPtr = QSharedPointer<Entity>(secEntity);
QString tblName = "";
if (r.getMappedBy().isEmpty()) {
tblName = builder.data()->generateManyToManyTableName(entity, secEntityPtr);
} else {
tblName = builder.data()->generateManyToManyTableName(secEntityPtr, entity);
}
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->manyToMany(tblName,
builder.data()->generateManyToManyColumnName(entity), entity.data()->getId(),
builder.data()->generateManyToManyColumnName(secEntityPtr),
secEntityPtr.data()->getTablename());
auto listMap = this->convertQueryResult(q);
auto entities = this->convert(listMap, entity.data()->getClassname());
this->setListProperty(entity, entities, property);
}
}
void EntityManager::oneToOne(const QSharedPointer<Entity> &entity, const Relation &r, const QMetaProperty &property,
......
QHash<QString, QVariant> EntityManager::findByPk(qint64 id, QString tblname) {
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->find(id, tblname);
this->db->select(q);
QSqlRecord rec = q.record();
QHash<QString, QVariant> map = QHash<QString, QVariant>();
if (q.next()) {
for (int var = 0; var < rec.count(); ++var) {
QVariant variant = rec.value(var);
if (variant.isValid()) {
map.insert(rec.fieldName(var), variant);
}
}
auto listMap = this->convertQueryResult(q);
if (!listMap.isEmpty()) {
return listMap.at(0);
}
return map;
// this->db->select(q);
// QSqlRecord rec = q.record();
// QHash<QString, QVariant> map = QHash<QString, QVariant>();
// if (q.next()) {
// for (int var = 0; var < rec.count(); ++var) {
// QVariant variant = rec.value(var);
// if (variant.isValid()) {
// map.insert(rec.fieldName(var), variant);
// }
// }
// }
// return map;
return QHash<QString, QVariant>();
}
src/entitymanager.h
QList<QHash<QString, QVariant> > findAllByAttributes(const QHash<QString, QVariant> &m, const QString &tblname,
bool ignoreID = false);
QSharedPointer<Entity> findById(const qint64 &id, Entity *&e);
void setListProperty(const QSharedPointer<Entity> &entity,QList<QSharedPointer<Entity>> &list,const QMetaProperty &property) const;
public:
EntityManager(QSqlDatabase database);
src/querybuilder.cpp
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);
h.insert(this->generateManyToManyColumnName(entity), this->schema.data()->TYPE_BIGINT);
auto m = props.value(r.getPropertyName());
Entity *e = EntityInstanceFactory::createInstance(m.type());
QSharedPointer<Entity> ptr = QSharedPointer<Entity>(e);
h.insert(QString(ptr.data()->metaObject()->className()) + QString("_id"),
h.insert(this->generateManyToManyColumnName(ptr),
this->schema.data()->TYPE_BIGINT);
if (r.getTableName().isEmpty()) {
relations.insert(this->generateManyToManyTableName(entity, ptr), h);
......
return q;
}
QSqlQuery QueryBuilder::oneToMany(const QString &tableName, const QString &attribute, const qint64 &id) {
QHash<QString, QVariant> values = QHash<QString, QVariant>();
values.insert(attribute, id);
return this->findByAttributes(values, tableName, false);
}
QSqlQuery QueryBuilder::manyToMany(const QString &tableName, const QString &attribute, const qint64 &id,
const QString &foreignKey, const QString &foreignTable) {
QSqlQuery q = this->database.data()->getQuery();
QString sql = "SELECT " + this->schema.data()->quoteTableName(foreignTable) + ".* FROM " +
this->schema.data()->quoteTableName(tableName) + this->leftJoin(foreignTable, tableName,
foreignKey) + " WHERE " + this->schema.data()->quoteColumnName(attribute) + "=:id;";
q.prepare(sql);
q.bindValue(":id", id);
//SELECT user.* FROM `parent_child` LEFT JOIN user ON user.id=parent_child.user_id WHERE child_id = 1
return q;
}
QString QueryBuilder::leftJoin(const QString &joinableTable, const QString &tableName, const QString &foreignKey) {
return "LEFT JOIN " + this->schema.data()->quoteTableName(joinableTable) + " ON " +
this->schema.data()->quoteColumnName(joinableTable + ".id") + "=" + this->schema.data()->quoteColumnName(
tableName + "." + foreignKey);
}
QString QueryBuilder::limit(const qint8 limit, const qint64 offset) const {
return " LIMIT " + QString(limit) + (offset > 0 ? QString("," + offset) : "");
}
QString QueryBuilder::generateManyToManyColumnName(const QSharedPointer<Entity> &entity) const {
return QString(entity.data()->metaObject()->className()) + QString("_id");
}
QSqlQuery QueryBuilder::getQuery() const {
return this->database.data()->getQuery();
}
src/querybuilder.h
QString transformAbstractTypeToRealDbType(QString typeName) const;
QString getColumnType(const QString &type) const;
QSqlQuery find(const qint64 &id, const QString &tableName) const;
QSqlQuery findByAttributes(const QHash<QString, QVariant> &m, const QString &tableName, const bool &ignoreID) const;
QSqlQuery findByAttributes(const QHash<QString, QVariant> &m, const QString &tableName, const bool &ignoreID = true) const;
QSqlQuery findByAttributes(const QSharedPointer<Entity> &e,bool ignoreID = true);
QSqlQuery findAll(const QString &tableName) const;
QSqlQuery remove(const QSharedPointer<Entity> &entity) const;
......
QSqlQuery count(const QString &tableName) const;
QSqlQuery merge(const QSharedPointer<Entity> &entity) const;
QSqlQuery create(const QSharedPointer<Entity> &entity) const;
QSqlQuery oneToMany(const QString &tableName, const QString &attribute, const qint64 &id);
QSqlQuery manyToMany(const QString &tableName, const QString &attribute, const qint64 &id, const QString &foreignKey, const QString &foreignTable);
virtual QString limit(const qint8 limit, const qint64 offset) const;
QString generateManyToManyColumnName(const QSharedPointer<Entity> &entity) const;
QSqlQuery getQuery() const;
protected:
......
QString where(const QHash<QString, QVariant> &m, const QString &conjunction = ",", bool ignoreID = false) const;
QString attributes(const QHash<QString, QVariant> &m, const QString &conjunction = ",", bool ignoreID = false) const;
QHash<QString, QVariant> saveAttributes(const QSharedPointer<Entity> &entity) const;
QString leftJoin(const QString &joinableTable, const QString &tableName, const QString &foreignKey);
QSharedPointer<Schema> schema;
QSharedPointer<Database> database;
src/relation.cpp
} else {
this->cascadeType = cascadeType;
}
}
Relation::~Relation() {

Auch abrufbar als: Unified diff