diff --git a/include/orm/databaseconnection.hpp b/include/orm/databaseconnection.hpp index f2f59fe9f..9c55f39bd 100644 --- a/include/orm/databaseconnection.hpp +++ b/include/orm/databaseconnection.hpp @@ -157,6 +157,8 @@ namespace Schema void bindValues(QSqlQuery &query, const QVector &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; diff --git a/include/orm/databasemanager.hpp b/include/orm/databasemanager.hpp index 6dced1d45..8039d6ab1 100644 --- a/include/orm/databasemanager.hpp +++ b/include/orm/databasemanager.hpp @@ -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. */ diff --git a/include/orm/db.hpp b/include/orm/db.hpp index 0b1f6a6d3..1c58e0a65 100644 --- a/include/orm/db.hpp +++ b/include/orm/db.hpp @@ -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. */ diff --git a/src/orm/databasemanager.cpp b/src/orm/databasemanager.cpp index c66c77356..93b12fbee 100644 --- a/src/orm/databasemanager.cpp +++ b/src/orm/databasemanager.cpp @@ -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 DatabaseManager::pretend(const std::function &callback, const QString &connection) diff --git a/src/orm/db.cpp b/src/orm/db.cpp index 03ac636ac..7c1d3e5f2 100644 --- a/src/orm/db.cpp +++ b/src/orm/db.cpp @@ -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 DB::pretend(const std::function &callback, const QString &connection) {