diff --git a/src/database.cpp b/src/database.cpp index 91fdd87..d8f3d65 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -89,4 +89,15 @@ QFuture Database::close() }); } +void Database::checkError(const QSqlQuery &query) +{ + QSqlError lastError = query.lastError(); + if (!lastError.isValid()) { + return; + } + QString errorMessage = lastError.text(); + emit getInstance().errorOcurred(errorMessage); + throw std::runtime_error(errorMessage.toStdString()); +} + #include "moc_database.cpp" diff --git a/src/database.h b/src/database.h index 620f3f8..8032afe 100644 --- a/src/database.h +++ b/src/database.h @@ -7,6 +7,7 @@ #include #include +class QSqlQuery; class Database : public QObject { Q_OBJECT @@ -27,6 +28,8 @@ public: const QString &connectOptions); Q_INVOKABLE QFuture close(); + static void checkError(const QSqlQuery &query); + template static auto query(Function &&f) { diff --git a/src/timelinelistmodel.cpp b/src/timelinelistmodel.cpp index 3acdff9..a142262 100644 --- a/src/timelinelistmodel.cpp +++ b/src/timelinelistmodel.cpp @@ -85,6 +85,7 @@ void TimelineListModel::fetch() Results results; { QSqlQuery query("select campsite_id, label from campsite order by label, campsite_id"); + Database::checkError(query); while (query.next()) { Lodging item{query.value(0).toInt(), query.value(1).toString()}; results.lodgings.append(item); @@ -104,6 +105,7 @@ void TimelineListModel::fetch() " join booking using (booking_id) " " order by lower(booking_campsite.stay), booking_id " ""); + Database::checkError(query); while (query.next()) { int lodgingId = query.value(0).toInt(); auto *item = new TimelineModel::Item{