DatabaseResultSet* PostgresPreparedStatement::RunQueryWithResults()
{
  for (unsigned int i=0; i<(m_Statements.size()-1); i++)
  {
    m_Statements[i].RunQuery();
    if (m_Statements[i].GetErrorCode() != DATABASE_LAYER_OK)
    {
      SetErrorCode(m_Statements[i].GetErrorCode());
      SetErrorMessage(m_Statements[i].GetErrorMessage());
      ThrowDatabaseException();
      return NULL;
    }
  }
  PostgresPreparedStatementWrapper* pLastStatement = &(m_Statements[m_Statements.size()-1]);
  DatabaseResultSet* pResultSet = pLastStatement->RunQueryWithResults();
  if (pLastStatement->GetErrorCode() != DATABASE_LAYER_OK)
  {
    SetErrorCode(pLastStatement->GetErrorCode());
    SetErrorMessage(pLastStatement->GetErrorMessage());
    ThrowDatabaseException();
  }

  LogResultSetForCleanup(pResultSet);
  return pResultSet;
}
Example #2
0
wxDatabaseResultSet* wxOdbcDatabase::RunQueryWithResults(const wxString& strQuery)
{
   ResetErrorCodes();

   wxOdbcPreparedStatement* pStatement = (wxOdbcPreparedStatement*)PrepareStatement( strQuery, true );

   if ( pStatement )
   {
     try
     {
       pStatement->SetOneTimer(true);
       wxDatabaseResultSet* pResults = pStatement->RunQueryWithResults(false /*false for "Don't log this result set for cleanup*/);
       LogResultSetForCleanup(pResults);
       return pResults;
     }
     catch (...)
     {
       SetErrorCode(pStatement->GetErrorCode());
       SetErrorMessage(pStatement->GetErrorMessage());
       wxDELETE( pStatement );
       ThrowDatabaseException();
       return NULL;
     }
   }
   else
     return NULL;
}
wxDatabaseResultSet* wxSqlitePreparedStatement::RunQueryWithResults()
{
  ResetErrorCodes();

  if (m_Statements.size() > 1)
  {
    for (unsigned int i=0; i<m_Statements.size()-1; i++)
    {
      int nReturn = sqlite3_step(m_Statements[i]);
 
      if (nReturn != SQLITE_ROW)
        sqlite3_reset(m_Statements[i]);

      if ((nReturn != SQLITE_ROW) && (nReturn != SQLITE_DONE))
      {
        wxLogError(_("Error with RunQueryWithResults\n"));
        SetErrorCode(wxSqliteDatabase::TranslateErrorCode(nReturn));
        SetErrorMessage(ConvertFromUnicodeStream(sqlite3_errmsg(m_pDatabase)));
        ThrowDatabaseException();
        return NULL;
      }
    }
  }
  // Work off the assumption that only the last statement will return result
  
  wxSqliteResultSet* pResultSet = new wxSqliteResultSet(this);
  if (pResultSet)
    pResultSet->SetEncoding(GetEncoding());

  LogResultSetForCleanup(pResultSet);
  return pResultSet;
}
Example #4
0
DatabaseResultSet* TdsDatabaseLayer::RunQueryWithResults(const wxString& strQuery)
{
  ResetErrorCodes();

  if (m_pDatabase != NULL)
  {
    FreeAllocatedResultSets();

    wxArrayString QueryArray = ParseQueries(strQuery);
     
    for (unsigned int i=0; i<(QueryArray.size()-1); i++)
    {
      char* szErrorMessage = NULL;
      wxString strErrorMessage = wxT("");
      wxString sql = RemoveLastSemiColon(QueryArray[i]);
      wxCharBuffer sqlBuffer = ConvertToUnicodeStream(sql);

      //fprintf(stderr, "Running query '%s'\n", sqlBuffer);
      int nReturn = tds_submit_query(m_pDatabase, sqlBuffer);
      if (nReturn != TDS_SUCCEED)
      {
        //fprintf(stderr, "tds_submit_query() failed for query '%s'\n", sql.c_str());
        FreeAllocatedResultSets();
        ThrowDatabaseException();
        return NULL;
      }
      FreeAllocatedResultSets();
    }

    // Create a Prepared statement for the last SQL statement and get a result set from it
    wxString strQuery = RemoveLastSemiColon(QueryArray[QueryArray.size()-1]);
    wxCharBuffer sqlBuffer = ConvertToUnicodeStream(strQuery);
    //fprintf(stderr, "Running query (with results) '%s'\n", sqlBuffer);
    int nReturn = tds_submit_query(m_pDatabase, sqlBuffer);
    if (nReturn != TDS_SUCCEED)
    {
        //fprintf(stderr, "tds_submit_query() failed for query '%s'\n", sqlBuffer);
        //fprintf(stderr, "tds_submit_query() failed for query '%s'\n", strQuery.c_str());
        FreeAllocatedResultSets();
        ThrowDatabaseException();
        return NULL;
    }
    TdsResultSet* pResultSet = new TdsResultSet(m_pDatabase);
    if (pResultSet)
      pResultSet->SetEncoding(GetEncoding());

    LogResultSetForCleanup(pResultSet);
    //fprintf(stderr, "Returning result set\n");
    return pResultSet;
  }
  else
  {
    return NULL;
  }
}
DatabaseResultSet* TdsPreparedStatement::RunQueryWithResults()
{
  ResetErrorCodes();

  //fprintf(stderr, "Running statement with results\n");
  // Prepare the statement
  wxCharBuffer sqlBuffer = ConvertToUnicodeStream(m_strOriginalQuery);
  //fprintf(stderr, "Preparing statement: '%s'\n", (const char*)sqlBuffer);
  m_pStatement = NULL;
  int nReturn = tds_submit_prepare(m_pDatabase, sqlBuffer, NULL, &m_pStatement, m_pParameters);
  if (nReturn != TDS_SUCCEED)
  {
    //fprintf(stderr, "tds_submit_prepare() failed for query '%s'\n", m_strOriginalQuery.c_str());

    if (m_pStatement != NULL)
      tds_free_dynamic(m_pDatabase, m_pStatement);
    if (m_pParameters != NULL)
      tds_free_param_results(m_pParameters);

    SetErrorInformationFromDatabaseLayer();
    FreeAllocatedResultSets();
    ThrowDatabaseException();
    return NULL;
  }
  FreeAllocatedResultSets();
  tds_free_input_params(m_pStatement);
  m_pStatement->params = m_pParameters;

  // Execute the query
  //fprintf(stderr, "Statement prepared.  Ready to execute\n");
  nReturn = tds_submit_execute(m_pDatabase, m_pStatement);
  if (nReturn != TDS_SUCCEED)
  {
      //fprintf(stderr, "tds_submit_execute() failed for query '%s'\n", m_pStatement->query);
      //fprintf(stderr, "tds_submit_execute() return code: %d\n", nReturn);
      SetErrorInformationFromDatabaseLayer();
      FreeAllocatedResultSets();
      ThrowDatabaseException();
      return NULL;
  }
  TdsResultSet* pResultSet = new TdsResultSet(m_pDatabase);
  if (pResultSet)
    pResultSet->SetEncoding(GetEncoding());

  LogResultSetForCleanup(pResultSet);
  //fprintf(stderr, "Returning prepared statement result set\n");
  return pResultSet;
}
DatabaseResultSet* SqliteDatabaseLayer::RunQueryWithResults(const wxString& strQuery)
{
  ResetErrorCodes();

  if (m_pDatabase != NULL)
  {
    wxArrayString QueryArray = ParseQueries(strQuery);
     
    for (unsigned int i=0; i<(QueryArray.size()-1); i++)
    {
      char* szErrorMessage = NULL;
      wxString strErrorMessage = _("");
      wxCharBuffer sqlBuffer = ConvertToUnicodeStream(QueryArray[i]);
      int nReturn = sqlite3_exec((sqlite3*)m_pDatabase, sqlBuffer, 0, 0, &szErrorMessage);
  
      if (szErrorMessage != NULL)
      {
        SetErrorCode(SqliteDatabaseLayer::TranslateErrorCode(sqlite3_errcode((sqlite3*)m_pDatabase)));
        strErrorMessage = ConvertFromUnicodeStream(szErrorMessage);
        sqlite3_free(szErrorMessage);
        return NULL;
      }

      if (nReturn != SQLITE_OK)
      {
        SetErrorCode(SqliteDatabaseLayer::TranslateErrorCode(sqlite3_errcode((sqlite3*)m_pDatabase)));
        SetErrorMessage(strErrorMessage);
        ThrowDatabaseException();
        return NULL;
      }
    }

    // Create a Prepared statement for the last SQL statement and get a result set from it
    SqlitePreparedStatement* pStatement = (SqlitePreparedStatement*)PrepareStatement(QueryArray[QueryArray.size()-1], false);
    SqliteResultSet* pResultSet = new SqliteResultSet(pStatement, true);
    if (pResultSet)
      pResultSet->SetEncoding(GetEncoding());

    LogResultSetForCleanup(pResultSet);
    return pResultSet;
  }
  else
  {
    return NULL;
  }
}
Example #7
0
DatabaseResultSet* OTLDatabaseLayer::RunQueryWithResults(const wxString& strQuery)
{
    OTLResultSet* pResultSet = NULL;
    try
    {
        wxArrayString QueryArray = ParseQueries(strQuery);

        if (QueryArray.size() > 0)
        {
            for (unsigned int i=0; i<(QueryArray.size()-1); i++)
            {
                wxCharBuffer sqlBuffer = ConvertToUnicodeStream(QueryArray[i]);
                std::string strSQL(sqlBuffer);
                strSQL = RemoveLastSemiColon(strSQL);
                //wxPrintf(_("RunQuery: '%s'\n"), strSQL.c_str());
                otl_stream otlStream(1,strSQL.c_str(),m_database);
            }

            // Deal with the last query separately
            wxCharBuffer sqlBuffer = ConvertToUnicodeStream(QueryArray[QueryArray.size()-1]);
            std::string strSQL(sqlBuffer);
            strSQL = RemoveLastSemiColon(strSQL);

            //wxPrintf(_("RunQuery: '%s'\n"), strSQL.c_str());
            otl_stream *otlInputStream = new otl_stream(1,strSQL.c_str(),m_database);
            {
                //fixme
                //pOTLStatement->setAutoCommit(m_bAutoCommit);
                pResultSet = new OTLResultSet(otlInputStream, true);
            }
        }
    }
    catch (otl_exception& e)
    {
        SetErrorCode(OTLDatabaseLayer::TranslateErrorCode(e.code));
        SetErrorMessage(ConvertFromUnicodeStream((char*)e.msg));
        ThrowDatabaseException();
        return NULL;
    }

    if (pResultSet)
        LogResultSetForCleanup(pResultSet);

    return pResultSet;
}
wxDatabaseResultSet* wxPostgresDatabase::RunQueryWithResults(const wxString& strQuery)
{
    ResetErrorCodes();

    wxCharBuffer sqlBuffer = ConvertToUnicodeStream(strQuery);
    PGresult* pResultCode = m_pInterface->GetPQexec()((PGconn*)m_pDatabase, sqlBuffer);
    if ((pResultCode == NULL) || (m_pInterface->GetPQresultStatus()(pResultCode) != PGRES_TUPLES_OK))
    {
        SetErrorCode(wxPostgresDatabase::TranslateErrorCode(m_pInterface->GetPQstatus()((PGconn*)m_pDatabase)));
        SetErrorMessage(ConvertFromUnicodeStream(m_pInterface->GetPQerrorMessage()((PGconn*)m_pDatabase)));
        m_pInterface->GetPQclear()(pResultCode);
        ThrowDatabaseException();
        return NULL;
    }
    else
    {
        wxPostgresResultSet* pResultSet = new wxPostgresResultSet(m_pInterface, pResultCode);
        pResultSet->SetEncoding(GetEncoding());
        LogResultSetForCleanup(pResultSet);
        return pResultSet;
    }
}
DatabaseResultSet* FirebirdDatabaseLayer::RunQueryWithResults(const wxString& strQuery)
{
  ResetErrorCodes();
  if (m_pDatabase != NULL)
  {
    wxCharBuffer sqlDebugBuffer = ConvertToUnicodeStream(strQuery);
    //wxLogDebug(_("Running query: \"%s\""), (const char*)sqlDebugBuffer);
    
    wxArrayString QueryArray = ParseQueries(strQuery);

    if (QueryArray.size() > 0)
    {
      bool bQuickieTransaction = false;
    
      if (m_pTransaction == NULL)
      {
        // If there's no transaction is progress, run this as a quick one-timer transaction
        bQuickieTransaction = true;
      }

      if (QueryArray.size() > 1)
      {
        if (bQuickieTransaction)
        {
          BeginTransaction();
          if (GetErrorCode() != DATABASE_LAYER_OK)
          {
            wxLogError(_("Unable to start transaction"));
            ThrowDatabaseException();
            return NULL;
          }
        }
      
        // Assume that only the last statement in the array returns the result set
        for (unsigned int i=0; i<QueryArray.size()-1; i++)
        {
          RunQuery(QueryArray[i], false);
          if (GetErrorCode() != DATABASE_LAYER_OK)
          {
            ThrowDatabaseException();
            return NULL;
          }
        }
      
        // Now commit all the previous queries before calling the query that returns a result set
        if (bQuickieTransaction)
        {
          Commit();
          if (GetErrorCode() != DATABASE_LAYER_OK)
          {
            ThrowDatabaseException();
            return NULL;
          }
        }
      } // End check if there are more than one query in the array
      
      isc_tr_handle pQueryTransaction = NULL;
      bool bManageTransaction = false;
      if (bQuickieTransaction)
      {
        bManageTransaction = true;
        isc_db_handle pDatabase = (isc_db_handle)m_pDatabase;
        int nReturn = m_pInterface->GetIscStartTransaction()(*(ISC_STATUS_ARRAY*)m_pStatus, &pQueryTransaction, 1, &pDatabase, 0 /*tpb_length*/, NULL/*tpb*/);
        m_pDatabase = pDatabase;
        if (nReturn != 0)
        {
          InterpretErrorCodes();
          ThrowDatabaseException();
        }
      }
      else
      {
        pQueryTransaction = m_pTransaction;
      }
      
      isc_stmt_handle pStatement = NULL;
      isc_db_handle pDatabase = (isc_db_handle)m_pDatabase;
      int nReturn = m_pInterface->GetIscDsqlAllocateStatement()(*(ISC_STATUS_ARRAY*)m_pStatus, &pDatabase, &pStatement);
      m_pDatabase = pDatabase;
      if (nReturn != 0)
      {
        InterpretErrorCodes();

        // Manually try to rollback the transaction rather than calling the member RollBack function
        //  so that we can ignore the error messages
        m_pInterface->GetIscRollbackTransaction()(*(ISC_STATUS_ARRAY*)m_pStatus, &pQueryTransaction);

        ThrowDatabaseException();
        return NULL;
      }
      
      wxCharBuffer sqlBuffer = ConvertToUnicodeStream(QueryArray[QueryArray.size()-1]);
      nReturn = m_pInterface->GetIscDsqlPrepare()(*(ISC_STATUS_ARRAY*)m_pStatus, &pQueryTransaction, &pStatement, 0, (char*)(const char*)sqlBuffer, SQL_DIALECT_CURRENT, NULL);
      if (nReturn != 0)
      {
        InterpretErrorCodes();

        // Manually try to rollback the transaction rather than calling the member RollBack function
        //  so that we can ignore the error messages
        m_pInterface->GetIscRollbackTransaction()(*(ISC_STATUS_ARRAY*)m_pStatus, &pQueryTransaction);

        ThrowDatabaseException();
        return NULL;
      }

//--------------------------------------------------------------

      XSQLDA* pOutputSqlda = (XSQLDA*)malloc(XSQLDA_LENGTH(1));
      pOutputSqlda->sqln = 1;
      pOutputSqlda->version = SQLDA_VERSION1;

      // Make sure that we have enough space allocated for the result set
      nReturn = m_pInterface->GetIscDsqlDescribe()(*(ISC_STATUS_ARRAY*)m_pStatus, &pStatement, SQL_DIALECT_CURRENT, pOutputSqlda);
      if (nReturn != 0)
      {
        free(pOutputSqlda);
        InterpretErrorCodes();

        // Manually try to rollback the transaction rather than calling the member RollBack function
        //  so that we can ignore the error messages
        m_pInterface->GetIscRollbackTransaction()(*(ISC_STATUS_ARRAY*)m_pStatus, &pQueryTransaction);

        ThrowDatabaseException();
        return NULL;
      }

      if (pOutputSqlda->sqld > pOutputSqlda->sqln)
      {
        int nColumns = pOutputSqlda->sqld;
        free(pOutputSqlda);
        pOutputSqlda = (XSQLDA*)malloc(XSQLDA_LENGTH(nColumns));
        pOutputSqlda->sqln = nColumns;
        pOutputSqlda->version = SQLDA_VERSION1;
        nReturn = m_pInterface->GetIscDsqlDescribe()(*(ISC_STATUS_ARRAY*)m_pStatus, &pStatement, SQL_DIALECT_CURRENT, pOutputSqlda);
        if (nReturn != 0)
        {
          free(pOutputSqlda);
          InterpretErrorCodes();

          // Manually try to rollback the transaction rather than calling the member RollBack function
          //  so that we can ignore the error messages
          m_pInterface->GetIscRollbackTransaction()(*(ISC_STATUS_ARRAY*)m_pStatus, &pQueryTransaction);

          ThrowDatabaseException();
          return NULL;
        }
      }

      // Create the result set object
      FirebirdResultSet* pResultSet = new FirebirdResultSet(m_pInterface, m_pDatabase, pQueryTransaction, pStatement, pOutputSqlda, true, bManageTransaction);
      pResultSet->SetEncoding(GetEncoding());
      if (pResultSet->GetErrorCode() != DATABASE_LAYER_OK)
      {
        SetErrorCode(pResultSet->GetErrorCode());
        SetErrorMessage(pResultSet->GetErrorMessage());
    
        // Manually try to rollback the transaction rather than calling the member RollBack function
        //  so that we can ignore the error messages
        m_pInterface->GetIscRollbackTransaction()(*(ISC_STATUS_ARRAY*)m_pStatus, &pQueryTransaction);

        // Wrap the result set deletion in try/catch block if using exceptions.
        //We want to make sure the original error gets to the user
#ifndef DONT_USE_DATABASE_LAYER_EXCEPTIONS
        try
        {
#endif
        delete pResultSet;
#ifndef DONT_USE_DATABASE_LAYER_EXCEPTIONS
        }
        catch (DatabaseLayerException& e)
        {
        }
#endif

        ThrowDatabaseException();
      }
  
      // Now execute the SQL
      nReturn = m_pInterface->GetIscDsqlExecute()(*(ISC_STATUS_ARRAY*)m_pStatus, &pQueryTransaction, &pStatement, SQL_DIALECT_CURRENT, NULL);
      if (nReturn != 0)
      {
        InterpretErrorCodes();

        // Manually try to rollback the transaction rather than calling the member RollBack function
        //  so that we can ignore the error messages
        m_pInterface->GetIscRollbackTransaction()(*(ISC_STATUS_ARRAY*)m_pStatus, &pQueryTransaction);

        // Wrap the result set deletion in try/catch block if using exceptions.
        //  We want to make sure the isc_dsql_execute error gets to the user
#ifndef DONT_USE_DATABASE_LAYER_EXCEPTIONS
        try
        {
#endif
        delete pResultSet;
#ifndef DONT_USE_DATABASE_LAYER_EXCEPTIONS
        }
        catch (DatabaseLayerException& e)
        {
        }
#endif
    
        ThrowDatabaseException();
        return NULL;
      }
      
//--------------------------------------------------------------

      LogResultSetForCleanup(pResultSet);
      return pResultSet;
    }
    else
      return NULL;
  }
  else
  {
    wxLogError(_("Database handle is NULL"));
    return NULL;
  }
}