author | František Kučera <franta-hg@frantovo.cz> |
Tue, 02 Jun 2020 23:31:55 +0200 | |
branch | v_0 |
changeset 44 | ec9694f3b343 |
parent 39 | b4af13653313 |
child 45 | 225e294ad1db |
permissions | -rw-r--r-- |
35
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* Relational pipes |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
|
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
#include <cstring> |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
#include <codecvt> |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <locale> |
36
91cb012d779a
use ODBC, avoid direct dependency on SQLite
František Kučera <franta-hg@frantovo.cz>
parents:
35
diff
changeset
|
21 |
#include <iostream> |
35
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
|
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include <sql.h> |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
#include <sqlext.h> |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
|
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
#include "SqlException.h" |
39
b4af13653313
improved exception handling: diagnostics of prepare statement error
František Kučera <franta-hg@frantovo.cz>
parents:
38
diff
changeset
|
27 |
#include "OdbcCommon.h" |
35
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
|
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
namespace relpipe { |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
namespace tr { |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
namespace sql { |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
|
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
SqlException::SqlException(std::wstring message) : message(message) { |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
} |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
|
39
b4af13653313
improved exception handling: diagnostics of prepare statement error
František Kučera <franta-hg@frantovo.cz>
parents:
38
diff
changeset
|
36 |
SqlException::SqlException(std::wstring message, SQLRETURN resultCode, SQLSMALLINT handleType, SQLHANDLE handle, bool freeHandle) : message(message), resultCode(resultCode) { |
35
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
SQLCHAR buffer[SQL_MAX_MESSAGE_LENGTH + 1]; |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1]; |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
memset(buffer, 0, sizeof (buffer)); |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
memset(sqlstate, 0, sizeof (sqlstate)); |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
SQLINTEGER sqlcode; |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
SQLSMALLINT length; |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
for (SQLSMALLINT i = 1; SQLGetDiagRec(handleType, handle, i, sqlstate, &sqlcode, buffer, SQL_MAX_MESSAGE_LENGTH + 1, &length) == SQL_SUCCESS; i++) { |
44
ec9694f3b343
faulty SQLGetDiagRec() call, unicode error, temporary workaround
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
45 |
for (size_t i = 0; i < sizeof (buffer); i++) if (buffer[i] > 126 || buffer[i] == '\r') buffer[i] = '?'; // keep only ASCII characters and avoid CR which breaks the output |
35
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
diagnostics.push_back({convertor.from_bytes((char*) sqlstate), sqlcode, convertor.from_bytes((char*) buffer)}); |
44
ec9694f3b343
faulty SQLGetDiagRec() call, unicode error, temporary workaround
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
47 |
// FIXME: decoding fails is error message contains unicode characters – exception is thrown: |
ec9694f3b343
faulty SQLGetDiagRec() call, unicode error, temporary workaround
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
48 |
// terminate called after throwing an instance of 'std::range_error' |
ec9694f3b343
faulty SQLGetDiagRec() call, unicode error, temporary workaround
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
49 |
// what(): wstring_convert::from_bytes |
ec9694f3b343
faulty SQLGetDiagRec() call, unicode error, temporary workaround
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
50 |
// Exception can be avoided by: |
ec9694f3b343
faulty SQLGetDiagRec() call, unicode error, temporary workaround
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
51 |
// std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor("", L"XXX Unable to decode error message from SQLGetDiagRec()"); |
ec9694f3b343
faulty SQLGetDiagRec() call, unicode error, temporary workaround
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
52 |
// but actual error message is then lost. |
ec9694f3b343
faulty SQLGetDiagRec() call, unicode error, temporary workaround
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
53 |
// So as a workaround we keep only ASCII characters. |
ec9694f3b343
faulty SQLGetDiagRec() call, unicode error, temporary workaround
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
54 |
// It seems that we sometimes get valid ISO-8859-2 or ISO-8859-1 encoded messages even if our platform encoding is UTF-8. |
ec9694f3b343
faulty SQLGetDiagRec() call, unicode error, temporary workaround
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
55 |
|
35
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
} |
39
b4af13653313
improved exception handling: diagnostics of prepare statement error
František Kučera <franta-hg@frantovo.cz>
parents:
38
diff
changeset
|
57 |
if (freeHandle) OdbcCommon::freeHandle(handleType, handle); |
35
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
58 |
} |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
|
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
60 |
std::vector<SqlException::SqlDiagnosticsRecord> SqlException::getDiagnostics() const { |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
return diagnostics; |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
} |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
|
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
std::wstring SqlException::getMessage() const { |
36
91cb012d779a
use ODBC, avoid direct dependency on SQLite
František Kučera <franta-hg@frantovo.cz>
parents:
35
diff
changeset
|
65 |
if (resultCode) return message + L" SQLRETURN=" + std::to_wstring(resultCode); |
91cb012d779a
use ODBC, avoid direct dependency on SQLite
František Kučera <franta-hg@frantovo.cz>
parents:
35
diff
changeset
|
66 |
else return message; |
35
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
} |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
|
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
SQLRETURN SqlException::getResultCode() const { |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
return resultCode; |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
} |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
|
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
|
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
|
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
75 |
} |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
76 |
} |
cd9db43db120
SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
77 |
} |