# HG changeset patch # User František Kučera # Date 1591024462 -7200 # Node ID b4af13653313085aa0f54e360de041713f2ec71c # Parent a871779a4e3c2627ee108736eda315df62d5c395 improved exception handling: diagnostics of prepare statement error diff -r a871779a4e3c -r b4af13653313 src/Connection.cpp --- a/src/Connection.cpp Mon Jun 01 00:14:40 2020 +0200 +++ b/src/Connection.cpp Mon Jun 01 17:14:22 2020 +0200 @@ -41,10 +41,7 @@ PreparedStatement* Connection::prepareStatement(relpipe::reader::string_t sql) { SQLHSTMT statement = OdbcCommon::allocateHandle(SQL_HANDLE_STMT, connection); SQLRETURN result = SQLPrepare(statement, (SQLCHAR*) convertor.to_bytes(sql).c_str(), SQL_NTS); - if (OdbcCommon::isNotSuccessful(result)) { - OdbcCommon::freeHandle(SQL_HANDLE_STMT, statement); - throw SqlException(L"Unable to prepare statement", result, SQL_HANDLE_DBC, connection); // TODO: SQL_HANDLE_STMT? - } + if (OdbcCommon::isNotSuccessful(result)) throw SqlException(L"Unable to prepare statement", result, SQL_HANDLE_STMT, statement, true); return new PreparedStatement(statement); } diff -r a871779a4e3c -r b4af13653313 src/SqlException.cpp --- a/src/SqlException.cpp Mon Jun 01 00:14:40 2020 +0200 +++ b/src/SqlException.cpp Mon Jun 01 17:14:22 2020 +0200 @@ -24,6 +24,7 @@ #include #include "SqlException.h" +#include "OdbcCommon.h" namespace relpipe { namespace tr { @@ -32,7 +33,7 @@ SqlException::SqlException(std::wstring message) : message(message) { } -SqlException::SqlException(std::wstring message, SQLRETURN resultCode, SQLSMALLINT handleType, SQLHANDLE handle) : message(message), resultCode(resultCode) { +SqlException::SqlException(std::wstring message, SQLRETURN resultCode, SQLSMALLINT handleType, SQLHANDLE handle, bool freeHandle) : message(message), resultCode(resultCode) { std::wstring_convert < std::codecvt_utf8> convertor; // TODO: support also other encodings SQLCHAR buffer[SQL_MAX_MESSAGE_LENGTH + 1]; SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1]; @@ -43,6 +44,7 @@ for (SQLSMALLINT i = 1; SQLGetDiagRec(handleType, handle, i, sqlstate, &sqlcode, buffer, SQL_MAX_MESSAGE_LENGTH + 1, &length) == SQL_SUCCESS; i++) { diagnostics.push_back({convertor.from_bytes((char*) sqlstate), sqlcode, convertor.from_bytes((char*) buffer)}); } + if (freeHandle) OdbcCommon::freeHandle(handleType, handle); } std::vector SqlException::getDiagnostics() const { diff -r a871779a4e3c -r b4af13653313 src/SqlException.h --- a/src/SqlException.h Mon Jun 01 00:14:40 2020 +0200 +++ b/src/SqlException.h Mon Jun 01 17:14:22 2020 +0200 @@ -41,7 +41,7 @@ SqlException(std::wstring message); - SqlException(std::wstring message, signed short int resultCode, signed short int handleType, void* handle); + SqlException(std::wstring message, signed short int resultCode, signed short int handleType, void* handle, bool freeHandle = false); std::wstring getMessage() const;