Skip to content

Commit

Permalink
added Concerns::ExplainQueries
Browse files Browse the repository at this point in the history
It is not enabled because Qt sql driver doesn't support EXPLAIN queries.
  • Loading branch information
silverqx committed Jan 18, 2022
1 parent 4c2351e commit 76ad095
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 11 deletions.
48 changes: 48 additions & 0 deletions include/orm/concerns/explainqueries.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once
#ifndef ORM_CONCERNS_EXPLAINQUERIES_HPP
#define ORM_CONCERNS_EXPLAINQUERIES_HPP

#include "orm/macros/systemheader.hpp"
TINY_SYSTEM_HEADER

#include <QtSql/QSqlQuery>

#include "orm/macros/commonnamespace.hpp"
#include "orm/macros/export.hpp"

TINYORM_BEGIN_COMMON_NAMESPACE

namespace Orm
{
namespace Query
{
class Builder;
}
using QueryBuilder = Query::Builder;

namespace Concerns
{

/*! Counts the number of executed queries and the elapsed time of queries. */
class SHAREDLIB_EXPORT ExplainQueries
{
public:
/*! Default constructor. */
inline ExplainQueries() = default;
/*! Virtual destructor. */
inline virtual ~ExplainQueries() = default;

/*! Explains the query. */
QSqlQuery explain();

private:
/*! Dynamic cast *this to the QueryBuilder & derived type. */
QueryBuilder &builder();
};

} // namespace Concerns
} // namespace Orm

TINYORM_END_COMMON_NAMESPACE

#endif // ORM_CONCERNS_EXPLAINQUERIES_HPP
11 changes: 0 additions & 11 deletions include/orm/tiny/tinybuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@ namespace Orm::Tiny
// FUTURE add Query Scopes feature silverqx
// { return $this->applyScopes()->getQuery(); }

/*! Explains the query. */
// QSqlQuery explain() const;

/*! Qualify the given column name by the model's table. */
inline QString qualifyColumn(const QString &column) const;

Expand Down Expand Up @@ -717,14 +714,6 @@ namespace Orm::Tiny
return m_model.qualifyColumn(column);
}

// BUG Qt sql driver does not support to call EXPLAIN as a prepared statement silverqx
// template<typename Model>
// QSqlQuery Builder<Model>::explain() const
// {
// return getConnection().select(QStringLiteral("EXPLAIN %1").arg(toSql()),
// getBindings());
// }

template<typename Model>
QVector<WithItem>
Builder<Model>::parseWithRelations(const QVector<WithItem> &relations)
Expand Down
30 changes: 30 additions & 0 deletions src/orm/concerns/explainqueries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "orm/concerns/explainqueries.hpp"

#include "orm/databaseconnection.hpp"
#include "orm/query/querybuilder.hpp"

TINYORM_BEGIN_COMMON_NAMESPACE

namespace Orm::Concerns
{

/* public */

// BUG Qt sql driver does not support to call EXPLAIN as a prepared statement, look at enum StatementType and QSqlDriver::sqlStatement in qsqldriver.h/cpp, also don't forget to add proxies when Qt will support EXPLAIN queries silverqx
QSqlQuery ExplainQueries::explain()
{
return builder().getConnection().select(
QStringLiteral("EXPLAIN %1").arg(builder().toSql()),
builder().getBindings());
}

/* private */

QueryBuilder &ExplainQueries::builder()
{
return dynamic_cast<QueryBuilder &>(*this);
}

} // namespace Orm::Concerns

TINYORM_END_COMMON_NAMESPACE

0 comments on commit 76ad095

Please sign in to comment.