--- a/src/DriverManager.cpp Mon May 25 21:11:17 2020 +0200
+++ b/src/DriverManager.cpp Sun May 31 16:56:07 2020 +0200
@@ -16,6 +16,7 @@
*/
#include <cstring>
+#include <iostream>
#include <sql.h>
#include <sqlext.h>
@@ -56,7 +57,41 @@
return list;
}
+Connection* DriverManager::getConnectionByDSN(relpipe::reader::string_t dataSourceName, relpipe::reader::string_t userName, relpipe::reader::string_t password) {
+ SQLHDBC connection = OdbcCommon::allocateHandle(SQL_HANDLE_DBC, env);
+ std::string dataSourceNameBytes = convertor.to_bytes(dataSourceName);
+ std::string userNameBytes = convertor.to_bytes(userName);
+ std::string passwordBytes = convertor.to_bytes(password);
+ SQLRETURN result = SQLConnect(connection,
+ (SQLCHAR*) dataSourceNameBytes.c_str(), SQL_NTS,
+ (SQLCHAR*) userNameBytes.c_str(), SQL_NTS,
+ (SQLCHAR*) password.c_str(), SQL_NTS);
+ if (OdbcCommon::isNotSuccessful(result)) {
+ OdbcCommon::freeHandle(SQL_HANDLE_DBC, connection);
+ throw SqlException(L"Unable to connect to " + dataSourceName, result, SQL_HANDLE_ENV, env);
+ }
+ return new Connection(connection);
+}
+
+Connection* DriverManager::getConnectionByString(relpipe::reader::string_t connectionString) {
+ SQLHDBC connection = OdbcCommon::allocateHandle(SQL_HANDLE_DBC, env);
+ char completeConnectionString[SQL_MAX_OPTION_STRING_LENGTH];
+ memset(completeConnectionString, 0, sizeof (completeConnectionString));
+ SQLSMALLINT completeConnectionStringLength = -1;
+ SQLRETURN result = SQLDriverConnect(connection, nullptr,
+ (SQLCHAR*) convertor.to_bytes(connectionString).c_str(), SQL_NTS,
+ (SQLCHAR*) completeConnectionString, sizeof (completeConnectionString), &completeConnectionStringLength,
+ SQL_DRIVER_NOPROMPT);
+ if (OdbcCommon::isNotSuccessful(result)) throw SqlException(L"Unable to connect to " + connectionString, result, SQL_HANDLE_ENV, env);
+ std::wcerr << "ODBC connected to: " << convertor.from_bytes(completeConnectionString) << std::endl; // FIXME: remove
+ return new Connection(connection);
+}
+
+relpipe::reader::string_t DriverManager::buildDSN(const std::map<relpipe::reader::string_t, relpipe::reader::string_t>& parameters) {
+ throw SqlException(L"not yet implemented: buildDSN()"); // FIXME: implement buildDSN()
+}
+
}
}
-}
\ No newline at end of file
+}