Revision 9070a496
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
src/entitymanager.cpp | ||
---|---|---|
|
||
void EntityManager::init() {
|
||
auto schema = Database::getSchema(Database::getDatabaseType(
|
||
this->db.data()->getDatabase().driverName()), this->db);
|
||
this->db.data()->getDatabase().driverName()), this->db);
|
||
this->schema = QSharedPointer<Schema>(schema);
|
||
this->schema.data()->setTables(this->schema.data()->getTableSchemas());
|
||
}
|
||
... | ... | |
EntityManager::connectionNames.removeOne(name);
|
||
}
|
||
|
||
QList<QSharedPointer<Entity>> EntityManager::findAllEntities(QSharedPointer<Entity> entity) {
|
||
auto maps = this->findAll(entity.data()->getTablename());
|
||
return this->convert(maps, entity.data()->getClassname());
|
||
}
|
||
|
||
|
||
QSharedPointer<Entity> EntityManager::convert(const QHash<QString, QVariant> &map, const char *classname) {
|
||
return QSharedPointer<Entity>(EntityInstanceFactory::createInstance(classname, map));
|
||
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) {
|
||
auto list = QList<QSharedPointer<Entity> >();
|
||
for (int var = 0; var < maps.size(); ++var) {
|
||
list.append(this->convert(maps.at(var), classname));
|
||
auto ptr = this->convert(maps.at(var), classname);
|
||
list.append(ptr);
|
||
this->cache.insert(ptr);
|
||
}
|
||
return list;
|
||
}
|
||
|
||
|
||
QSharedPointer<Entity> EntityManager::findEntity(QSharedPointer<Entity> entity) {
|
||
auto map = this->find(entity);
|
||
return this->convert(map, entity.data()->getClassname());
|
||
}
|
||
|
||
QList<QSharedPointer<Entity> > EntityManager::findEntityByAttributes(const QSharedPointer<Entity> &entity,
|
||
bool ignoreID) {
|
||
auto maps = this->findAllByAttributes(entity, ignoreID);
|
||
... | ... | |
return EntityManager::connectionNames;
|
||
}
|
||
|
||
QHash<QString, QVariant> EntityManager::find(qint64 id, QString tblname) {
|
||
QHash<QString, QVariant> EntityManager::findById(qint64 id, QString tblname) {
|
||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->find(id, tblname);
|
||
this->db->select(q);
|
||
QSqlRecord rec = q.record();
|
||
... | ... | |
return this->convertQueryResult(q);
|
||
}
|
||
|
||
QHash<QString, QVariant> EntityManager::find(QSharedPointer<Entity> entity) {
|
||
return this->find(entity.data()->getId(), entity.data()->getTablename());
|
||
}
|
||
|
||
bool EntityManager::save(QSharedPointer<Entity> &entity) {
|
||
if (entity.data()->getId() > -1) {
|
||
return this->merge(entity);
|
||
... | ... | |
bool rc = false;
|
||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->remove(entity);
|
||
if (this->db->transaction(q)) {
|
||
this->cache.remove(entity);
|
||
entity.clear();
|
||
rc = true;
|
||
}
|
src/entitymanager.h | ||
---|---|---|
protected:
|
||
void init();
|
||
QList<QHash<QString, QVariant> > findAll(QString tblname);
|
||
QHash<QString, QVariant> find(QSharedPointer<Entity> entity);
|
||
QHash<QString, QVariant> find(qint64 id, QString tblname);
|
||
QHash<QString, QVariant> findById(qint64 id, QString tblname);
|
||
QSharedPointer<Entity> convert(const QHash<QString, QVariant> &map, const char *classname);
|
||
QList<QSharedPointer<Entity>> convert(QList<QHash<QString, QVariant> > maps, const char *classname);
|
||
|
||
... | ... | |
*/
|
||
bool startup(QString version, QStringList toInitialize);
|
||
static void removeConnectionName(const QString &name);
|
||
QList<QSharedPointer<Entity>> findAllEntities(QSharedPointer<Entity> entity);
|
||
QSharedPointer<Entity> findEntity(QSharedPointer<Entity> entity);
|
||
QList<QSharedPointer<Entity>> findEntityByAttributes(const QSharedPointer<Entity> &entity, bool ignoreID = false);
|
||
bool create(QList<QSharedPointer<Entity>> entities);
|
||
bool create(QSharedPointer<Entity> &entity);
|
||
bool save(QSharedPointer<Entity> &entity);
|
||
qint64 findId(QSharedPointer<Entity> &entity);
|
||
bool merge(QSharedPointer<Entity> &entity, bool withRelations = true);
|
||
bool remove(QSharedPointer<Entity> &entity);
|
||
bool removeAll(QString tblname);
|
||
bool createTable(const QSharedPointer<Entity> &entity);
|
||
qint8 count(const QSharedPointer<Entity> &entity, bool ignoreID = true);
|
||
qint8 count(const QString &tableName);
|
||
QSharedPointer<Database> getDb() const;
|
||
void setDb(const QSharedPointer<Database> &value);
|
||
QSharedPointer<Schema> getSchema() const;
|
||
void setSchema(const QSharedPointer<Schema> &value);
|
||
|
||
/**
|
||
*@TODO use conditions
|
||
*/
|
||
template<class T> qint8 count(QHash<QString, QString> condition = QHash<QString, QString>()) {
|
||
Entity *e = EntityInstanceFactory::createInstance<T>();
|
||
qint8 rc = 0;
|
||
if (e) {
|
||
rc = this->count(e->getTablename());
|
||
delete e;
|
||
}
|
||
return rc;
|
||
}
|
||
|
||
template<class T> QList<QSharedPointer<Entity>> findAll() {
|
||
Entity *e = EntityInstanceFactory::createInstance<T>();
|
||
if (e) {
|
||
auto maps = this->findAll(e->getTablename());
|
||
const char *className = e->getClassname();
|
||
delete e;
|
||
return this->convert(maps, className);
|
||
}
|
||
return QList<QSharedPointer<Entity>>();
|
||
}
|
||
|
||
template<class T> QSharedPointer<Entity> findById(qint64 id) {
|
||
Entity *e = EntityInstanceFactory::createInstance<T>();
|
||
QSharedPointer<Entity> r;
|
||
if (e) {
|
||
if ((r = this->cache.get(id, QString(e->getClassname()))) && !r.data()) {
|
||
auto map = this->findById(id, e->getTablename());
|
||
r = this->convert(map, e->getClassname());
|
||
}
|
||
delete e;
|
||
}
|
||
return r;
|
||
}
|
||
|
||
template<class T> QSharedPointer<Entity> findById(const qint64 &id) {
|
||
Entity *e = EntityInstanceFactory::createInstance<T>();
|
||
QSharedPointer<Entity> ptr = QSharedPointer<Entity>(e);
|
||
e->setId(id);
|
||
return this->findEntity(ptr);
|
||
QSharedPointer<Entity> r;
|
||
if (e) {
|
||
if ((r = this->cache.get(id, QString(e->getClassname()))) && !r.data()) {
|
||
auto map = this->findById(id, e->getTablename());
|
||
r = this->convert(map, e->getClassname());
|
||
}
|
||
delete e;
|
||
}
|
||
return r;
|
||
}
|
||
|
||
template<class T> QSharedPointer<Entity> findEntityByAttributes(const QHash<QString, QString> &attributes) {
|
||
auto list = this->findAllEntitiesByAttributes<T>(attributes, 1, 0);
|
||
if (list.isEmpty()) {
|
||
... | ... | |
return QList<QSharedPointer<Entity>>();
|
||
}
|
||
|
||
bool create(QList<QSharedPointer<Entity>> entities);
|
||
bool create(QSharedPointer<Entity> &entity);
|
||
bool save(QSharedPointer<Entity> &entity);
|
||
qint64 findId(QSharedPointer<Entity> &entity);
|
||
bool merge(QSharedPointer<Entity> &entity, bool withRelations = true);
|
||
template<class T> bool remove(const QList<qint64> &ids) {
|
||
bool ok = true;
|
||
foreach (qint64 var, ids) {
|
||
... | ... | |
|
||
template<class T> bool remove(qint64 id) {
|
||
Entity *e = EntityInstanceFactory::createInstance<T>();
|
||
QSharedPointer<Entity> ptr = QSharedPointer<Entity>(e);
|
||
e->setId(id);
|
||
return this->remove(ptr);
|
||
if (e) {
|
||
QSharedPointer<Entity> ptr = QSharedPointer<Entity>(e);
|
||
e->setId(id);
|
||
return this->remove(ptr);
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool remove(QSharedPointer<Entity> &entity);
|
||
bool removeAll(QString tblname);
|
||
bool createTable(const QSharedPointer<Entity> &entity);
|
||
qint8 count(const QSharedPointer<Entity> &entity, bool ignoreID = true);
|
||
qint8 count(const QString &tableName);
|
||
//template<class T> count(QHash<QString, QString> condition = QHash<QString, QString>());
|
||
QSharedPointer<Database> getDb() const;
|
||
void setDb(const QSharedPointer<Database> &value);
|
||
QSharedPointer<Schema> getSchema() const;
|
||
void setSchema(const QSharedPointer<Schema> &value);
|
||
|
||
};
|
||
}
|
||
#endif // ENTITYMANAGER_H
|
Auch abrufbar als: Unified diff
using cache