added isOpen()/connectEagerly()/pingDatabase()

- added connectEagerly() that allows to force connection to the DB
 - added isOpen()
 - exposed isOpen()/connectEagerly()/pingDatabase() to DB and
   DatabaseManager
This commit is contained in:
silverqx
2022-03-06 12:31:10 +01:00
parent bd889521db
commit 9a54b520bc
5 changed files with 64 additions and 22 deletions
+17 -7
View File
@@ -157,6 +157,8 @@ namespace Schema
void bindValues(QSqlQuery &query,
const QVector<QVariant> &bindings) const;
/*! Determine whether the database connection is currently open. */
inline bool isOpen();
/*! Check database connection and show warnings when the state changed. */
virtual bool pingDatabase();
@@ -165,6 +167,9 @@ namespace Schema
void reconnect() const;
/*! Disconnect from the underlying Qt's connection. */
void disconnect();
/*! Force connection to the database (creates physical connection), doesn't have
to be called before querying a database. */
inline QSqlDatabase connectEagerly();
/*! Get the query grammar used by the connection. */
inline const QueryGrammar &getQueryGrammar() const;
@@ -196,8 +201,6 @@ namespace Schema
inline const QString &getDatabaseName() const;
/*! Get the host name of the connected database. */
inline const QString &getHostName() const;
/*! Determine whether the database connection is currently open. */
inline bool isOpen();
/* Others */
/*! Execute the given callback in "dry run" mode. */
@@ -371,6 +374,18 @@ namespace Schema
return m_qtConnectionResolver;
}
bool DatabaseConnection::isOpen()
{
return m_qtConnection && getQtConnection().isOpen();
}
QSqlDatabase DatabaseConnection::connectEagerly()
{
reconnectIfMissingConnection();
return getQtConnection();
}
const QueryGrammar &DatabaseConnection::getQueryGrammar() const
{
return *m_queryGrammar;
@@ -413,11 +428,6 @@ namespace Schema
return m_hostName;
}
inline bool DatabaseConnection::isOpen()
{
return m_qtConnection && getQtConnection().isOpen();
}
bool DatabaseConnection::pretending() const
{
return m_pretending;
+9 -3
View File
@@ -104,7 +104,12 @@ namespace Query
/*! Rollback to a named transaction savepoint. */
bool rollbackToSavepoint(std::size_t id);
/*! Get the number of active transactions. */
std::size_t transactionLevel();
std::size_t transactionLevel(const QString &connection = "");
/*! Determine whether the database connection is currently open. */
bool isOpen(const QString &connection = "");
/*! Check database connection and show warnings when the state changed. */
bool pingDatabase(const QString &connection = "");
/* DatabaseManager */
/*! Obtain a shared pointer to the DatabaseManager. */
@@ -132,6 +137,9 @@ namespace Query
DatabaseConnection &reconnect(const QString &name = "");
/*! Disconnect from the given database. */
void disconnect(const QString &name = "") const;
/*! Force connection to the database (creates physical connection), doesn't have
to be called before querying a database. */
QSqlDatabase connectEagerly(const QString &name = "");
/*! Get all of the support drivers. */
QStringList supportedDrivers() const;
@@ -256,8 +264,6 @@ namespace Query
const QString &databaseName(const QString &connection = "");
/*! Return the host name of the connected database. */
const QString &hostName(const QString &connection = "");
/*! Determine whether the database connection is currently open. */
bool isOpen(const QString &connection = "");
/* Others */
/*! Execute the given callback in "dry run" mode. */
+8 -2
View File
@@ -64,6 +64,9 @@ namespace Orm
static DatabaseConnection &reconnect(const QString &name = "");
/*! Disconnect from the given database. */
static void disconnect(const QString &name = "");
/*! Force connection to the database (creates physical connection), doesn't have
to be called before querying a database. */
static QSqlDatabase connectEagerly(const QString &name = "");
/*! Get all of the support drivers. */
static QStringList supportedDrivers();
@@ -141,6 +144,11 @@ namespace Orm
/*! Get the number of active transactions. */
static std::size_t transactionLevel(const QString &connection = "");
/*! Determine whether the database connection is currently open. */
static bool isOpen(const QString &connection = "");
/*! Check database connection and show warnings when the state changed. */
static bool pingDatabase(const QString &connection = "");
/* Queries execution time counter */
/*! Determine whether we're counting queries execution time. */
static bool
@@ -254,8 +262,6 @@ namespace Orm
static const QString &databaseName(const QString &connection = "");
/*! Return the host name of the connected database. */
static const QString &hostName(const QString &connection = "");
/*! Determine whether the database connection is currently open. */
static bool isOpen(const QString &connection = "");
/* Others */
/*! Execute the given callback in "dry run" mode. */
+15 -5
View File
@@ -188,6 +188,16 @@ size_t DatabaseManager::transactionLevel()
return connection().transactionLevel();
}
bool DatabaseManager::isOpen(const QString &connection)
{
return this->connection(connection).isOpen();
}
bool DatabaseManager::pingDatabase(const QString &connection)
{
return this->connection(connection).pingDatabase();
}
namespace
{
const auto *const InstanceExceptionMessage =
@@ -319,6 +329,11 @@ void DatabaseManager::disconnect(const QString &name) const
(*m_connections).find(name_)->second->disconnect();
}
QSqlDatabase DatabaseManager::connectEagerly(const QString &name)
{
return connection(name).connectEagerly();
}
QStringList DatabaseManager::supportedDrivers() const
{
// FUTURE add method to not only supported drivers, but also check if driver is available/loadable by qsqldatabase silverqx
@@ -671,11 +686,6 @@ DatabaseManager::hostName(const QString &connection)
return this->connection(connection).getHostName();
}
bool DatabaseManager::isOpen(const QString &connection)
{
return this->connection(connection).isOpen();
}
QVector<Log>
DatabaseManager::pretend(const std::function<void()> &callback,
const QString &connection)
+15 -5
View File
@@ -67,6 +67,11 @@ void DB::disconnect(const QString &name)
manager().disconnect(name);
}
QSqlDatabase DB::connectEagerly(const QString &name)
{
return manager().connectEagerly(name);
}
QStringList DB::supportedDrivers()
{
return manager().supportedDrivers();
@@ -208,6 +213,16 @@ size_t DB::transactionLevel(const QString &connection)
return manager().connection(connection).transactionLevel();
}
bool DB::isOpen(const QString &connection)
{
return manager().connection(connection).isOpen();
}
bool DB::pingDatabase(const QString &connection)
{
return manager().connection(connection).pingDatabase();
}
bool DB::countingElapsed(const QString &connection)
{
return manager().connection(connection).countingElapsed();
@@ -429,11 +444,6 @@ const QString &DB::hostName(const QString &connection)
return manager().connection(connection).getHostName();
}
bool DB::isOpen(const QString &connection)
{
return manager().connection(connection).isOpen();
}
QVector<Log>
DB::pretend(const std::function<void()> &callback, const QString &connection)
{