Database error handling.

print
Josef Rokos 9 years ago
parent 3d9e182531
commit 1f4f318a26

@ -5,6 +5,7 @@
#include <autoform.h> #include <autoform.h>
#include "data/person.h" #include "data/person.h"
#include "accommodation-odb.hxx"
namespace Ui { namespace Ui {
class AcForm; class AcForm;

@ -3,14 +3,14 @@
Person::Person() Person::Person()
{ {
} }
int Person::getId() const int Person::id() const
{ {
return id; return m_id;
} }
void Person::setId(int value) void Person::setId(int value)
{ {
id = value; m_id = value;
} }
QString Person::getFirstName() const QString Person::getFirstName() const
{ {

@ -16,7 +16,7 @@ class Person : public QObject
public: public:
Person(); Person();
int getId() const; int id() const;
void setId(int value); void setId(int value);
QString getFirstName() const; QString getFirstName() const;
@ -28,7 +28,7 @@ public:
private: private:
friend class odb::access; friend class odb::access;
#pragma db id auto #pragma db id auto
int id; int m_id;
QString firstName; QString firstName;
QString lastName; QString lastName;

@ -8,10 +8,13 @@
#include <QMetaMethod> #include <QMetaMethod>
#include <QDebug> #include <QDebug>
#include <QVariant> #include <QVariant>
#include <QMessageBox>
#include "iform.h" #include "iform.h"
#include "service.h" #include "service.h"
#include "ivalidator.h" #include "ivalidator.h"
#include "iservice.h"
#include "iplugin.h"
template <class T> template <class T>
@ -20,6 +23,8 @@ class AutoForm : public IForm
public: public:
explicit AutoForm(QWidget *parent = 0) : IForm(parent) { explicit AutoForm(QWidget *parent = 0) : IForm(parent) {
m_newRec = false; m_newRec = false;
m_serviceConnected = false;
m_saved = false;
} }
virtual ~AutoForm() { virtual ~AutoForm() {
@ -93,14 +98,54 @@ public slots:
return false; return false;
} }
if (!m_serviceConnected)
{
connect(service(), &IService::dbError, [this](QString msg) {
QMessageBox::critical(this, tr("Database error"), tr(msg.toStdString().c_str()));
m_saved = false;
});
connect(service(), &IService::dataChanged, [this]() {
m_saved = true;
});
m_serviceConnected = true;
}
if (m_newRec) { if (m_newRec) {
service()->save(entity());
if (m_saved)
{
emit recordAdded(); emit recordAdded();
}
} else { } else {
service()->update(entity());
if (m_saved)
{
emit recordUpdated(); emit recordUpdated();
} }
}
return true; return m_saved;
} }
private:
Service<T> *service() {
IPlugin *plugin = Context::instance().plugin(pluginId());
if (plugin == NULL) {
Q_ASSERT(false);
return NULL;
}
Service<T> *service = plugin->service<T>();
if (service == NULL) {
Q_ASSERT(false);
return NULL;
}
return service;
}
bool m_serviceConnected;
bool m_saved;
}; };
#endif // AUTOFORM_H #endif // AUTOFORM_H

@ -130,6 +130,11 @@ void Context::setCurrentUser(const QSharedPointer<User> &currentUser)
m_currentUser = currentUser; m_currentUser = currentUser;
} }
odb::session &Context::session()
{
return m_session;
}
void Context::checkDb(const QString &path) void Context::checkDb(const QString &path)
{ {
{ {

@ -37,6 +37,8 @@ public:
QSharedPointer<User> currentUser() const; QSharedPointer<User> currentUser() const;
void setCurrentUser(const QSharedPointer<User> &currentUser); void setCurrentUser(const QSharedPointer<User> &currentUser);
odb::session &session();
private: private:
Context(); Context();
QList<IPlugin*> m_plugins; QList<IPlugin*> m_plugins;

@ -39,3 +39,9 @@ void FormDialog::accept()
QDialog::accept(); QDialog::accept();
} }
} }
void FormDialog::reject()
{
m_form->refresh();
QDialog::reject();
}

@ -31,6 +31,10 @@ private slots:
// QDialog interface // QDialog interface
public slots: public slots:
void accept(); void accept();
// QDialog interface
public slots:
void reject() override;
}; };
#endif // FORMDIALOG_H #endif // FORMDIALOG_H

@ -10,8 +10,8 @@
#include "autotablemodel.h" #include "autotablemodel.h"
#include "context.h" #include "context.h"
#include "iplugin.h" #include "iplugin.h"
#include "igridform.h" #include "igridform.h"
#include "iservice.h"
template<class T> template<class T>
class GridForm : public IGridForm class GridForm : public IGridForm
@ -20,14 +20,15 @@ class GridForm : public IGridForm
public: public:
explicit GridForm(QWidget *parent = 0) : explicit GridForm(QWidget *parent = 0) :
IGridForm(parent) IGridForm(parent)
{ {
m_form = NULL; m_serviceConnected = false;
m_tableModel = NULL; m_tableModel = NULL;
m_formHandler = new DefaultFormHandler(); m_formHandler = new DefaultFormHandler();
m_filterUi = new FilterUi(this, new T); m_filterUi = new FilterUi(this, new T);
filterWidget()->layout()->addWidget(m_filterUi); filterWidget()->layout()->addWidget(m_filterUi);
} }
virtual ~GridForm() virtual ~GridForm()
{ {
if (m_form != NULL && m_form->parent() == NULL) if (m_form != NULL && m_form->parent() == NULL)
@ -43,20 +44,26 @@ public:
delete m_formHandler; delete m_formHandler;
} }
void setForm(AutoForm<T> *form) { void setForm(AutoForm<T> *aForm) {
Q_ASSERT(m_form == NULL); Q_ASSERT(m_form == NULL);
m_form = form; m_form = aForm;
connect(m_form, &IForm::recordAdded, [this](){ connect(m_form, &IForm::recordAdded, [this](){
service()->save(m_form->entity()); //service()->save(form()->entity());
m_tableModel->addRow(m_form->entity()); m_tableModel->addRow(form()->entity());
emit dataChanged(); emit dataChanged();
}); });
connect(m_form, &IForm::recordUpdated, [this](){ connect(m_form, &IForm::recordUpdated, [this](){
service()->update(m_form->entity()); //service()->update(form()->entity());
emit dataChanged(); emit dataChanged();
}); });
connect(m_form, &IForm::refreshEntity, [this](){
if (m_tableModel != NULL) {
m_tableModel->setItemToIndex(tableView()->currentIndex(),
service()->reload(m_tableModel->itemFromIndex(tableView()->currentIndex())->id()));
}
});
} }
void setTableModel(AutoTableModel<T> *tableModel) { void setTableModel(AutoTableModel<T> *tableModel) {
@ -79,6 +86,8 @@ public slots:
return; return;
} }
connectService();
m_tableModel->setData(service()->all()); m_tableModel->setData(service()->all());
tableView()->setModel(m_tableModel); tableView()->setModel(m_tableModel);
@ -93,7 +102,6 @@ public slots:
} }
private: private:
AutoForm<T> *m_form;
AutoTableModel<T> *m_tableModel; AutoTableModel<T> *m_tableModel;
IFormHandler *m_formHandler; IFormHandler *m_formHandler;
@ -113,6 +121,26 @@ private:
return service; return service;
} }
AutoForm<T> *form() {
return (AutoForm<T>*)m_form;
}
void connectService() {
if (!m_serviceConnected)
{
connect(service(), &IService::dbErrorRead, [this](QString msg) {
QMessageBox::critical(this, tr("Database error"), tr(msg.toStdString().c_str()));
});
connect(service(), &IService::dbErrorDelete, [this](QString msg) {
QMessageBox::critical(this, tr("Database error"), tr(msg.toStdString().c_str()));
});
m_serviceConnected = true;
}
}
bool m_serviceConnected;
// IGridForm interface // IGridForm interface
protected: protected:
void handleNewRecord() override void handleNewRecord() override
@ -123,8 +151,8 @@ protected:
return; return;
} }
m_form->setEntity(QSharedPointer<T>(new T())); form()->setEntity(QSharedPointer<T>(new T()));
m_form->setNewRec(true); form()->setNewRec(true);
m_formHandler->showForm(m_form); m_formHandler->showForm(m_form);
} }
@ -136,13 +164,14 @@ protected:
return; return;
} }
m_form->setEntity(m_tableModel->itemFromIndex(tableView()->currentIndex())); form()->setEntity(m_tableModel->itemFromIndex(tableView()->currentIndex()));
m_form->setNewRec(false); form()->setNewRec(false);
m_formHandler->showForm(m_form); m_formHandler->showForm(m_form);
} }
void handleDeleteRecord() override void handleDeleteRecord() override
{ {
connectService();
if (m_form == NULL || m_tableModel == NULL || tableView()->currentIndex().row() < 0) if (m_form == NULL || m_tableModel == NULL || tableView()->currentIndex().row() < 0)
{ {
Q_ASSERT(false); Q_ASSERT(false);

@ -8,3 +8,18 @@ IForm::~IForm()
{ {
} }
QString IForm::pluginId() const
{
return m_pluginId;
}
void IForm::setPluginId(const QString &pluginId)
{
m_pluginId = pluginId;
}
void IForm::refresh()
{
emit refreshEntity();
}

@ -2,6 +2,7 @@
#define IFORM_H #define IFORM_H
#include <QWidget> #include <QWidget>
#include <QString>
#include "core_global.h" #include "core_global.h"
@ -13,13 +14,21 @@ public:
explicit IForm(QWidget *parent = 0); explicit IForm(QWidget *parent = 0);
virtual ~IForm(); virtual ~IForm();
QString pluginId() const;
void setPluginId(const QString &pluginId);
signals: signals:
void recordAdded(); void recordAdded();
void recordUpdated(); void recordUpdated();
void validationError(const QString &errMessage); void validationError(const QString &errMessage);
void refreshEntity();
public slots: public slots:
virtual bool saveRecord() = 0; virtual bool saveRecord() = 0;
void refresh();
private:
QString m_pluginId;
}; };
#endif // IFORM_H #endif // IFORM_H

@ -14,6 +14,7 @@ IGridForm::IGridForm(QWidget *parent) :
ui->filterWidget->setVisible(false); ui->filterWidget->setVisible(false);
m_contextMenu = new QMenu(this); m_contextMenu = new QMenu(this);
m_contextMenu->addAction(ui->actionSelectColumns); m_contextMenu->addAction(ui->actionSelectColumns);
m_form = NULL;
m_columnDialog = new ColumnDialog(this); m_columnDialog = new ColumnDialog(this);
connect(m_columnDialog, SIGNAL(accepted()), this, SLOT(columnsAccepted())); connect(m_columnDialog, SIGNAL(accepted()), this, SLOT(columnsAccepted()));
@ -32,6 +33,11 @@ void IGridForm::setPluginId(const QString &pluginId)
{ {
m_filterUi->setPluginId(pluginId); m_filterUi->setPluginId(pluginId);
} }
if (m_form != NULL)
{
m_form->setPluginId(pluginId);
}
} }
QString IGridForm::pluginId() QString IGridForm::pluginId()

@ -60,6 +60,7 @@ private:
protected: protected:
FilterUi *m_filterUi; FilterUi *m_filterUi;
IForm *m_form;
}; };
#endif // IGRIDFORM_H #endif // IGRIDFORM_H

@ -16,8 +16,13 @@ public:
signals: signals:
void dbError(QString errMsg); void dbError(QString errMsg);
void dbErrorRead(QString errMsg);
void dbErrorInsert(QString errMsg);
void dbErrorUpdate(QString errMsg);
void dbErrorDelete(QString errMsg);
void dataChanged(); void dataChanged();
void permissionDenied(); void updateDenied();
void readDenied();
public slots: public slots:
}; };

@ -67,6 +67,7 @@ public:
catch (const odb::exception &ex) catch (const odb::exception &ex)
{ {
emit dbError(ex.what()); emit dbError(ex.what());
emit dbErrorUpdate(ex.what());
return; return;
} }
@ -88,6 +89,8 @@ public:
catch (const odb::exception &ex) catch (const odb::exception &ex)
{ {
emit dbError(ex.what()); emit dbError(ex.what());
emit dbErrorInsert(ex.what());
return;
} }
emit dataChanged(); emit dataChanged();
@ -109,11 +112,21 @@ public:
catch (const odb::exception &ex) catch (const odb::exception &ex)
{ {
emit dbError(ex.what()); emit dbError(ex.what());
emit dbErrorRead(ex.what());
} }
return entity; return entity;
} }
QSharedPointer<T> reload(int id) {
odb::database *db = Context::instance().db();
Q_ASSERT(db);
Context::instance().session().cache_erase<T>(*db, id);
return loadById(id);
}
void erase(QSharedPointer<T> entity) { void erase(QSharedPointer<T> entity) {
odb::database *db = Context::instance().db(); odb::database *db = Context::instance().db();
@ -129,6 +142,7 @@ public:
catch (const odb::exception &ex) catch (const odb::exception &ex)
{ {
emit dbError(ex.what()); emit dbError(ex.what());
emit dbErrorDelete(ex.what());
} }
} }

Loading…
Cancel
Save