src/SqlException.cpp
branchv_0
changeset 35 cd9db43db120
child 36 91cb012d779a
equal deleted inserted replaced
34:24c05e69d68f 35:cd9db43db120
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 
       
    18 #include <cstring>
       
    19 #include <codecvt>
       
    20 #include <locale>
       
    21 
       
    22 #include <sql.h>
       
    23 #include <sqlext.h>
       
    24 
       
    25 #include "SqlException.h"
       
    26 
       
    27 namespace relpipe {
       
    28 namespace tr {
       
    29 namespace sql {
       
    30 
       
    31 SqlException::SqlException(std::wstring message) : message(message) {
       
    32 }
       
    33 
       
    34 SqlException::SqlException(std::wstring message, SQLRETURN resultCode, SQLSMALLINT handleType, SQLHANDLE handle) : message(message), resultCode(resultCode) {
       
    35 	std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings
       
    36 	SQLCHAR buffer[SQL_MAX_MESSAGE_LENGTH + 1];
       
    37 	SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1];
       
    38 	memset(buffer, 0, sizeof (buffer));
       
    39 	memset(sqlstate, 0, sizeof (sqlstate));
       
    40 	SQLINTEGER sqlcode;
       
    41 	SQLSMALLINT length;
       
    42 	for (SQLSMALLINT i = 1; SQLGetDiagRec(handleType, handle, i, sqlstate, &sqlcode, buffer, SQL_MAX_MESSAGE_LENGTH + 1, &length) == SQL_SUCCESS; i++) {
       
    43 		diagnostics.push_back({convertor.from_bytes((char*) sqlstate), sqlcode, convertor.from_bytes((char*) buffer)});
       
    44 	}
       
    45 }
       
    46 
       
    47 std::vector<SqlException::SqlDiagnosticsRecord> SqlException::getDiagnostics() const {
       
    48 	return diagnostics;
       
    49 }
       
    50 
       
    51 std::wstring SqlException::getMessage() const {
       
    52 	return message;
       
    53 }
       
    54 
       
    55 SQLRETURN SqlException::getResultCode() const {
       
    56 	return resultCode;
       
    57 }
       
    58 
       
    59 
       
    60 
       
    61 }
       
    62 }
       
    63 }