Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision e8d1537c

Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt

  • ID e8d1537ce72e8ebfd612e1631b54ad9f4b704cd3
  • Vorgänger f9cef58f
  • Nachfolger 9d62f4aa

entityhelper class

Unterschiede anzeigen:

EntityManager.pro
src/relation.h \
src/entityinstancefactory.h \
src/condition.h \
src/cache.h
src/cache.h \
src/entityhelper.h
SOURCES += \
src/entity.cpp \
......
src/relation.cpp \
src/entityinstancefactory.cpp \
src/condition.cpp \
src/cache.cpp
src/cache.cpp \
src/entityhelper.cpp
unix {
target.path = /usr/lib
src/cache.cpp
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "cache.h"
#include "entityhelper.h"
using namespace CuteEntityManager;
Cache::Cache() {
......
void Cache::insert(QSharedPointer<Entity> &entity) {
if (entity && entity->getId() > -1) {
QString key = this->generateKey(entity->getId(),
QString(entity->getClassname()));
EntityHelper::getClassName(entity.data()));
if (this->cache.contains(key)) {
QSharedPointer<Entity> ptr = this->cache.value(key).toStrongRef();
if (ptr) {
......
void Cache::remove(const QSharedPointer<Entity> &entity) {
if (entity.data() && entity->getId() > -1) {
this->remove(entity->getId(), QString(entity->getClassname()));
this->remove(entity->getId(), EntityHelper::getClassName(entity.data()));
}
}
src/cache.h
#include <QSharedPointer>
#include "entityinstancefactory.h"
#include "entity.h"
#include "entityhelper.h"
namespace CuteEntityManager {
class Entity;
......
bool ok = false;
Entity *e = EntityInstanceFactory::createInstance<T>();
if (e) {
ok = this->contains(id, QString(e->getClassname()));
ok = this->contains(id, QString(EntityHelper::getClassname(e)));
delete e;
}
return ok;
......
template<class T> void remove(qint64 id) {
Entity *e = EntityInstanceFactory::createInstance<T>();
if (e) {
this->remove(id, QString(e->getClassname()));
this->remove(id, QString(EntityHelper::getClassname(e)));
delete e;
}
}
......
template<class T> QSharedPointer<Entity> get(qint64 id) {
Entity *e = EntityInstanceFactory::createInstance<T>();
if (e) {
return this->get(id, QString(e->getClassname()));
return this->get(id, QString(EntityHelper::getClassname(e)));
delete e;
}
return QSharedPointer<Entity>();
src/entity.cpp
*/
#include "entity.h"
#include "entityhelper.h"
using namespace CuteEntityManager;
Entity::Entity(QObject *parent) : QObject(parent) {
......
QString Entity::toString() const {
QString r = "";
r.append(this->getClassname());
r.append(EntityHelper::getClassName(this));
r.append(": {");
auto properties = this->getMetaProperties();
auto properties = EntityHelper::getMetaProperties(this);
for (auto var = properties.constBegin(); var != properties.constEnd(); ++var) {
QString val = "";
auto value = var.value().read(this);
......
QString Entity::slimToString() const {
QString r = "";
r.append(this->getClassname());
r.append(EntityHelper::getClassName(this));
r.append(": {");
r.append("id: ") + this->getId() + "}";
return r;
......
return QHash<QString, Relation>();
}
const QHash<QString, Relation> Entity::getNonInheritedRelations() const {
auto relations = this->getRelations();
auto superObject = EntityInstanceFactory::newSuperClassInstance(this);
if (superObject) {
auto superRelations = superObject->getRelations();
auto iterator = superRelations.constBegin();
while (iterator != relations.constEnd()) {
if (relations.contains(iterator.key())) {
relations.remove(iterator.key());
}
++iterator;
}
delete superObject;
superObject = nullptr;
}
return relations;
}
const QStringList Entity::getTransientAttributes() const {
return QStringList();
}
......
return "id";
}
const QList<const QMetaObject *> Entity::superClasses(bool
stopAtSingleTableInheritance) const {
QList<const QMetaObject *> classes = QList<const QMetaObject *>();
auto superMetaObject = this->metaObject()->superClass();
if (this->getInheritanceStrategy() == InheritanceStrategy::JOINED_TABLE) {
Entity *e = nullptr;
while (superMetaObject && QString(superMetaObject->className()) !=
QString("CuteEntityManager::Entity")) {
e = EntityInstanceFactory::createInstance(superMetaObject->className());
if (e) {
classes.append(superMetaObject);
superMetaObject = superMetaObject->superClass();
InheritanceStrategy s = e->getInheritanceStrategy();
delete e;
e = nullptr;
if (stopAtSingleTableInheritance && s == InheritanceStrategy::PER_CLASS_TABLE) {
break;
}
} else {
break;
}
}
}
return classes;
}
const QHash<QString, QMetaProperty> Entity::getMetaProperties() const {
return Entity::getMetaProperties(this->metaObject());
}
const QHash<QString, QMetaProperty> Entity::getSuperMetaProperties() const {
auto superMetaObjectPropertyMap = QHash<QString, QMetaProperty>();
auto superMeta = this->metaObject()->superClass();
if (QString(superMeta->className()) != QString("CuteEntityManager::Entity")
&& this->getInheritanceStrategy() == InheritanceStrategy::JOINED_TABLE) {
for (int var = 0; var < superMeta->propertyCount(); ++var) {
QMetaProperty prop = superMeta->property(var);
if (prop.isReadable() && prop.isWritable()) {
superMetaObjectPropertyMap.insert(QString(prop.name()), prop);
}
}
}
return superMetaObjectPropertyMap;
}
const QHash<QString, QMetaProperty> Entity::getMetaProperties(
const QMetaObject *object) {
auto h = QHash<QString, QMetaProperty>();
for (int var = 0; var < object->propertyCount(); ++var) {
QMetaProperty m = object->property(var);
if (m.isValid() && m.name() != QString("objectName")) {
h.insert(m.name(), m);
}
}
return h;
}
const QHash<QString, QMetaProperty> Entity::getInheritedMetaProperties() const {
auto classes = this->superClasses();
auto wholeProperties = QHash<QString, QMetaProperty>();
for (int var = classes.size() - 1; var >= 0; --var) {
auto metaObject = classes.at(var);
auto properties = Entity::getMetaProperties(metaObject);
auto iterator = properties.constBegin();
while (iterator != properties.constEnd()) {
wholeProperties.insert(iterator.key(), iterator.value());
++iterator;
}
}
return wholeProperties;
}
const QHash<Relation, QMetaProperty> Entity::getRelationProperties() const {
auto h = QHash<Relation, QMetaProperty>();
auto relations = this->getRelations();
for (int var = 0; var < this->metaObject()->propertyCount(); ++var) {
QMetaProperty m = this->metaObject()->property(var);
if (m.isValid() && relations.contains(QString(m.name()))) {
h.insert(relations.value(m.name()), m);
}
}
return h;
}
const char *Entity::getClassname() const {
return this->metaObject()->className();
}
QVariant Entity::getProperty(const QString &name) const {
if (!name.isEmpty()) {
return QObject::property(name.toLatin1().constData());
src/entity.h
virtual bool isInheritanceCascaded() const;
//return value must be the exact name defined in Q_PROPERTY
virtual QString getPrimaryKey() const;
const QHash<QString, Relation> getNonInheritedRelations() const;
const QList<const QMetaObject *> superClasses(bool stopAtSingleTableInheritance
= false) const;
const QHash<QString, QMetaProperty> getMetaProperties() const;
const QHash<QString, QMetaProperty> getSuperMetaProperties() const;
static const QHash<QString, QMetaProperty> getMetaProperties(
const QMetaObject *object);
const QHash<QString, QMetaProperty> getInheritedMetaProperties() const;
const QHash<Relation, QMetaProperty> getRelationProperties() const;
const char *getClassname() const;
QVariant getProperty(const QString &name) const;
bool setProperty(const QString &name, const QVariant &value);
qint64 getId() const;
void setId(const qint64 &value);
src/entityhelper.cpp
#include "entityhelper.h"
#include "entity.h"
#include "relation.h"
#include "cache.h"
using namespace CuteEntityManager;
EntityHelper::EntityHelper()
{
}
const QHash<QString, Relation> EntityHelper::getNonInheritedRelations(const Entity *entity) {
auto relations = entity->getRelations();
auto superObject = EntityInstanceFactory::newSuperClassInstance(entity);
if (superObject) {
auto superRelations = superObject->getRelations();
auto iterator = superRelations.constBegin();
while (iterator != relations.constEnd()) {
if (relations.contains(iterator.key())) {
relations.remove(iterator.key());
}
++iterator;
}
delete superObject;
superObject = nullptr;
}
return relations;
}
const QList<const QMetaObject *> EntityHelper::superClasses(const Entity *entity,bool
stopAtSingleTableInheritance) {
QList<const QMetaObject *> classes = QList<const QMetaObject *>();
auto superMetaObject = entity->metaObject()->superClass();
if (entity->getInheritanceStrategy() == InheritanceStrategy::JOINED_TABLE) {
Entity *e = nullptr;
while (superMetaObject && QString(superMetaObject->className()) !=
QString("CuteEntityManager::Entity")) {
e = EntityInstanceFactory::createInstance(superMetaObject->className());
if (e) {
classes.append(superMetaObject);
superMetaObject = superMetaObject->superClass();
InheritanceStrategy s = e->getInheritanceStrategy();
delete e;
e = nullptr;
if (stopAtSingleTableInheritance && s == InheritanceStrategy::PER_CLASS_TABLE) {
break;
}
} else {
break;
}
}
}
return classes;
}
const QHash<QString, QMetaProperty> EntityHelper::getMetaProperties(const Entity *entity) {
return EntityHelper::getMetaProperties(entity->metaObject());
}
const QHash<QString, QMetaProperty> EntityHelper::getSuperMetaProperties(const Entity *entity) {
auto superMetaObjectPropertyMap = QHash<QString, QMetaProperty>();
auto superMeta = entity->metaObject()->superClass();
if (QString(superMeta->className()) != QString("CuteEntityManager::Entity")
&& entity->getInheritanceStrategy() == InheritanceStrategy::JOINED_TABLE) {
for (int var = 0; var < superMeta->propertyCount(); ++var) {
QMetaProperty prop = superMeta->property(var);
if (prop.isReadable() && prop.isWritable()) {
superMetaObjectPropertyMap.insert(QString(prop.name()), prop);
}
}
}
return superMetaObjectPropertyMap;
}
const QHash<QString, QMetaProperty> EntityHelper::getMetaProperties(
const QMetaObject *object) {
auto h = QHash<QString, QMetaProperty>();
for (int var = 0; var < object->propertyCount(); ++var) {
QMetaProperty m = object->property(var);
if (m.isValid() && m.name() != QString("objectName")) {
h.insert(m.name(), m);
}
}
return h;
}
const QHash<QString, QMetaProperty> EntityHelper::getInheritedMetaProperties(const Entity *entity) {
auto classes = EntityHelper::superClasses(entity);
auto wholeProperties = QHash<QString, QMetaProperty>();
for (int var = classes.size() - 1; var >= 0; --var) {
auto metaObject = classes.at(var);
auto properties = EntityHelper::getMetaProperties(metaObject);
auto iterator = properties.constBegin();
while (iterator != properties.constEnd()) {
wholeProperties.insert(iterator.key(), iterator.value());
++iterator;
}
}
return wholeProperties;
}
const QHash<Relation, QMetaProperty> EntityHelper::getRelationProperties(const Entity *entity) {
auto h = QHash<Relation, QMetaProperty>();
auto relations = entity->getRelations();
for (int var = 0; var < entity->metaObject()->propertyCount(); ++var) {
QMetaProperty m = entity->metaObject()->property(var);
if (m.isValid() && relations.contains(QString(m.name()))) {
h.insert(relations.value(m.name()), m);
}
}
return h;
}
const char *EntityHelper::getClassname(const Entity *entity) {
return entity->metaObject()->className();
}
const QString EntityHelper::getClassName(const Entity *entity)
{
return QString(entity->metaObject()->className());
}
void EntityHelper::setListProperty(const QSharedPointer<Entity> &entity,
QList<QSharedPointer<Entity> > &list,
const QMetaProperty &property) {
QVariant var;
var.setValue<QList<QSharedPointer<Entity>>>(list);
property.write(entity.data(),var);
}
void EntityHelper::addEntityToListProperty(const QSharedPointer<Entity>
&entity, QSharedPointer<Entity> add, const QMetaProperty &property) {
QVariant var = property.read(entity.data());
if (!var.isNull() && var.canConvert<QList<QVariant>>()) {
auto list = EntityInstanceFactory::castQVariantList(var);
if (!list.contains(add)) {
list.append(add);
EntityHelper::setListProperty(entity, list, property);
}
}
}
void EntityHelper::setProperty(const QSharedPointer<Entity> &entity,
QSharedPointer<Entity> value,
const QMetaProperty &property) {
if (value && value->getProperty(value->getPrimaryKey()).toLongLong()
> -1) {
QVariant var;
var.setValue<QSharedPointer<Entity>>(value);
property.write(entity.data(), var);
}
}
void EntityHelper::setProperty(const QSharedPointer<Entity> &entity, QSharedPointer<Entity> value, const QString property) {
auto props = EntityHelper::getMetaProperties(entity.data());
if(props.contains(property)) {
QVariant var;
var.setValue<QSharedPointer<Entity>>(value);
entity->setProperty(property,var);
}
}
QMetaProperty EntityHelper::mappedProperty(const Relation &r,
const QSharedPointer<Entity> &foreignEntity) {
QMetaProperty prop;
auto props = EntityHelper::getMetaProperties(foreignEntity.data());
if (!r.getMappedBy().isEmpty() && props.contains(r.getMappedBy())) {
prop = props.value(r.getMappedBy());
} else {
auto relations = foreignEntity->getRelations();
for (auto iterator = relations.constBegin(); iterator != relations.constEnd();
++iterator) {
auto rel = iterator.value();
if (rel.getMappedBy() == r.getPropertyName()) {
prop = props.value(rel.getPropertyName());
break;
}
}
}
return prop;
}
src/entityhelper.h
#ifndef ENTITYHELPER_H
#define ENTITYHELPER_H
#include <QString>
#include <QHash>
#include <QMetaProperty>
namespace CuteEntityManager {
class Relation;
class Entity;
class Cache;
class EntityHelper
{
public:
EntityHelper();
static const QHash<QString, Relation> getNonInheritedRelations(const Entity *entity);
static const QList<const QMetaObject *> superClasses(const Entity *entity,bool stopAtSingleTableInheritance
= false);
static const QHash<QString, QMetaProperty> getMetaProperties(const Entity *entity);
static const QHash<QString, QMetaProperty> getSuperMetaProperties(const Entity *entity);
static const QHash<QString, QMetaProperty> getMetaProperties(const QMetaObject *object);
static const QHash<QString, QMetaProperty> getInheritedMetaProperties(const Entity *entity);
static const QHash<Relation, QMetaProperty> getRelationProperties(const Entity *entity);
static const char *getClassname(const Entity *entity);
static const QString getClassName(const Entity *entity);
static void addEntityToListProperty(const QSharedPointer<Entity> &entity,
QSharedPointer<Entity> add, const QMetaProperty &property);
static void setListProperty(const QSharedPointer<Entity> &entity,
QList<QSharedPointer<Entity>> &list,
const QMetaProperty &property);
static void setProperty(const QSharedPointer<Entity> &entity,
QSharedPointer<Entity> value,
const QMetaProperty &property);
static void setProperty(const QSharedPointer<Entity> &entity,
QSharedPointer<Entity> value,
const QString property);
static QMetaProperty mappedProperty(const Relation &r,
const QSharedPointer<Entity> &foreignEntity) ;
};
}
#endif // ENTITYHELPER_H
src/entityinstancefactory.cpp
*/
#include "entityinstancefactory.h"
#include "entity.h"
#include "entityhelper.h"
#include <QMetaType>
using namespace CuteEntityManager;
EntityInstanceFactory::EntityInstanceFactory() {
......
prop.write(e, iterator.value());
}
} else {
qDebug() << prop.name() << "on Entity" << e->getClassname() << "not writeable!";
qDebug() << prop.name() << "on Entity" << EntityHelper::getClassname(e) << "not writeable!";
}
}
++iterator;
......
Entity *EntityInstanceFactory::setAttributes(Entity *&e,
const QHash<QString, QVariant> &attributes) {
if (!attributes.isEmpty()) {
auto metaprops = e->getMetaProperties();
auto metaprops = EntityHelper::getMetaProperties(e);
return EntityInstanceFactory::setAttributes(e, attributes, metaprops);
} else {
return e;
src/entitymanager.cpp
void EntityManager::refresh(QSharedPointer<Entity> &entity) {
entity = this->findById(entity->getProperty(
entity->getPrimaryKey()).toLongLong(),
QString(entity->getClassname()));
EntityHelper::getClassName(entity.data()));
}
void EntityManager::setSchema(const QSharedPointer<Schema> &value) {
......
const bool refresh) {
QSharedPointer<Entity> r;
if (!e.isNull() && (refresh
|| !(r = this->cache.get(id, QString(e->getClassname()))))) {
|| !(r = this->cache.get(id, EntityHelper::getClassname(e.data()))))) {
e->setId(id);
auto map = this->findByPk(id, e);
r = this->convert(map, e->getClassname(), refresh);
r = this->convert(map, EntityHelper::getClassname(e.data()), refresh);
}
return r;
}
......
return this->findById(id, e);
}
QSharedPointer<Entity> EntityManager::convert(const QHash<QString, QVariant>
&map,
const char *classname, const bool refresh) {
auto ptr = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(
classname, map));
this->cache.insert(ptr);
this->resolveRelations(ptr, map, refresh);
return ptr;
}
QList<QSharedPointer<Entity> > EntityManager::convert(
QList<QHash<QString, QVariant> > maps,
const char *classname, const bool refresh) {
auto list = QList<QSharedPointer<Entity> >();
for (int var = 0; var < maps.size(); ++var) {
auto ptr = this->convert(maps.at(var), classname, refresh);
list.append(ptr);
this->cache.insert(ptr);
}
return list;
}
void EntityManager::manyToOne(const QSharedPointer<Entity> &entity,
const QVariant &id,
const QMetaProperty &property, const bool refresh) {
......
&& (ptr = this->cache.get(convertedId, className)) && ptr)) {
ptr = this->findById(convertedId, className);
}
this->setProperty(entity, ptr, property);
EntityHelper::setProperty(entity, ptr, property);
}
}
......
this->schema->getQueryBuilder()->generateColumnNameID(r.getMappedBy()),
entity->getId());
auto listMap = this->convertQueryResult(q);
auto entities = this->convert(listMap, e->getClassname(), refresh);
this->setListProperty(entity, entities, property);
auto entities = this->convert(listMap, EntityHelper::getClassname(e.data()), refresh);
EntityHelper::setListProperty(entity, entities, property);
}
}
}
......
r.getMappedBy()),
entity->getProperty(entity->getPrimaryKey()).toLongLong(), 1);
auto listMap = this->convertQueryResult(q);
auto entities = this->convert(listMap, e->getClassname(), refresh);
auto entities = this->convert(listMap, EntityHelper::getClassname(e.data()), refresh);
if (!entities.isEmpty()) {
QSharedPointer<Entity> ptr = entities.at(0);
this->setProperty(entity, ptr, property);
EntityHelper::setProperty(entity, ptr, property);
}
}
}
......
return relation.getType() == r && var.canConvert<QVariantList>();
}
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::addEntityToListProperty(const QSharedPointer<Entity>
&entity, QSharedPointer<Entity> add, const QMetaProperty &property) {
QVariant var = property.read(entity.data());
if (!var.isNull() && var.canConvert<QList<QVariant>>()) {
auto list = EntityInstanceFactory::castQVariantList(var);
if (!list.contains(add)) {
list.append(add);
this->setListProperty(entity, list, property);
}
}
}
void EntityManager::setProperty(const QSharedPointer<Entity> &entity,
QSharedPointer<Entity> value,
const QMetaProperty &property) const {
if (value && value->getProperty(value->getPrimaryKey()).toLongLong()
> -1) {
QVariant var;
var.setValue<QSharedPointer<Entity>>(value);
property.write(entity.data(), var);
}
}
void EntityManager::savePrePersistedRelations(const QSharedPointer<Entity>
&entity) {
auto relations = entity->getRelationProperties();
auto relations = EntityHelper::getRelationProperties(entity.data());
auto iterator = relations.constBegin();
while (iterator != relations.constEnd()) {
const Relation r = iterator.key();
......
auto e = EntityInstanceFactory::castQVariant(var);
if (this->shouldBeSaved(e, r)) {
this->save(e);
if(!r.getMappedBy().isEmpty()) {
auto props = EntityHelper::getMetaProperties(e.data());
if(props.contains(r.getMappedBy())) {
EntityHelper::addEntityToListProperty(e,entity,props.value(r.getMappedBy()));
}
}
}
} else if (r.getType() == RelationType::ONE_TO_ONE
&& r.getMappedBy().isEmpty()) {
auto entity = EntityInstanceFactory::castQVariant(var);
this->save(entity);
} else if (r.getType() == RelationType::ONE_TO_ONE) {
auto e = EntityInstanceFactory::castQVariant(var);
this->save(e);
/**
@todo find relation of the other entity
*/
// if(!r.getMappedBy().isEmpty()) {
// e->setProperty(r.getMappedBy(),entity);
// }
}
}
++iterator;
......
void EntityManager::savePostPersistedRelations(const QSharedPointer<Entity>
&entity) {
auto relations = entity->getRelationProperties();
auto relations = EntityHelper::getRelationProperties(entity.data());
auto iterator = relations.constBegin();
while (iterator != relations.constEnd()) {
const Relation r = iterator.key();
......
} else if (this->canPersistRelation(r, RelationType::ONE_TO_MANY, var)) {
QList<QSharedPointer<Entity>> list = EntityInstanceFactory::castQVariantList(
var);
if(!list.isEmpty()) {
//auto foreignEntityProps = list.at(0)->getMetaProperties();
//QString propertyName = r.getMappedBy() ||
for (int var = 0; var < list.size(); ++var) {
auto entity = list.at(var);
if (this->shouldBeSaved(entity, r)) {
this->save(entity);
auto e = list.at(var);
if (this->shouldBeSaved(e, r)) {
this->save(e);
}
//this->addEntityToListProperty();
}
}
} else if (r.getType() == RelationType::ONE_TO_ONE
&& !r.getMappedBy().isEmpty()) {
auto entity = EntityInstanceFactory::castQVariant(var);
this->save(entity);
auto e = EntityInstanceFactory::castQVariant(var);
this->save(e);
}
}
++iterator;
......
builder->generateManyToManyColumnName(entity),
builder->generateManyToManyColumnName(ptr));
q.bindValue(0, entity->getProperty(entity->getPrimaryKey()));
auto prop = this->mappedProperty(r, ptr);
auto prop = EntityHelper::mappedProperty(r, ptr);
QSharedPointer<Entity> item;
for (int var = 0; var < saved.size(); ++var) {
item = list.at(var);
......
q.bindValue(1, item->getProperty(ptr->getPrimaryKey()));
this->schema->getDatabase()->exec(q);
if (prop.isReadable()) {
this->addEntityToListProperty(ptr, entity, prop);
EntityHelper::addEntityToListProperty(ptr, entity, prop);
} else {
qDebug() << "Query exec for many to many relation failed." <<
q.lastError().text();
qDebug() << "Involved entities: " << entity->getClassname() <<
"(MainEntitiy) and " << ptr->getClassname();
qDebug() << "Involved entities: " << EntityHelper::getClassName(entity.data()) <<
"(MainEntitiy) and " << EntityHelper::getClassName(ptr.data());
qDebug() << "Relation:" << r.getPropertyName();
}
}
......
}
}
QMetaProperty EntityManager::mappedProperty(const Relation &r,
const QSharedPointer<Entity> &foreignEntity) const {
QMetaProperty prop;
auto props = foreignEntity->getMetaProperties();
if (!r.getMappedBy().isEmpty() && props.contains(r.getMappedBy())) {
prop = props.value(r.getMappedBy());
} else {
auto relations = foreignEntity->getRelations();
for (auto iterator = relations.constBegin(); iterator != relations.constEnd();
++iterator) {
auto rel = iterator.value();
if (rel.getMappedBy() == r.getPropertyName()) {
prop = props.value(rel.getPropertyName());
break;
}
}
}
return prop;
}
bool EntityManager::shouldBeSaved(QSharedPointer<Entity> &entity,
const Relation &r) {
return entity && (r.getCascadeType().contains(CascadeType::ALL)
......
}
void EntityManager::removeRelations(const QSharedPointer<Entity> &entity) {
auto props = entity->getRelationProperties();
auto props = EntityHelper::getRelationProperties(entity.data());
auto iterator = props.constBegin();
while (iterator != props.constEnd()) {
const Relation r = iterator.key();
......
&& var.canConvert<QVariantList>()) {
auto list = EntityInstanceFactory::castQVariantList(var);
if (!list.isEmpty()) {
auto metas = list.at(0)->getMetaProperties();
auto metas = EntityHelper::getMetaProperties(list.at(0).data());
if (metas.contains(r.getMappedBy())) {
for (int var = 0; var < list.size(); ++var) {
auto entity = list.at(var);
this->setProperty(entity, QSharedPointer<Entity>(),
EntityHelper::setProperty(entity, QSharedPointer<Entity>(),
metas.value(r.getMappedBy()));
this->save(entity);
}
......
this->removeEntity(var);
} else if (!r.getMappedBy().isEmpty() && !var.isNull()) {
auto e = EntityInstanceFactory::castQVariant(var);
auto metas = e->getMetaProperties();
auto metas = EntityHelper::getMetaProperties(e.data());
if (metas.contains(r.getMappedBy())) {
this->setProperty(e, QSharedPointer<Entity>(), metas.value(r.getMappedBy()));
EntityHelper::setProperty(e, QSharedPointer<Entity>(), metas.value(r.getMappedBy()));
this->save(e);
}
}
}
void EntityManager::removeEntity(QVariant &var) {
if (!var.isNull()) {
auto e = EntityInstanceFactory::castQVariant(var);
......
auto id = listMap.at(var).value(builder->generateManyToManyColumnName(
secEntityPtr));
if (!refresh
&& this->cache.contains(id.toLongLong(), secEntityPtr->getClassname())) {
auto entity2 = this->cache.get(id.toLongLong(), secEntityPtr->getClassname());
&& this->cache.contains(id.toLongLong(), EntityHelper::getClassname(secEntityPtr.data()))) {
auto entity2 = this->cache.get(id.toLongLong(), EntityHelper::getClassname(secEntityPtr.data()));
entities.append(entity2);
//this->addEntityToListProperty(entity,entity2,property);
} else {
auto entity2 = this->findById(id.toLongLong(), secEntityPtr->getClassname());
auto entity2 = this->findById(id.toLongLong(), EntityHelper::getClassname(secEntityPtr.data()));
//this->addEntityToListProperty(entity,entity2,property);
entities.append(entity2);
}
}
if(!entities.isEmpty()) {
this->setListProperty(entity, entities, property);
EntityHelper::setListProperty(entity, entities, property);
}
} else {
qDebug() << "MANY_TO_MANY Table " << tblName << " not exists";
......
&entity,
bool ignoreID) {
auto maps = this->findAllByAttributes(entity, ignoreID);
return this->convert(maps, entity->getClassname());
return this->convert(maps, EntityHelper::getClassname(entity.data()));
}
/**
......
rc = this->db->exec(query);
if (!rc) {
qDebug() << "Query failed:" << query.lastError().text() << " of class " <<
entity->getClassname();
EntityHelper::getClassname(entity.data());
break;
}
if (first) {
......
void EntityManager::resolveRelations(const QSharedPointer<Entity> &entity,
const QHash<QString, QVariant> &map, const bool refresh) {
auto props = entity->getRelationProperties();
auto props = EntityHelper::getRelationProperties(entity.data());
auto iterator = props.constBegin();
while (iterator != props.constEnd()) {
const Relation r = iterator.key();
......
void EntityManager::setConnectionNames(QStringList list) {
EntityManager::connectionNames = list;
}
QSharedPointer<Entity> EntityManager::convert(const QHash<QString, QVariant>
&map,
const char *classname,const bool refresh) {
auto ptr = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(
classname, map));
this->cache.insert(ptr);
this->resolveRelations(ptr, map, refresh);
return ptr;
}
QList<QSharedPointer<Entity> > EntityManager::convert(
QList<QHash<QString, QVariant> > maps,
const char *classname,const bool refresh) {
auto list = QList<QSharedPointer<Entity> >();
for (int var = 0; var < maps.size(); ++var) {
auto ptr = this->convert(maps.at(var), classname, refresh);
list.append(ptr);
this->cache.insert(ptr);
}
return list;
}
src/entitymanager.h
(EntityInstanceFactory::createInstance<T>());
if (ptr) {
auto maps = this->findAll(ptr);
return this->convert(maps, ptr->getClassname());
return this->convert(maps, EntityHelper::getClassname(ptr.data()));
}
return QList<QSharedPointer<Entity>>();
}
......
(EntityInstanceFactory::createInstance<T>());
if (e) {
auto results = this->findAllByAttributes(attributes, e->getTablename());
auto list = this->convert(results, e->getClassname());
auto list = this->convert(results, EntityHelper::getClassname(e.data()));
return list;
}
return QList<QSharedPointer<Entity>>();
......
if (e) {
QSqlQuery q = this->db.data()->select(sql);
auto result = this->convertQueryResult(q);
auto ret = this->convert(result, e->getClassname());
auto ret = this->convert(result, EntityHelper::getClassname(e));
delete e;
return ret;
}
......
void resolveRelations(const QSharedPointer<Entity> &entity,
const QHash<QString, QVariant> &map, const bool refresh = false);
QHash<QString, QVariant> findByPk(qint64 id, const QSharedPointer<Entity> &e);
QSharedPointer<Entity> convert(const QHash<QString, QVariant> &map,
const char *classname, const bool refresh = false);
QList<QSharedPointer<Entity>> convert(QList<QHash<QString, QVariant> > maps,
const char *classname, const bool refresh = false);
void manyToOne(const QSharedPointer<Entity> &entity, const QVariant &id,
const QMetaProperty &property, const bool refresh = false);
void oneToMany(const QSharedPointer<Entity> &entity, const Relation &r,
......
bool ignoreID = false);
QSharedPointer<Entity> findById(const qint64 &id, QSharedPointer<Entity> &e,
const bool refresh = false);
void addEntityToListProperty(const QSharedPointer<Entity> &entity,
QSharedPointer<Entity> add, const QMetaProperty &property);
void setListProperty(const QSharedPointer<Entity> &entity,
QList<QSharedPointer<Entity>> &list,
const QMetaProperty &property) const;
void setProperty(const QSharedPointer<Entity> &entity,
QSharedPointer<Entity> value,
const QMetaProperty &property) const;
void savePrePersistedRelations(const QSharedPointer<Entity> &entity);
void savePostPersistedRelations(const QSharedPointer<Entity> &entity);
......
void removeEntity(QVariant &var);
void setNullOneToManyRelation(QVariant &var, const Relation &r);
void setNullEntityPropertyRelation(QVariant &var, const Relation &r);
QMetaProperty mappedProperty(const Relation &r,
const QSharedPointer<Entity> &foreignEntity) const;
QSharedPointer<Entity> convert(const QHash<QString, QVariant> &map,
const char *classname,const bool refresh = false);
QList<QSharedPointer<Entity>> convert(QList<QHash<QString, QVariant> > maps,
const char *classname,const bool refresh = false);
private:
static QStringList connectionNames;
src/querybuilder.cpp
#include "entity.h"
#include <QRegularExpression>
#include "entityinstancefactory.h"
#include "entityhelper.h"
using namespace CuteEntityManager;
......
this->schema->containsTable(tableName) ? rc = true : rc = false;
if (!rc) {
QSqlQuery q = this->database->getQuery(this->createTable(tableName,
tableDefinition));
tableDefinition));
if (this->database->exec(q)) {
if (createRelationTables) {
auto relTables = this->generateRelationTables(entity);
auto i = relTables.constBegin();
while (i != relTables.constEnd()) {
auto query = this->database->getQuery(this->createTable(i.key(),
i.value()));
i.value()));
this->database->exec(query);
++i;
}
......
const {
QStringList queries = QStringList();
if (this->supportsForeignKeys()) {
auto relations = entity->getNonInheritedRelations();
auto props = entity->getMetaProperties();
auto relations = EntityHelper::getNonInheritedRelations(entity.data());
auto props = EntityHelper::getMetaProperties(entity.data());
auto iterator = relations.constBegin();
while (iterator != relations.constEnd()) {
auto relation = iterator.value();
if (relation.getMappedBy().isEmpty() && !relation.getCascadeType().isEmpty()) {
QString update = relation.getCascadeType().contains(CascadeType::MERGE)
|| relation.getCascadeType().contains(CascadeType::ALL) ?
this->getForeignKeyCascade(
CASCADE) : this->getForeignKeyCascade(NO_ACTION);
|| relation.getCascadeType().contains(CascadeType::ALL) ?
this->getForeignKeyCascade(
CASCADE) : this->getForeignKeyCascade(NO_ACTION);
QString remove = relation.getCascadeType().contains(CascadeType::REMOVE)
|| relation.getCascadeType().contains(CascadeType::ALL) ?
this->getForeignKeyCascade(
CASCADE) : this->getForeignKeyCascade(DbForeignKeyCascade::SET_NULL);
|| relation.getCascadeType().contains(CascadeType::ALL) ?
this->getForeignKeyCascade(
CASCADE) : this->getForeignKeyCascade(DbForeignKeyCascade::SET_NULL);
this->createRelationFK(queries, entity, relation,
props.value(relation.getPropertyName()), update, remove);
}
......
const QMetaProperty &metaProperty, const QString &update,
const QString &remove) const {
auto ptr = QSharedPointer<Entity>
(EntityInstanceFactory::createInstance(metaProperty.type()));
(EntityInstanceFactory::createInstance(metaProperty.type()));
if (ptr) {
if (relation.getType() == RelationType::ONE_TO_ONE
|| relation.getType() == RelationType::MANY_TO_ONE) {
QString indexName = this->generateIndexName(relation.getPropertyName(),
entity->getTablename(),
this->generateColumnNameID(relation.getPropertyName()),
ptr->getTablename(), true);
entity->getTablename(),
this->generateColumnNameID(relation.getPropertyName()),
ptr->getTablename(), true);
queries.append(this->addForeignKey(indexName, entity->getTablename(),
QStringList(this->generateColumnNameID(relation.getPropertyName())),
ptr->getTablename(),
......
} else if (relation.getType() == RelationType::MANY_TO_MANY) {
QString tableName = this->generateManyToManyTableName(entity, ptr, relation);
queries.append(this->createForeignKeyManyToMany(tableName, entity, update,
remove));
remove));
queries.append(this->createForeignKeyManyToMany(tableName, ptr, update,
remove));
remove));
}
}
}
QString QueryBuilder::createForeignKeyManyToMany(const QString &tableName,
const QSharedPointer<Entity> &entity, const QString &update,
const QString &remove) const {
const QSharedPointer<Entity> &entity, const QString &update,
const QString &remove) const {
QString fkColumn = this->generateManyToManyColumnName(entity);
QString indexName = this->generateIndexName(fkColumn,
tableName, fkColumn,
entity->getTablename(), true);
tableName, fkColumn,
entity->getTablename(), true);
return this->addForeignKey(indexName, tableName, QStringList(fkColumn),
entity->getTablename(), QStringList(entity->getPrimaryKey()),
remove, update);
......
&& QString(superMetaObject->className()) !=
this->entityClassname()) {
Entity *superClass = EntityInstanceFactory::createInstance(
superMetaObject->className());
superMetaObject->className());
if (superClass) {
QString refColumn = superClass->getPrimaryKey();
QString refTable = superClass->getTablename();
r = this->addForeignKey(this->generateIndexName(e->getPrimaryKey(),
e->getTablename(), refColumn, refTable, true), e->getTablename(),
e->getTablename(), refColumn, refTable, true), e->getTablename(),
QStringList(e->getPrimaryKey()), refTable, QStringList(refColumn),
this->getForeignKeyCascade(CASCADE),
this->getForeignKeyCascade(CASCADE));
......
s.append(", ");
}
s.append(this->schema->quoteColumnName(i.key())).append(" " +
this->getColumnType(
i.value()));
this->getColumnType(
i.value()));
++i;
}
s.append(");");
......
QString QueryBuilder::renameTable(QString tableName, QString newName) const {
return "RENAME TABLE " + this->schema->quoteTableName(
tableName) + " TO " +
this->schema->quoteTableName(
newName);
tableName) + " TO " +
this->schema->quoteTableName(
newName);
}
QString QueryBuilder::dropTable(QString tableName) const {
......
QString QueryBuilder::addColumn(QString tableName, QString columnName,
QString columnType) const {
return "ALTER TABLE " + this->schema->quoteTableName(
tableName) + " ADD " +
this->schema->quoteColumnName(
columnName) + " " + this->getColumnType(columnType);
tableName) + " ADD " +
this->schema->quoteColumnName(
columnName) + " " + this->getColumnType(columnType);
}
QString QueryBuilder::dropColumn(QString tableName, QString columName) const {
return "ALTER TABLE " + this->schema->quoteTableName(
tableName) + " DROP COLUMN " +
this->schema->quoteColumnName(columName);
tableName) + " DROP COLUMN " +
this->schema->quoteColumnName(columName);
}
QString QueryBuilder::renameColumn(QString tableName, QString oldName,
QString newName) const {
return "ALTER TABLE " + this->schema->quoteTableName(
tableName) + " RENAME COLUMN " +
this->schema->quoteColumnName(oldName) + " TO " +
this->schema->quoteColumnName(
newName);
tableName) + " RENAME COLUMN " +
this->schema->quoteColumnName(oldName) + " TO " +
this->schema->quoteColumnName(
newName);
}
QString QueryBuilder::alterColumn(QString tableName, QString columnName,
QString newType) const {
return "ALTER TABLE " + this->schema->quoteTableName(
tableName) + " CHANGE " +
this->schema->quoteColumnName(columnName) + " " +
this->schema->quoteColumnName(columnName) + this->getColumnType(newType);
tableName) + " CHANGE " +
this->schema->quoteColumnName(columnName) + " " +
this->schema->quoteColumnName(columnName) + this->getColumnType(newType);
}
QString QueryBuilder::addPrimaryKey(QString name, QString tableName,
QStringList columns) const {
return "ALTER TABLE " + this->schema->quoteTableName(
tableName) + " ADD CONSTRAINT " +
this->schema->quoteColumnName(name) + "PRIMARY KEY (" +
this->buildColumns(columns) + " )";
tableName) + " ADD CONSTRAINT " +
this->schema->quoteColumnName(name) + "PRIMARY KEY (" +
this->buildColumns(columns) + " )";
}
QString QueryBuilder::dropPrimaryKey(QString name, QString tableName) const {
return "ALTER TABLE " + this->schema->quoteTableName(
tableName) + " DROP CONSTRAINT " +
this->schema->quoteColumnName(name);
tableName) + " DROP CONSTRAINT " +
this->schema->quoteColumnName(name);
}
QString QueryBuilder::addForeignKey(QString name, QString tableName,
......
QStringList refColumns, QString deleteConstraint,
QString updateConstraint) const {
QString r = "ALTER TABLE " + this->schema->quoteTableName(
tableName) + "ADD CONSTRAINT " +
this->schema->quoteColumnName(name)
+ " FOREIGN KEY (" + this->buildColumns(columns) + ")" + " REFERENCES " +
this->schema->quoteTableName(
refTableName) +
" (" + this->buildColumns(refColumns) + ")";
tableName) + "ADD CONSTRAINT " +
this->schema->quoteColumnName(name)
+ " FOREIGN KEY (" + this->buildColumns(columns) + ")" + " REFERENCES " +
this->schema->quoteTableName(
refTableName) +
" (" + this->buildColumns(refColumns) + ")";
if (!deleteConstraint.isEmpty()) {
r.append(" ON DELETE " + deleteConstraint);
}
......
const QString &table, const QString &refColumn, const QString &refTable,
const bool fk) const {
return QString(fk ? "fk" : "idx").append("_").append(name).append(table).append(
refColumn).append(refTable);
refColumn).append(refTable);
}
QString QueryBuilder::generateColumnNameID(QString name) const {
......
QString QueryBuilder::dropForeignKey(QString name, QString tableName) const {
return "ALTER TABLE " + this->schema->quoteTableName(
tableName) + " DROP CONSTRAINT " +
this->schema->quoteColumnName(name);
tableName) + " DROP CONSTRAINT " +
this->schema->quoteColumnName(name);
}
QString QueryBuilder::createIndex(QString name, QString tableName,
QStringList columns,
bool unique) const {
QString s = (unique ? "CREATE UNIQUE INDEX " : "CREATE INDEX ") +
this->schema->quoteTableName(
name) + " ON " + this->schema->quoteTableName(tableName) + " (";
this->schema->quoteTableName(
name) + " ON " + this->schema->quoteTableName(tableName) + " (";
s.append(this->buildColumns(columns));
s.append(");");
return s;
......
QString QueryBuilder::dropIndex(QString name, QString tableName) const {
return "DROP INDEX " + this->schema->quoteTableName(name) + " ON " +
this->schema->quoteTableName(
tableName);
this->schema->quoteTableName(
tableName);
}
QSharedPointer<Database> QueryBuilder::getDatabase() const {
......
}
QHash<QString, QString> QueryBuilder::generateTableDefinition(
const QSharedPointer<Entity> &entity)
const QSharedPointer<Entity> &entity)
const {
auto map = QHash<QString, QString>();
auto o = entity->metaObject();
auto superMetaObject = entity->metaObject()->superClass();
auto superMetaObjectPropertyMap = entity->getSuperMetaProperties();
auto superMetaObjectPropertyMap = EntityHelper::getSuperMetaProperties(entity.data());
QHash<QString, Relation> relations = entity->getRelations();
for (int var = 0; var < o->propertyCount(); ++var) {
auto m = o->property(var);
if ((!superMetaObjectPropertyMap.contains(QString(m.name()))
|| entity->getInheritanceStrategy() == InheritanceStrategy::PER_CLASS_TABLE)
|| entity->getInheritanceStrategy() == InheritanceStrategy::PER_CLASS_TABLE)
&& m.name() != QString("objectName") && m.isReadable()
&& !entity->getTransientAttributes().contains(m.name())) {
if (m.isEnumType()) {
......
}
QString QueryBuilder::generateManyToManyTableName(const QSharedPointer<Entity>
&firstEntity,
const QSharedPointer<Entity> &secondEntity, const Relation &r) const {
&firstEntity,
const QSharedPointer<Entity> &secondEntity, const Relation &r) const {
if (r.getMappedBy().isEmpty()) {
return firstEntity->getTablename() + "_" + r.getPropertyName();
} else {
......
const {
auto relations = QHash<QString, QHash<QString, QString>>();
QHash<QString, Relation> m = entity->getRelations();
auto props = entity->getMetaProperties();
auto props = EntityHelper::getMetaProperties(entity.data());
for (auto i = m.begin(); i != m.end(); ++i) {
Relation r = i.value();
if (r.getType() == RelationType::MANY_TO_MANY && r.getMappedBy().isEmpty()) {
......
this->schema->TYPE_BIGINT);
auto meta = props.value(r.getPropertyName());
QSharedPointer<Entity> ptr = QSharedPointer<Entity>
(EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(
QMetaType::typeName(meta.userType()))));
(EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(
QMetaType::typeName(meta.userType()))));
h.insert(this->generateManyToManyColumnName(ptr),
this->schema->TYPE_BIGINT);
relations.insert(this->generateManyToManyTableName(entity, ptr, r), h);
......
}
QString QueryBuilder::transformAbstractTypeToRealDbType(
QString typeName) const {
QString typeName) const {
return this->schema->getTypeMap()->value(typeName);
}
......
//cant believe that this could work in Qt
//https://github.com/yiisoft/yii2/blob/master/framework/db/QueryBuilder.php
QRegularExpression reg = QRegularExpression(
QRegularExpression::escape("/^(\\w+)\((.+?)\\)(.*)$/"));
QRegularExpression::escape("/^(\\w+)\((.+?)\\)(.*)$/"));
reg.optimize();
QRegularExpressionMatchIterator i = reg.globalMatch(type, 0,
QRegularExpression::PartialPreferFirstMatch);
QRegularExpression::PartialPreferFirstMatch);
short s = 0;
bool ok = false;
QString before = "";
......
QSqlQuery QueryBuilder::find(const qint64 &id, const QString &tableName) const {
QString pk = "id";
QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
tableName)) + " WHERE " + pk + " = " + this->placeHolder(pk) + " LIMIT 1;");
tableName)) + " WHERE " + pk + " = " + this->placeHolder(pk) + " LIMIT 1;");
this->bindValue(pk, id, q);
return q;
}
......
QSqlQuery QueryBuilder::find(const qint64 &id,
const QSharedPointer<Entity> &entity, qint64 offset, QString pk) const {
QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
entity->getTablename())) + this->joinSuperClasses(
entity) + " WHERE " + this->schema->quoteColumnName(
pk) + "= " + this->placeHolder(pk) + this->limit(1, offset));
entity->getTablename())) + this->joinSuperClasses(
entity) + " WHERE " + this->schema->quoteColumnName(
pk) + "= " + this->placeHolder(pk) + this->limit(1, offset));
this->bindValue(pk, id, q);
return q;
}
......
QSqlQuery QueryBuilder::findAll(const QSharedPointer<Entity> &entity,
const qint64 limit, qint64 offset) {
return this->database->getQuery(this->selectBase(QStringList(
entity->getTablename())) + " " + this->joinSuperClasses(
entity->getTablename())) + " " + this->joinSuperClasses(
entity) + this->limit(limit, offset) + ";");
}
......
* @return
*/
QSqlQuery QueryBuilder::findByAttributes(const QHash<QString, QVariant> &m,
const QString &tableName,
const bool &ignoreID, const qint64 limit, const qint64 offset) const {
const QString &tableName,
const bool &ignoreID, const qint64 limit, const qint64 offset) const {
QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
tableName)) + this->where(m, "AND", ignoreID) + this->limit(limit, offset));
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,
const qint64 limit,
const qint64 offset) {
bool ignoreID,
const qint64 limit,
const qint64 offset) {
QHash<QString, QVariant> values = this->getEntityAttributes(
e->getMetaProperties(), e);
EntityHelper::getMetaProperties(e.data()), e);
return this->findByAttributes(values, e->getTablename(), ignoreID, limit,
offset);
}
......
entity->getProperty(entity->getPrimaryKey()).toLongLong()));
if (entity->getInheritanceStrategy() != InheritanceStrategy::PER_CLASS_TABLE
&& entity->isInheritanceCascaded()) {
auto classes = entity->superClasses(true);
auto classes = EntityHelper::superClasses(entity.data(),true);
for (int var = 0; var < classes.size(); ++var) {
auto item = classes.at(var);
auto instance = EntityInstanceFactory::createInstance(item->className());
......
const qint64 &id, const QString &primaryKey) const {
QSqlQuery q = this->database->getQuery("DELETE FROM " +
this->schema->quoteTableName(
tableName) + " WHERE " +
tableName) + " WHERE " +
this->schema->quoteColumnName(primaryKey) + "=" + this->placeHolder(
primaryKey) + ";");
primaryKey) + ";");
this->bindValue(primaryKey, id, q);
return q;
}
QSqlQuery QueryBuilder::findId(const QSharedPointer<Entity> &entity) const {
QHash<QString, QVariant> values = this->getEntityAttributes(
entity->getMetaProperties(),
entity);
EntityHelper::getMetaProperties(entity.data()),
entity);
QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
entity->getTablename()),
QStringList(entity->getPrimaryKey())) + this->where(values,
"AND", true) + " LIMIT 1");
entity->getTablename()),
QStringList(entity->getPrimaryKey())) + this->where(values,
"AND", true) + " LIMIT 1");
this->bindValues(values, q);
return q;
}
......
QSqlQuery QueryBuilder::count(const QSharedPointer<Entity> &entity,
bool ignoreID) const {
QHash<QString, QVariant> values = this->getEntityAttributes(
entity->getMetaProperties(),
entity);
EntityHelper::getMetaProperties(entity.data()),
entity);
QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
entity->getTablename()),
QStringList(this->countFunction())) + this->where(
values, "AND", ignoreID));
entity->getTablename()),
QStringList(this->countFunction())) + this->where(
values, "AND", ignoreID));
this->bindValues(values, q, ignoreID);
return q;
}
QSqlQuery QueryBuilder::count(const QString &tableName) const {
QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
tableName), QStringList(this->countFunction())) + ";");
tableName), QStringList(this->countFunction())) + ";");
return q;
}
......
QList<QSqlQuery> QueryBuilder::createOrMerge(const QSharedPointer<Entity>
&entity, bool insert) const {
&entity, bool insert) const {
const QList<ClassAttributes> attrs = this->inheritedAttributes(entity);
auto queries = QList<QSqlQuery>();
bool first = true;
......
}
QSqlQuery q = this->database->getQuery();
QString p1 = "INSERT INTO " + this->schema->quoteTableName(
tableName) + "(";
tableName) + "(";
QString p2 = "VALUES(";
if (!attributes.isEmpty()) {
q.prepare(this->buildCreateQuery(attributes.constBegin(), attributes.constEnd(),
......
QSqlQuery QueryBuilder::update(const QString &tableName,
QHash<QString, QVariant> &attributes, const QString &primaryKey) const {
QSqlQuery q = this->database->getQuery("UPDATE " + this->schema->quoteTableName(
tableName) + " SET " + this->attributes(attributes) + " WHERE " +
tableName) + " SET " + this->attributes(attributes) + " WHERE " +
this->schema->quoteColumnName(primaryKey) + " = " + this->placeHolder(
primaryKey) + ";");
primaryKey) + ";");
this->bindValues(attributes, q);
return q;
}
......
QString pk = "id";
sql += " WHERE ";
sql += this->schema->quoteColumnName(
attribute);
attribute);
sql += " = " + this->placeHolder(pk) + ";";
q.prepare(sql);
this->bindValue(pk, id, q);
......
}
QSqlQuery QueryBuilder::manyToManyDelete(const QString &tableName,
const QString &attribute, const qint64 &id) {
const QString &attribute, const qint64 &id) {
QSqlQuery q = this->database->getQuery();
QString pkCol = "id";
QString sql = "DELETE FROM " + this->schema->quoteTableName(
tableName) + " WHERE " + this->schema->quoteColumnName(
attribute) + "=" + this->placeHolder(pkCol);
tableName) + " WHERE " + this->schema->quoteColumnName(
attribute) + "=" + this->placeHolder(pkCol);
q.prepare(sql);
this->bindValue(pkCol, id, q);
return q;
}
QSqlQuery QueryBuilder::manyToManyInsert(const QString &tableName,
const QString &col1, const QString &col2) const {
const QString &col1, const QString &col2) const {
QSqlQuery q = this->database->getQuery();
QString sql = "INSERT INTO " + this->schema->quoteTableName(
tableName) + "(" + col1 + "," + col2 + ")"
+ "VALUES(?, ?);";
tableName) + "(" + col1 + "," + col2 + ")"
+ "VALUES(?, ?);";
q.prepare(sql);
return q;
}
......
QString QueryBuilder::joinSuperClasses(const QSharedPointer<Entity> &entity)
const {
auto classes = entity->superClasses(true);
auto classes = EntityHelper::superClasses(entity.data(),true);
QString joined = "";
Entity *e = 0;
for (int var = 0; var < classes.size(); ++var) {
......
QString QueryBuilder::countFunction(const QString &distinctColumn) const {
return QString("COUNT(" + distinctColumn.isEmpty() ? "*" : (this->distinct() +
this->schema->quoteColumnName(distinctColumn)) + ")");
this->schema->quoteColumnName(distinctColumn)) + ")");
}
QString QueryBuilder::distinct() const {
......
}
QString QueryBuilder::generateManyToManyColumnName(const QSharedPointer<Entity>
&entity) const {
&entity) const {
if (entity) {
return this->generateColumnNameID(entity->getTablename());
}
......
QHash<QString, QVariant> QueryBuilder::saveAttributes(const
QSharedPointer<Entity> &entity, QHash<QString, QMetaProperty> props,
QHash<QString, Relation> relations) const {
QSharedPointer<Entity> &entity, QHash<QString, QMetaProperty> props,
QHash<QString, Relation> relations) const {
if (props.isEmpty()) {
props = entity->getMetaProperties();
props = EntityHelper::getMetaProperties(entity.data());
}
auto values = this->getEntityAttributes(props, entity);
auto relValues = this->getManyToOneAttributes(props, entity, relations);
......
}
QHash<QString, QMetaProperty> QueryBuilder::processProperties(
const QSharedPointer<Entity> &e,
QHash<QString, QMetaProperty> &usedProperties) const {
auto properties = e->getMetaProperties();
const QSharedPointer<Entity> &e,
QHash<QString, QMetaProperty> &usedProperties) const {
auto properties = EntityHelper::getMetaProperties(e.data());
QMutableHashIterator<QString, QMetaProperty> i(properties);
while (i.hasNext()) {
i.next();
......
}
QHash<QString, Relation> QueryBuilder::processRelations(
const QSharedPointer<Entity> &e,
QHash<QString, Relation> &usedRelations) const {
const QSharedPointer<Entity> &e,
QHash<QString, Relation> &usedRelations) const {
auto relations = e->getRelations();
auto i = QMutableHashIterator<QString, Relation>(relations);
while (i.hasNext()) {
......
}
QList<QueryBuilder::ClassAttributes> QueryBuilder::inheritedAttributes(
const QSharedPointer<Entity> &entity) const {
const QSharedPointer<Entity> &entity) const {
auto list = QList<QueryBuilder::ClassAttributes>();
if (entity->getInheritanceStrategy() == InheritanceStrategy::JOINED_TABLE) {
auto classes = QList<const QMetaObject *>();
classes.append(entity->metaObject());
classes.append(entity->superClasses(true));
classes.append(EntityHelper::superClasses(entity.data(),true));
auto usedProperties = QHash<QString, QMetaProperty>();
auto usedRelations = QHash<QString, Relation>();
QSharedPointer<Entity> e;
......
metaObj));
if (e) {
list.append(QueryBuilder::ClassAttributes(e->getTablename(),
this->saveAttributes(entity, this->processProperties(e, usedProperties),
this->processRelations(e, usedRelations)), e->getPrimaryKey()));
this->saveAttributes(entity, this->processProperties(e, usedProperties),
this->processRelations(e, usedRelations)), e->getPrimaryKey()));
} else {
qDebug() << "Instance of " << metaObj->className() << " could not be created";
break;
......
}
} else {
list.append(QueryBuilder::ClassAttributes(entity->getTablename(),
this->saveAttributes(entity), entity->getPrimaryKey()));
this->saveAttributes(entity), entity->getPrimaryKey()));
}
return list;
}
......
const QString &tableName, const QString &foreignKey,
const QString &primaryKey) const {
return "LEFT JOIN " + this->schema->quoteTableName(
foreignTable) + " ON " +
this->schema->quoteColumnName(foreignTable + "." + primaryKey) + "=" +
this->schema->quoteColumnName(
tableName + "." + foreignKey);
foreignTable) + " ON " +
this->schema->quoteColumnName(foreignTable + "." + primaryKey) + "=" +
this->schema->quoteColumnName(
tableName + "." + foreignKey);
}
QHash<QString, QVariant> QueryBuilder::getEntityAttributes(
const QHash<QString, QMetaProperty>
&props,
const QSharedPointer<Entity> &entity) const {
const QHash<QString, QMetaProperty>
&props,
const QSharedPointer<Entity> &entity) const {
auto map = QHash<QString, QVariant>();
auto transientAttrs = entity->getTransientAttributes();
auto relations = entity->getRelations();
......
}
QHash<QString, QVariant> QueryBuilder::getManyToOneAttributes(
QHash<QString, QMetaProperty>
props,
const QSharedPointer<Entity> &entity,
QHash<QString, Relation> relations) const {
QHash<QString, QMetaProperty>
props,
const QSharedPointer<Entity> &entity,
QHash<QString, Relation> relations) const {
auto map = QHash<QString, QVariant>();
if (relations.isEmpty()) {
relations = entity->getRelations();
......
QString QueryBuilder::where(const QSharedPointer<Entity> &entity,
QString conjunction,
bool ignoreID) const {
return this->where(this->getEntityAttributes(entity->getMetaProperties(),
entity),
return this->where(this->getEntityAttributes(EntityHelper::getMetaProperties(entity.data()),
entity),
conjunction, ignoreID, entity->getPrimaryKey());
}
......
}
QueryBuilder::ClassAttributes::ClassAttributes(const QString name,
const QHash<QString, QVariant> attributes, QString pk) {
const QHash<QString, QVariant> attributes, QString pk) {
this->name = name;
this->attributes = attributes;
this->pk = pk;
......
}
void QueryBuilder::ClassAttributes::setAttributes(const QHash<QString, QVariant>
&value) {
&value) {
attributes = value;
}

Auch abrufbar als: Unified diff