|
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. |
|
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 |
|
20 #include <sql.h> |
|
21 #include <sqlext.h> |
|
22 |
|
23 #include "SqlException.h" |
|
24 #include "DriverManager.h" |
|
25 #include "OdbcCommon.h" |
|
26 |
|
27 namespace relpipe { |
|
28 namespace tr { |
|
29 namespace sql { |
|
30 |
|
31 DriverManager::DriverManager() { |
|
32 env = OdbcCommon::allocateHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE); |
|
33 SQLRETURN result = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void*) SQL_OV_ODBC3, 0); |
|
34 if (OdbcCommon::isNotSuccessful(result)) throw SqlException(L"Unable to set ODBC version"); // TODO:, result, SQL_HANDLE_ENV, environment); |
|
35 } |
|
36 |
|
37 DriverManager::~DriverManager() { |
|
38 OdbcCommon::freeHandle(SQL_HANDLE_ENV, env); |
|
39 } |
|
40 |
|
41 std::vector<DriverManager::DataSource> DriverManager::getDataSources() { |
|
42 std::vector<DriverManager::DataSource> list; |
|
43 SQLCHAR name[SQL_MAX_DSN_LENGTH + 1]; |
|
44 SQLCHAR description[255]; |
|
45 memset(name, 0, sizeof (name)); |
|
46 memset(description, 0, sizeof (description)); |
|
47 SQLSMALLINT nameLength; |
|
48 SQLSMALLINT descriptionLength; |
|
49 while (true) { |
|
50 SQLRETURN result = SQLDataSources(env, SQL_FETCH_NEXT, name, sizeof (name), &nameLength, description, sizeof (description), &descriptionLength); |
|
51 // TODO: check nameLength and descriptionLength whether values were truncated? |
|
52 if (OdbcCommon::isSuccessful(result)) list.push_back({convertor.from_bytes((char*) name), convertor.from_bytes((char*) description)}); |
|
53 else if (result == SQL_NO_DATA_FOUND) break; |
|
54 else throw SqlException(L"Unable to list data sources: " + std::to_wstring(result)); |
|
55 } |
|
56 return list; |
|
57 } |
|
58 |
|
59 |
|
60 } |
|
61 } |
|
62 } |