diff -r cd9db43db120 -r 91cb012d779a src/DriverManager.cpp --- 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 +#include #include #include @@ -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& parameters) { + throw SqlException(L"not yet implemented: buildDSN()"); // FIXME: implement buildDSN() +} + } } -} \ No newline at end of file +}