diff options
| author | 2024-08-06 09:51:19 +0100 | |
|---|---|---|
| committer | 2024-08-06 09:51:19 +0100 | |
| commit | 424b5f6bf34d3ebd70841044a6950b3741516d2b (patch) | |
| tree | 35d4b495442aed1d1f1fcf8aa80007aef226420a /src/modules/extra/m_mysql.cpp | |
| parent | Fix the pgsql module showing duplicate packages on Ubuntu. (diff) | |
Add a helper template for checking MySQL errors.
Diffstat (limited to 'src/modules/extra/m_mysql.cpp')
| -rw-r--r-- | src/modules/extra/m_mysql.cpp | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/src/modules/extra/m_mysql.cpp b/src/modules/extra/m_mysql.cpp index 17df7ca4a..aaf87f076 100644 --- a/src/modules/extra/m_mysql.cpp +++ b/src/modules/extra/m_mysql.cpp @@ -85,6 +85,16 @@ class SQLConnection; class MySQLresult; class DispatcherThread; +namespace +{ + template<typename Numeric> + bool IsMySQLError(Numeric num) + { + // MySQL regularly returns -1 on error in unsigned fields. + return num == static_cast<Numeric>(-1); + } +} + struct QueryQueueItem final { // An SQL database which this query is executed on. @@ -170,8 +180,7 @@ public: MySQLresult(MYSQL_RES* res, unsigned long affected_rows) : err(SQL::SUCCESS) { - // mysql returns -1 on error even though the return type is unsigned - if (affected_rows >= 1 && affected_rows != (unsigned long)-1) + if (affected_rows >= 1 && !IsMySQLError(affected_rows)) { rows = int(affected_rows); fieldlists.resize(rows); @@ -285,7 +294,7 @@ private: // unsigned type even though -1 is returned on error so checking whether an error // happened is a bit cursed. unsigned long escapedsize = mysql_escape_string(buffer.data(), in.c_str(), in.length()); - if (escapedsize == static_cast<unsigned long>(-1)) + if (IsMySQLError(escapedsize)) { SQL::Error err(SQL::QSEND_FAIL, INSP_FORMAT("{}: {}", mysql_errno(connection), mysql_error(connection))); query->OnError(err); |
