Skip to content

Commit

Permalink
renamed all getQtQuery and similar to getSqlQuery
Browse files Browse the repository at this point in the history
These methods had a wrong names after the TinyDrivers were added as
all can return QSqlXyz or Orm::Drivers::SqlXyz instances.

Changing Qt to Sql covers both cases.

 - updated auto tests
 - updated docs
  • Loading branch information
silverqx committed Jul 28, 2024
1 parent b455794 commit 7f897b3
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 69 deletions.
8 changes: 4 additions & 4 deletions docs/database/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -847,16 +847,16 @@ If your application needs to use multiple connections, you may access each conne
auto query = DB::connection("mysql_test").select(...);
```

You may access the raw underlying `QSqlQuery` instance of a connection using the `getQtQuery` method on a connection instance:
You may access the raw underlying `QSqlQuery` instance of a connection using the `getSqlQuery` method on a connection instance:

```cpp
auto query = DB::connection().getQtQuery();
auto query = DB::connection().getSqlQuery();
```

Or you can use the shortcut method `qtQuery` provided by the `DB` facade:
Or you can use the shortcut method `sqlQuery` provided by the `DB` facade:

```cpp
auto query = DB::qtQuery();
auto query = DB::sqlQuery();
```

## Database Transactions
Expand Down
2 changes: 1 addition & 1 deletion include/orm/concerns/managestransactions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace Concerns
/*! Transform QtSql transaction error (QSqlError) to TinyORM SqlTransactionError
exception (shortcut method). */
[[noreturn]] static void
throwSqlTransactionError(const QString &functionName, const QSqlQuery &qtQuery);
throwSqlTransactionError(const QString &functionName, const QSqlQuery &sqlQuery);
#endif

/*! Reset in transaction state and savepoints. */
Expand Down
28 changes: 14 additions & 14 deletions include/orm/databaseconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,19 @@ namespace Orm
SqlQuery unprepared(const QString &queryString);

/* Obtain connection instance */
/*! Get underlying database connection (QSqlDatabase). */
TSqlDatabase getQtConnection();
/*! Get underlying database connection (Q/SqlDatabase). */
TSqlDatabase getSqlConnection();
/*! Get underlying database connection without executing any reconnect logic. */
TSqlDatabase getRawQtConnection() const;
TSqlDatabase getRawSqlConnection() const;
/*! Get the connection resolver for an underlying database connection. */
inline const std::function<Connectors::ConnectionName()> &
getQtConnectionResolver() const noexcept;
getSqlConnectionResolver() const noexcept;
/*! Set the connection resolver for an underlying database connection. */
DatabaseConnection &setQtConnectionResolver(
DatabaseConnection &setSqlConnectionResolver(
const std::function<Connectors::ConnectionName()> &resolver);

/*! Get a new QSqlQuery instance for the current connection. */
TSqlQuery getQtQuery();
/*! Get a new Q/SqlQuery instance for the current connection. */
TSqlQuery getSqlQuery();

/*! Prepare the query bindings for execution. */
QList<QVariant> &prepareBindings(QList<QVariant> &bindings) const;
Expand Down Expand Up @@ -320,8 +320,8 @@ namespace Orm
private:
/*! Prepare an SQL statement and return the query object. */
TSqlQuery prepareQuery(const QString &queryString);
/*! Get a new QSqlQuery instance for the pretend for the current connection. */
inline TSqlQuery getQtQueryForPretend();
/*! Get a new Q/SqlQuery instance for the pretend for the current connection. */
inline TSqlQuery getSqlQueryForPretend();

/*! Prepare the QDateTime query binding for execution. */
QDateTime prepareBinding(const QDateTime &binding) const;
Expand Down Expand Up @@ -418,22 +418,22 @@ namespace Orm
/* Obtain connection instance */

const std::function<Connectors::ConnectionName()> &
DatabaseConnection::getQtConnectionResolver() const noexcept
DatabaseConnection::getSqlConnectionResolver() const noexcept
{
return m_qtConnectionResolver;
}

bool DatabaseConnection::isOpen()
{
return m_qtConnection && getQtConnection().isOpen();
return m_qtConnection && getSqlConnection().isOpen();
}

void DatabaseConnection::connectEagerly()
{
reconnectIfMissingConnection();

// This opens a physical database connection
std::ignore = getQtConnection();
std::ignore = getSqlConnection();
}

const QueryGrammar &DatabaseConnection::getQueryGrammar() const noexcept
Expand Down Expand Up @@ -587,9 +587,9 @@ namespace Orm

/* private */

TSqlQuery DatabaseConnection::getQtQueryForPretend()
TSqlQuery DatabaseConnection::getSqlQueryForPretend()
{
return getQtQuery();
return getSqlQuery();
}

template<typename Return>
Expand Down
8 changes: 4 additions & 4 deletions include/orm/databasemanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ namespace Query

/*! Get a new query builder instance for the connection. */
std::shared_ptr<QueryBuilder> query(const QString &connection = "");
/*! Get a new QSqlQuery instance for the connection. */
TSqlQuery qtQuery(const QString &connection = "");
/*! Get a new Q/SqlQuery instance for the connection. */
TSqlQuery sqlQuery(const QString &connection = "");

// TODO next add support for named bindings, Using Named Bindings silverqx
/*! Run a select statement against the database. */
Expand Down Expand Up @@ -393,9 +393,9 @@ namespace Query
std::shared_ptr<DatabaseConnection>
configure(std::shared_ptr<DatabaseConnection> &&connection) const;

/*! Refresh an underlying QSqlDatabase connection resolver on a given
/*! Refresh an underlying Q/SqlDatabase connection resolver on a given
TinyORM connection. */
DatabaseConnection &refreshQtConnection(const QString &connection);
DatabaseConnection &refreshSqlConnection(const QString &connection);

/*! Throw an exception if DatabaseManager instance already exists. */
static void checkInstance();
Expand Down
4 changes: 2 additions & 2 deletions include/orm/db.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ namespace Orm

/*! Get a new query builder instance for the connection. */
static std::shared_ptr<QueryBuilder> query(const QString &connection = "");
/*! Get a new QSqlQuery instance for the connection. */
static TSqlQuery qtQuery(const QString &connection = "");
/*! Get a new Q/SqlQuery instance for the connection. */
static TSqlQuery sqlQuery(const QString &connection = "");

/*! Run a select statement against the database. */
static SqlQuery
Expand Down
2 changes: 1 addition & 1 deletion include/orm/tiny/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1802,7 +1802,7 @@ namespace Orm::Tiny
/* This makes it much safer because a user must explicitly select this
behavior using the all parameter. */
if (!all)
return {-1, getConnection().getQtQuery()};
return {-1, getConnection().getSqlQuery()};

return invokeIncrementOrDecrement(*builder, column, amount, extra, method,
all);
Expand Down
32 changes: 16 additions & 16 deletions src/orm/concerns/managestransactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bool ManagesTransactions::beginTransaction()
runBeginTransaction(connection);
#elif defined(TINYORM_USING_TINYDRIVERS)
if (!connection.pretending())
connection.getQtConnection().transaction();
connection.getSqlConnection().transaction();
#endif
} catch (const SqlTransactionError &e) {
tryAgainIfCausedByLostConnectionStart(connection, std::current_exception(),
Expand Down Expand Up @@ -119,7 +119,7 @@ bool ManagesTransactions::commit()
runCommit(connection);
#elif defined(TINYORM_USING_TINYDRIVERS)
if (!connection.pretending())
connection.getRawQtConnection().commit();
connection.getRawSqlConnection().commit();
#endif
} catch (const SqlTransactionError &e) {
tryAgainIfCausedByLostConnectionCommon(std::current_exception(),
Expand Down Expand Up @@ -160,7 +160,7 @@ bool ManagesTransactions::rollBack()
runRollBack(connection);
#elif defined(TINYORM_USING_TINYDRIVERS)
if (!connection.pretending())
connection.getRawQtConnection().rollback();
connection.getRawSqlConnection().rollback();
#endif
} catch (const SqlTransactionError &e) {
tryAgainIfCausedByLostConnectionCommon(std::current_exception(),
Expand Down Expand Up @@ -205,7 +205,7 @@ bool ManagesTransactions::savepoint(const QString &id)
runCommonSavepointQuery(connection, queryString, *SavepointFunction);
#elif defined(TINYORM_USING_TINYDRIVERS)
if (!connection.pretending())
connection.getQtQuery().exec(queryString);
connection.getSqlQuery().exec(queryString);
#endif
} catch (const SqlTransactionError &e) {
tryAgainIfCausedByLostConnectionCommon(std::current_exception(),
Expand Down Expand Up @@ -251,7 +251,7 @@ bool ManagesTransactions::rollbackToSavepoint(const QString &id)
runCommonSavepointQuery(connection, queryString, *RollbackToSavepointFunction);
#elif defined(TINYORM_USING_TINYDRIVERS)
if (!connection.pretending())
connection.getQtQuery().exec(queryString);
connection.getSqlQuery().exec(queryString);
#endif
} catch (const SqlTransactionError &e) {
tryAgainIfCausedByLostConnectionCommon(std::current_exception(),
Expand Down Expand Up @@ -287,7 +287,7 @@ ManagesTransactions::setSavepointNamespace(const QString &savepointNamespace)
#ifdef TINYORM_USING_QTSQLDRIVERS
void ManagesTransactions::runBeginTransaction(DatabaseConnection &connection)
{
auto qtConnection = connection.getQtConnection();
auto qtConnection = connection.getSqlConnection();

// Nothing to do
if (connection.pretending() || qtConnection.transaction())
Expand All @@ -299,7 +299,7 @@ void ManagesTransactions::runBeginTransaction(DatabaseConnection &connection)

void ManagesTransactions::runCommit(DatabaseConnection &connection)
{
auto qtConnection = connection.getRawQtConnection();
auto qtConnection = connection.getRawSqlConnection();

// Nothing to do
if (connection.pretending() || qtConnection.commit())
Expand All @@ -310,7 +310,7 @@ void ManagesTransactions::runCommit(DatabaseConnection &connection)

void ManagesTransactions::runRollBack(DatabaseConnection &connection)
{
auto qtConnection = connection.getRawQtConnection();
auto qtConnection = connection.getRawSqlConnection();

// Nothing to do
if (connection.pretending() || qtConnection.rollback())
Expand All @@ -323,13 +323,13 @@ void ManagesTransactions::runCommonSavepointQuery(
DatabaseConnection &connection, const QString &queryString,
const QString &functionName)
{
auto qtQuery = connection.getQtQuery();
auto sqlQuery = connection.getSqlQuery();

// Nothing to do
if (connection.pretending() || qtQuery.exec(queryString))
if (connection.pretending() || sqlQuery.exec(queryString))
return;

throwSqlTransactionError(functionName, qtQuery);
throwSqlTransactionError(functionName, sqlQuery);
}

void ManagesTransactions::throwSqlTransactionError(
Expand All @@ -342,13 +342,13 @@ void ManagesTransactions::throwSqlTransactionError(
}

void ManagesTransactions::throwSqlTransactionError(const QString &functionName,
const QSqlQuery &qtQuery)
const QSqlQuery &sqlQuery)
{
auto executedQuery = qtQuery.executedQuery();
auto executedQuery = sqlQuery.executedQuery();
if (executedQuery.isEmpty())
executedQuery = qtQuery.lastQuery();
executedQuery = sqlQuery.lastQuery();

throwSqlTransactionError(functionName, executedQuery, qtQuery.lastError());
throwSqlTransactionError(functionName, executedQuery, sqlQuery.lastError());
}
#endif

Expand Down Expand Up @@ -379,7 +379,7 @@ void ManagesTransactions::tryAgainIfCausedByLostConnectionStart(

connection.reconnect();

connection.getQtConnection().transaction();
connection.getSqlConnection().transaction();
}

void ManagesTransactions::tryAgainIfCausedByLostConnectionCommon(
Expand Down
30 changes: 15 additions & 15 deletions src/orm/databaseconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ DatabaseConnection::select(const QString &queryString, QList<QVariant> bindings)
-> TSqlQuery
{
if (m_pretending)
return getQtQueryForPretend();
return getSqlQueryForPretend();

// Prepare QSqlQuery
auto query = prepareQuery(queryString_);
Expand Down Expand Up @@ -171,7 +171,7 @@ DatabaseConnection::statement(const QString &queryString, QList<QVariant> bindin
-> TSqlQuery
{
if (m_pretending)
return getQtQueryForPretend();
return getSqlQueryForPretend();

// Prepare QSqlQuery
auto query = prepareQuery(queryString_);
Expand Down Expand Up @@ -220,7 +220,7 @@ DatabaseConnection::affectingStatement(const QString &queryString,
-> std::tuple<int, TSqlQuery>
{
if (m_pretending)
return {-1, getQtQueryForPretend()};
return {-1, getSqlQueryForPretend()};

// Prepare QSqlQuery
auto query = prepareQuery(queryString_);
Expand Down Expand Up @@ -264,10 +264,10 @@ SqlQuery DatabaseConnection::unprepared(const QString &queryString)
-> TSqlQuery
{
if (m_pretending)
return getQtQueryForPretend();
return getSqlQueryForPretend();

// Prepare unprepared QSqlQuery 🙂
auto query = getQtQuery();
auto query = getSqlQuery();

if (query.exec(queryString_)) {
// Query statements counter
Expand Down Expand Up @@ -298,7 +298,7 @@ SqlQuery DatabaseConnection::unprepared(const QString &queryString)

/* Obtain connection instance */

TSqlDatabase DatabaseConnection::getQtConnection()
TSqlDatabase DatabaseConnection::getSqlConnection()
{
if (!m_qtConnection) {
// This should never happen 🤔
Expand All @@ -319,7 +319,7 @@ TSqlDatabase DatabaseConnection::getQtConnection()
return TSqlDatabase::database(*m_qtConnection, true);
}

TSqlDatabase DatabaseConnection::getRawQtConnection() const
TSqlDatabase DatabaseConnection::getRawSqlConnection() const
{
if (!m_qtConnection)
throw Exceptions::RuntimeError(
Expand All @@ -330,7 +330,7 @@ TSqlDatabase DatabaseConnection::getRawQtConnection() const
}

DatabaseConnection &
DatabaseConnection::setQtConnectionResolver(
DatabaseConnection::setSqlConnectionResolver(
const std::function<Connectors::ConnectionName()> &resolver)
{
/* Reset transaction and savepoints as the underlying connection will be
Expand All @@ -354,9 +354,9 @@ DatabaseConnection::setQtConnectionResolver(
return *this;
}

TSqlQuery DatabaseConnection::getQtQuery()
TSqlQuery DatabaseConnection::getSqlQuery()
{
return TSqlQuery(getQtConnection());
return TSqlQuery(getSqlConnection());
}

QList<QVariant> &
Expand Down Expand Up @@ -432,13 +432,13 @@ bool DatabaseConnection::pingDatabase()

const TSqlDriver *DatabaseConnection::driver()
{
return getQtConnection().driver();
return getSqlConnection().driver();
}

#ifdef TINYORM_USING_TINYDRIVERS
std::weak_ptr<const TSqlDriver> DatabaseConnection::driverWeak()
{
return getQtConnection().driverWeak();
return getSqlConnection().driverWeak();
}
#endif

Expand Down Expand Up @@ -483,7 +483,7 @@ void DatabaseConnection::disconnect()
from QSqlDatabase connection repository, so it can be reused, it's
better for performance.
Revisited, it's ok and will not cause any leaks or dangling connection. */
getRawQtConnection().close();
getRawSqlConnection().close();

m_qtConnection.reset();
m_qtConnectionResolver = nullptr;
Expand Down Expand Up @@ -532,7 +532,7 @@ bool DatabaseConnection::hasConfig(const QString &option) const

QString DatabaseConnection::driverName()
{
return getQtConnection().driverName();
return getSqlConnection().driverName();
}

/*! Printable driver name hash type. */
Expand Down Expand Up @@ -639,7 +639,7 @@ void DatabaseConnection::useDefaultPostProcessor()
TSqlQuery DatabaseConnection::prepareQuery(const QString &queryString)
{
// Prepare query string
auto query = getQtQuery();
auto query = getSqlQuery();

// TODO solve setForwardOnly() in DatabaseConnection class, again this problem 🤔 silverqx
// query.setForwardOnly(m_forwardOnly);
Expand Down
Loading

0 comments on commit 7f897b3

Please sign in to comment.