Added IService QObject derived class for emiting Qt signals from service objects.

This commit is contained in:
2016-02-09 16:05:02 +01:00
parent bc77f671ae
commit 3d9e182531
5 changed files with 106 additions and 18 deletions
+64 -15
View File
@@ -12,14 +12,15 @@
#include "core_global.h"
#include "context.h"
#include "iservice.h"
#include "transaction.h"
template<class T>
class Service
class Service : public IService
{
public:
Service() { }
explicit Service(QObject *parent = NULL) :IService(parent) { }
explicit Service(const QString &pluginId) {
m_pluginId = pluginId;
@@ -31,14 +32,23 @@ public:
Q_ASSERT(db);
Transaction tx;
odb::result<T> res = db->template query<T>();
QList<QSharedPointer<T> > ret;
for (typename odb::result<T>::iterator it = res.begin(); it != res.end(); it++) {
ret.append(it.load());
try
{
odb::result<T> res = db->template query<T>();
for (typename odb::result<T>::iterator it = res.begin(); it != res.end(); it++) {
ret.append(it.load());
}
tx.commit();
}
catch (const odb::exception &ex)
{
emit dbError(ex.what());
}
tx.commit();
return ret;
}
@@ -48,8 +58,19 @@ public:
Q_ASSERT(db);
Transaction tx;
db->persist(entity);
tx.commit();
try
{
db->persist(entity);
tx.commit();
}
catch (const odb::exception &ex)
{
emit dbError(ex.what());
return;
}
emit dataChanged();
}
void update(QSharedPointer<T> entity) {
@@ -58,8 +79,18 @@ public:
Q_ASSERT(db);
Transaction tx;
db->update(entity);
tx.commit();
try
{
db->update(entity);
tx.commit();
}
catch (const odb::exception &ex)
{
emit dbError(ex.what());
}
emit dataChanged();
}
QSharedPointer<T> loadById(int id) {
@@ -68,8 +99,18 @@ public:
Q_ASSERT(db);
Transaction tx;
QSharedPointer<T> entity = db->template load<T>(id);
tx.commit();
QSharedPointer<T> entity;
try
{
entity = db->template load<T>(id);
tx.commit();
}
catch (const odb::exception &ex)
{
emit dbError(ex.what());
}
return entity;
}
@@ -79,8 +120,16 @@ public:
Q_ASSERT(db);
Transaction tx;
db->erase(entity);
tx.commit();
try
{
db->erase(entity);
tx.commit();
}
catch (const odb::exception &ex)
{
emit dbError(ex.what());
}
}
void setPluginId(const QString &pluginId) {