Esempio n. 1
0
// query database
int OTLDatabaseLayer::RunQuery(const wxString& strQuery, bool bParseQuery)
{
    try
    {
        wxArrayString QueryArray;
        if (bParseQuery)
            QueryArray = ParseQueries(strQuery);
        else
            QueryArray.push_back(strQuery);

        wxArrayString::iterator start = QueryArray.begin();
        wxArrayString::iterator stop = QueryArray.end();

        while (start != stop)
        {
            wxCharBuffer sqlBuffer = ConvertToUnicodeStream((*start));
            std::string strSQL(sqlBuffer);
            strSQL = RemoveLastSemiColon(strSQL);
            //wxPrintf(_("RunQuery: '%s'\n"), strSQL.c_str());
            otl_stream otlStream(1,strSQL.c_str(),m_database);
            start++;
        }
    }
    catch (otl_exception& e)
    {
        SetErrorCode(OTLDatabaseLayer::TranslateErrorCode(e.code));
        SetErrorMessage(ConvertFromUnicodeStream((char*)e.msg));
        ThrowDatabaseException();
        return false;
    }
    return true;
}
Esempio n. 2
0
File: uri.cpp Progetto: yzhenma/brpc
void URI::InitializeQueryMap() const {
    if (!_query_map.initialized()) {
        CHECK_EQ(0, _query_map.init(QUERY_MAP_INITIAL_BUCKET));
    }
    ParseQueries(_query_map, _query);
    _query_was_modified = false;
    _initialized_query_map = true;
}
Esempio n. 3
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* 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;
  }
}
Esempio n. 5
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;
}
// query database
int SqliteDatabaseLayer::RunQuery(const wxString& strQuery, bool bParseQuery)
{
  ResetErrorCodes();

  if (m_pDatabase == NULL)
    return false;

  wxArrayString QueryArray;
  if (bParseQuery)
    QueryArray = ParseQueries(strQuery);
  else
    QueryArray.push_back(strQuery);

  wxArrayString::iterator start = QueryArray.begin();
  wxArrayString::iterator stop = QueryArray.end();

  while (start != stop)
  {
    char* szErrorMessage = NULL;
    wxString strErrorMessage = _("");
    wxCharBuffer sqlBuffer = ConvertToUnicodeStream(*start);
    int nReturn = sqlite3_exec((sqlite3*)m_pDatabase, sqlBuffer, 0, 0, &szErrorMessage);
  
    if (szErrorMessage != NULL)
    {
      strErrorMessage = ConvertFromUnicodeStream(szErrorMessage);
      sqlite3_free(szErrorMessage);
    }

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

    start++;
  }
  return (sqlite3_changes((sqlite3*)m_pDatabase));
}
PostgresPreparedStatement* PostgresPreparedStatement::CreateStatement(PostgresInterface* pInterface, PGconn* pDatabase, const wxString& strSQL)
{
  wxArrayString Queries = ParseQueries(strSQL);

  wxArrayString::iterator start = Queries.begin();
  wxArrayString::iterator stop = Queries.end();

  PostgresPreparedStatement* pStatement = new PostgresPreparedStatement(pInterface);
  const char* strEncoding = pInterface->GetPQencodingToChar()(pInterface->GetPQclientEncoding()(pDatabase));
  wxCSConv conv((const wxChar*)strEncoding);
  pStatement->SetEncoding(&conv);
  while (start != stop)
  {
    wxString strName = PostgresPreparedStatement::GenerateRandomStatementName();
    pStatement->AddStatement(pDatabase, (*start), strName);
    wxCharBuffer nameBuffer = DatabaseStringConverter::ConvertToUnicodeStream(strName, strEncoding);
    wxCharBuffer sqlBuffer = DatabaseStringConverter::ConvertToUnicodeStream(TranslateSQL((*start)), strEncoding);
    PGresult* pResult = pInterface->GetPQprepare()(pDatabase, nameBuffer, sqlBuffer, 0, NULL);
    if (pResult == NULL)
    {
      delete pStatement;
      return NULL;
    }

    if (pInterface->GetPQresultStatus()(pResult) != PGRES_COMMAND_OK)
    {
      pStatement->SetErrorCode(PostgresDatabaseLayer::TranslateErrorCode(pInterface->GetPQresultStatus()(pResult)));
      pStatement->SetErrorMessage(DatabaseStringConverter::ConvertFromUnicodeStream(
        pInterface->GetPQresultErrorMessage()(pResult), strEncoding));
      pInterface->GetPQclear()(pResult);
      pStatement->ThrowDatabaseException();
      return pStatement;
    }
    pInterface->GetPQclear()(pResult);

    start++;
  }
    
  // Success?  Return the statement
  return pStatement;
}
Esempio n. 8
0
// query database
int TdsDatabaseLayer::RunQuery(const wxString& strQuery, bool bParseQuery)
{
  ResetErrorCodes();

  if (m_pDatabase == NULL)
    return false;

  FreeAllocatedResultSets();

  wxArrayString QueryArray;
  if (bParseQuery)
    QueryArray = ParseQueries(strQuery);
  else
    QueryArray.push_back(strQuery);

  wxArrayString::iterator start = QueryArray.begin();
  wxArrayString::iterator stop = QueryArray.end();

  while (start != stop)
  {
    char* szErrorMessage = NULL;
    wxString strErrorMessage = wxT("");
    wxString sql = RemoveLastSemiColon(*start);
    wxCharBuffer sqlBuffer = ConvertToUnicodeStream(sql);

    //fprintf(stderr, "Running query '%s'\n", (const char*)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", (sql).c_str());
      FreeAllocatedResultSets();
      ThrowDatabaseException();
      return false;
    }
    FreeAllocatedResultSets();

    start++;
  }
  return true;
}
Esempio n. 9
0
wxPreparedStatement* wxOdbcDatabase::PrepareStatement( const wxString& strQuery, bool bParseQuery )
{
    ResetErrorCodes();

    wxArrayString QueryArray;
    if (bParseQuery)
      QueryArray = ParseQueries(strQuery);
    else
      QueryArray.push_back(strQuery);

    wxOdbcPreparedStatement* pReturnStatement = new wxOdbcPreparedStatement(m_pInterface, (SQLHENV)m_sqlEnvHandle, (SQLHDBC)m_sqlHDBC);

    if (pReturnStatement)
        pReturnStatement->SetEncoding(GetEncoding());

    for (unsigned int i=0; i<(QueryArray.size()); i++)
    {
//#if wxUSE_UNICODE
//        void* sqlBuffer = (void*)(QueryArray[i].c_str());
//#else
        wxCharBuffer sqlBuffer = ConvertToUnicodeStream(QueryArray[i]);
//#endif
        //wxPrintf(_("Preparing statement: '%s'\n"), sqlBuffer);

        SQLHSTMT pSqlStatement = allocStmth();
        SQLRETURN nRet = m_pInterface->GetSQLPrepare()(pSqlStatement, (SQLTCHAR*)(const char*)sqlBuffer, SQL_NTS);
        if ( nRet != SQL_SUCCESS && nRet != SQL_SUCCESS_WITH_INFO )
        {
            InterpretErrorCodes( nRet );
            m_pInterface->GetSQLFreeStmt()(pSqlStatement, SQL_CLOSE);
            ThrowDatabaseException();
            return NULL;
        }

        if ( pSqlStatement )
            pReturnStatement->AddPreparedStatement( pSqlStatement );
    }

    return pReturnStatement;
}
Esempio n. 10
0
// PreparedStatement support
PreparedStatement* OTLDatabaseLayer::PrepareStatement(const wxString& strQuery)
{
    OTLPreparedStatement* pStatement = NULL;
    try
    {
        wxArrayString QueryArray = ParseQueries(strQuery);

        wxArrayString::iterator start = QueryArray.begin();
        wxArrayString::iterator stop = QueryArray.end();

        while (start != stop)
        {
            wxCharBuffer sqlBuffer = ConvertToUnicodeStream((*start));
            std::string strSQL(sqlBuffer);
            //fixme
            /*
               OTL::occi::Statement* pOTLStatement = m_pDatabase->createStatement(strSQL);
               if (pOTLStatement)
               {
                 pOTLStatement->setAutoCommit(m_bAutoCommit);
                 pStatement->AddStatement(pOTLStatement);
               }
            */
            start++;
        }
    }
    catch (otl_exception& e)
    {
        SetErrorCode(OTLDatabaseLayer::TranslateErrorCode(e.code));
        SetErrorMessage(ConvertFromUnicodeStream((char*)e.msg));
        wxDELETE(pStatement);
        ThrowDatabaseException();
        return NULL;
    }

    if (pStatement)
        LogStatementForCleanup(pStatement);

    return pStatement;
}
Esempio n. 11
0
PreparedStatement* SqliteDatabaseLayer::PrepareStatement(const wxString& strQuery, bool bLogForCleanup)
{
  ResetErrorCodes();

  if (m_pDatabase != NULL)
  {
    SqlitePreparedStatement* pReturnStatement = new SqlitePreparedStatement((sqlite3*)m_pDatabase);
    if (pReturnStatement)
      pReturnStatement->SetEncoding(GetEncoding());
    
    wxArrayString QueryArray = ParseQueries(strQuery);

    wxArrayString::iterator start = QueryArray.begin();
    wxArrayString::iterator stop = QueryArray.end();

    while (start != stop)
    {
      const char* szTail=0;
      wxCharBuffer sqlBuffer;
      do
      {
        sqlite3_stmt* pStatement;
        wxString strSQL;
        if (szTail != 0)
        {
          strSQL = (wxChar*)szTail;
        }
        else
        {
          strSQL = (*start);
        }
        sqlBuffer = ConvertToUnicodeStream(strSQL);
#if SQLITE_VERSION_NUMBER>=3003009
        int nReturn = sqlite3_prepare_v2((sqlite3*)m_pDatabase, sqlBuffer, -1, &pStatement, &szTail);
#else
        int nReturn = sqlite3_prepare((sqlite3*)m_pDatabase, sqlBuffer, -1, &pStatement, &szTail);
#endif
   
        if (nReturn != SQLITE_OK)
        {
          SetErrorCode(SqliteDatabaseLayer::TranslateErrorCode(nReturn));
          SetErrorMessage(ConvertFromUnicodeStream(sqlite3_errmsg((sqlite3*)m_pDatabase)));
          wxDELETE(pReturnStatement);
          ThrowDatabaseException();
          return NULL;
        }
        pReturnStatement->AddPreparedStatement(pStatement);

#if wxUSE_UNICODE
      } while (strlen(szTail) > 0);
#else
      } while (wxStrlen(szTail) > 0);
#endif    
      
      start++;
    }

    if (bLogForCleanup)
      LogStatementForCleanup(pReturnStatement);
    return pReturnStatement;
  }
Esempio n. 12
0
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;
  }
}
Esempio n. 13
0
// query database
int FirebirdDatabaseLayer::RunQuery(const wxString& strQuery, bool bParseQuery)
{
  ResetErrorCodes();
  if (m_pDatabase != NULL)
  {
    wxCharBuffer sqlDebugBuffer = ConvertToUnicodeStream(strQuery);
    //wxLogDebug(_("Running query: \"%s\"\n"), (const char*)sqlDebugBuffer);

    wxArrayString QueryArray;
    if (bParseQuery)
      QueryArray = ParseQueries(strQuery);
    else
      QueryArray.push_back(strQuery);

    wxArrayString::iterator start = QueryArray.begin();
    wxArrayString::iterator stop = QueryArray.end();

    int nRows = 1;
    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 (bQuickieTransaction)
      {
        BeginTransaction();
        if (GetErrorCode() != DATABASE_LAYER_OK)
        {
          wxLogError(_("Unable to start transaction"));
          ThrowDatabaseException();
          return DATABASE_LAYER_QUERY_RESULT_ERROR;
        }
      }

      while (start != stop)
      {
        wxCharBuffer sqlBuffer = ConvertToUnicodeStream(*start);
        isc_db_handle pDatabase = (isc_db_handle)m_pDatabase;
        isc_tr_handle pTransaction = (isc_tr_handle)m_pTransaction;
        //int nReturn = m_pInterface->GetIscDsqlExecuteImmediate()(*(ISC_STATUS_ARRAY*)m_pStatus, &pDatabase, &pTransaction, 0, (char*)(const char*)sqlBuffer, SQL_DIALECT_CURRENT, NULL);
        int nReturn = m_pInterface->GetIscDsqlExecuteImmediate()(*(ISC_STATUS_ARRAY*)m_pStatus, &pDatabase, &pTransaction, GetEncodedStreamLength(*start), (char*)(const char*)sqlBuffer, SQL_DIALECT_CURRENT, NULL);
        m_pDatabase = pDatabase;
        m_pTransaction = pTransaction;
        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
          isc_tr_handle pTransaction = (isc_tr_handle)m_pTransaction;
          m_pInterface->GetIscRollbackTransaction()(*(ISC_STATUS_ARRAY*)m_pStatus, &pTransaction);
          m_pTransaction = NULL;

          ThrowDatabaseException();
          return DATABASE_LAYER_QUERY_RESULT_ERROR;
        }
        start++;
      }

      if (bQuickieTransaction)
      {
        Commit();
        if (GetErrorCode() != DATABASE_LAYER_OK)
        {
          ThrowDatabaseException();
          return DATABASE_LAYER_QUERY_RESULT_ERROR;
        }
      }
    }

    return nRows;
  }
  else
  {
    wxLogError(_("Database handle is NULL"));
    return DATABASE_LAYER_QUERY_RESULT_ERROR;
  }
}