示例#1
0
   DALConnection::ConnectionResult
   ADOConnection::Connect(String &sErrorMessage)
   {
      String sUsername = m_pDatabaseSettings->GetUsername();
      String sPassword = m_pDatabaseSettings->GetPassword();
      String sServer = m_pDatabaseSettings->GetServer();
      String sDatabase = m_pDatabaseSettings->GetDatabaseName();
      String sServerFailoverPartner = m_pDatabaseSettings->GetDatabaseServerFailoverPartner();

      String sProvider = "sqloledb";
      if (!sServerFailoverPartner.IsEmpty())
      {
         sProvider = "SQLNCLI";
      }

      if (bConnected)
      {
         assert(0); // --- Already connected!
         return Connected;
      }

      String sConnectionString = "Provider=" + sProvider + ";";
      sConnectionString.append("Server=" + sServer + ";");
      sConnectionString.append("Initial Catalog=" + sDatabase + ";");

      if (sUsername.IsEmpty())
      {
         // Windows authentication.
         sConnectionString.append(_T("Integrated Security=SSPI;"));
      }
      else
      {
         // User/password authentication
         sConnectionString.append(_T("User ID=" + sUsername +";"));
         sConnectionString.append(_T("Password="******";"));
      }

      if (!sServerFailoverPartner.IsEmpty())
      {
         sConnectionString.append("FailoverPartner=" + sServerFailoverPartner + ";");
      }

      BSTR bsConnection = sConnectionString.AllocSysString();
      BSTR bsUser = sUsername.AllocSysString();
      BSTR bsPassword = sPassword.AllocSysString();
  
      try
      {
         HRESULT hr = cADOConnection->Open( bsConnection, bsUser, bsPassword, NULL );
         cADOConnection->PutCursorLocation(adUseClient);
      
         if ( !SUCCEEDED( hr ) )
         {
            ErrorManager::Instance()->ReportError(ErrorManager::Critical, 5027, "ADOConnection::Connect", "Open failed");
            return TemporaryFailure;
         }  

      }
      catch ( _com_error &err )
      {
         assert(0);

         _bstr_t bstrSource( err.Source() );
         _bstr_t bstrDescription( err.Description() );
     
         LPCSTR lpcSource = bstrSource;
         String sErrSource = lpcSource;

         LPCSTR lpcDesc = bstrDescription;
         String sErrDesc = lpcDesc;

         sErrorMessage = "ADO: " + sErrDesc;
         
         String sMessage = "Error when connecting to database. " + sErrSource + " " + sErrDesc + " Check your database settings in hMailServer.ini.";

         ErrorManager::Instance()->ReportError(ErrorManager::Critical, 5028, "ADOConnection::Connect", sMessage);

         SysFreeString(bsConnection);
         SysFreeString(bsUser);
         SysFreeString(bsPassword);

         return TemporaryFailure;
      

      }
      catch( ... )
      {
         cADOConnection = 0;
     
         sErrorMessage = "ADO: Unknown database connection error";

         ErrorManager::Instance()->ReportError(ErrorManager::Critical, 5029, "ADOConnection::Connect", "An unknown error occurred when connecting to database. Check your database settings in hMailServer.ini.");

         SysFreeString(bsConnection);
         SysFreeString(bsUser);
         SysFreeString(bsPassword);

         return TemporaryFailure;

      }

      SysFreeString(bsConnection);
      SysFreeString(bsUser);
      SysFreeString(bsPassword);

      bConnected = true;

      // Change the format of dates. We always insert dates using the format yyyy-mm-dd.
      // We need to inform MSSQL about this.
      String sSetDateFormat = "SET DATEFORMAT ymd";
      String sErrorMsg;

      SQLCommand command1(sSetDateFormat);
      TryExecute(command1, sErrorMsg, 0, 0);

      // Set transaction isolation level to read uncommitted. This may give phantom
      // reads but that isn't a problem since we've implemented our own locking 
      // mechanism.
      String sIsolationLevel = "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED";
      
      sErrorMsg = "";

      SQLCommand command2(sIsolationLevel);
      TryExecute(command2, sErrorMsg, 0, 0);

      return Connected;
   }
示例#2
0
   DALConnection::ExecutionResult
   ADOConnection::TryExecute(const SQLCommand &command, String &sErrorMessage, __int64 *iInsertID, int iIgnoreErrors)
   {
      if (!bConnected)
         return DALConnectionProblem; // --- already disconnected.

      String queryString = command.GetQueryString();

      try
      {
         // Execute the SQL statement.
         _CommandPtr adoCommand;
         adoCommand.CreateInstance(__uuidof(Command));
         InitializeCommandParameters(adoCommand, command, queryString);

         adoCommand->CommandType = adCmdText;
         adoCommand->CommandText = _bstr_t(queryString);
         adoCommand->ActiveConnection = cADOConnection;
         adoCommand->Execute(NULL, NULL, adCmdText);

         // Check what unique ID we got back (if we're interested).
         _RecordsetPtr pIdentityRS;
         BSTR bsIdentity;
         if (iInsertID > 0)
         {
            pIdentityRS.CreateInstance(__uuidof(Recordset));
            pIdentityRS->PutCursorLocation(adUseClient);
            pIdentityRS->PutRefActiveConnection(cADOConnection); 
            String sIdentitySQL = "SELECT @@IDENTITY AS IDENT";
            bsIdentity = sIdentitySQL.AllocSysString();

            HRESULT hr = pIdentityRS->Open( bsIdentity, vtMissing, adOpenKeyset, adLockOptimistic , adCmdText);
            pIdentityRS->PutRefActiveConnection(NULL); 
            
         }

         if (iInsertID)
         {
            *iInsertID = _GetIdentityFromRS(pIdentityRS);
            ::SysFreeString( bsIdentity );
         }
      }
      catch ( _com_error &err )
      {
         ExecutionResult dbErr = GetErrorType(_com_error::WCodeToHRESULT(err.WCode()));
         if (iIgnoreErrors > 0 && iIgnoreErrors & dbErr)
            return DALConnection::DALSuccess;

         if (queryString.Find(_T("[IGNORE-ERRORS]")) >= 0)
            return DALConnection::DALSuccess;

         _bstr_t bstrSource( err.Source() );
         _bstr_t bstrDescription( err.Description() );

         LPCSTR lpcSource = bstrSource;
         String sErrSource = lpcSource;

         LPCSTR lpcDesc = bstrDescription;
         String sErrDesc = lpcDesc;

         sErrorMessage = "Source: ADOConnection::Execute(), Code: HM10044, Description: Error while executing SQL statement: \n";
         sErrorMessage += queryString;
         sErrorMessage += "\n";
         sErrorMessage += sErrSource;
         sErrorMessage += "\n";
         sErrorMessage += sErrDesc;

         return dbErr;
      }
      catch ( ... )
      {

         sErrorMessage = "Source: ADOConnection::Execute(), Code: HM10045, Description: Unknown error when executing SQL statement: \n";
         sErrorMessage.append(queryString);

         return DALUnknown;
      }

      return DALSuccess;
   }