--- 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);
}
--- 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 <sqlext.h>
#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<wchar_t>> 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::SqlDiagnosticsRecord> SqlException::getDiagnostics() const {
--- 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;