22 |
22 |
23 #include <sql.h> |
23 #include <sql.h> |
24 #include <sqlext.h> |
24 #include <sqlext.h> |
25 |
25 |
26 #include "SqlException.h" |
26 #include "SqlException.h" |
|
27 #include "OdbcCommon.h" |
27 |
28 |
28 namespace relpipe { |
29 namespace relpipe { |
29 namespace tr { |
30 namespace tr { |
30 namespace sql { |
31 namespace sql { |
31 |
32 |
32 SqlException::SqlException(std::wstring message) : message(message) { |
33 SqlException::SqlException(std::wstring message) : message(message) { |
33 } |
34 } |
34 |
35 |
35 SqlException::SqlException(std::wstring message, SQLRETURN resultCode, SQLSMALLINT handleType, SQLHANDLE handle) : message(message), resultCode(resultCode) { |
36 SqlException::SqlException(std::wstring message, SQLRETURN resultCode, SQLSMALLINT handleType, SQLHANDLE handle, bool freeHandle) : message(message), resultCode(resultCode) { |
36 std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings |
37 std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings |
37 SQLCHAR buffer[SQL_MAX_MESSAGE_LENGTH + 1]; |
38 SQLCHAR buffer[SQL_MAX_MESSAGE_LENGTH + 1]; |
38 SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1]; |
39 SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1]; |
39 memset(buffer, 0, sizeof (buffer)); |
40 memset(buffer, 0, sizeof (buffer)); |
40 memset(sqlstate, 0, sizeof (sqlstate)); |
41 memset(sqlstate, 0, sizeof (sqlstate)); |
41 SQLINTEGER sqlcode; |
42 SQLINTEGER sqlcode; |
42 SQLSMALLINT length; |
43 SQLSMALLINT length; |
43 for (SQLSMALLINT i = 1; SQLGetDiagRec(handleType, handle, i, sqlstate, &sqlcode, buffer, SQL_MAX_MESSAGE_LENGTH + 1, &length) == SQL_SUCCESS; i++) { |
44 for (SQLSMALLINT i = 1; SQLGetDiagRec(handleType, handle, i, sqlstate, &sqlcode, buffer, SQL_MAX_MESSAGE_LENGTH + 1, &length) == SQL_SUCCESS; i++) { |
44 diagnostics.push_back({convertor.from_bytes((char*) sqlstate), sqlcode, convertor.from_bytes((char*) buffer)}); |
45 diagnostics.push_back({convertor.from_bytes((char*) sqlstate), sqlcode, convertor.from_bytes((char*) buffer)}); |
45 } |
46 } |
|
47 if (freeHandle) OdbcCommon::freeHandle(handleType, handle); |
46 } |
48 } |
47 |
49 |
48 std::vector<SqlException::SqlDiagnosticsRecord> SqlException::getDiagnostics() const { |
50 std::vector<SqlException::SqlDiagnosticsRecord> SqlException::getDiagnostics() const { |
49 return diagnostics; |
51 return diagnostics; |
50 } |
52 } |