src/DriverManager.cpp
branchv_0
changeset 36 91cb012d779a
parent 35 cd9db43db120
child 37 3de41719d7eb
equal deleted inserted replaced
35:cd9db43db120 36:91cb012d779a
    14  * You should have received a copy of the GNU General Public License
    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/>.
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  */
    16  */
    17 
    17 
    18 #include <cstring>
    18 #include <cstring>
       
    19 #include <iostream>
    19 
    20 
    20 #include <sql.h>
    21 #include <sql.h>
    21 #include <sqlext.h>
    22 #include <sqlext.h>
    22 
    23 
    23 #include "SqlException.h"
    24 #include "SqlException.h"
    54 		else throw SqlException(L"Unable to list data sources: " + std::to_wstring(result), result, SQL_HANDLE_ENV, env);
    55 		else throw SqlException(L"Unable to list data sources: " + std::to_wstring(result), result, SQL_HANDLE_ENV, env);
    55 	}
    56 	}
    56 	return list;
    57 	return list;
    57 }
    58 }
    58 
    59 
       
    60 Connection* DriverManager::getConnectionByDSN(relpipe::reader::string_t dataSourceName, relpipe::reader::string_t userName, relpipe::reader::string_t password) {
       
    61 	SQLHDBC connection = OdbcCommon::allocateHandle(SQL_HANDLE_DBC, env);
       
    62 	std::string dataSourceNameBytes = convertor.to_bytes(dataSourceName);
       
    63 	std::string userNameBytes = convertor.to_bytes(userName);
       
    64 	std::string passwordBytes = convertor.to_bytes(password);
       
    65 	SQLRETURN result = SQLConnect(connection,
       
    66 			(SQLCHAR*) dataSourceNameBytes.c_str(), SQL_NTS,
       
    67 			(SQLCHAR*) userNameBytes.c_str(), SQL_NTS,
       
    68 			(SQLCHAR*) password.c_str(), SQL_NTS);
       
    69 	if (OdbcCommon::isNotSuccessful(result)) {
       
    70 		OdbcCommon::freeHandle(SQL_HANDLE_DBC, connection);
       
    71 		throw SqlException(L"Unable to connect to " + dataSourceName, result, SQL_HANDLE_ENV, env);
       
    72 	}
       
    73 	return new Connection(connection);
       
    74 }
       
    75 
       
    76 Connection* DriverManager::getConnectionByString(relpipe::reader::string_t connectionString) {
       
    77 	SQLHDBC connection = OdbcCommon::allocateHandle(SQL_HANDLE_DBC, env);
       
    78 	char completeConnectionString[SQL_MAX_OPTION_STRING_LENGTH];
       
    79 	memset(completeConnectionString, 0, sizeof (completeConnectionString));
       
    80 	SQLSMALLINT completeConnectionStringLength = -1;
       
    81 	SQLRETURN result = SQLDriverConnect(connection, nullptr,
       
    82 			(SQLCHAR*) convertor.to_bytes(connectionString).c_str(), SQL_NTS,
       
    83 			(SQLCHAR*) completeConnectionString, sizeof (completeConnectionString), &completeConnectionStringLength,
       
    84 			SQL_DRIVER_NOPROMPT);
       
    85 	if (OdbcCommon::isNotSuccessful(result)) throw SqlException(L"Unable to connect to " + connectionString, result, SQL_HANDLE_ENV, env);
       
    86 	std::wcerr << "ODBC connected to: " << convertor.from_bytes(completeConnectionString) << std::endl; // FIXME: remove
       
    87 	return new Connection(connection);
       
    88 }
       
    89 
       
    90 relpipe::reader::string_t DriverManager::buildDSN(const std::map<relpipe::reader::string_t, relpipe::reader::string_t>& parameters) {
       
    91 	throw SqlException(L"not yet implemented: buildDSN()"); // FIXME: implement buildDSN()
       
    92 }
       
    93 
    59 
    94 
    60 }
    95 }
    61 }
    96 }
    62 }
    97 }