Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision c599658a

Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt

  • ID c599658aa5cc583f60075e2ddb5221d83d552c3d
  • Vorgänger 97846191
  • Nachfolger e0e1ead8

onetoone

Unterschiede anzeigen:

src/entitymanager.cpp
if ((convertedId == id.toLongLong(&ok)) && ok && convertedId > -1) {
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 {
if (!(this->cache.contains(convertedId, className) && (ptr = this->cache.get(convertedId, className)) && ptr.data())) {
ptr = this->findById(convertedId, className);
}
if (ptr.data() && ptr.data()->getId() > -1) {
property.write(entity.data(), QVariant(ptr));
}
this->setProperty(entity, ptr, property);
}
}
......
}
}
void EntityManager::oneToOne(const QSharedPointer<Entity> &entity, const Relation &r, const QMetaProperty &property,
const QVariant &id) {
qint64 convertedId = -1;
bool ok = false;
if (r.getMappedBy().isEmpty()) {
this->manyToOne(entity, id, property);
} else {
Entity *e = EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(property.typeName()));
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->oneToMany(e->getTablename(),
r.getMappedBy() + "_id", entity.data()->getId(), 1);
auto listMap = this->convertQueryResult(q);
auto entities = this->convert(listMap, e->getClassname());
if (!entities.isEmpty()) {
QSharedPointer<Entity> ptr = entities.at(0);
this->setProperty(entity, ptr, property);
}
delete e;
}
}
void EntityManager::setListProperty(const QSharedPointer<Entity> &entity, QList<QSharedPointer<Entity> > &list,
const QMetaProperty &property) const {
QVariant var;
......
property.write(entity.data(), var);
}
void EntityManager::setProperty(const QSharedPointer<Entity> &entiy, QSharedPointer<Entity> value,
const QMetaProperty &property) const {
if (value.data() && value.data()->getId() > -1) {
property.write(entiy.data(), QVariant(value));
}
}
void EntityManager::manyToMany(const QSharedPointer<Entity> &entity, const Relation &r, const QMetaProperty &property) {
Entity *secEntity = EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(QString(
......
/**
* maybe it would be better, to fetch first the ids, look up cache and then request missing entities
*/
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);
if (this->schema.data()->getTables().contains(tblName)) {
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);
} else {
qDebug() << "MANY_TO_MANY Table " << tblName << " not exists";
}
}
}
void EntityManager::oneToOne(const QSharedPointer<Entity> &entity, const Relation &r, const QMetaProperty &property,
const QVariant &id) {
}
QList<QSharedPointer<Entity> > EntityManager::findEntityByAttributes(const QSharedPointer<Entity> &entity,
bool ignoreID) {
auto maps = this->findAllByAttributes(entity, ignoreID);
......
if (!listMap.isEmpty()) {
return listMap.at(0);
}
// 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
QSharedPointer<Entity> findById(const qint64 &id, Entity *&e);
void setListProperty(const QSharedPointer<Entity> &entity, QList<QSharedPointer<Entity>> &list,
const QMetaProperty &property) const;
void setProperty(const QSharedPointer<Entity> &entiy, QSharedPointer<Entity> value, const QMetaProperty &property) const;
public:
EntityManager(QSqlDatabase database);
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::MANY_TO_ONE) {
if (r.getType() == RelationType::MANY_TO_ONE || (r.getType() == RelationType::ONE_TO_ONE
&& r.getMappedBy().isEmpty())) {
map.insert(QString(m.name()) + "_id", this->schema.data()->TYPE_BIGINT);
}
} else if (entity.data()->getBLOBColumns().contains(m.name())) {
......
* @return
*/
QSqlQuery QueryBuilder::findByAttributes(const QHash<QString, QVariant> &m, const QString &tableName,
const bool &ignoreID) const {
const bool &ignoreID, const qint64 limit, const qint64 offset) const {
QSqlQuery q = this->database.data()->getQuery("SELECT * FROM " + this->schema.data()->quoteTableName(
tableName) + this->where(m, "AND", ignoreID));
tableName) + this->where(m, "AND", ignoreID) + this->limit(limit, offset));
this->bindValues(m, q, ignoreID);
return q;
}
QSqlQuery QueryBuilder::findByAttributes(const QSharedPointer<Entity> &e, bool ignoreID) {
QSqlQuery QueryBuilder::findByAttributes(const QSharedPointer<Entity> &e, bool ignoreID, const qint64 limit,
const qint64 offset) {
QHash<QString, QVariant> values = this->getEntityAttributes(e.data()->getMetaProperties(), e);
return this->findByAttributes(values, e.data()->getTablename(), ignoreID);
return this->findByAttributes(values, e.data()->getTablename(), ignoreID, limit, offset);
}
QSqlQuery QueryBuilder::findAll(const QString &tableName) const {
......
return q;
}
QSqlQuery QueryBuilder::oneToMany(const QString &tableName, const QString &attribute, const qint64 &id) {
QSqlQuery QueryBuilder::oneToMany(const QString &tableName, const QString &attribute, const qint64 &id,
const qint64 &limit) {
QHash<QString, QVariant> values = QHash<QString, QVariant>();
values.insert(attribute, id);
return this->findByAttributes(values, tableName, false);
return this->findByAttributes(values, tableName, false, limit);
}
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,
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(
QString QueryBuilder::leftJoin(const QString &foreignTable, const QString &tableName, const QString &foreignKey) {
return "LEFT JOIN " + this->schema.data()->quoteTableName(foreignTable) + " ON " +
this->schema.data()->quoteColumnName(foreignTable + ".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::limit(const qint64 &limit, const qint64 &offset) const {
QString s = "";
if (limit > 0) {
s.append(" LIMIT ").append(QString::number(limit));
}
if (offset > 0) {
s.append(" OFFSET ").append(QString::number(offset));
}
return s;
}
QString QueryBuilder::generateManyToManyColumnName(const QSharedPointer<Entity> &entity) const {
src/querybuilder.h
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 = true) const;
QSqlQuery findByAttributes(const QSharedPointer<Entity> &e, bool ignoreID = true);
const bool &ignoreID = true, const qint64 limit = 0, const qint64 offset = 0) const;
QSqlQuery findByAttributes(const QSharedPointer<Entity> &e, bool ignoreID = true,const qint64 limit = 0, const qint64 offset = 0);
QSqlQuery findAll(const QString &tableName) const;
QSqlQuery remove(const QSharedPointer<Entity> &entity) const;
QSqlQuery findId(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 oneToMany(const QString &tableName, const QString &attribute, const qint64 &id, const qint64 &limit = 0);
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;
virtual QString limit(const qint64 &limit, const qint64 &offset) const;
QString generateManyToManyColumnName(const QSharedPointer<Entity> &entity) const;
QSqlQuery getQuery() const;
......
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);
QString leftJoin(const QString &foreignTable, const QString &tableName, const QString &foreignKey);
QSharedPointer<Schema> schema;
QSharedPointer<Database> database;

Auch abrufbar als: Unified diff