summaryrefslogtreecommitdiff
path: root/qt_db_helpers.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'qt_db_helpers.hpp')
-rwxr-xr-xqt_db_helpers.hpp192
1 files changed, 96 insertions, 96 deletions
diff --git a/qt_db_helpers.hpp b/qt_db_helpers.hpp
index dcf9d033..46bbd3bf 100755
--- a/qt_db_helpers.hpp
+++ b/qt_db_helpers.hpp
@@ -1,96 +1,96 @@
-#ifndef QT_DB_HELPERS_HPP_
-#define QT_DB_HELPERS_HPP_
-
-#include <utility>
-#include <stdexcept>
-#include <QString>
-#include <QSqlError>
-#include <QSqlTableModel>
-#include "boost/core/noncopyable.hpp"
-
-template<typename T, typename Func, typename... Args>
-void SQL_error_check (T&& object, Func func, Args&&... args)
-{
- if (!(std::forward<T> (object).*func) (std::forward<Args> (args)...))
- {
- auto error = object.lastError ();
- if (QSqlError::NoError != error.type ())
- {
- throw std::runtime_error {("Database Error: " + error.text ()).toStdString ()};
- }
- }
-}
-
-class ConditionalTransaction final
- : private boost::noncopyable
-{
-public:
- explicit ConditionalTransaction (QSqlTableModel& model)
- : model_ (model)
- , submitted_ {false}
- {
- model_.database ().transaction ();
- }
-
- bool submit (bool throw_on_error = true)
- {
- bool ok {true};
- if (throw_on_error)
- {
- SQL_error_check (model_
- , QSqlTableModel::OnManualSubmit == model_.editStrategy ()
- ? &QSqlTableModel::submitAll
- : &QSqlTableModel::submit);
- }
- else
- {
- ok = QSqlTableModel::OnManualSubmit == model_.editStrategy ()
- ? model_.submitAll () : model_.submit ();
- }
- submitted_ = submitted_ || ok;
- return ok;
- }
-
- void revert ()
- {
- if (QSqlTableModel::OnManualSubmit == model_.editStrategy ())
- {
- model_.revertAll ();
- }
- else
- {
- model_.revert ();
- }
- }
-
- ~ConditionalTransaction ()
- {
- if (model_.isDirty ())
- {
- // abandon un-submitted changes to the model
- if (QSqlTableModel::OnManualSubmit == model_.editStrategy ())
- {
- model_.revertAll ();
- }
- else
- {
- model_.revert ();
- }
- }
- auto database = model_.database ();
- if (submitted_)
- {
- SQL_error_check (database, &QSqlDatabase::commit);
- }
- else
- {
- database.rollback ();
- }
- }
-
-private:
- QSqlTableModel& model_;
- bool submitted_;
-};
-
-#endif
+#ifndef QT_DB_HELPERS_HPP_
+#define QT_DB_HELPERS_HPP_
+
+#include <utility>
+#include <stdexcept>
+#include <QString>
+#include <QSqlError>
+#include <QSqlTableModel>
+#include "boost/core/noncopyable.hpp"
+
+template<typename T, typename Func, typename... Args>
+void SQL_error_check (T&& object, Func func, Args&&... args)
+{
+ if (!(std::forward<T> (object).*func) (std::forward<Args> (args)...))
+ {
+ auto error = object.lastError ();
+ if (QSqlError::NoError != error.type ())
+ {
+ throw std::runtime_error {("Database Error: " + error.text ()).toStdString ()};
+ }
+ }
+}
+
+class ConditionalTransaction final
+ : private boost::noncopyable
+{
+public:
+ explicit ConditionalTransaction (QSqlTableModel& model)
+ : model_ (model)
+ , submitted_ {false}
+ {
+ model_.database ().transaction ();
+ }
+
+ bool submit (bool throw_on_error = true)
+ {
+ bool ok {true};
+ if (throw_on_error)
+ {
+ SQL_error_check (model_
+ , QSqlTableModel::OnManualSubmit == model_.editStrategy ()
+ ? &QSqlTableModel::submitAll
+ : &QSqlTableModel::submit);
+ }
+ else
+ {
+ ok = QSqlTableModel::OnManualSubmit == model_.editStrategy ()
+ ? model_.submitAll () : model_.submit ();
+ }
+ submitted_ = submitted_ || ok;
+ return ok;
+ }
+
+ void revert ()
+ {
+ if (QSqlTableModel::OnManualSubmit == model_.editStrategy ())
+ {
+ model_.revertAll ();
+ }
+ else
+ {
+ model_.revert ();
+ }
+ }
+
+ ~ConditionalTransaction ()
+ {
+ if (model_.isDirty ())
+ {
+ // abandon un-submitted changes to the model
+ if (QSqlTableModel::OnManualSubmit == model_.editStrategy ())
+ {
+ model_.revertAll ();
+ }
+ else
+ {
+ model_.revert ();
+ }
+ }
+ auto database = model_.database ();
+ if (submitted_)
+ {
+ SQL_error_check (database, &QSqlDatabase::commit);
+ }
+ else
+ {
+ database.rollback ();
+ }
+ }
+
+private:
+ QSqlTableModel& model_;
+ bool submitted_;
+};
+
+#endif