-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from matty0ung/dbFixes
Db fixes etc
- Loading branch information
Showing
115 changed files
with
2,580 additions
and
2,432 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/*====================================================================================================================== | ||
* database/DbTransaction.cpp is part of Brewken, and is copyright the following authors 2021-2022: | ||
* database/DbTransaction.cpp is part of Brewken, and is copyright the following authors 2021-2024: | ||
* • Matt Young <[email protected]> | ||
* | ||
* Brewken is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License | ||
|
@@ -19,11 +19,15 @@ | |
#include <QSqlError> | ||
|
||
#include "database/Database.h" | ||
#include "Logging.h" | ||
|
||
|
||
DbTransaction::DbTransaction(Database & database, QSqlDatabase & connection, DbTransaction::SpecialBehaviours specialBehaviours) : | ||
DbTransaction::DbTransaction(Database & database, | ||
QSqlDatabase & connection, | ||
QString const nameForLogging, | ||
DbTransaction::SpecialBehaviours specialBehaviours) : | ||
database{database}, | ||
connection{connection}, | ||
nameForLogging{nameForLogging}, | ||
committed{false}, | ||
specialBehaviours{specialBehaviours} { | ||
// Note that, on SQLite at least, turning foreign keys on and off has to happen outside a transaction, so we have to | ||
|
@@ -33,9 +37,12 @@ DbTransaction::DbTransaction(Database & database, QSqlDatabase & connection, DbT | |
} | ||
|
||
bool succeeded = this->connection.transaction(); | ||
qDebug() << Q_FUNC_INFO << "Database transaction begin: " << (succeeded ? "succeeded" : "failed"); | ||
qDebug() << | ||
Q_FUNC_INFO << "Database transaction" << this->nameForLogging << "begin: " << (succeeded ? "succeeded" : "failed"); | ||
if (!succeeded) { | ||
qCritical() << Q_FUNC_INFO << "Unable to start database transaction:" << connection.lastError().text(); | ||
qCritical() << | ||
Q_FUNC_INFO << "Unable to start database transaction" << this->nameForLogging << ":" << connection.lastError().text(); | ||
qCritical().noquote() << Q_FUNC_INFO << Logging::getStackTrace(); | ||
} | ||
return; | ||
} | ||
|
@@ -44,9 +51,11 @@ DbTransaction::~DbTransaction() { | |
qDebug() << Q_FUNC_INFO; | ||
if (!committed) { | ||
bool succeeded = this->connection.rollback(); | ||
qDebug() << Q_FUNC_INFO << "Database transaction rollback: " << (succeeded ? "succeeded" : "failed"); | ||
qDebug() << | ||
Q_FUNC_INFO << "Database transaction" << this->nameForLogging << "rollback: " << (succeeded ? "succeeded" : "failed"); | ||
if (!succeeded) { | ||
qCritical() << Q_FUNC_INFO << "Unable to rollback database transaction:" << connection.lastError().text(); | ||
qCritical() << | ||
Q_FUNC_INFO << "Unable to rollback database transaction" << this->nameForLogging << ":" << connection.lastError().text(); | ||
} | ||
} | ||
|
||
|
@@ -59,9 +68,11 @@ DbTransaction::~DbTransaction() { | |
|
||
bool DbTransaction::commit() { | ||
this->committed = connection.commit(); | ||
qDebug() << Q_FUNC_INFO << "Database transaction commit: " << (this->committed ? "succeeded" : "failed"); | ||
qDebug() << | ||
Q_FUNC_INFO << "Database transaction" << this->nameForLogging << "commit: " << (this->committed ? "succeeded" : "failed"); | ||
if (!this->committed) { | ||
qCritical() << Q_FUNC_INFO << "Unable to commit database transaction:" << connection.lastError().text(); | ||
qCritical() << | ||
Q_FUNC_INFO << "Unable to commit database transaction" << this->nameForLogging << ":" << connection.lastError().text(); | ||
} | ||
return this->committed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/*====================================================================================================================== | ||
* database/DbTransaction.h is part of Brewken, and is copyright the following authors 2021: | ||
* database/DbTransaction.h is part of Brewken, and is copyright the following authors 2021-2024: | ||
* • Matt Young <[email protected]> | ||
* | ||
* Brewken is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License | ||
|
@@ -34,7 +34,10 @@ class DbTransaction { | |
/** | ||
* \brief Constructing a \c DbTransaction will start a DB transaction | ||
*/ | ||
DbTransaction(Database & database, QSqlDatabase & connection, SpecialBehaviours specialBehaviours = NONE); | ||
DbTransaction(Database & database, | ||
QSqlDatabase & connection, | ||
QString const nameForLogging = "???", | ||
SpecialBehaviours specialBehaviours = NONE); | ||
|
||
/** | ||
* \brief When a \c DbTransaction goes out of scope and its destructor is called, the transaction started in the | ||
|
@@ -53,6 +56,9 @@ class DbTransaction { | |
Database & database; | ||
// This is intended to be a short-lived object, so it's OK to store a reference to a QSqlDatabase object | ||
QSqlDatabase & connection; | ||
// This is useful for diagnosing problems such as | ||
// 'Unable to start database transaction: "cannot start a transaction within a transaction Unable to begin transaction"' | ||
QString const nameForLogging; | ||
bool committed; | ||
int specialBehaviours; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.