commit 11bbe9a6120fc2e5c135f9ecb89d15cc0cd24a52
Author: Christian Ehringfeld <c.ehringfeld@t-online.de>
Date:   Sat May 9 01:04:35 2015 +0200

    cache stuff

diff --git a/src/cache.cpp b/src/cache.cpp
index abad713..a61f8eb 100644
--- a/src/cache.cpp
+++ b/src/cache.cpp
@@ -3,4 +3,51 @@ using namespace CuteEntityManager;
 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("");
+}
 
diff --git a/src/cache.h b/src/cache.h
index 1c2a890..047dc95 100644
--- a/src/cache.h
+++ b/src/cache.h
@@ -1,10 +1,53 @@
 #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;
 };
 }
 
diff --git a/src/database.cpp b/src/database.cpp
index 484d9e7..9069c3c 100644
--- a/src/database.cpp
+++ b/src/database.cpp
@@ -62,12 +62,10 @@ QString Database::getConnectionName() {
 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);
     }
@@ -93,11 +91,7 @@ bool Database::transaction(const QStringList &queries) {
         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);
     }
@@ -105,25 +99,26 @@ bool Database::transaction(const QStringList &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;
     }
@@ -131,15 +126,10 @@ bool Database::transaction(QList<QSqlQuery> &queries) {
 }
 
 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) {
@@ -193,6 +183,10 @@ QSqlQuery Database::select(const QString &query) {
     return q;
 }
 
+void Database::startTransaction() {
+    this->database.transaction();
+}
+
 QSqlDatabase Database::getDatabase() {
     return this->database;
 }
diff --git a/src/database.h b/src/database.h
index d7fbb3d..626421c 100644
--- a/src/database.h
+++ b/src/database.h
@@ -58,6 +58,8 @@ class Database {
     void debugQuery(const QSqlQuery &query) const;
     bool select(QSqlQuery &query);
     QSqlQuery select(const QString &query);
+    void startTransaction();
+    bool commitTransaction();
 
 };
 }
diff --git a/src/entitymanager.h b/src/entitymanager.h
index 8b1be9c..a20b0a6 100644
--- a/src/entitymanager.h
+++ b/src/entitymanager.h
@@ -81,16 +81,31 @@ class EntityManager {
         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);
@@ -98,9 +113,24 @@ class EntityManager {
     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);
diff --git a/src/querybuilder.cpp b/src/querybuilder.cpp
index cec9042..650ccc5 100644
--- a/src/querybuilder.cpp
+++ b/src/querybuilder.cpp
@@ -371,6 +371,10 @@ QString QueryBuilder::limit(const qint8 limit, const qint64 offset) const {
     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);
diff --git a/src/querybuilder.h b/src/querybuilder.h
index 22c060e..ff60de8 100644
--- a/src/querybuilder.h
+++ b/src/querybuilder.h
@@ -57,6 +57,7 @@ class QueryBuilder {
     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;
