Database error handling.
This commit is contained in:
+50
-5
@@ -8,10 +8,13 @@
|
||||
#include <QMetaMethod>
|
||||
#include <QDebug>
|
||||
#include <QVariant>
|
||||
#include <QMessageBox>
|
||||
|
||||
#include "iform.h"
|
||||
#include "service.h"
|
||||
#include "ivalidator.h"
|
||||
#include "iservice.h"
|
||||
#include "iplugin.h"
|
||||
|
||||
|
||||
template <class T>
|
||||
@@ -20,6 +23,8 @@ class AutoForm : public IForm
|
||||
public:
|
||||
explicit AutoForm(QWidget *parent = 0) : IForm(parent) {
|
||||
m_newRec = false;
|
||||
m_serviceConnected = false;
|
||||
m_saved = false;
|
||||
}
|
||||
|
||||
virtual ~AutoForm() {
|
||||
@@ -93,14 +98,54 @@ public slots:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_newRec) {
|
||||
emit recordAdded();
|
||||
} else {
|
||||
emit recordUpdated();
|
||||
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;
|
||||
}
|
||||
|
||||
return true;
|
||||
if (m_newRec) {
|
||||
service()->save(entity());
|
||||
if (m_saved)
|
||||
{
|
||||
emit recordAdded();
|
||||
}
|
||||
} else {
|
||||
service()->update(entity());
|
||||
if (m_saved)
|
||||
{
|
||||
emit recordUpdated();
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -130,6 +130,11 @@ void Context::setCurrentUser(const QSharedPointer<User> ¤tUser)
|
||||
m_currentUser = currentUser;
|
||||
}
|
||||
|
||||
odb::session &Context::session()
|
||||
{
|
||||
return m_session;
|
||||
}
|
||||
|
||||
void Context::checkDb(const QString &path)
|
||||
{
|
||||
{
|
||||
|
||||
@@ -37,6 +37,8 @@ public:
|
||||
QSharedPointer<User> currentUser() const;
|
||||
void setCurrentUser(const QSharedPointer<User> ¤tUser);
|
||||
|
||||
odb::session &session();
|
||||
|
||||
private:
|
||||
Context();
|
||||
QList<IPlugin*> m_plugins;
|
||||
|
||||
@@ -39,3 +39,9 @@ void FormDialog::accept()
|
||||
QDialog::accept();
|
||||
}
|
||||
}
|
||||
|
||||
void FormDialog::reject()
|
||||
{
|
||||
m_form->refresh();
|
||||
QDialog::reject();
|
||||
}
|
||||
|
||||
@@ -31,6 +31,10 @@ private slots:
|
||||
// QDialog interface
|
||||
public slots:
|
||||
void accept();
|
||||
|
||||
// QDialog interface
|
||||
public slots:
|
||||
void reject() override;
|
||||
};
|
||||
|
||||
#endif // FORMDIALOG_H
|
||||
|
||||
+47
-18
@@ -10,8 +10,8 @@
|
||||
#include "autotablemodel.h"
|
||||
#include "context.h"
|
||||
#include "iplugin.h"
|
||||
|
||||
#include "igridform.h"
|
||||
#include "iservice.h"
|
||||
|
||||
template<class T>
|
||||
class GridForm : public IGridForm
|
||||
@@ -20,14 +20,15 @@ class GridForm : public IGridForm
|
||||
public:
|
||||
explicit GridForm(QWidget *parent = 0) :
|
||||
IGridForm(parent)
|
||||
{
|
||||
m_form = NULL;
|
||||
m_tableModel = NULL;
|
||||
m_formHandler = new DefaultFormHandler();
|
||||
{
|
||||
m_serviceConnected = false;
|
||||
m_tableModel = NULL;
|
||||
m_formHandler = new DefaultFormHandler();
|
||||
|
||||
m_filterUi = new FilterUi(this, new T);
|
||||
filterWidget()->layout()->addWidget(m_filterUi);
|
||||
}
|
||||
|
||||
m_filterUi = new FilterUi(this, new T);
|
||||
filterWidget()->layout()->addWidget(m_filterUi);
|
||||
}
|
||||
virtual ~GridForm()
|
||||
{
|
||||
if (m_form != NULL && m_form->parent() == NULL)
|
||||
@@ -43,20 +44,26 @@ public:
|
||||
delete m_formHandler;
|
||||
}
|
||||
|
||||
void setForm(AutoForm<T> *form) {
|
||||
void setForm(AutoForm<T> *aForm) {
|
||||
Q_ASSERT(m_form == NULL);
|
||||
|
||||
m_form = form;
|
||||
m_form = aForm;
|
||||
|
||||
connect(m_form, &IForm::recordAdded, [this](){
|
||||
service()->save(m_form->entity());
|
||||
m_tableModel->addRow(m_form->entity());
|
||||
//service()->save(form()->entity());
|
||||
m_tableModel->addRow(form()->entity());
|
||||
emit dataChanged();
|
||||
});
|
||||
connect(m_form, &IForm::recordUpdated, [this](){
|
||||
service()->update(m_form->entity());
|
||||
//service()->update(form()->entity());
|
||||
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) {
|
||||
@@ -79,6 +86,8 @@ public slots:
|
||||
return;
|
||||
}
|
||||
|
||||
connectService();
|
||||
|
||||
m_tableModel->setData(service()->all());
|
||||
tableView()->setModel(m_tableModel);
|
||||
|
||||
@@ -93,7 +102,6 @@ public slots:
|
||||
}
|
||||
|
||||
private:
|
||||
AutoForm<T> *m_form;
|
||||
AutoTableModel<T> *m_tableModel;
|
||||
IFormHandler *m_formHandler;
|
||||
|
||||
@@ -113,6 +121,26 @@ private:
|
||||
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
|
||||
protected:
|
||||
void handleNewRecord() override
|
||||
@@ -123,8 +151,8 @@ protected:
|
||||
return;
|
||||
}
|
||||
|
||||
m_form->setEntity(QSharedPointer<T>(new T()));
|
||||
m_form->setNewRec(true);
|
||||
form()->setEntity(QSharedPointer<T>(new T()));
|
||||
form()->setNewRec(true);
|
||||
m_formHandler->showForm(m_form);
|
||||
}
|
||||
|
||||
@@ -136,13 +164,14 @@ protected:
|
||||
return;
|
||||
}
|
||||
|
||||
m_form->setEntity(m_tableModel->itemFromIndex(tableView()->currentIndex()));
|
||||
m_form->setNewRec(false);
|
||||
form()->setEntity(m_tableModel->itemFromIndex(tableView()->currentIndex()));
|
||||
form()->setNewRec(false);
|
||||
m_formHandler->showForm(m_form);
|
||||
}
|
||||
|
||||
void handleDeleteRecord() override
|
||||
{
|
||||
connectService();
|
||||
if (m_form == NULL || m_tableModel == NULL || tableView()->currentIndex().row() < 0)
|
||||
{
|
||||
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
|
||||
|
||||
#include <QWidget>
|
||||
#include <QString>
|
||||
|
||||
#include "core_global.h"
|
||||
|
||||
@@ -13,13 +14,21 @@ public:
|
||||
explicit IForm(QWidget *parent = 0);
|
||||
virtual ~IForm();
|
||||
|
||||
QString pluginId() const;
|
||||
void setPluginId(const QString &pluginId);
|
||||
|
||||
signals:
|
||||
void recordAdded();
|
||||
void recordUpdated();
|
||||
void validationError(const QString &errMessage);
|
||||
void refreshEntity();
|
||||
|
||||
public slots:
|
||||
virtual bool saveRecord() = 0;
|
||||
void refresh();
|
||||
|
||||
private:
|
||||
QString m_pluginId;
|
||||
};
|
||||
|
||||
#endif // IFORM_H
|
||||
|
||||
@@ -14,6 +14,7 @@ IGridForm::IGridForm(QWidget *parent) :
|
||||
ui->filterWidget->setVisible(false);
|
||||
m_contextMenu = new QMenu(this);
|
||||
m_contextMenu->addAction(ui->actionSelectColumns);
|
||||
m_form = NULL;
|
||||
|
||||
m_columnDialog = new ColumnDialog(this);
|
||||
connect(m_columnDialog, SIGNAL(accepted()), this, SLOT(columnsAccepted()));
|
||||
@@ -32,6 +33,11 @@ void IGridForm::setPluginId(const QString &pluginId)
|
||||
{
|
||||
m_filterUi->setPluginId(pluginId);
|
||||
}
|
||||
|
||||
if (m_form != NULL)
|
||||
{
|
||||
m_form->setPluginId(pluginId);
|
||||
}
|
||||
}
|
||||
|
||||
QString IGridForm::pluginId()
|
||||
|
||||
@@ -60,6 +60,7 @@ private:
|
||||
|
||||
protected:
|
||||
FilterUi *m_filterUi;
|
||||
IForm *m_form;
|
||||
};
|
||||
|
||||
#endif // IGRIDFORM_H
|
||||
|
||||
+6
-1
@@ -16,8 +16,13 @@ public:
|
||||
|
||||
signals:
|
||||
void dbError(QString errMsg);
|
||||
void dbErrorRead(QString errMsg);
|
||||
void dbErrorInsert(QString errMsg);
|
||||
void dbErrorUpdate(QString errMsg);
|
||||
void dbErrorDelete(QString errMsg);
|
||||
void dataChanged();
|
||||
void permissionDenied();
|
||||
void updateDenied();
|
||||
void readDenied();
|
||||
|
||||
public slots:
|
||||
};
|
||||
|
||||
@@ -67,6 +67,7 @@ public:
|
||||
catch (const odb::exception &ex)
|
||||
{
|
||||
emit dbError(ex.what());
|
||||
emit dbErrorUpdate(ex.what());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -88,6 +89,8 @@ public:
|
||||
catch (const odb::exception &ex)
|
||||
{
|
||||
emit dbError(ex.what());
|
||||
emit dbErrorInsert(ex.what());
|
||||
return;
|
||||
}
|
||||
|
||||
emit dataChanged();
|
||||
@@ -109,11 +112,21 @@ public:
|
||||
catch (const odb::exception &ex)
|
||||
{
|
||||
emit dbError(ex.what());
|
||||
emit dbErrorRead(ex.what());
|
||||
}
|
||||
|
||||
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) {
|
||||
odb::database *db = Context::instance().db();
|
||||
|
||||
@@ -129,6 +142,7 @@ public:
|
||||
catch (const odb::exception &ex)
|
||||
{
|
||||
emit dbError(ex.what());
|
||||
emit dbErrorDelete(ex.what());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user