src/DriverManager.cpp
branchv_0
changeset 34 24c05e69d68f
child 35 cd9db43db120
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/DriverManager.cpp	Mon May 25 19:36:06 2020 +0200
@@ -0,0 +1,62 @@
+/**
+ * Relational pipes
+ * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <cstring>
+
+#include <sql.h>
+#include <sqlext.h>
+
+#include "SqlException.h"
+#include "DriverManager.h"
+#include "OdbcCommon.h"
+
+namespace relpipe {
+namespace tr {
+namespace sql {
+
+DriverManager::DriverManager() {
+	env = OdbcCommon::allocateHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE);
+	SQLRETURN result = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void*) SQL_OV_ODBC3, 0);
+	if (OdbcCommon::isNotSuccessful(result)) throw SqlException(L"Unable to set ODBC version"); // TODO:, result, SQL_HANDLE_ENV, environment);
+}
+
+DriverManager::~DriverManager() {
+	OdbcCommon::freeHandle(SQL_HANDLE_ENV, env);
+}
+
+std::vector<DriverManager::DataSource> DriverManager::getDataSources() {
+	std::vector<DriverManager::DataSource> list;
+	SQLCHAR name[SQL_MAX_DSN_LENGTH + 1];
+	SQLCHAR description[255];
+	memset(name, 0, sizeof (name));
+	memset(description, 0, sizeof (description));
+	SQLSMALLINT nameLength;
+	SQLSMALLINT descriptionLength;
+	while (true) {
+		SQLRETURN result = SQLDataSources(env, SQL_FETCH_NEXT, name, sizeof (name), &nameLength, description, sizeof (description), &descriptionLength);
+		// TODO: check nameLength and descriptionLength whether values were truncated?
+		if (OdbcCommon::isSuccessful(result)) list.push_back({convertor.from_bytes((char*) name), convertor.from_bytes((char*) description)});
+		else if (result == SQL_NO_DATA_FOUND) break;
+		else throw SqlException(L"Unable to list data sources: " + std::to_wstring(result));
+	}
+	return list;
+}
+
+
+}
+}
+}
\ No newline at end of file