Mercurial > hg > orthanc-databases
comparison Framework/Odbc/OdbcEnvironment.cpp @ 329:b5fb8b77ce4d
initial commit of ODBC framework
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 10 Aug 2021 20:08:53 +0200 |
parents | |
children | 674bbb9d1c83 |
comparison
equal
deleted
inserted
replaced
328:6a49c495c940 | 329:b5fb8b77ce4d |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "OdbcEnvironment.h" | |
23 | |
24 #include <Logging.h> | |
25 #include <OrthancException.h> | |
26 | |
27 #include <boost/lexical_cast.hpp> | |
28 #include <sqlext.h> | |
29 | |
30 | |
31 namespace OrthancDatabases | |
32 { | |
33 OdbcEnvironment::OdbcEnvironment() | |
34 { | |
35 LOG(INFO) << "Creating the ODBC environment"; | |
36 | |
37 /* Allocate an environment handle */ | |
38 if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &handle_))) | |
39 { | |
40 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, | |
41 "Cannot create ODBC environment"); | |
42 } | |
43 | |
44 /* We want ODBC 3 support */ | |
45 if (!SQL_SUCCEEDED(SQLSetEnvAttr(handle_, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0))) | |
46 { | |
47 SQLFreeHandle(SQL_HANDLE_ENV, handle_); | |
48 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, | |
49 "Your environment doesn't support ODBC 3.x"); | |
50 } | |
51 } | |
52 | |
53 | |
54 OdbcEnvironment::~OdbcEnvironment() | |
55 { | |
56 LOG(INFO) << "Destructing the ODBC environment"; | |
57 | |
58 if (!SQL_SUCCEEDED(SQLFreeHandle(SQL_HANDLE_ENV, handle_))) | |
59 { | |
60 LOG(ERROR) << "Cannot tear down ODBC environment"; | |
61 } | |
62 } | |
63 | |
64 | |
65 std::string OdbcEnvironment::FormatError(SQLHANDLE handle, | |
66 SQLSMALLINT type) | |
67 { | |
68 SQLINTEGER i = 0; | |
69 SQLINTEGER native; | |
70 SQLCHAR state[SQL_SQLSTATE_SIZE + 1]; | |
71 SQLCHAR text[256]; | |
72 SQLSMALLINT len; | |
73 | |
74 std::string s; | |
75 | |
76 for (;;) | |
77 { | |
78 SQLRETURN ret = SQLGetDiagRec(type, handle, ++i, state, &native, text, sizeof(text), &len); | |
79 if (SQL_SUCCEEDED(ret)) | |
80 { | |
81 if (i >= 2) | |
82 { | |
83 s += "\n"; | |
84 } | |
85 | |
86 s += (std::string(reinterpret_cast<const char*>(state)) + " : " + | |
87 boost::lexical_cast<std::string>(i) + "/" + | |
88 boost::lexical_cast<std::string>(native) + " " + | |
89 std::string(reinterpret_cast<const char*>(text))); | |
90 } | |
91 else | |
92 { | |
93 return s; | |
94 } | |
95 } | |
96 } | |
97 } |