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

print
Josef Rokos 9 years ago
parent bc77f671ae
commit 3d9e182531

@ -42,7 +42,8 @@ SOURCES += \
samestringvalidator.cpp \ samestringvalidator.cpp \
savefilterdialog.cpp \ savefilterdialog.cpp \
filterdialog.cpp \ filterdialog.cpp \
itablemodel.cpp itablemodel.cpp \
iservice.cpp
HEADERS += core.h\ HEADERS += core.h\
core_global.h \ core_global.h \
@ -82,7 +83,8 @@ HEADERS += core.h\
savefilterdialog.h \ savefilterdialog.h \
filterdialog.h \ filterdialog.h \
itablemodel.h \ itablemodel.h \
data/core_global.h data/core_global.h \
iservice.h
unix { unix {
target.path = /usr/lib target.path = /usr/lib

@ -58,7 +58,7 @@ public:
protected: protected:
QWidget *m_ui; QWidget *m_ui;
void *m_service; IService *m_service;
}; };
#define PluginInterface_iid "cz.itsolved.prodejna.IPlugin" #define PluginInterface_iid "cz.itsolved.prodejna.IPlugin"

@ -0,0 +1,12 @@
#include "iservice.h"
IService::IService(QObject *parent) : QObject(parent)
{
}
IService::~IService()
{
}

@ -0,0 +1,25 @@
#ifndef ISERVICE_H
#define ISERVICE_H
#include <QObject>
#include <QString>
#include "core_global.h"
class CORESHARED_EXPORT IService : public QObject
{
Q_OBJECT
public:
explicit IService(QObject *parent = 0);
virtual ~IService();
signals:
void dbError(QString errMsg);
void dataChanged();
void permissionDenied();
public slots:
};
#endif // ISERVICE_H

@ -12,14 +12,15 @@
#include "core_global.h" #include "core_global.h"
#include "context.h" #include "context.h"
#include "iservice.h"
#include "transaction.h" #include "transaction.h"
template<class T> template<class T>
class Service class Service : public IService
{ {
public: public:
Service() { } explicit Service(QObject *parent = NULL) :IService(parent) { }
explicit Service(const QString &pluginId) { explicit Service(const QString &pluginId) {
m_pluginId = pluginId; m_pluginId = pluginId;
@ -31,14 +32,23 @@ public:
Q_ASSERT(db); Q_ASSERT(db);
Transaction tx; Transaction tx;
odb::result<T> res = db->template query<T>();
QList<QSharedPointer<T> > ret; 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; return ret;
} }
@ -48,8 +58,19 @@ public:
Q_ASSERT(db); Q_ASSERT(db);
Transaction tx; 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) { void update(QSharedPointer<T> entity) {
@ -58,8 +79,18 @@ public:
Q_ASSERT(db); Q_ASSERT(db);
Transaction tx; 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) { QSharedPointer<T> loadById(int id) {
@ -68,8 +99,18 @@ public:
Q_ASSERT(db); Q_ASSERT(db);
Transaction tx; Transaction tx;
QSharedPointer<T> entity = db->template load<T>(id); QSharedPointer<T> entity;
tx.commit();
try
{
entity = db->template load<T>(id);
tx.commit();
}
catch (const odb::exception &ex)
{
emit dbError(ex.what());
}
return entity; return entity;
} }
@ -79,8 +120,16 @@ public:
Q_ASSERT(db); Q_ASSERT(db);
Transaction tx; 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) { void setPluginId(const QString &pluginId) {

Loading…
Cancel
Save