src/SqlException.cpp
author František Kučera <franta-hg@frantovo.cz>
Mon, 25 May 2020 21:11:17 +0200
branchv_0
changeset 35 cd9db43db120
child 36 91cb012d779a
permissions -rw-r--r--
SqlException: ODBC diagnostics
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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>
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <sql.h>
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sqlext.h>
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include "SqlException.h"
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
namespace relpipe {
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace tr {
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace sql {
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
SqlException::SqlException(std::wstring message) : message(message) {
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
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
SqlException::SqlException(std::wstring message, SQLRETURN resultCode, SQLSMALLINT handleType, SQLHANDLE handle) : message(message), resultCode(resultCode) {
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	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
    36
	SQLCHAR buffer[SQL_MAX_MESSAGE_LENGTH + 1];
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1];
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	memset(buffer, 0, sizeof (buffer));
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	memset(sqlstate, 0, sizeof (sqlstate));
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	SQLINTEGER sqlcode;
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	SQLSMALLINT length;
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	for (SQLSMALLINT i = 1; SQLGetDiagRec(handleType, handle, i, sqlstate, &sqlcode, buffer, SQL_MAX_MESSAGE_LENGTH + 1, &length) == SQL_SUCCESS; i++) {
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		diagnostics.push_back({convertor.from_bytes((char*) sqlstate), sqlcode, convertor.from_bytes((char*) buffer)});
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	}
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
}
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
std::vector<SqlException::SqlDiagnosticsRecord> SqlException::getDiagnostics() const {
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	return diagnostics;
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
}
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
std::wstring SqlException::getMessage() const {
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	return message;
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
}
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
SQLRETURN SqlException::getResultCode() const {
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	return resultCode;
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
}
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
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
}
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
}