added free methods to DatabaseManager, DB, Schema

[skip ci]
This commit is contained in:
silverqx
2024-05-07 13:52:08 +02:00
parent 609320a949
commit 18d92da387
7 changed files with 59 additions and 0 deletions

View File

@@ -149,6 +149,9 @@ namespace Query
/*! Obtain a reference to the DatabaseManager. */
static DatabaseManager &reference();
/*! Releases the ownership of the DatabaseManager managed object. */
static void free() noexcept;
/*! Get a database connection instance. */
DatabaseConnection &connection(const QString &name = "") final; // NOLINT(google-default-arguments)
/*! Begin a fluent query against the database on a given connection (alias for

View File

@@ -359,6 +359,10 @@ namespace Orm
/*! Reset the number of executed queries on given connections. */
static void resetStatementCounters(const QStringList &connections);
/* DB */
/*! Releases the ownership of the DatabaseManager managed object. */
static void free() noexcept;
private:
/*! Get a reference to the DatabaseManager. */
static DatabaseManager &manager();

View File

@@ -103,6 +103,9 @@ namespace Orm
const QString &connection = "");
/* Schema */
/*! Releases the ownership of the DatabaseManager managed object. */
static void free() noexcept;
/*! Get a schema builder instance for the default connection. */
static SchemaBuilder &connection(const QString &name = "");
/*! Get a schema builder instance for a connection. (alias for the connection()

View File

@@ -292,6 +292,11 @@ DatabaseManager &DatabaseManager::reference()
throw Exceptions::RuntimeError(InstanceExceptionMessage);
}
void DatabaseManager::free() noexcept
{
m_instance.reset();
}
DatabaseConnection &DatabaseManager::connection(const QString &name) // NOLINT(google-default-arguments)
{
const auto &connectionName = parseConnectionName(name);

View File

@@ -628,6 +628,15 @@ void DB::resetStatementCounters(const QStringList &connections)
manager().resetStatementCounters(connections);
}
/* DB */
void DB::free() noexcept
{
// Reset both, DatabaseManager and DB ownership of the DatabaseManager managed object
m_manager.reset();
DatabaseManager::free();
}
/* private */
DatabaseManager &DB::manager()

View File

@@ -139,6 +139,11 @@ bool Schema::hasColumns(const QString &table, const QVector<QString> &columns,
/* Schema */
void Schema::free() noexcept
{
m_manager.reset();
}
SchemaBuilder &Schema::connection(const QString &name)
{
return schemaBuilder(name);

View File

@@ -83,6 +83,10 @@ private Q_SLOTS:
void MySQL_addUseAndRemoveConnection_FiveTimes() const;
void MySQL_addUseAndRemoveThreeConnections_FiveTimes() const;
// Can't be enabled (one time test only, run as needed)
// void reset_DatabaseManager() const;
// void reset_DB_DatabaseManager() const;
// NOLINTNEXTLINE(readability-redundant-access-specifiers)
private:
/*! Test case class name. */
@@ -908,6 +912,32 @@ void tst_DatabaseManager::MySQL_addUseAndRemoveThreeConnections_FiveTimes() cons
QVERIFY(m_dm->openedConnectionNames().isEmpty());
}
}
/* These two tests can't be enabled as it resets the DatabaseManager::m_instance and
destroys all connections and they can't be re-created easily. No need to invest
more time to this and hack things around. */
//void tst_DatabaseManager::reset_DatabaseManager() const
//{
// // tst_DatabaseManager::m_dm, Databases::m_dm, and DatabaseManager::m_instance
// QCOMPARE(m_dm.use_count(), 3);
// Orm::DatabaseManager::free();
// QCOMPARE(m_dm.use_count(), 2);
//}
//void tst_DatabaseManager::reset_DB_DatabaseManager() const
//{
// // tst_DatabaseManager::m_dm, Databases::m_dm, and DatabaseManager::m_instance
// QCOMPARE(m_dm.use_count(), 3);
// // To assign/populate the DB::m_manager shared instance
// Orm::DB::getDefaultConnection();
// QCOMPARE(m_dm.use_count(), 4);
// Orm::DB::free();
// QCOMPARE(m_dm.use_count(), 2);
//}
// NOLINTEND(readability-convert-member-functions-to-static)
/* private */