mirror of
https://github.com/silverqx/TinyORM.git
synced 2026-05-23 02:08:48 -05:00
enhanced exception for sole()
This commit is contained in:
@@ -17,22 +17,29 @@ namespace Orm::Exceptions
|
||||
{
|
||||
public:
|
||||
/*! Constructor. */
|
||||
inline explicit MultipleRecordsFoundError(int count);
|
||||
inline explicit MultipleRecordsFoundError(int count, QString &&functionName);
|
||||
|
||||
/*! Get the number of records found. */
|
||||
inline int count() const noexcept;
|
||||
/*! Get a function name where the exception occured. */
|
||||
inline const QString &functionName() const noexcept;
|
||||
|
||||
protected:
|
||||
/*! The number of records found. */
|
||||
int m_count;
|
||||
/*! Function name where the exception occured. */
|
||||
QString m_functionName;
|
||||
};
|
||||
|
||||
/* public */
|
||||
|
||||
MultipleRecordsFoundError::MultipleRecordsFoundError(const int count)
|
||||
: RuntimeError(QStringLiteral("%1 records were found.").arg(count)
|
||||
MultipleRecordsFoundError::MultipleRecordsFoundError(const int count,
|
||||
QString &&functionName)
|
||||
: RuntimeError(QStringLiteral("%1 records were found in %2().")
|
||||
.arg(count).arg(functionName)
|
||||
.toUtf8().constData())
|
||||
, m_count(count)
|
||||
, m_functionName(std::move(functionName))
|
||||
{}
|
||||
|
||||
int MultipleRecordsFoundError::count() const noexcept
|
||||
@@ -40,6 +47,11 @@ namespace Orm::Exceptions
|
||||
return m_count;
|
||||
}
|
||||
|
||||
const QString &MultipleRecordsFoundError::functionName() const noexcept
|
||||
{
|
||||
return m_functionName;
|
||||
}
|
||||
|
||||
} // namespace Orm::Exceptions
|
||||
|
||||
TINYORM_END_COMMON_NAMESPACE
|
||||
|
||||
@@ -287,7 +287,7 @@ namespace Concerns
|
||||
QStringLiteral("No records found in %1().").arg(__tiny_func__));
|
||||
|
||||
if (count > 1)
|
||||
throw Orm::Exceptions::MultipleRecordsFoundError(count);
|
||||
throw Orm::Exceptions::MultipleRecordsFoundError(count, __tiny_func__);
|
||||
|
||||
return std::move(models.first());
|
||||
}
|
||||
|
||||
@@ -185,7 +185,7 @@ SqlQuery BuildsQueries::sole(const QVector<Column> &columns)
|
||||
QStringLiteral("No records found in %1().").arg(__tiny_func__));
|
||||
|
||||
if (count > 1)
|
||||
throw Exceptions::MultipleRecordsFoundError(count);
|
||||
throw Exceptions::MultipleRecordsFoundError(count, __tiny_func__);
|
||||
|
||||
query.first();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user