Revision 6be60ddf
Von Christian Ehringfeld vor etwa 9 Jahren hinzugefügt
EntityManager.pri | ||
---|---|---|
contains(EM_LIBRARY_TYPE,staticlib) {
|
||
DEFINES += CUTE_ENTITY_MANAGER_LIBRARY_STATIC
|
||
} else {
|
||
# Windows needs the major library version
|
||
win32 {
|
||
EM_LIB = -lCuteEntityManager0
|
||
}
|
||
DEFINES += CUTE_ENTITY_MANAGER_LIBRARY_SHARED
|
||
}
|
||
|
src/entityhelper.cpp | ||
---|---|---|
superObject = nullptr;
|
||
}
|
||
return wholeProperties;
|
||
|
||
|
||
}
|
||
|
||
const QHash<Relation, QMetaProperty> EntityHelper::getRelationProperties(
|
||
... | ... | |
for (int i = 0; i < metaObject->propertyCount(); ++i) {
|
||
auto property = metaObject->property(i);
|
||
if(property.isValid() && property.name() != entity->getPrimaryKey()) {
|
||
property.write(newInstance,property.read(entity));
|
||
property.write(newInstance, property.read(entity));
|
||
}
|
||
}
|
||
}
|
||
... | ... | |
}
|
||
|
||
void EntityHelper::setListProperty(const QSharedPointer<Entity> &entity,
|
||
QList<QSharedPointer<Entity> > &list,
|
||
QList<QSharedPointer<Entity>> &list,
|
||
const QMetaProperty &property) {
|
||
QVariant var;
|
||
var.setValue<QList<QSharedPointer<Entity>>>(list);
|
src/entityinstancefactory.cpp | ||
---|---|---|
if(!e) {
|
||
e = qobject_cast<Entity *>(metaObject->newInstance());
|
||
qDebug() << "Backup method for dynamic object creation was called. Maybe the class " +
|
||
metaObject->className()+"isn't registered?";
|
||
QString(metaObject->className()) + " isn't registered?";
|
||
}
|
||
} else {
|
||
void *newObj = QMetaType::create(metaTypeId);
|
src/entitymanager.cpp | ||
---|---|---|
QStringList EntityManager::connectionNames = QStringList();
|
||
|
||
QHash<QString, EntityManager *> EntityManager::instances =
|
||
QHash<QString, EntityManager *>();
|
||
QHash<QString, EntityManager *>();
|
||
|
||
QStringList EntityManager::getConnectionNames() {
|
||
return EntityManager::connectionNames;
|
||
... | ... | |
void EntityManager::init(bool inspect, const MsgType msgType) {
|
||
EntityInstanceFactory::registerClass<DatabaseMigration>();
|
||
this->schema = QSharedPointer<Schema>(Database::getSchema(Database::getDatabaseType(
|
||
this->db->getDatabase().driverName()), this->db));
|
||
this->db->getDatabase().driverName()), this->db));
|
||
this->schema->setTables(this->schema->getTableSchemas());
|
||
this->queryInterpreter = QSharedPointer<QueryInterpreter>(new QueryInterpreter(
|
||
this->schema->getQueryBuilder().data()));
|
||
this->schema->getQueryBuilder().data()));
|
||
this->appendToInstanceList();
|
||
if (inspect) {
|
||
EntityInspector inspector = EntityInspector(msgType);
|
||
... | ... | |
loggerFile.replace("[", "");
|
||
loggerFile.replace("]", "");
|
||
this->logger = QSharedPointer<Logger>(new Logger(QDir::currentPath() + "/" +
|
||
loggerFile, msgType));
|
||
loggerFile, msgType));
|
||
}
|
||
|
||
EntityManager::EntityManager(const QString &databaseType, QString databasename,
|
||
QString hostname, QString username, QString password, QString port,
|
||
bool logQueries, QString databaseOptions, const bool inspectEntities,
|
||
CuteEntityManager::MsgType logActions) : QObject() {
|
||
this->db = QSharedPointer<Database>(new Database(databaseType, this->createConnection(), hostname,
|
||
databasename, username,
|
||
password,
|
||
port.toInt(), true, logQueries, databaseOptions, logActions));
|
||
this->db = QSharedPointer<Database>(new Database(databaseType, this->createConnection(),
|
||
hostname,
|
||
databasename, username,
|
||
password,
|
||
port.toInt(), true, logQueries, databaseOptions, logActions));
|
||
this->init(inspectEntities, logActions);
|
||
}
|
||
|
||
... | ... | |
}
|
||
this->db->startTransaction();
|
||
QList<QSqlQuery> q = this->schema->getQueryBuilder()->merge(
|
||
entity);
|
||
entity);
|
||
ok = this->db->exec(q);
|
||
|
||
if (!ok || !this->db->commitTransaction()) {
|
||
this->db->rollbackTransaction();
|
||
ok = false;
|
||
... | ... | |
}
|
||
this->db->startTransaction();
|
||
QList<QSqlQuery> q = this->schema->getQueryBuilder()->create(
|
||
entity);
|
||
entity);
|
||
bool first = true;
|
||
QVariant id = -1;
|
||
for (int var = 0; var < q.size(); ++var) {
|
||
... | ... | |
checkDuplicate, validate, relationsIgnoreHasChanged);
|
||
}
|
||
|
||
bool EntityManager::create(QList<QSharedPointer<Entity> > &entities,
|
||
bool EntityManager::create(QList<QSharedPointer<Entity>> &entities,
|
||
const bool persistRelations, const bool checkDuplicate, const bool validate,
|
||
const bool relationsIgnoreHasChanged) {
|
||
bool ok = true;
|
||
... | ... | |
ignoreHasChanged, validate, relationsIgnoreHasChanged);
|
||
}
|
||
|
||
bool EntityManager::save(QList<QSharedPointer<Entity> > &entities,
|
||
bool EntityManager::save(QList<QSharedPointer<Entity>> &entities,
|
||
const bool persistRelations, const bool ignoreHasChanged, const bool validate,
|
||
const bool relationsIgnoreHasChanged) {
|
||
bool ok = true;
|
||
... | ... | |
ignoreHasChanged, validate, relationsIgnoreHasChanged);
|
||
if (!ok) {
|
||
break;
|
||
this->logger->logMsg("Object " + QString::number(entity->getId()) +
|
||
" of class " +
|
||
EntityHelper::getClassName(entity.data()) + " couldn't saved.",
|
||
MsgType::WARNING);
|
||
/**
|
||
* @todo error handling
|
||
*
|
||
*/
|
||
}
|
||
}
|
||
return ok;
|
||
... | ... | |
bool EntityManager::startup(QString version, QStringList toInitialize,
|
||
bool createIndices) {
|
||
QSharedPointer<Entity> dbm = QSharedPointer<DatabaseMigration>
|
||
(new DatabaseMigration());
|
||
(new DatabaseMigration());
|
||
QHash<QString, QVariant> map = QHash<QString, QVariant>();
|
||
bool ok = true;
|
||
map.insert("version", version);
|
||
... | ... | |
if (ok) {
|
||
QString c = toInitialize.at(var);
|
||
auto entity = QSharedPointer<Entity>
|
||
(EntityInstanceFactory::createInstance(c));
|
||
(EntityInstanceFactory::createInstance(c));
|
||
ok = this->createTable(entity, false);
|
||
entities.append(entity);
|
||
} else {
|
||
this->logger->logMsg("startup of version " + version + " failed",
|
||
MsgType::CRITICAL);
|
||
this->logger->logMsg( "erroneous entity:" + (var == 0 ?
|
||
"null, this should not happen!" : toInitialize.at(var - 1)), MsgType::CRITICAL);
|
||
"null, this should not happen!" : toInitialize.at(var - 1)), MsgType::CRITICAL);
|
||
break;
|
||
}
|
||
}
|
||
... | ... | |
EntityHelper::getClassName(entity.data()));
|
||
}
|
||
|
||
QList<QHash<QString, QVariant> > EntityManager::selectByQuery(Query &query) {
|
||
QList<QHash<QString, QVariant>> EntityManager::selectByQuery(Query &query) {
|
||
QSqlQuery q = this->queryInterpreter->build(query);
|
||
return this->convertQueryResult(q);
|
||
}
|
||
|
||
QList<QHash<QString, QVariant> > EntityManager::selectBySql(
|
||
const QString &sql) {
|
||
QList<QHash<QString, QVariant>> EntityManager::selectBySql(
|
||
const QString &sql) {
|
||
QSqlQuery q = this->db->select(sql);
|
||
return this->convertQueryResult(q);
|
||
}
|
||
... | ... | |
for (int i = 0; i < rules.size(); ++i) {
|
||
ValidationRule rule = rules.at(i);
|
||
QSharedPointer<Validator> validator = ValidatorFactory::getValidatorObject(
|
||
rule.getValidatorName());
|
||
rule.getValidatorName());
|
||
if (validator) {
|
||
for (int var = 0; var < rule.getAttributes().size(); ++var) {
|
||
QString attr = rule.getAttributes().at(var);
|
||
QList<ErrorMsg> msgs = validator->validate(entity->getProperty(attr),
|
||
rule.getParams());
|
||
rule.getParams());
|
||
for (int i = 0; i < msgs.size(); ++i) {
|
||
QString emsg = msgs.at(i).getErrorMsg().replace("<property>", attr);
|
||
ErrorMsg m = msgs.at(i);
|
||
... | ... | |
for (auto i = listmap.constBegin(); i != listmap.constEnd(); ++i) {
|
||
if (rels.contains(i.key())) {
|
||
QString appendix =
|
||
this->schema->getQueryBuilder()->columnNameIDAppendix();
|
||
this->schema->getQueryBuilder()->columnNameIDAppendix();
|
||
QString relKey = i.key();
|
||
QVariant v = entity->getProperty(relKey.remove(relKey.size() - appendix.size(),
|
||
appendix.size()));
|
||
appendix.size()));
|
||
if (!v.isNull()) {
|
||
auto entity = EntityInstanceFactory::castQVariant(v);
|
||
if (entity->getProperty(entity->getPrimaryKey()) != i.value()) {
|
||
... | ... | |
}
|
||
|
||
QSharedPointer<Entity> EntityManager::findById(const qint64 &id,
|
||
QSharedPointer<Entity> &e,
|
||
const bool refresh) {
|
||
QSharedPointer<Entity> &e,
|
||
const bool refresh) {
|
||
QSharedPointer<Entity> r;
|
||
if (!e.isNull() && (refresh
|
||
|| !(r = this->cache.get(id, EntityHelper::getClassname(e.data()))))) {
|
||
... | ... | |
}
|
||
|
||
QSharedPointer<Entity> EntityManager::findById(const qint64 &id,
|
||
const QString &classname) {
|
||
const QString &classname) {
|
||
QSharedPointer<Entity> e = QSharedPointer<Entity>
|
||
(EntityInstanceFactory::createInstance(classname));
|
||
(EntityInstanceFactory::createInstance(classname));
|
||
return this->findById(id, e);
|
||
}
|
||
|
||
... | ... | |
bool ok = false;
|
||
if ((convertedId = id.toLongLong(&ok)) && ok && convertedId > -1) {
|
||
QString className = EntityInstanceFactory::extractEntityType(
|
||
property.typeName());
|
||
property.typeName());
|
||
QSharedPointer<Entity> ptr = QSharedPointer<Entity>();
|
||
if (refresh || !(this->cache.contains(convertedId, className)
|
||
&& (ptr = this->cache.get(convertedId, className)) && ptr)) {
|
||
... | ... | |
property.typeName())));
|
||
if (e) {
|
||
QSqlQuery q = this->schema->getQueryBuilder()->oneToMany(e->getTablename(),
|
||
this->schema->getQueryBuilder()->generateColumnNameID(r.getMappedBy()),
|
||
entity->getId());
|
||
this->schema->getQueryBuilder()->generateColumnNameID(r.getMappedBy()),
|
||
entity->getId());
|
||
auto listMap = this->convertQueryResult(q);
|
||
auto entities = this->convert(listMap, EntityHelper::getClassname(e.data()),
|
||
refresh);
|
||
... | ... | |
EntityInstanceFactory::extractEntityType(property.typeName())));
|
||
if (e) {
|
||
QSqlQuery q = this->schema->getQueryBuilder()->oneToMany(
|
||
e->getTablename(),
|
||
this->schema->getQueryBuilder()->generateColumnNameID(
|
||
r.getMappedBy()),
|
||
entity->getProperty(entity->getPrimaryKey()).toLongLong(), 1);
|
||
e->getTablename(),
|
||
this->schema->getQueryBuilder()->generateColumnNameID(
|
||
r.getMappedBy()),
|
||
entity->getProperty(entity->getPrimaryKey()).toLongLong(), 1);
|
||
auto listMap = this->convertQueryResult(q);
|
||
auto entities = this->convert(listMap, EntityHelper::getClassname(e.data()),
|
||
refresh);
|
||
... | ... | |
}
|
||
|
||
void EntityManager::savePrePersistedRelations(const QSharedPointer<Entity>
|
||
&entity, QList<Entity *> &mergedObjects, bool ignoreHasChanged) {
|
||
&entity, QList<Entity *> &mergedObjects, bool ignoreHasChanged) {
|
||
auto relations = EntityHelper::getRelationProperties(entity.data());
|
||
auto iterator = relations.constBegin();
|
||
while (iterator != relations.constEnd()) {
|
||
... | ... | |
}
|
||
|
||
void EntityManager::savePostPersistedRelations(const QSharedPointer<Entity>
|
||
&entity, QList<Entity *> &mergedObjects, bool ignoreHasChanged, bool newItem) {
|
||
&entity, QList<Entity *> &mergedObjects, bool ignoreHasChanged, bool newItem) {
|
||
auto relations = EntityHelper::getRelationProperties(entity.data());
|
||
for (auto i = relations.constBegin(); i != relations.constEnd(); ++i) {
|
||
const Relation r = i.key();
|
||
... | ... | |
newItem);
|
||
} else if (r.getType() == RelationType::ONE_TO_MANY) {
|
||
QList<QSharedPointer<Entity>> list = EntityInstanceFactory::castQVariantList(
|
||
var);
|
||
var);
|
||
if (!list.isEmpty()) {
|
||
auto fkProp = EntityHelper::mappedProperty(r, list.at(0));
|
||
for (int var = 0; var < list.size(); ++var) {
|
||
... | ... | |
auto fkProp = EntityHelper::mappedProperty(r, e);
|
||
EntityHelper::setProperty(e, entity, fkProp);
|
||
this->saveObject(e, mergedObjects, true, ignoreHasChanged);
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void EntityManager::persistMappedByRelation(const QList<QSharedPointer<Entity> >
|
||
&list, QSqlQuery &q, const QSharedPointer<Entity> &entity,
|
||
const QSharedPointer<Entity> &ptr, const Relation &r,
|
||
const QString &tblName, QList<Entity *> &mergedObjects) {
|
||
void EntityManager::persistMappedByRelation(const QList<QSharedPointer<Entity>>
|
||
&list, QSqlQuery &q, const QSharedPointer<Entity> &entity,
|
||
const QSharedPointer<Entity> &ptr, const Relation &r,
|
||
const QString &tblName, QList<Entity *> &mergedObjects) {
|
||
q.clear();
|
||
QList<QSharedPointer<Entity>> saved =
|
||
r.getCascadeType().contains(CascadeType::ALL) ||
|
||
r.getCascadeType().contains(CascadeType::MERGE) ||
|
||
r.getCascadeType().contains(CascadeType::PERSIST) ?
|
||
this->saveRelationEntities(list, r, mergedObjects) : list;
|
||
r.getCascadeType().contains(CascadeType::ALL) ||
|
||
r.getCascadeType().contains(CascadeType::MERGE) ||
|
||
r.getCascadeType().contains(CascadeType::PERSIST) ?
|
||
this->saveRelationEntities(list, r, mergedObjects) : list;
|
||
this->db->startTransaction();
|
||
auto builder = this->schema->getQueryBuilder();
|
||
q = builder->manyToManyInsert(tblName,
|
||
... | ... | |
}
|
||
|
||
EntityManager *EntityManager::getDefaultInstance() {
|
||
for (auto i = EntityManager::instances.constBegin();
|
||
i != EntityManager::instances.constEnd(); ++i) {
|
||
return i.value();
|
||
auto keys = EntityManager::instances.uniqueKeys();
|
||
if(!keys.isEmpty()) {
|
||
return EntityManager::instances.value(keys.first());
|
||
}
|
||
return nullptr;
|
||
}
|
||
... | ... | |
}
|
||
|
||
void EntityManager::setNullEntityPropertyRelation(QVariant &var,
|
||
const Relation &r) {
|
||
const Relation &r) {
|
||
if (r.getCascadeType().contains(CascadeType::REMOVE)
|
||
|| r.getCascadeType().contains(CascadeType::ALL)) {
|
||
this->removeEntity(var);
|
||
... | ... | |
}
|
||
|
||
void EntityManager::removeManyToManyEntityList(const QSharedPointer<Entity> &e,
|
||
const Relation &r,
|
||
QVariant &var) {
|
||
const Relation &r,
|
||
QVariant &var) {
|
||
if (!var.isNull() && var.canConvert<QVariantList>()) {
|
||
auto list = EntityInstanceFactory::castQVariantList(var);
|
||
if (!list.isEmpty()) {
|
||
... | ... | |
QString tblName = builder->generateManyToManyTableName(e, ptr, r);
|
||
if (this->schema->getTables().contains(tblName)) {
|
||
QSqlQuery q = builder->manyToManyDelete(
|
||
tblName, builder->generateManyToManyColumnName(e),
|
||
e->getProperty(e->getPrimaryKey()).toLongLong());
|
||
tblName, builder->generateManyToManyColumnName(e),
|
||
e->getProperty(e->getPrimaryKey()).toLongLong());
|
||
if (this->db->exec(q)) {
|
||
bool refresh = r.getCascadeType().contains(CascadeType::REFRESH)
|
||
|| r.getCascadeType().contains(CascadeType::ALL);
|
||
|| r.getCascadeType().contains(CascadeType::ALL);
|
||
bool remove = r.getCascadeType().contains(CascadeType::REMOVE)
|
||
|| r.getCascadeType().contains(CascadeType::ALL);
|
||
|| r.getCascadeType().contains(CascadeType::ALL);
|
||
auto fkProp = EntityHelper::mappedProperty(r, ptr);
|
||
for (int var = 0; var < list.size(); ++var) {
|
||
auto entity = list.at(var);
|
||
... | ... | |
}
|
||
|
||
QList<QSharedPointer<Entity>> EntityManager::saveRelationEntities(
|
||
const QList<QSharedPointer<Entity> > &list, const Relation &r,
|
||
QList<Entity *> &mergedObjects) {
|
||
const QList<QSharedPointer<Entity>> &list, const Relation &r,
|
||
QList<Entity *> &mergedObjects) {
|
||
QList<QSharedPointer<Entity>> saved = QList<QSharedPointer<Entity>>();
|
||
QSharedPointer<Entity> ptr;
|
||
for (int var = 0; var < list.size(); ++var) {
|
||
... | ... | |
QSqlQuery q;
|
||
if (!newItem) {
|
||
q = builder->manyToManyDelete(
|
||
tblName, builder->generateManyToManyColumnName(entity),
|
||
entity->getProperty(entity->getPrimaryKey()).toLongLong());
|
||
tblName, builder->generateManyToManyColumnName(entity),
|
||
entity->getProperty(entity->getPrimaryKey()).toLongLong());
|
||
ok = this->db->exec(q);
|
||
} else {
|
||
q = builder->getQuery();
|
||
... | ... | |
}
|
||
|
||
void EntityManager::missingManyToManyTable(const QString &tblName,
|
||
const QSharedPointer<Entity> &e, const Relation &r) {
|
||
const QSharedPointer<Entity> &e, const Relation &r) {
|
||
this->logger->logMsg("MANY_TO_MANY Table " + tblName + " is missing.\n" +
|
||
"Entity " + EntityHelper::getClassName(e.data()) +
|
||
" is affected.\n" + "Relation of property: " + r.getPropertyName(),
|
||
... | ... | |
void EntityManager::manyToMany(const QSharedPointer<Entity> &entity,
|
||
const QMetaProperty &property, const Relation &relation, const bool refresh) {
|
||
QSharedPointer<Entity> secEntityPtr = QSharedPointer<Entity>
|
||
(EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(
|
||
QString(property.typeName()))));
|
||
(EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(
|
||
QString(property.typeName()))));
|
||
auto builder = this->schema->getQueryBuilder();
|
||
if (secEntityPtr) {
|
||
QString tblName = builder->generateManyToManyTableName(entity, secEntityPtr,
|
||
relation);
|
||
relation);
|
||
if (this->schema->getTables().contains(tblName)) {
|
||
QSqlQuery q = builder->manyToMany(tblName,
|
||
builder->generateManyToManyColumnName(entity),
|
||
... | ... | |
}
|
||
}
|
||
|
||
QList<QSharedPointer<Entity> > EntityManager::findEntityByAttributes(
|
||
const QSharedPointer<Entity>
|
||
&entity,
|
||
bool ignoreID) {
|
||
QList<QSharedPointer<Entity>> EntityManager::findEntityByAttributes(
|
||
const QSharedPointer<Entity>
|
||
&entity,
|
||
bool ignoreID) {
|
||
auto maps = this->findAllByAttributes(entity, ignoreID);
|
||
return this->convert(maps, EntityHelper::getClassname(entity.data()));
|
||
}
|
||
|
||
QHash<QString, QVariant> EntityManager::findByPk(qint64 id,
|
||
const QSharedPointer<Entity>
|
||
&e) {
|
||
const QSharedPointer<Entity>
|
||
&e) {
|
||
QSqlQuery q = this->schema->getQueryBuilder()->find(id, e, 0,
|
||
e->getPrimaryKey());
|
||
e->getPrimaryKey());
|
||
auto listMap = this->convertQueryResult(q);
|
||
if (!listMap.isEmpty()) {
|
||
return listMap.at(0);
|
||
... | ... | |
}
|
||
|
||
|
||
QList<QHash<QString, QVariant> > EntityManager::findAllByAttributes(
|
||
const QSharedPointer<Entity>
|
||
&entity,
|
||
bool ignoreID) {
|
||
QList<QHash<QString, QVariant>> EntityManager::findAllByAttributes(
|
||
const QSharedPointer<Entity>
|
||
&entity,
|
||
bool ignoreID) {
|
||
QSqlQuery q = this->schema->getQueryBuilder()->findByAttributes(
|
||
entity, ignoreID);
|
||
entity, ignoreID);
|
||
return this->convertQueryResult(q);
|
||
}
|
||
|
||
QList<QHash <QString, QVariant> > EntityManager::findAllByAttributes(
|
||
const QHash<QString, QVariant> &m, const QString &tblname, bool ignoreID) {
|
||
QList<QHash <QString, QVariant>> EntityManager::findAllByAttributes(
|
||
const QHash<QString, QVariant> &m, const QString &tblname, bool ignoreID) {
|
||
QSqlQuery q = this->schema->getQueryBuilder()->findByAttributes(m,
|
||
tblname, ignoreID);
|
||
tblname, ignoreID);
|
||
return this->convertQueryResult(q);
|
||
}
|
||
|
||
QList<QHash<QString, QVariant> > EntityManager::convertQueryResult(
|
||
QSqlQuery &q) {
|
||
QList<QHash <QString, QVariant> > listmap = QList<QHash <QString, QVariant> >();
|
||
QList<QHash<QString, QVariant>> EntityManager::convertQueryResult(
|
||
QSqlQuery &q) {
|
||
QList<QHash <QString, QVariant>> listmap = QList<QHash <QString, QVariant>>();
|
||
this->db->select(q);
|
||
QSqlRecord rec = q.record();
|
||
QStringList l = QStringList();
|
||
... | ... | |
return listmap;
|
||
}
|
||
|
||
QList<QHash <QString, QVariant> > EntityManager::findAll(
|
||
const QSharedPointer<Entity> &e) {
|
||
QList<QHash <QString, QVariant>> EntityManager::findAll(
|
||
const QSharedPointer<Entity> &e) {
|
||
QSqlQuery q = this->schema->getQueryBuilder()->findAll(e);
|
||
return this->convertQueryResult(q);
|
||
}
|
||
... | ... | |
const Relation r = iterator.key();
|
||
const QMetaProperty property = iterator.value();
|
||
QString colName = this->schema->getQueryBuilder()->generateColumnNameID(
|
||
r.getPropertyName());
|
||
r.getPropertyName());
|
||
switch (r.getType()) {
|
||
case RelationType::MANY_TO_ONE:
|
||
if (map.contains(colName)) {
|
||
... | ... | |
bool EntityManager::createTable(const QSharedPointer<Entity> &entity,
|
||
bool createRelationTables) {
|
||
return this->schema->getQueryBuilder()->createTable(entity,
|
||
createRelationTables);
|
||
createRelationTables);
|
||
}
|
||
|
||
bool EntityManager::createTable(QString className, bool createRelationTables) {
|
||
QSharedPointer<Entity> e = QSharedPointer<Entity>
|
||
(EntityInstanceFactory::createInstance(className));
|
||
(EntityInstanceFactory::createInstance(className));
|
||
return this->schema->getQueryBuilder()->createTable(e, createRelationTables);
|
||
}
|
||
|
||
... | ... | |
if (followInheritance) {
|
||
q.appendJoins(qb->joinBaseClasses(entity));
|
||
values = EntityHelper::getEntityAttributes(EntityHelper::getMetaProperties(
|
||
entity.data()), entity);
|
||
entity.data()), entity);
|
||
} else {
|
||
values = EntityHelper::getEntityAttributes(
|
||
EntityHelper::getNonInheritedMetaProperties(entity.data()), entity);
|
||
EntityHelper::getNonInheritedMetaProperties(entity.data()), entity);
|
||
}
|
||
//maybe filter MANY_TO_MANY and ONE_TO_MANY relations
|
||
q.appendFrom(entity->getTablename());
|
||
... | ... | |
}
|
||
|
||
QSharedPointer<Entity> EntityManager::convert(const QHash<QString, QVariant>
|
||
&map,
|
||
const char *classname, const bool refresh, const bool resolveRelations) {
|
||
&map,
|
||
const char *classname, const bool refresh, const bool resolveRelations) {
|
||
auto ptr = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(
|
||
classname, map));
|
||
this->cache.insert(ptr);
|
||
... | ... | |
return ptr;
|
||
}
|
||
|
||
QList<QSharedPointer<Entity> > EntityManager::convert(
|
||
QList<QHash<QString, QVariant> > maps,
|
||
const char *classname, const bool refresh, const bool resolveRelations) {
|
||
auto list = QList<QSharedPointer<Entity> >();
|
||
QList<QSharedPointer<Entity>> EntityManager::convert(
|
||
QList<QHash<QString, QVariant>> maps,
|
||
const char *classname, const bool refresh, const bool resolveRelations) {
|
||
auto list = QList<QSharedPointer<Entity>>();
|
||
for (int var = 0; var < maps.size(); ++var) {
|
||
auto ptr = this->convert(maps.at(var), classname, refresh, resolveRelations);
|
||
list.append(ptr);
|
src/entitymanager.h | ||
---|---|---|
* @param entity
|
||
*/
|
||
void refresh(QSharedPointer<Entity> &entity);
|
||
QList<QHash<QString, QVariant> > selectByQuery(Query &query);
|
||
QList<QHash<QString, QVariant> > selectBySql(const QString &sql);
|
||
QList<QHash<QString, QVariant>> selectByQuery(Query &query);
|
||
QList<QHash<QString, QVariant>> selectBySql(const QString &sql);
|
||
qint8 count(Query &query);
|
||
/**
|
||
* @brief EntityManager::validate
|
||
... | ... | |
auto converted = this->convert(maps, EntityHelper::getClassname(ptr.data()),
|
||
false,
|
||
resolveRelations);
|
||
return this->convertList<T>(converted);
|
||
return EntityManager::convertList<T>(converted);
|
||
}
|
||
return QList<QSharedPointer<T>>();
|
||
}
|
||
... | ... | |
auto converted = this->convert(maps, EntityHelper::getClassname(ptr.data()),
|
||
false,
|
||
resolveRelations);
|
||
return this->convertList<T>(converted);
|
||
return EntityManager::convertList<T>(converted);
|
||
}
|
||
return QList<QSharedPointer<T>>();
|
||
}
|
||
... | ... | |
|
||
template<class T> QList<QSharedPointer<T>> findAllEntitiesByAttributes(
|
||
const QHash<QString, QVariant> &attributes =
|
||
QHash<QString, QString>(), quint64 limit = 0, quint64 offset = 0,
|
||
QHash<QString, QVariant>(), quint64 limit = 0, quint64 offset = 0,
|
||
bool joinBaseClasses = false, const bool resolveRelations = true) {
|
||
QSharedPointer<Entity> e = QSharedPointer<Entity>
|
||
(EntityInstanceFactory::createInstance<T*>());
|
||
... | ... | |
auto results = this->convertQueryResult(q);
|
||
auto list = this->convert(results, EntityHelper::getClassname(e.data()), false,
|
||
resolveRelations);
|
||
return this->convertList<T>(list);
|
||
return EntityManager::convertList<T>(list);
|
||
}
|
||
return QList<QSharedPointer<T>>();
|
||
}
|
||
... | ... | |
QSqlQuery q = this->schema->getDatabase()->getQuery(sql);
|
||
auto result = this->convertQueryResult(q);
|
||
auto converted = this->convert(result, EntityHelper::getClassname(e));
|
||
return this->convertList<T>(converted);
|
||
return EntityManager::convertList<T>(converted);
|
||
}
|
||
return QList<QSharedPointer<T>>();
|
||
}
|
||
... | ... | |
}
|
||
|
||
template<class T, class X>
|
||
QList<QSharedPointer<T>> convertList(const QList<QSharedPointer<X>> &list) {
|
||
static QList<QSharedPointer<T>> convertList(const QList<QSharedPointer<X>> &list) {
|
||
QList<QSharedPointer<T>> newList = QList<QSharedPointer<T>>();
|
||
for (int i = 0; i < list.size(); ++i) {
|
||
newList.append(qSharedPointerObjectCast<T>(list.at(i)));
|
||
... | ... | |
const bool checkDuplicate, const bool validate,
|
||
const bool relationsIgnoreHasChanged = false);
|
||
void init(bool inspect, const MsgType msgType);
|
||
QList<QHash<QString, QVariant> > findAll(const QSharedPointer<Entity> &e);
|
||
QList<QHash<QString, QVariant>> findAll(const QSharedPointer<Entity> &e);
|
||
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);
|
||
... | ... | |
void persistManyToMany(const QSharedPointer<Entity> &entity, const Relation &r,
|
||
QVariant &property, QList<Entity *> &mergedObjects,
|
||
const bool ignoreHasChanged = false, const bool newItem = false);
|
||
QList<QHash<QString, QVariant> > findAllByAttributes(const
|
||
QSharedPointer<Entity> &entity,
|
||
bool ignoreID = false);
|
||
QList<QHash<QString, QVariant> > findAllByAttributes(const
|
||
QHash<QString, QVariant> &m,
|
||
const QString &tblname,
|
||
bool ignoreID = false);
|
||
QList<QHash<QString, QVariant>> findAllByAttributes(const
|
||
QSharedPointer<Entity> &entity,
|
||
bool ignoreID = false);
|
||
QList<QHash<QString, QVariant>> findAllByAttributes(const
|
||
QHash<QString, QVariant> &m,
|
||
const QString &tblname,
|
||
bool ignoreID = false);
|
||
QSharedPointer<Entity> findById(const qint64 &id, QSharedPointer<Entity> &e,
|
||
const bool refresh = false);
|
||
/**
|
||
... | ... | |
QSharedPointer<Entity> convert(const QHash<QString, QVariant> &map,
|
||
const char *classname, const bool refresh = false,
|
||
const bool resolveRelations = true);
|
||
QList<QSharedPointer<Entity>> convert(QList<QHash<QString, QVariant> > maps,
|
||
QList<QSharedPointer<Entity>> convert(QList<QHash<QString, QVariant>> maps,
|
||
const char *classname, const bool refresh = false,
|
||
const bool resolveRelations = true);
|
||
void missingManyToManyTable(const QString &tblName,
|
||
... | ... | |
QSharedPointer<Database> db;
|
||
Cache cache;
|
||
QString createConnection();
|
||
QList<QHash<QString, QVariant> > convertQueryResult(QSqlQuery &q);
|
||
QList<QHash<QString, QVariant>> convertQueryResult(QSqlQuery &q);
|
||
/**
|
||
* @brief EntityManager::checkTable
|
||
* Checks if a table has been already created, if not it will create it
|
src/src.pro | ||
---|---|---|
}
|
||
|
||
CONFIG += c++14
|
||
QMAKE_CXXFLAGS += -Wall -Wextra -Wunsafe-loop-optimizations -pedantic -Wfloat-equal -Wundef -Wpointer-arith -Wcast-align -Wunreachable-code
|
||
#headers.path = $$PREFIX/include/cuteEntityManager
|
||
#headers.files = $$HEADERS
|
||
#target.path = $$PREFIX/$$LIBDIR
|
||
#INSTALLS += target
|
||
QMAKE_CXXFLAGS += -Wall -Wextra -pedantic -Wfloat-equal -Wundef -Wpointer-arith -Wcast-align -Wunreachable-code
|
||
headers.path = $$PREFIX/include/cuteEntityManager
|
||
headers.files = $$HEADERS
|
||
target.path = $$PREFIX/$$LIBDIR
|
||
INSTALLS += target headers
|
||
|
||
CONFIG(release, debug|release):DEFINES += QT_NO_DEBUG_OUTPUT
|
Auch abrufbar als: Unified diff
bugfixxes