bool SqliteDatabaseLayer::Open(const wxString& strDatabase)
{
  ResetErrorCodes();

  //if (m_pDatabase == NULL)
  //  m_pDatabase = new sqlite3;

  wxCharBuffer databaseNameBuffer = ConvertToUnicodeStream(strDatabase);
  sqlite3* pDbPtr = (sqlite3*)m_pDatabase;
  int nReturn = sqlite3_open(databaseNameBuffer, &pDbPtr);
  m_pDatabase = pDbPtr;
  if (nReturn != SQLITE_OK)
  {
    SetErrorCode(SqliteDatabaseLayer::TranslateErrorCode(sqlite3_errcode((sqlite3*)m_pDatabase)));
    SetErrorMessage(ConvertFromUnicodeStream(sqlite3_errmsg((sqlite3*)m_pDatabase)));
    ThrowDatabaseException();
    return false;
  }
  return true;
}
Beispiel #2
0
void* OTLResultSet::GetResultBlob(int nField, wxMemoryBuffer& Buffer)
{
  try
  {
    /*fixme
       OTL::occi::Blob blob = m_pResultSet->getBlob(nField);
       blob.open(OTL::occi::OCCI_LOB_READONLY);
       int nLength = blob.length();
       unsigned char* pBuffer = (unsigned char*)(Buffer.GetWriteBuf(nLength));
       int nLengthRead = blob.read(nLength, pBuffer, nLength);
    */
  }
  catch (otl_exception& e)
  {
    SetErrorCode(OTLDatabaseLayer::TranslateErrorCode(e.code));
    SetErrorMessage(ConvertFromUnicodeStream((char*)e.msg));
    ThrowDatabaseException();
  }
  return Buffer.GetData();
}
Beispiel #3
0
void wxOdbcResultSet::InterpretErrorCodes( long WXUNUSED(nCode), SQLHSTMT stmth_ptr )
{
  //if ((nCode != SQL_SUCCESS) ) // && (nCode != SQL_SUCCESS_WITH_INFO))
  {
    SQLINTEGER iNativeCode;
    SQLTCHAR strState[ERR_STATE_LEN];
    SQLTCHAR strBuffer[ERR_BUFFER_LEN];
    SQLSMALLINT iMsgLen;

    memset(strState, 0, ERR_STATE_LEN*sizeof(SQLTCHAR));
    memset(strBuffer, 0, ERR_BUFFER_LEN*sizeof(SQLTCHAR));

    m_pInterface->GetSQLGetDiagRec()(SQL_HANDLE_STMT, stmth_ptr, 1, strState, &iNativeCode, 
      strBuffer, ERR_BUFFER_LEN, &iMsgLen);  
 
    SetErrorCode((int)iNativeCode);
    SetErrorMessage(ConvertFromUnicodeStream((char*)strBuffer)); // AML
    //AML SetErrorMessage(wxString((wxChar*)strBuffer));
  }
}
wxString wxPostgresResultSet::GetResultString(int nField)
{
  wxString strValue = wxT("");
  if (m_bBinaryResults)
  {
    wxLogError(_("Not implemented\n"));
  }
  else
  {
    if (nField != -1)
    {
      if (m_pInterface->GetPQgetisnull()(m_pResult, m_nCurrentRow, nField-1) != 1)
      {
        strValue = ConvertFromUnicodeStream(m_pInterface->GetPQgetvalue()(m_pResult, m_nCurrentRow, nField-1));
      }
    }
  }

  return strValue;
}
// 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));
}
Beispiel #6
0
void OTLResultSet::Close()
{
  try
  {
    CloseMetaData();

    for (unsigned int i = 0 ; i < m_otldesc.size() ;++i)
    {
      delete m_otldesc[i];
    }
    m_otldesc.clear();

    if (m_pResultSet != NULL)
    {
      delete m_pResultSet;
      m_pResultSet =NULL;
    }
    //fixme
    /*
      otl_stream* pStatement = m_pResultSet->getStatement();
     
       if (pStatement)
       {
         pStatement->closeResultSet(m_pResultSet);
     
         if (m_bManageStatement)
         {
           OTL::occi::Connection* pDatabase = pStatement->getConnection();
           if (pDatabase)
             pDatabase->terminateStatement(pStatement);
         }
       }
    */
  }
  catch (otl_exception& e)
  {
    SetErrorCode(OTLDatabaseLayer::TranslateErrorCode(e.code));
    SetErrorMessage(ConvertFromUnicodeStream((char*)e.msg));
    ThrowDatabaseException();
  }
}
Beispiel #7
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;
}
// close database  
bool SqliteDatabaseLayer::Close()
{
  ResetErrorCodes();

  CloseResultSets();
  CloseStatements();

  if (m_pDatabase != NULL)
  {
    int nReturn = sqlite3_close((sqlite3*)m_pDatabase);
    if (nReturn != SQLITE_OK)
    {
      SetErrorCode(SqliteDatabaseLayer::TranslateErrorCode(sqlite3_errcode((sqlite3*)m_pDatabase)));
      SetErrorMessage(ConvertFromUnicodeStream(sqlite3_errmsg((sqlite3*)m_pDatabase)));
      ThrowDatabaseException();
      return false;
    }
    m_pDatabase = NULL;
  }

  return true;
}
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* PostgresPreparedStatementWrapper::RunQueryWithResults()
{
  int nParameters = m_Parameters.GetSize();
  char** paramValues = m_Parameters.GetParamValues();
  int* paramLengths = m_Parameters.GetParamLengths();
  int* paramFormats = m_Parameters.GetParamFormats();
  int nResultFormat = 0; // 0 = text, 1 = binary (all or none on the result set, not column based)
  wxCharBuffer statementNameBuffer = ConvertToUnicodeStream(m_strStatementName);
  PGresult* pResult = m_pInterface->GetPQexecPrepared()(m_pDatabase, statementNameBuffer, nParameters, paramValues, paramLengths, paramFormats, nResultFormat);
  if (pResult != NULL)
  {
    ExecStatusType status = m_pInterface->GetPQresultStatus()(pResult);
    if ((status != PGRES_COMMAND_OK) && (status != PGRES_TUPLES_OK))
    {
      SetErrorCode(PostgresDatabaseLayer::TranslateErrorCode(status));
      SetErrorMessage(ConvertFromUnicodeStream(m_pInterface->GetPQresultErrorMessage()(pResult)));
    }
    else
    {
      delete []paramValues;
      delete []paramLengths;
      delete []paramFormats;

      PostgresResultSet* pResultSet = new PostgresResultSet(m_pInterface, pResult);
      pResultSet->SetEncoding(GetEncoding());
      return pResultSet;
    }
    m_pInterface->GetPQclear()(pResult);
  }
  delete []paramValues;
  delete []paramLengths;
  delete []paramFormats;

  ThrowDatabaseException();

  return NULL;
}
wxString OdbcResultSetMetaData::GetColumnName(int i)
{
  wxString columnName;

  SQLTCHAR      col_name[8192];
  SQLSMALLINT   col_name_length;
  SQLSMALLINT   col_data_type;
  SQLUINTEGER   col_size;
  SQLSMALLINT   col_decimal_digits;
  SQLSMALLINT   col_nullable;

  memset(col_name, 0, 8192);

  SQLRETURN nRet = m_pInterface->GetSQLDescribeCol()( m_pOdbcStatement, i, col_name, 
      8192, &col_name_length, &col_data_type, &col_size, &col_decimal_digits, &col_nullable );

  if ( nRet == SQL_SUCCESS )
  {
      //return wxString((wxChar*)col_name);
	  columnName = ConvertFromUnicodeStream((const char*)(wxChar*)col_name);
  }

  return columnName;
}
wxString OTLResultSetMetaData::GetColumnName(int i)
{
  wxString strField = ConvertFromUnicodeStream(m_desc[i]->name);
  return strField;
}
int OTLResultSetMetaData::GetColumnType(int i)
{
  int fieldType = COLUMN_UNKNOWN;
  int OTLFieldType = m_desc[i]->dbtype;
  wxString strV(ConvertFromUnicodeStream(m_desc[i]->name));


  switch (OTLFieldType)
  {
  case extLong:
  case extUInt:
  case extNumber:
  case extInt:
  case extVarNum:
  case extRowId: //fixme: check
    fieldType = COLUMN_INTEGER;
    break;

  case extVarChar2:
  case extLongVarChar:
  case extCChar:
  case extVarChar:
  case extChar:
  case extCharZ:
  case extMslabel://fixme : Check
    fieldType = COLUMN_STRING;
    break;

  case extFloat:
    fieldType = COLUMN_DOUBLE;
    break;
    /*
     case SQL_BIT:
       fieldType = COLUMN_BOOL;
       break;
    */
  case extBLOB:
  case extCLOB:
  case extVarRaw: //fixme : Check
    //case extRaw: //fixme : Check
  case extLongRaw://fixme : Check
  case extLongVarRaw://fixme : Check
    fieldType = COLUMN_BLOB;
    break;

  case extDate:
#if (defined(OTL_ORA8I)||defined(OTL_ORA9I))&&defined(OTL_ORA_TIMESTAMP)

  case extTimestamp:
  case extTimestamp_TZ:
  case extTimestamp_LTZ:
#endif

    fieldType = COLUMN_DATE;
    break;

  default:
    fieldType = COLUMN_UNKNOWN;
    break;
  };
  return fieldType;
}
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;
  }
Beispiel #15
0
// open database
bool OTLDatabaseLayer::Open()
{
    try
    {
        wxString tnsString = wxT("");

        if (m_strServer != wxEmptyString)
        {
            tnsString += _("Host=") + m_strServer;
            if (m_strPort != wxEmptyString)
            {
                // Add the parameter separater (colon) if needed
                if (tnsString.length() > 0)
                    tnsString += _(";");
                tnsString += _("Port=") + m_strPort;
            }
        }
        if (m_strDatabase != wxEmptyString)
        {
            // Add the parameter separater (colon) if needed
            if (tnsString.length() > 0)
                tnsString += _(";");
            tnsString += _("Service Name=") + m_strDatabase;
        }

        wxCharBuffer tnsStringBuffer = ConvertToUnicodeStream(tnsString);
        std::string strTnsString(tnsStringBuffer);

        wxString connectString = m_strUser + wxT("/") + m_strPassword;
        if (m_strDatabase != wxEmptyString)
            connectString += wxT("@") + m_strDatabase;

        try
        {
            m_database.rlogon(connectString.mb_str());
        }
        catch (otl_exception& e)
        {
            SetErrorCode(OTLDatabaseLayer::TranslateErrorCode(e.code));
            SetErrorMessage(ConvertFromUnicodeStream((char*)e.msg));
            ThrowDatabaseException();
            return false;
        }
        catch (...)
        {
            if (GetErrorCode() == DATABASE_LAYER_OK)
            {
                SetErrorCode(DATABASE_LAYER_ERROR);
                SetErrorMessage(_("Error creating database connection"));
            }
            ThrowDatabaseException();
            return false;
        }
    }
    catch (otl_exception& e)
    {
        SetErrorCode(OTLDatabaseLayer::TranslateErrorCode(e.code));
        SetErrorMessage(ConvertFromUnicodeStream((char*)e.msg));
        ThrowDatabaseException();
        return false;
    }
    return true;
}
Beispiel #16
0
void wxOdbcResultSet::RetrieveFieldData(int nField)
{
    if (nField != -1)
    {
      SQLRETURN rc;
      SQLSMALLINT buflen;
      unsigned long int colType;
      rc = m_pInterface->GetSQLColAttribute()(m_pOdbcStatement, nField, SQL_DESC_TYPE, NULL, 0,
        &buflen, &colType);

      if (SQL_FLOAT == colType || SQL_DOUBLE == colType)
      {
        SQLFLOAT ret;
        SQLINTEGER sqlPtr;
        rc = m_pInterface->GetSQLGetData()(m_pOdbcStatement, nField, SQL_C_DOUBLE, &ret, 0, &sqlPtr);
        if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
        {
          InterpretErrorCodes(rc, m_pOdbcStatement);
          ThrowDatabaseException();
          return;
        }
        // Mark this field as retrieved
        m_RetrievedValues.insert(nField);
        // Record whether this field is NULL
        if (sqlPtr == SQL_NULL_DATA)
        {
          m_NullValues.insert(nField);
          ret = 0;
          m_fieldValues[nField-1] = ret;
        }
        else
        {
          m_fieldValues[nField-1] = ret;
        }
      }
      else if (SQL_DATETIME == colType)
      {
        TIMESTAMP_STRUCT ret;
        SQLINTEGER sqlPtr;
        rc = m_pInterface->GetSQLGetData()(m_pOdbcStatement, nField, SQL_C_TIMESTAMP, &ret, sizeof(ret),
          &sqlPtr);

        if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
        {
          InterpretErrorCodes(rc, m_pOdbcStatement);
          ThrowDatabaseException();
          return;
        }
        // Mark this field as retrieved
        m_RetrievedValues.insert(nField);
        // Record whether this field is NULL
        if (sqlPtr == SQL_NULL_DATA)
        {
          m_NullValues.insert(nField);
          //m_fieldValues[nField-1] = wxInvalidDateTime;
        }
        else
        {
          /*
          wxPrintf(_T("day = %d, month = %d, year = %d, hour = %d, minute = %d, second = %d, fraction = %d\n"), 
            ret.day, ret.month, ret.year, ret.hour, ret.minute, ret.second, ret.fraction);*/
          wxDateTime dt(ret.day, wxDateTime::Month(ret.month-1), ret.year, ret.hour,
            ret.minute, ret.second, ret.fraction);
          m_fieldValues[nField-1] = dt;
        }
      }
      else
      {
        wxString strValue;
        SQLPOINTER buff[8192];

        memset(buff, 0, 8192*sizeof(SQLTCHAR));

        SQLINTEGER  col_size         = 8192;
        SQLINTEGER  real_size        = 0;

        if (m_pOdbcStatement == NULL)
            m_pOdbcStatement = m_pStatement->GetLastStatement();

		SQLRETURN nRet = m_pInterface->GetSQLGetData()( m_pOdbcStatement, nField, SQL_C_TCHAR, buff, col_size, &real_size ); // should SQL_C_CHAR be SQL_C_TCHAR?
        if ( nRet != SQL_SUCCESS && nRet != SQL_SUCCESS_WITH_INFO )
        {
            InterpretErrorCodes(nRet, m_pOdbcStatement);
            ThrowDatabaseException();
            return;
        }
        strValue += ConvertFromUnicodeStream((const wxChar*)/*(const char*)*/buff);
        // Mark this field as retrieved
        m_RetrievedValues.insert(nField);
        // Record whether this field is NULL
        if (real_size == SQL_NULL_DATA)
          m_NullValues.insert(nField);

        if ( real_size > col_size )
        {
            while ( nRet != SQL_NO_DATA )
            {
                nRet = m_pInterface->GetSQLGetData()( m_pOdbcStatement, nField, SQL_C_TCHAR, buff, col_size, &real_size ); // should SQL_C_CHAR be SQL_C_TCHAR?
                if ( nRet != SQL_SUCCESS && nRet != SQL_SUCCESS_WITH_INFO && nRet != SQL_NO_DATA )
                {
                    InterpretErrorCodes(nRet, m_pOdbcStatement);
                    ThrowDatabaseException();
                    return;
                }
                strValue += ConvertFromUnicodeStream((const wxChar*)/*(const char*)*/buff);
            }
        }

        m_fieldValues[nField-1] = strValue;//.Trim();
      }
    }
}
wxString OracleResultSetMetaData::GetColumnName(int i)
{
  wxString strField = ConvertFromUnicodeStream(m_MetaData[i].getString(oracle::occi::MetaData::ATTR_NAME).c_str());
  return strField;
}