Revision 11bbe9a6
Von Christian Ehringfeld vor mehr als 10 Jahren hinzugefügt
| src/cache.cpp | ||
|---|---|---|
|
Cache::Cache() {
|
||
|
|
||
|
}
|
||
|
QHash<QString, QWeakPointer<Entity> > Cache::getCache() const {
|
||
|
return cache;
|
||
|
}
|
||
|
|
||
|
bool Cache::contains(qint64 id, const QString &classname) {
|
||
|
QString key = this->generateKey(id, classname);
|
||
|
return this->contains(key);
|
||
|
}
|
||
|
|
||
|
bool Cache::contains(const QString &key) {
|
||
|
if (!key.isEmpty()) {
|
||
|
return this->cache.contains(key);
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
void Cache::insert(QSharedPointer<Entity> entity) {
|
||
|
if (entity.data() && entity.data()->getId() > -1) {
|
||
|
this->cache.insert(this->generateKey(entity.data()->getId(), QString(entity.data()->getClassname())),
|
||
|
entity.toWeakRef());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void Cache::remove(QSharedPointer<Entity> entity) {
|
||
|
if (entity.data() && entity.data()->getId() > -1) {
|
||
|
this->remove(entity.data()->getId(), QString(entity.data()->getClassname()));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void Cache::remove(const qint64 &id, const QString &classname) {
|
||
|
this->cache.remove(this->generateKey(id, classname));
|
||
|
}
|
||
|
|
||
|
QSharedPointer<Entity> Cache::get(qint64 id, const QString &classname) {
|
||
|
QString key = this->generateKey(id, classname);
|
||
|
if (this->contains(key)) {
|
||
|
return this->cache.value(key).toStrongRef();
|
||
|
}
|
||
|
return QSharedPointer<Entity>();
|
||
|
}
|
||
|
|
||
|
QString Cache::generateKey(qint64 id, const QString &classname) {
|
||
|
if (id > -1) {
|
||
|
return QString::number(id).append("[").append(classname).append("]");
|
||
|
}
|
||
|
return QString("");
|
||
|
}
|
||
|
|
||
| src/cache.h | ||
|---|---|---|
|
#ifndef CACHE_H
|
||
|
#define CACHE_H
|
||
|
#include <QHash>
|
||
|
#include <QWeakPointer>
|
||
|
#include <QSharedPointer>
|
||
|
#include "entityinstancefactory.h"
|
||
|
#include "entity.h"
|
||
|
|
||
|
namespace CuteEntityManager {
|
||
|
class Entity;
|
||
|
class Cache {
|
||
|
public:
|
||
|
Cache();
|
||
|
QHash<QString, QWeakPointer<Entity> > getCache() const;
|
||
|
bool contains(qint64 id, const QString &classname);
|
||
|
bool contains(const QString &key);
|
||
|
template<class T> bool contains(qint64 id) {
|
||
|
bool ok = false;
|
||
|
Entity *e = EntityInstanceFactory::createInstance<T>();
|
||
|
if (e) {
|
||
|
ok = this->contains(id, QString(e->getClassname()));
|
||
|
delete e;
|
||
|
}
|
||
|
return ok;
|
||
|
}
|
||
|
void insert(QSharedPointer<Entity> entity);
|
||
|
void remove(QSharedPointer<Entity> entity);
|
||
|
void remove(const qint64 &id, const QString &classname);
|
||
|
template<class T> void remove(qint64 id) {
|
||
|
Entity *e = EntityInstanceFactory::createInstance<T>();
|
||
|
if (e) {
|
||
|
this->remove(id, QString(e->getClassname()));
|
||
|
delete e;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
QSharedPointer<Entity> get(qint64 id, const QString &classname);
|
||
|
template<class T> QSharedPointer<Entity> get(qint64 id) {
|
||
|
Entity *e = EntityInstanceFactory::createInstance<T>();
|
||
|
if (e) {
|
||
|
return this->get(id, QString(e->getClassname()));
|
||
|
delete e;
|
||
|
}
|
||
|
return QSharedPointer<Entity>();
|
||
|
}
|
||
|
|
||
|
protected:
|
||
|
QString generateKey(qint64 id, const QString &classname);
|
||
|
private:
|
||
|
QHash<QString, QWeakPointer<Entity>> cache;
|
||
|
};
|
||
|
}
|
||
|
|
||
| src/database.cpp | ||
|---|---|---|
|
bool Database::transaction(const QString &query) {
|
||
|
bool rc = false;
|
||
|
if (supportTransactions) {
|
||
|
this->database.transaction();
|
||
|
this->startTransaction();
|
||
|
QSqlQuery sqlquery = QSqlQuery(this->database);
|
||
|
sqlquery.exec(query);
|
||
|
if (!this->database.commit()) {
|
||
|
this->database.rollback();
|
||
|
}
|
||
|
this->commitTransaction();
|
||
|
} else {
|
||
|
rc = this->exec(query);
|
||
|
}
|
||
| ... | ... | |
|
for (int var = 0; var < queries.size(); ++var) {
|
||
|
sqlquery.exec(queries.at(var));
|
||
|
}
|
||
|
if (!this->database.commit()) {
|
||
|
this->database.rollback();
|
||
|
} else {
|
||
|
ok = true;
|
||
|
}
|
||
|
ok = this->commitTransaction();
|
||
|
} else {
|
||
|
ok = this->exec(queries);
|
||
|
}
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
bool Database::transaction(QSqlQuery &query) {
|
||
|
this->database.transaction();
|
||
|
this->startTransaction();
|
||
|
query.exec();
|
||
|
this->debugQuery(query);
|
||
|
if (!this->database.commit()) {
|
||
|
this->database.rollback();
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
return this->commitTransaction();
|
||
|
}
|
||
|
|
||
|
bool Database::transaction(QList<QSqlQuery> &queries) {
|
||
|
this->database.transaction();
|
||
|
this->startTransaction();
|
||
|
QSqlQuery q;
|
||
|
for (int var = 0; var < queries.size(); ++var) {
|
||
|
q = queries.at(var);
|
||
|
q.exec();
|
||
|
this->debugQuery(q);
|
||
|
}
|
||
|
if (!this->database.commit()) {
|
||
|
return this->commitTransaction();
|
||
|
}
|
||
|
|
||
|
|
||
|
bool Database::commitTransaction() {
|
||
|
if (this->supportTransactions && !this->database.commit()) {
|
||
|
this->database.rollback();
|
||
|
return false;
|
||
|
}
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
bool Database::exec(const QString &query) {
|
||
|
this->database.transaction();
|
||
|
QSqlQuery q = QSqlQuery(this->database);
|
||
|
q.exec(query);
|
||
|
bool ok = q.exec(query);
|
||
|
this->debugQuery(q);
|
||
|
if (!this->database.commit()) {
|
||
|
this->database.rollback();
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
return ok;
|
||
|
}
|
||
|
|
||
|
bool Database::exec(QStringList queries) {
|
||
| ... | ... | |
|
return q;
|
||
|
}
|
||
|
|
||
|
void Database::startTransaction() {
|
||
|
this->database.transaction();
|
||
|
}
|
||
|
|
||
|
QSqlDatabase Database::getDatabase() {
|
||
|
return this->database;
|
||
|
}
|
||
| src/database.h | ||
|---|---|---|
|
void debugQuery(const QSqlQuery &query) const;
|
||
|
bool select(QSqlQuery &query);
|
||
|
QSqlQuery select(const QString &query);
|
||
|
void startTransaction();
|
||
|
bool commitTransaction();
|
||
|
|
||
|
};
|
||
|
}
|
||
| src/entitymanager.h | ||
|---|---|---|
|
e->setId(id);
|
||
|
return this->findEntity(ptr);
|
||
|
}
|
||
|
template<class T> QSharedPointer<Entity> findEntityByAttributes(QHash<QString, QString> attributes) {
|
||
|
auto list = this->findAllEntitiesByAttributes<T>(attributes,1,0);
|
||
|
if(list.isEmpty()) {
|
||
|
return QSharedPointer<Entity>();
|
||
|
}
|
||
|
return list.at(0);
|
||
|
template<class T> QSharedPointer<Entity> findEntityByAttributes(const QHash<QString, QString> &attributes) {
|
||
|
auto list = this->findAllEntitiesByAttributes<T>(attributes, 1, 0);
|
||
|
if (list.isEmpty()) {
|
||
|
return QSharedPointer<Entity>();
|
||
|
}
|
||
|
return list.at(0);
|
||
|
}
|
||
|
|
||
|
template<class T> QList<QSharedPointer<Entity>> findAllEntitiesByAttributes(QHash<QString, QString> attributes = QHash<QString, QString>(),quint32 limit = 0, quint32 offset = 0) {
|
||
|
template<class T> QList<QSharedPointer<Entity>> findAllEntitiesByAttributes(const QHash<QString, QString> &attributes =
|
||
|
QHash<QString, QString>(), quint32 limit = 0, quint32 offset = 0) {
|
||
|
auto list = this->findAllEntitiesByAttributes<T>(attributes);
|
||
|
return list;
|
||
|
}
|
||
|
|
||
|
template<class T> QList<QSharedPointer<Entity>> findEntitiesBySql(const QString &sql) {
|
||
|
Entity *e = EntityInstanceFactory::createInstance<T>();
|
||
|
if (e) {
|
||
|
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->getQuery();
|
||
|
q = this->db.data()->select(sql);
|
||
|
auto result = this->convertQueryResult(q);
|
||
|
auto ret = this->convert(result, e->getClassname());
|
||
|
delete e;
|
||
|
return ret;
|
||
|
}
|
||
|
return QList<QSharedPointer<Entity>>();
|
||
|
}
|
||
|
|
||
|
bool create(QList<QSharedPointer<Entity>> entities);
|
||
| ... | ... | |
|
bool save(QSharedPointer<Entity> &entity);
|
||
|
qint64 findId(QSharedPointer<Entity> &entity);
|
||
|
bool merge(QSharedPointer<Entity> &entity, bool withRelations = true);
|
||
|
template<class T> bool remove(QList<qint64> ids) {
|
||
|
template<class T> bool remove(const QList<qint64> &ids) {
|
||
|
bool ok = true;
|
||
|
foreach (qint64 var, ids) {
|
||
|
if (!this->remove<T>(var)) {
|
||
|
ok = false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
return ok;
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
bool remove(QSharedPointer<Entity> &entity);
|
||
|
bool removeAll(QString tblname);
|
||
|
bool createTable(const QSharedPointer<Entity> &entity);
|
||
| src/querybuilder.cpp | ||
|---|---|---|
|
return " LIMIT " + QString(limit) + (offset > 0 ? QString("," + offset) : "");
|
||
|
}
|
||
|
|
||
|
QSqlQuery QueryBuilder::getQuery() const {
|
||
|
return this->database.data()->getQuery();
|
||
|
}
|
||
|
|
||
|
QHash<QString, QVariant> QueryBuilder::saveAttributes(const QSharedPointer<Entity> &entity) const {
|
||
|
auto props = entity.data()->getMetaProperties();
|
||
|
auto values = this->getEntityAttributes(props, entity);
|
||
| src/querybuilder.h | ||
|---|---|---|
|
QSqlQuery merge(const QSharedPointer<Entity> &entity) const;
|
||
|
QSqlQuery create(const QSharedPointer<Entity> &entity) const;
|
||
|
virtual QString limit(const qint8 limit, const qint64 offset) const;
|
||
|
QSqlQuery getQuery() const;
|
||
|
|
||
|
protected:
|
||
|
void insertRelationId(const Entity *e, QHash<QString, QVariant> &map, QString relName) const;
|
||
Auch abrufbar als: Unified diff
cache stuff