src/SqlException.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 31 May 2020 16:56:07 +0200
branchv_0
changeset 36 91cb012d779a
parent 35 cd9db43db120
child 38 a871779a4e3c
permissions -rw-r--r--
use ODBC, avoid direct dependency on SQLite
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>
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"
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace tr {
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace sql {
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
SqlException::SqlException(std::wstring message) : message(message) {
36
91cb012d779a use ODBC, avoid direct dependency on SQLite
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
    33
	std::wcerr << L"XXX SqlException: " << message.c_str() << std::endl << std::flush; // FIXME: remove
35
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
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
SqlException::SqlException(std::wstring message, SQLRETURN resultCode, SQLSMALLINT handleType, SQLHANDLE handle) : message(message), resultCode(resultCode) {
36
91cb012d779a use ODBC, avoid direct dependency on SQLite
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
    37
	std::wcerr << L"XXX SqlException: " << message.c_str() << std::endl << std::flush; // FIXME: remove
35
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	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
    39
	SQLCHAR buffer[SQL_MAX_MESSAGE_LENGTH + 1];
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1];
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	memset(buffer, 0, sizeof (buffer));
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	memset(sqlstate, 0, sizeof (sqlstate));
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	SQLINTEGER sqlcode;
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	SQLSMALLINT length;
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	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
    46
		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
    47
	}
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
}
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
std::vector<SqlException::SqlDiagnosticsRecord> SqlException::getDiagnostics() const {
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	return diagnostics;
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
}
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
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
    55
	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
    56
	else return message;
35
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
SQLRETURN SqlException::getResultCode() const {
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	return resultCode;
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
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
}
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
}
cd9db43db120 SqlException: ODBC diagnostics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
}