Skip to content

Commit

Permalink
added sections to DB and DatabaseManager
Browse files Browse the repository at this point in the history
  • Loading branch information
silverqx committed Aug 10, 2022
1 parent 7be189b commit aaf5038
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
3 changes: 2 additions & 1 deletion include/orm/databasemanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ namespace Query
/*! Return the host name of the connected database. */
const QString &hostName(const QString &connection = "");

/* Others */
/* Pretending */
/*! Execute the given callback in "dry run" mode. */
QVector<Log>
pretend(const std::function<void()> &callback,
Expand All @@ -301,6 +301,7 @@ namespace Query
pretend(const std::function<void(DatabaseConnection &)> &callback,
const QString &connection = "");

/* Records was modified */
/*! Check if any records have been modified. */
bool getRecordsHaveBeenModified(const QString &connection = "");
/*! Indicate if any records have been modified. */
Expand Down
3 changes: 2 additions & 1 deletion include/orm/db.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ namespace Orm
/*! Return the host name of the connected database. */
static const QString &hostName(const QString &connection = "");

/* Others */
/* Pretending */
/*! Execute the given callback in "dry run" mode. */
static QVector<Log>
pretend(const std::function<void()> &callback,
Expand All @@ -300,6 +300,7 @@ namespace Orm
pretend(const std::function<void(DatabaseConnection &)> &callback,
const QString &connection = "");

/* Records was modified */
/*! Check if any records have been modified. */
static bool getRecordsHaveBeenModified(const QString &connection = "");
/*! Indicate if any records have been modified. */
Expand Down
24 changes: 24 additions & 0 deletions src/orm/databasemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ TINYORM_BEGIN_COMMON_NAMESPACE
namespace Orm
{

/* public */

/* This is needed because of the std::unique_ptr is used in the m_connections
data member 😲, and when this dtor is not defined in the cpp, it will be
generated by the compiler as inline dtor, what causes a compile error. */
DatabaseManager::~DatabaseManager() = default;

/* private */

std::shared_ptr<DatabaseManager> DatabaseManager::m_instance;

// FUTURE add support for ::read and ::write db connections silverx
Expand Down Expand Up @@ -54,6 +58,8 @@ DatabaseManager &DatabaseManager::setupDefaultReconnector()
return *this;
}

/* public */

std::shared_ptr<DatabaseManager>
DatabaseManager::create(const QString &defaultConnection)
{
Expand Down Expand Up @@ -83,6 +89,8 @@ DatabaseManager::create(const ConfigurationsType &configs,
new DatabaseManager(configs, defaultConnection));
}

/* Proxy methods to the DatabaseConnection */

std::shared_ptr<QueryBuilder>
DatabaseManager::table(const QString &table, const QString &connection)
{
Expand Down Expand Up @@ -219,6 +227,8 @@ QSqlDriver *DatabaseManager::driver(const QString &connection)
return this->connection(connection).driver();
}

/* DatabaseManager */

namespace
{
const auto *const InstanceExceptionMessage =
Expand Down Expand Up @@ -439,6 +449,8 @@ DatabaseManager::setReconnector(const ReconnectorType &reconnector)
return *this;
}

/* Queries execution time counter */

bool DatabaseManager::countingElapsed(const QString &connection)
{
return this->connection(connection).countingElapsed();
Expand Down Expand Up @@ -559,6 +571,8 @@ void DatabaseManager::resetElapsedCounters(const QStringList &connections)
}
}

/* Queries executed counter */

bool DatabaseManager::countingStatements(const QString &connection)
{
return this->connection(connection).countingStatements();
Expand Down Expand Up @@ -689,6 +703,8 @@ void DatabaseManager::resetStatementCounters(const QStringList &connections)
}
}

/* Logging */

std::shared_ptr<QVector<Log>>
DatabaseManager::getQueryLog(const QString &connection)
{
Expand Down Expand Up @@ -720,6 +736,8 @@ std::size_t DatabaseManager::getQueryLogOrder()
return DatabaseConnection::getQueryLogOrder();
}

/* Getters */

QString DatabaseManager::driverName(const QString &connection)
{
return this->connection(connection).driverName();
Expand All @@ -743,6 +761,8 @@ DatabaseManager::hostName(const QString &connection)
return this->connection(connection).getHostName();
}

/* Pretending */

QVector<Log>
DatabaseManager::pretend(const std::function<void()> &callback,
const QString &connection)
Expand All @@ -757,6 +777,8 @@ DatabaseManager::pretend(const std::function<void(DatabaseConnection &)> &callba
return this->connection(connection).pretend(callback);
}

/* Records was modified */

bool DatabaseManager::getRecordsHaveBeenModified(const QString &connection)
{
return this->connection(connection).getRecordsHaveBeenModified();
Expand All @@ -773,6 +795,8 @@ void DatabaseManager::forgetRecordModificationState(const QString &connection)
this->connection(connection).forgetRecordModificationState();
}

/* private */

const QString &
DatabaseManager::parseConnectionName(const QString &name) const
{
Expand Down
22 changes: 22 additions & 0 deletions src/orm/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ TINYORM_BEGIN_COMMON_NAMESPACE
namespace Orm
{

/* private */

std::shared_ptr<DatabaseManager> DB::m_manager;

/* public */

/* Proxy methods to the DatabaseManager */

std::shared_ptr<DatabaseManager>
DB::create(const QString &defaultConnection)
{
Expand Down Expand Up @@ -136,6 +142,8 @@ DB::setReconnector(const ReconnectorType &reconnector)
return manager().setReconnector(reconnector);
}

/* Proxy methods to the DatabaseConnection */

std::shared_ptr<QueryBuilder>
DB::table(const QString &table, const QString &connection)
{
Expand Down Expand Up @@ -270,6 +278,8 @@ QSqlDriver *DB::driver(const QString &connection)
return manager().connection(connection).driver();
}

/* Queries execution time counter */

bool DB::countingElapsed(const QString &connection)
{
return manager().connection(connection).countingElapsed();
Expand Down Expand Up @@ -355,6 +365,8 @@ void DB::resetElapsedCounters(const QStringList &connections)
manager().resetElapsedCounters(connections);
}

/* Queries executed counter */

bool DB::countingStatements(const QString &connection)
{
return manager().connection(connection).countingStatements();
Expand Down Expand Up @@ -440,6 +452,8 @@ void DB::resetStatementCounters(const QStringList &connections)
manager().resetStatementCounters(connections);
}

/* Logging */

std::shared_ptr<QVector<Log>>
DB::getQueryLog(const QString &connection)
{
Expand Down Expand Up @@ -471,6 +485,8 @@ std::size_t DB::getQueryLogOrder()
return manager().getQueryLogOrder();
}

/* Getters */

QString DB::driverName(const QString &connection)
{
return manager().connection(connection).driverName();
Expand All @@ -491,6 +507,8 @@ const QString &DB::hostName(const QString &connection)
return manager().connection(connection).getHostName();
}

/* Pretending */

QVector<Log>
DB::pretend(const std::function<void()> &callback, const QString &connection)
{
Expand All @@ -504,6 +522,8 @@ DB::pretend(const std::function<void(DatabaseConnection &)> &callback,
return manager().connection(connection).pretend(callback);
}

/* Records was modified */

bool DB::getRecordsHaveBeenModified(const QString &connection)
{
return manager().connection(connection).getRecordsHaveBeenModified();
Expand All @@ -519,6 +539,8 @@ void DB::forgetRecordModificationState(const QString &connection)
manager().connection(connection).forgetRecordModificationState();
}

/* private */

DatabaseManager &DB::manager()
{
if (m_manager) T_LIKELY
Expand Down

0 comments on commit aaf5038

Please sign in to comment.