diff options
author | Lionel Elie Mamane <lionel@mamane.lu> | 2020-05-09 13:29:44 +0200 |
---|---|---|
committer | Lionel Elie Mamane <lionel@mamane.lu> | 2020-05-09 20:51:12 +0200 |
commit | c558ad61ba56d45a46e286c5f1329c436e009617 (patch) | |
tree | 20677d10f1dceab091778e33e7cf0aabef165cf4 /connectivity | |
parent | 2091098303b93cf267ef2d22dd890a8926ecb845 (diff) |
mysql-sdbc: fill SQLSTATE field of SQLException when throwing it
Change-Id: I0970fcfeb0ca92e6401b6f71ca4f54202fc27598
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/93848
Tested-by: Jenkins
Reviewed-by: Lionel Elie Mamane <lionel@mamane.lu>
Diffstat (limited to 'connectivity')
7 files changed, 56 insertions, 34 deletions
diff --git a/connectivity/source/drivers/mysqlc/mysqlc_connection.cxx b/connectivity/source/drivers/mysqlc/mysqlc_connection.cxx index 1070dd1fd68f..045da3b41a77 100644 --- a/connectivity/source/drivers/mysqlc/mysqlc_connection.cxx +++ b/connectivity/source/drivers/mysqlc/mysqlc_connection.cxx @@ -176,8 +176,9 @@ void OConnection::construct(const OUString& url, const Sequence<PropertyValue>& // flags can also be passed as last parameter if (!mysql_real_connect(&m_mysql, host_str.getStr(), user_str.getStr(), pass_str.getStr(), schema_str.getStr(), nPort, socket_str.getStr(), 0)) - mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_error(&m_mysql), mysql_errno(&m_mysql), - *this, getConnectionEncoding()); + mysqlc_sdbc_driver::throwSQLExceptionWithMsg( + mysql_error(&m_mysql), mysql_sqlstate(&m_mysql), mysql_errno(&m_mysql), *this, + getConnectionEncoding()); // Check if the server is 4.1 or above if (getMysqlVersion() < 40100) @@ -231,7 +232,8 @@ Reference<XPreparedStatement> SAL_CALL OConnection::prepareStatement(const OUStr unsigned int nErrorNum = mysql_errno(&m_mysql); if (nErrorNum != 0) - mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_error(&m_mysql), nErrorNum, *this, + mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_error(&m_mysql), + mysql_sqlstate(&m_mysql), nErrorNum, *this, getConnectionEncoding()); Reference<XPreparedStatement> xStatement = new OPreparedStatement(this, pStmt); @@ -262,8 +264,9 @@ void SAL_CALL OConnection::setAutoCommit(sal_Bool autoCommit) MutexGuard aGuard(m_aMutex); checkDisposed(OConnection_BASE::rBHelper.bDisposed); if (!mysql_autocommit(&m_mysql, autoCommit)) - mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_error(&m_mysql), mysql_errno(&m_mysql), - *this, getConnectionEncoding()); + mysqlc_sdbc_driver::throwSQLExceptionWithMsg( + mysql_error(&m_mysql), mysql_sqlstate(&m_mysql), mysql_errno(&m_mysql), *this, + getConnectionEncoding()); } sal_Bool SAL_CALL OConnection::getAutoCommit() @@ -284,8 +287,9 @@ void SAL_CALL OConnection::commit() checkDisposed(OConnection_BASE::rBHelper.bDisposed); if (!mysql_commit(&m_mysql)) - mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_error(&m_mysql), mysql_errno(&m_mysql), - *this, getConnectionEncoding()); + mysqlc_sdbc_driver::throwSQLExceptionWithMsg( + mysql_error(&m_mysql), mysql_sqlstate(&m_mysql), mysql_errno(&m_mysql), *this, + getConnectionEncoding()); } void SAL_CALL OConnection::rollback() @@ -294,8 +298,9 @@ void SAL_CALL OConnection::rollback() checkDisposed(OConnection_BASE::rBHelper.bDisposed); if (!mysql_rollback(&m_mysql)) - mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_error(&m_mysql), mysql_errno(&m_mysql), - *this, getConnectionEncoding()); + mysqlc_sdbc_driver::throwSQLExceptionWithMsg( + mysql_error(&m_mysql), mysql_sqlstate(&m_mysql), mysql_errno(&m_mysql), *this, + getConnectionEncoding()); } sal_Bool SAL_CALL OConnection::isClosed() diff --git a/connectivity/source/drivers/mysqlc/mysqlc_general.cxx b/connectivity/source/drivers/mysqlc/mysqlc_general.cxx index 90995ad31315..7ed11fe3ff13 100644 --- a/connectivity/source/drivers/mysqlc/mysqlc_general.cxx +++ b/connectivity/source/drivers/mysqlc/mysqlc_general.cxx @@ -104,14 +104,19 @@ void throwInvalidArgumentException(const char* _pAsciiFeatureName, throw SQLException(sMessage, _rxContext, "HYC00", 0, Any()); } -void throwSQLExceptionWithMsg(const char* msg, unsigned int errorNum, +void throwSQLExceptionWithMsg(const char* msg, const char* SQLSTATE, unsigned int errorNum, const css::uno::Reference<css::uno::XInterface>& _context, const rtl_TextEncoding encoding) { OString errorMsg{ msg }; - // TODO error code? - throw SQLException(OStringToOUString(errorMsg, encoding), _context, OUString(), errorNum, - Any()); + throwSQLExceptionWithMsg(OStringToOUString(errorMsg, encoding), SQLSTATE, errorNum, _context); +} + +void throwSQLExceptionWithMsg(const OUString& msg, const char* SQLSTATE, unsigned int errorNum, + const css::uno::Reference<css::uno::XInterface>& _context) +{ + throw SQLException(msg, _context, OStringToOUString(SQLSTATE, RTL_TEXTENCODING_ASCII_US), + errorNum, Any()); } sal_Int32 mysqlToOOOType(int eType, int charsetnr) noexcept diff --git a/connectivity/source/drivers/mysqlc/mysqlc_general.hxx b/connectivity/source/drivers/mysqlc/mysqlc_general.hxx index 6fc9e46f71f2..c00a11f412c5 100644 --- a/connectivity/source/drivers/mysqlc/mysqlc_general.hxx +++ b/connectivity/source/drivers/mysqlc/mysqlc_general.hxx @@ -98,10 +98,13 @@ void throwFeatureNotImplementedException( void throwInvalidArgumentException(const char* _pAsciiFeatureName, const css::uno::Reference<css::uno::XInterface>& _rxContext); -void throwSQLExceptionWithMsg(const char* msg, unsigned int errorNum, +void throwSQLExceptionWithMsg(const char* msg, const char* SQLSTATE, unsigned int errorNum, const css::uno::Reference<css::uno::XInterface>& _context, const rtl_TextEncoding encoding); +void throwSQLExceptionWithMsg(const OUString& msg, const char* SQLSTATE, unsigned int errorNum, + const css::uno::Reference<css::uno::XInterface>& _context); + sal_Int32 mysqlToOOOType(int eType, int charsetnr) noexcept; OUString mysqlTypeToStr(unsigned mysql_type, unsigned mysql_flags); diff --git a/connectivity/source/drivers/mysqlc/mysqlc_prepared_resultset.cxx b/connectivity/source/drivers/mysqlc/mysqlc_prepared_resultset.cxx index 93b31eb99604..8031bfdf24b0 100644 --- a/connectivity/source/drivers/mysqlc/mysqlc_prepared_resultset.cxx +++ b/connectivity/source/drivers/mysqlc/mysqlc_prepared_resultset.cxx @@ -143,8 +143,8 @@ bool OPreparedResultSet::fetchResult() if (failure == 1) { MYSQL* pMysql = m_rConnection.getMysqlConnection(); - mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_error(pMysql), mysql_errno(pMysql), - *this, m_encoding); + mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_error(pMysql), mysql_sqlstate(pMysql), + mysql_errno(pMysql), *this, m_encoding); } else if (failure == MYSQL_NO_DATA) return false; @@ -341,12 +341,12 @@ ORowSetValue OPreparedResultSet::getRowSetValue(sal_Int32 nColumnIndex) case MYSQL_TYPE_NEWDECIMAL: return getString(nColumnIndex); case MYSQL_TYPE_BLOB: - throw SQLException("Column with type BLOB cannot be converted", *this, OUString(), 1, + throw SQLException("Column with type BLOB cannot be converted", *this, "22000", 1, Any()); default: SAL_WARN("connectivity.mysqlc", "OPreparedResultSet::getRowSetValue: unknown type: " << m_aFields[nColumnIndex - 1].type); - throw SQLException("Unknown column type when fetching result", *this, OUString(), 1, + throw SQLException("Unknown column type when fetching result", *this, "22000", 1, Any()); } } @@ -1073,11 +1073,11 @@ css::uno::Reference<css::beans::XPropertySetInfo> SAL_CALL OPreparedResultSet::g void OPreparedResultSet::checkColumnIndex(sal_Int32 index) { if (!m_aData) - throw SQLException("Cursor out of range", *this, OUString(), 1, Any()); + throw SQLException("Cursor out of range", *this, "HY109", 1, Any()); if (index < 1 || index > static_cast<int>(m_nColumnCount)) { /* static object for efficiency or thread safety is a problem ? */ - throw SQLException("index out of range", *this, OUString(), 1, Any()); + throw SQLException("index out of range", *this, "42S22", 1, Any()); } } diff --git a/connectivity/source/drivers/mysqlc/mysqlc_preparedstatement.cxx b/connectivity/source/drivers/mysqlc/mysqlc_preparedstatement.cxx index d105365e39b1..8c9ec1d250db 100644 --- a/connectivity/source/drivers/mysqlc/mysqlc_preparedstatement.cxx +++ b/connectivity/source/drivers/mysqlc/mysqlc_preparedstatement.cxx @@ -129,7 +129,8 @@ sal_Bool SAL_CALL OPreparedStatement::execute() if (!m_binds.empty() && mysql_stmt_bind_param(m_pStmt, m_binds.data())) { MYSQL* pMysql = m_xConnection->getMysqlConnection(); - mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_stmt_error(m_pStmt), mysql_errno(pMysql), + mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_stmt_error(m_pStmt), + mysql_sqlstate(pMysql), mysql_errno(pMysql), *this, m_xConnection->getConnectionEncoding()); } @@ -137,7 +138,8 @@ sal_Bool SAL_CALL OPreparedStatement::execute() if (nFail != 0) { MYSQL* pMysql = m_xConnection->getMysqlConnection(); - mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_stmt_error(m_pStmt), mysql_errno(pMysql), + mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_stmt_error(m_pStmt), + mysql_sqlstate(pMysql), mysql_errno(pMysql), *this, m_xConnection->getConnectionEncoding()); } @@ -152,7 +154,8 @@ sal_Int32 SAL_CALL OPreparedStatement::executeUpdate() if (!m_binds.empty() && mysql_stmt_bind_param(m_pStmt, m_binds.data())) { MYSQL* pMysql = m_xConnection->getMysqlConnection(); - mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_stmt_error(m_pStmt), mysql_errno(pMysql), + mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_stmt_error(m_pStmt), + mysql_sqlstate(pMysql), mysql_errno(pMysql), *this, m_xConnection->getConnectionEncoding()); } @@ -161,7 +164,8 @@ sal_Int32 SAL_CALL OPreparedStatement::executeUpdate() if (nFail != 0) { MYSQL* pMysql = m_xConnection->getMysqlConnection(); - mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_stmt_error(m_pStmt), mysql_errno(pMysql), + mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_stmt_error(m_pStmt), + mysql_sqlstate(pMysql), mysql_errno(pMysql), *this, m_xConnection->getConnectionEncoding()); } @@ -200,7 +204,8 @@ Reference<XResultSet> SAL_CALL OPreparedStatement::executeQuery() if (!m_binds.empty() && mysql_stmt_bind_param(m_pStmt, m_binds.data())) { MYSQL* pMysql = m_xConnection->getMysqlConnection(); - mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_stmt_error(m_pStmt), mysql_errno(pMysql), + mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_stmt_error(m_pStmt), + mysql_sqlstate(pMysql), mysql_errno(pMysql), *this, m_xConnection->getConnectionEncoding()); } @@ -209,7 +214,8 @@ Reference<XResultSet> SAL_CALL OPreparedStatement::executeQuery() if (nFail != 0) { MYSQL* pMysql = m_xConnection->getMysqlConnection(); - mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_stmt_error(m_pStmt), mysql_errno(pMysql), + mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_stmt_error(m_pStmt), + mysql_sqlstate(pMysql), mysql_errno(pMysql), *this, m_xConnection->getConnectionEncoding()); } diff --git a/connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx b/connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx index 1168d79470cb..bd405dea973d 100644 --- a/connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx +++ b/connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx @@ -163,8 +163,8 @@ void OResultSet::fetchResult() } unsigned errorNum = mysql_errno(m_pMysql); if (errorNum) - mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_error(m_pMysql), errorNum, *this, - m_encoding); + mysqlc_sdbc_driver::throwSQLExceptionWithMsg( + mysql_error(m_pMysql), mysql_sqlstate(m_pMysql), errorNum, *this, m_encoding); m_bResultFetched = true; mysql_free_result(m_pResult); } diff --git a/connectivity/source/drivers/mysqlc/mysqlc_statement.cxx b/connectivity/source/drivers/mysqlc/mysqlc_statement.cxx index 5444389ec17a..5e9128a80f1a 100644 --- a/connectivity/source/drivers/mysqlc/mysqlc_statement.cxx +++ b/connectivity/source/drivers/mysqlc/mysqlc_statement.cxx @@ -130,8 +130,9 @@ sal_Bool SAL_CALL OCommonStatement::execute(const OUString& sql) int failure = mysql_real_query(pMySql, toExec.getStr(), toExec.getLength()); if (failure) - mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_error(pMySql), mysql_errno(pMySql), - *this, m_xConnection->getConnectionEncoding()); + mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_error(pMySql), mysql_sqlstate(pMySql), + mysql_errno(pMySql), *this, + m_xConnection->getConnectionEncoding()); m_nAffectedRows = mysql_affected_rows(pMySql); return !failure; @@ -149,14 +150,16 @@ Reference<XResultSet> SAL_CALL OCommonStatement::executeQuery(const OUString& sq // toExec = mysqlc_sdbc_driver::escapeSql(toExec); int failure = mysql_real_query(pMySql, toExec.getStr(), toExec.getLength()); if (failure) - mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_error(pMySql), mysql_errno(pMySql), - *this, m_xConnection->getConnectionEncoding()); + mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_error(pMySql), mysql_sqlstate(pMySql), + mysql_errno(pMySql), *this, + m_xConnection->getConnectionEncoding()); m_pMysqlResult = mysql_store_result(pMySql); if (m_pMysqlResult == nullptr) { - mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_error(pMySql), mysql_errno(pMySql), - *this, m_xConnection->getConnectionEncoding()); + mysqlc_sdbc_driver::throwSQLExceptionWithMsg(mysql_error(pMySql), mysql_sqlstate(pMySql), + mysql_errno(pMySql), *this, + m_xConnection->getConnectionEncoding()); } m_xResultSet = new OResultSet(*getOwnConnection(), this, m_pMysqlResult, |