STDMETHODIMP InterfaceDatabase::SetDefaultDatabase(eDBtype ServerType, BSTR ServerName, long lPort, BSTR DatabaseName, BSTR Username, BSTR Password) { try { if (!GetIsServerAdmin()) return GetAccessDenied(); HM::String sEmpty; HM::String sServerName = ServerName; HM::String sDatabaseName = DatabaseName; HM::String sUsername = Username; HM::String sPassword = Password; if (sDatabaseName.Find(_T(" ")) >= 0) return COMError::GenerateError("The database name may not contain spaces."); // Create a settings object for the connection ... std::shared_ptr<HM::DatabaseSettings> pSettings = std::shared_ptr<HM::DatabaseSettings>( new HM::DatabaseSettings(sServerName, sEmpty, sUsername, sPassword, sEmpty, sEmpty,(HM::DatabaseSettings::SQLDBType) ServerType, lPort)); // Connect to the database server. std::shared_ptr<HM::DALConnection> pConn = HM::DALConnectionFactory::CreateConnection(pSettings); HM::String sErrorMessage; if (pConn->Connect(sErrorMessage) != HM::DALConnection::Connected) return COMError::GenerateError(sErrorMessage); // Disconnect again. pConn->Disconnect(); ini_file_settings_->SetDatabaseDirectory(""); ini_file_settings_->SetDatabaseType((HM::DatabaseSettings::SQLDBType) ServerType); ini_file_settings_->SetUsername(sUsername); ini_file_settings_->SetPassword(sPassword); ini_file_settings_->SetDatabasePort(lPort); ini_file_settings_->SetDatabaseServer(sServerName); ini_file_settings_->SetDatabaseName(sDatabaseName); ini_file_settings_->SetIsInternalDatabase(ServerType == hDBTypeMSSQLCE); return S_OK; } catch (...) { return COMError::GenerateGenericMessage(); } }
STDMETHODIMP InterfaceDatabase::CreateExternalDatabase(eDBtype ServerType, BSTR ServerName, long lPort, BSTR DatabaseName, BSTR Username, BSTR Password) { try { if (!config_) return GetAccessDenied(); if (!GetIsServerAdmin()) return GetAccessDenied(); const HM::String sEmpty; const HM::String sServerName = ServerName; const HM::String sDatabaseName = DatabaseName; const HM::String sUsername = Username; const HM::String sPassword = Password; if (sDatabaseName.Find(_T(" ")) >= 0) return COMError::GenerateError("The database name may not contain spaces."); // Create a settings object for the connection ... std::shared_ptr<HM::DatabaseSettings> pSettings = std::shared_ptr<HM::DatabaseSettings>( new HM::DatabaseSettings(sServerName, sEmpty, sUsername, sPassword, sEmpty, sEmpty,(HM::DatabaseSettings::SQLDBType) ServerType, lPort)); // Connect to the database serve std::shared_ptr<HM::DALConnection> pConn = HM::DALConnectionFactory::CreateConnection(pSettings); HM::String sErrorMessage; if (pConn->Connect(sErrorMessage) != HM::DALConnection::Connected) return COMError::GenerateError(sErrorMessage); // Create the database HM::String sCreateDB = HM::SQLStatement::GetCreateDatabase(pSettings, sDatabaseName); if (!pConn->Execute(HM::SQLCommand(sCreateDB), sErrorMessage, 0, 0)) return COMError::GenerateError(sErrorMessage); // Disconnect again. pConn->Disconnect(); // Create a new settings object where we specify the database name as well. pSettings = std::shared_ptr<HM::DatabaseSettings>( new HM::DatabaseSettings(sServerName, sDatabaseName, sUsername, sPassword, sEmpty, sEmpty,(HM::DatabaseSettings::SQLDBType) ServerType, lPort)); // Reconnect to the new database. pConn = HM::DALConnectionFactory::CreateConnection(pSettings); if (pConn->Connect(sErrorMessage) != HM::DALConnection::Connected) return COMError::GenerateError(sErrorMessage); // Run the scripts HM::SQLScriptRunner scriptRunner; if (!scriptRunner.ExecuteScript(pConn, pSettings->GetDefaultScript(), sErrorMessage)) return COMError::GenerateError(sErrorMessage); ini_file_settings_->SetDatabaseDirectory(""); ini_file_settings_->SetDatabaseType((HM::DatabaseSettings::SQLDBType) ServerType); ini_file_settings_->SetUsername(sUsername); ini_file_settings_->SetPassword(sPassword); ini_file_settings_->SetDatabasePort(lPort); ini_file_settings_->SetDatabaseServer(sServerName); ini_file_settings_->SetDatabaseName(sDatabaseName); ini_file_settings_->SetIsInternalDatabase(false); return S_OK; } catch (...) { return COMError::GenerateGenericMessage(); } }