Skip to content

Commit

Permalink
count query result size manually if needed
Browse files Browse the repository at this point in the history
Fixed all query.size() calls, added QueryUtils::queryResultSize() helper
method that counts query result size manually if needed.
  • Loading branch information
silverqx committed Jun 23, 2022
1 parent 8e1d0ec commit 38dcfca
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
6 changes: 5 additions & 1 deletion include/orm/query/querybuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ TINY_SYSTEM_HEADER
#include "orm/ormconcepts.hpp"
#include "orm/ormtypes.hpp"
#include "orm/query/grammars/grammar.hpp"
#include "orm/utils/query.hpp"

TINYORM_BEGIN_COMMON_NAMESPACE

Expand All @@ -29,7 +30,10 @@ namespace Orm::Query
/*! Database query builder. */
class SHAREDLIB_EXPORT Builder // clazy:exclude=copyable-polymorphic
{
/*! Alias for the query grammar. */
using QueryGrammar = Query::Grammars::Grammar;
/*! Alias for query utils. */
using QueryUtils = Orm::Utils::Query;

public:
/*! Constructor. */
Expand Down Expand Up @@ -708,7 +712,7 @@ namespace Orm::Query
the results and get the exact data that was requested for the query. */
auto query = get({column, key});

const auto size = query.size();
const auto size = QueryUtils::queryResultSize(query, m_connection);

// Empty result
if (size == 0)
Expand Down
12 changes: 10 additions & 2 deletions include/orm/utils/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ class QSqlQuery;

TINYORM_BEGIN_COMMON_NAMESPACE

namespace Orm::Utils
namespace Orm
{
class DatabaseConnection;

namespace Utils
{

/*! Library class for database query. */
Expand Down Expand Up @@ -43,9 +47,13 @@ namespace Orm::Utils
static QVector<QVariantMap>
zipForInsert(const QVector<QString> &columns,
const QVector<QVector<QVariant>> &values);

/*! Returns the size of the result (number of rows returned). */
static int queryResultSize(QSqlQuery &query, DatabaseConnection &connection);
};

} // namespace Orm::Utils
} // namespace Utils
} // namespace Orm

TINYORM_END_COMMON_NAMESPACE

Expand Down
2 changes: 1 addition & 1 deletion src/orm/query/querybuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ QVector<QVariant> Builder::pluck(const QString &column)
and get the exact data that was requested for the query. */
auto query = get({column});

const auto size = query.size();
const auto size = QueryUtils::queryResultSize(query, m_connection);

// Empty result
if (size == 0)
Expand Down
13 changes: 4 additions & 9 deletions src/orm/schema/schemabuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

#include "orm/databaseconnection.hpp"
#include "orm/exceptions/logicerror.hpp"
#include "orm/utils/query.hpp"

using QueryUtils = Orm::Utils::Query;

TINYORM_BEGIN_COMMON_NAMESPACE

Expand Down Expand Up @@ -165,15 +168,7 @@ bool SchemaBuilder::hasTable(const QString &table) const
auto query = m_connection.selectFromWriteConnection(
m_grammar.compileTableExists(), {table_});

if (m_connection.driver()->hasFeature(QSqlDriver::QuerySize))
return query.size() > 0;

// Count manually
auto size = 0;
while (query.next())
++size;

return size > 0;
return QueryUtils::queryResultSize(query, m_connection) > 0;
}

// CUR schema, test in functional tests silverqx
Expand Down
19 changes: 19 additions & 0 deletions src/orm/utils/query.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "orm/utils/query.hpp"

#include <QDebug>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlQuery>

#include "orm/databaseconnection.hpp"
#include "orm/exceptions/invalidargumenterror.hpp"
#include "orm/utils/type.hpp"

Expand Down Expand Up @@ -115,6 +117,23 @@ Query::zipForInsert(const QVector<QString> &columns,
return zippedValues;
}

int Query::queryResultSize(QSqlQuery &query, DatabaseConnection &connection)
{
if (connection.driver()->hasFeature(QSqlDriver::QuerySize))
return query.size();

query.seek(QSql::BeforeFirstRow);

// Count manually
int size = 0;
while (query.next())
++size;

query.seek(QSql::BeforeFirstRow);

return size;
}

} // namespace Orm::Utils

TINYORM_END_COMMON_NAMESPACE

0 comments on commit 38dcfca

Please sign in to comment.