drivers added SqlQuery::isEmpty()

- used in some test methods
 - extracted throw an exception logic
This commit is contained in:
silverqx
2024-07-28 12:52:55 +02:00
parent 7c6270ec29
commit 840dbf2071
6 changed files with 45 additions and 9 deletions
@@ -160,9 +160,12 @@ namespace MySql
/*! Determine if the field with the given field name is NULL (QVariant value). */
bool isNull(const QString &name) const;
/*! Determine whether the current result contains any rows/records. */
bool isEmpty() const;
/*! Get the size of the result (number of rows returned), -1 if the size can't be
determined (database must support reporting about query size). */
size_type size() const;
/*! Get the number of affected rows for DML queries or -1 if the size can't be
determined. */
size_type numRowsAffected() const;
@@ -208,6 +211,8 @@ namespace MySql
void throwIfNoValidResultSet() const;
/*! Throw an exception if the field name doesn't exist or was not fetched. */
[[noreturn]] void throwNoFieldName(const QString &name) const;
/*! Throw an exception if database doesn't support reporting about query size. */
void throwIfNoQuerySizeReporting() const;
/* Constructors */
/*! Initialize implementation-dependent query result set for the default
@@ -164,6 +164,8 @@ namespace Orm::Drivers
/*! Determine whether the field at the given index is NULL (QVariant value). */
virtual bool isNull(size_type index) const = 0;
/*! Determine whether the current result contains any rows/records. */
virtual bool isEmpty() const noexcept = 0;
/*! Get the size of the result (number of rows returned), -1 if the size can't be
determined (database must support reporting about query size). */
virtual size_type size() const noexcept = 0;
+23 -9
View File
@@ -390,17 +390,17 @@ bool SqlQuery::isNull(const QString &name) const
throwNoFieldName(name);
}
bool SqlQuery::isEmpty() const
{
throwIfNoQuerySizeReporting();
throwIfNoResultSet();
return m_sqlResult->isEmpty();
}
SqlQuery::size_type SqlQuery::size() const
{
// Nothing to do
if (const auto driver = driverWeak().lock();
!driver->hasFeature(SqlDriver::QuerySize)
)
throw Exceptions::LogicError(
u"The '%1' database driver doesn't support query size reporting, "
"for '%2' database connection in %3()."_s
.arg(driver->driverName(), connectionName(), __tiny_func__));
throwIfNoQuerySizeReporting();
throwIfNoResultSet();
return m_sqlResult->size();
@@ -642,6 +642,20 @@ void SqlQuery::throwNoFieldName(const QString &name) const
.arg(name, connectionName(), __tiny_func__));
}
void SqlQuery::throwIfNoQuerySizeReporting() const
{
const auto driver = driverWeak().lock();
// Nothing to do
if (driver->hasFeature(SqlDriver::QuerySize))
return;
throw Exceptions::LogicError(
u"The '%1' database driver doesn't support query size reporting, "
"for '%2' database connection in %3()."_s
.arg(driver->driverName(), connectionName(), __tiny_func__));
}
/* Constructors */
std::unique_ptr<SqlResult> SqlQuery::initSqlResult()
@@ -69,6 +69,8 @@ namespace Orm::Drivers::MySql
/*! Determine whether the field at the given index is NULL. */
bool isNull(size_type index) const final;
/*! Determine whether the current result contains any rows/records. */
inline bool isEmpty() const noexcept final;
/*! Get the size of the result (number of rows returned), -1 if the size can't be
determined. */
size_type size() const noexcept final;
@@ -107,6 +109,13 @@ namespace Orm::Drivers::MySql
void cleanupForBoth();
};
/* public */
bool MySqlResult::isEmpty() const noexcept
{
return size() <= 0;
}
} // namespace Orm::Drivers::MySql
TINYORM_END_COMMON_NAMESPACE
@@ -126,6 +126,7 @@ void tst_SqlQuery_Normal::select_All() const
QCOMPARE(users.at(), BeforeFirstRow);
const auto querySize = users.size();
QCOMPARE(querySize, 5);
QVERIFY(!users.isEmpty());
// Behaves the same as the size() for SELECT queries
QCOMPARE(users.numRowsAffected(), 5);
QCOMPARE(users.numericalPrecisionPolicy(), LowPrecisionDouble);
@@ -177,6 +178,7 @@ void tst_SqlQuery_Normal::select_EmptyResultSet() const
QCOMPARE(users.at(), BeforeFirstRow);
const auto querySize = users.size();
QCOMPARE(querySize, 0);
QVERIFY(users.isEmpty());
// Behaves the same as the size() for SELECT queries
QCOMPARE(users.numRowsAffected(), 0);
QCOMPARE(users.executedQuery(), query);
@@ -212,6 +214,7 @@ void tst_SqlQuery_Normal::select_FetchingSameResultSet() const
QCOMPARE(users.at(), BeforeFirstRow);
const auto querySize = users.size();
QCOMPARE(querySize, 3);
QVERIFY(!users.isEmpty());
// Behaves the same as the size() for SELECT queries
QCOMPARE(users.numRowsAffected(), 3);
QCOMPARE(users.executedQuery(), query);
@@ -142,6 +142,7 @@ void tst_SqlQuery_Prepared::select_All() const
QCOMPARE(users.at(), BeforeFirstRow);
const auto querySize = users.size();
QCOMPARE(querySize, 5);
QVERIFY(!users.isEmpty());
// Behaves the same as the size() for SELECT queries
QCOMPARE(users.numRowsAffected(), 5);
QCOMPARE(users.numericalPrecisionPolicy(), LowPrecisionDouble);
@@ -201,6 +202,7 @@ void tst_SqlQuery_Prepared::select_EmptyResultSet() const
QCOMPARE(users.at(), BeforeFirstRow);
const auto querySize = users.size();
QCOMPARE(querySize, 0);
QVERIFY(users.isEmpty());
// Behaves the same as the size() for SELECT queries
QCOMPARE(users.numRowsAffected(), 0);
QCOMPARE(users.executedQuery(), query);
@@ -246,6 +248,7 @@ void tst_SqlQuery_Prepared::select_WithWhere() const
QCOMPARE(users.at(), BeforeFirstRow);
const auto querySize = users.size();
QCOMPARE(querySize, 3);
QVERIFY(!users.isEmpty());
// Behaves the same as the size() for SELECT queries
QCOMPARE(users.numRowsAffected(), 3);
QCOMPARE(users.executedQuery(), query);