void TdsPreparedStatement::SetParamDate(int nPosition, const wxDateTime& dateValue)
{
  //fprintf(stderr, "Setting param %d to date %s\n", nPosition, dateValue.Format().c_str());
  ResetErrorCodes();

  AllocateParameter(nPosition);

  wxString dateAsString = dateValue.Format(wxT("%Y-%m-%d %H:%M:%S"));
  //fprintf(stderr, "Setting param %d to date %s\n", nPosition, dateAsString.c_str());
  CONV_RESULT cr;
  wxCharBuffer dateCharBuffer = ConvertToUnicodeStream(dateAsString);
  int bufferLength = GetEncodedStreamLength(dateAsString);
  int ret = tds_convert(this->m_pDatabase->tds_ctx, SYBVARCHAR, 
    (TDS_CHAR*)(const char*)dateCharBuffer, bufferLength, SYBDATETIME, &cr);
  //fprintf(stderr, "tds_convert returned %d, sizeof TDS_DATETIME = %d\n", ret, sizeof(TDS_DATETIME));

  int valueSize = (ret < 0) ? sizeof(TDS_DATETIME) : ret;

  TDSCOLUMN* curcol = m_pParameters->columns[nPosition-1];
  curcol->column_type = SYBDATETIMN;
  curcol->on_server.column_type = SYBDATETIMN;
  curcol->column_size =  valueSize;
  curcol->on_server.column_size =  valueSize;
  curcol->column_varint_size = 1;
  curcol->column_cur_size = valueSize;

  //tds_alloc_param_data(m_pParameters, curcol);
  tds_alloc_param_data(curcol);
  memcpy(curcol->column_data, &cr.dt, valueSize);
}
Esempio n. 2
0
int wxOdbcDatabase::RunQuery( const wxString& strQuery, bool bParseQuery )
{
   ResetErrorCodes();

   //wxPrintf("Running: '%s'\n", strQuery.c_str());
   wxOdbcPreparedStatement* pStatement = (wxOdbcPreparedStatement*)PrepareStatement( strQuery, bParseQuery );

   if ( pStatement )
   {
     try
     {
       int nRows = pStatement->RunQuery();
       wxDELETE( pStatement );
       return nRows;
     }
     catch (...)
     {
       SetErrorCode(pStatement->GetErrorCode());
       SetErrorMessage(pStatement->GetErrorMessage());
       wxDELETE( pStatement );
       ThrowDatabaseException();
       return wxDATABASE_QUERY_RESULT_ERROR;
     }
   }
   else
     return wxDATABASE_QUERY_RESULT_ERROR;
}
Esempio n. 3
0
// query database
int wxPostgresDatabase::RunQuery(const wxString& strQuery, bool WXUNUSED(bParseQuery))
{
    // PostgreSQL takes care of parsing the queries itself so bParseQuery is ignored

    ResetErrorCodes();

    wxCharBuffer sqlBuffer = ConvertToUnicodeStream(strQuery);
    PGresult* pResultCode = m_pInterface->GetPQexec()((PGconn*)m_pDatabase, sqlBuffer);
    if ((pResultCode == NULL) || (m_pInterface->GetPQresultStatus()(pResultCode) != PGRES_COMMAND_OK))
    {
        SetErrorCode(wxPostgresDatabase::TranslateErrorCode(m_pInterface->GetPQresultStatus()(pResultCode)));
        SetErrorMessage(ConvertFromUnicodeStream(m_pInterface->GetPQerrorMessage()((PGconn*)m_pDatabase)));
        m_pInterface->GetPQclear()(pResultCode);
        ThrowDatabaseException();
        return wxDATABASE_QUERY_RESULT_ERROR;
    }
    else
    {
        wxString rowsAffected = ConvertFromUnicodeStream(m_pInterface->GetPQcmdTuples()(pResultCode));
        long rows = -1;
        rowsAffected.ToLong(&rows);
        m_pInterface->GetPQclear()(pResultCode);
        return (int)rows;
    }
}
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;
}
Esempio n. 5
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;
}
int wxSqlitePreparedStatement::RunQuery()
{
  ResetErrorCodes();

  wxSqliteStatementVector::iterator start = m_Statements.begin();
  wxSqliteStatementVector::iterator stop = m_Statements.end();
  while (start != stop)
  {
    int nReturn = sqlite3_step((sqlite3_stmt*)(*start));
 
    if (nReturn != SQLITE_ROW)
      sqlite3_reset((sqlite3_stmt*)(*start));

    if ((nReturn != SQLITE_ROW) && (nReturn != SQLITE_DONE))
    {
      SetErrorCode(wxSqliteDatabase::TranslateErrorCode(nReturn));
      SetErrorMessage(ConvertFromUnicodeStream(sqlite3_errmsg(m_pDatabase)));
      ThrowDatabaseException();
      return wxDATABASE_QUERY_RESULT_ERROR;
    }
    start++;
  }

  return sqlite3_changes(m_pDatabase);
}
Esempio n. 7
0
// wxPreparedStatement support
wxPreparedStatement* wxPostgresDatabase::PrepareStatement(const wxString& strQuery)
{
    ResetErrorCodes();

    wxPostgresPreparedStatement* pStatement = wxPostgresPreparedStatement::CreateStatement(m_pInterface, (PGconn*)m_pDatabase, strQuery);
    LogStatementForCleanup(pStatement);
    return pStatement;
}
Esempio n. 8
0
bool wxOdbcDatabase::Open( )
{
   ResetErrorCodes();

   if ( !m_strDSN.IsEmpty() )
   {
#ifdef wxUSE_UNICODE
     wxCharBuffer dsnCharBuffer = ConvertToUnicodeStream(m_strDSN);
     wxCharBuffer userCharBuffer = ConvertToUnicodeStream(m_strUser);
     wxCharBuffer passwordCharBuffer = ConvertToUnicodeStream(m_strPassword);
#else
     void* dsnCharBuffer = (void*)m_strDSN.c_str();
     void* userCharBuffer = (void*)m_strUser.c_str();
     void* passwordCharBuffer = (void*)m_strPassword.c_str();
#endif

     SQLRETURN nRet;
     nRet = m_pInterface->GetSQLConnect()((SQLHDBC)m_sqlHDBC, (SQLTCHAR FAR*)(const char*)dsnCharBuffer,
                SQL_NTS, (SQLTCHAR FAR*)(const char*)userCharBuffer, SQL_NTS,
                (SQLTCHAR FAR*)(const char*)passwordCharBuffer, SQL_NTS);
     if ( nRet != SQL_SUCCESS && nRet != SQL_SUCCESS_WITH_INFO )
     {
       InterpretErrorCodes( nRet );
       ThrowDatabaseException();
     }
   }
   else if ( !m_strConnection.IsEmpty() )
   {
     SQLTCHAR buff[8192];
     SQLSMALLINT iLen;

     memset(buff, 0, 8192*sizeof(SQLTCHAR));
     
     wxCharBuffer connectionCharBuffer = ConvertToUnicodeStream(m_strConnection); //AML
     //AMLvoid* connectionCharBuffer = (void*)m_strConnection.c_str();
#if wxUSE_GUI
     SQLRETURN nRet = m_pInterface->GetSQLDriverConnect()((SQLHDBC)m_sqlHDBC, m_pParent ? (SQLHWND)m_pParent->GetHandle() : NULL, (SQLTCHAR*)(const char*)connectionCharBuffer,
         (SQLSMALLINT)m_strConnection.Length(), (SQLTCHAR*)buff, 8192, &iLen, m_bPrompt ? SQL_DRIVER_PROMPT : SQL_DRIVER_NOPROMPT);
#else
     SQLRETURN nRet = m_pInterface->GetSQLDriverConnect()((SQLHDBC)m_sqlHDBC, NULL, (SQLTCHAR*)(const char*)connectionCharBuffer,
         (SQLSMALLINT)m_strConnection.Length(), (SQLTCHAR*)buff, 8192, &iLen, SQL_DRIVER_NOPROMPT);
#endif

     if ( nRet != SQL_SUCCESS && nRet != SQL_SUCCESS_WITH_INFO )
     {
       InterpretErrorCodes( nRet );
       ThrowDatabaseException();
     }
   }
   else
   {
     return false;
   }

   m_bIsConnected = true;    
        
   return true;
}
Esempio n. 9
0
// open database
bool wxPostgresDatabase::Open()
{
    ResetErrorCodes();

    wxCharBuffer serverCharBuffer;
    const char* pHost = NULL;
    wxCharBuffer pDatabaseBuffer = ConvertToUnicodeStream(m_strDatabase);
    const char* pDatabase = pDatabaseBuffer;
    wxCharBuffer userCharBuffer;
    const char* pUser = NULL;
    wxCharBuffer passwordCharBuffer;
    const char* pPassword = NULL;
    const char* pTty = NULL;
    const char* pOptions = NULL;
    wxCharBuffer portCharBuffer;
    const char* pPort = NULL;

    if (m_strServer != _("localhost") && m_strServer != wxT(""))
    {
        serverCharBuffer = ConvertToUnicodeStream(m_strServer);
        pHost = serverCharBuffer;
    }

    if (m_strUser != wxT(""))
    {
        userCharBuffer = ConvertToUnicodeStream(m_strUser);
        pUser = userCharBuffer;
    }

    if (m_strPassword != wxT(""))
    {
        passwordCharBuffer = ConvertToUnicodeStream(m_strPassword);
        pPassword = passwordCharBuffer;
    }

    if (m_strPort != wxT(""))
    {
        portCharBuffer = ConvertToUnicodeStream(m_strPort);
        pPort = portCharBuffer;
    }

    m_pDatabase = m_pInterface->GetPQsetdbLogin()(pHost, pPort, pOptions, pTty, pDatabase, pUser, pPassword);
    if (m_pInterface->GetPQstatus()((PGconn*)m_pDatabase) == CONNECTION_BAD)
    {
        SetErrorCode(wxPostgresDatabase::TranslateErrorCode(m_pInterface->GetPQstatus()((PGconn*)m_pDatabase)));
        SetErrorMessage(ConvertFromUnicodeStream(m_pInterface->GetPQerrorMessage()((PGconn*)m_pDatabase)));
        ThrowDatabaseException();
        return false;
    }

    m_pInterface->GetPQsetClientEncoding()((PGconn*)m_pDatabase, "UTF-8");
    wxCSConv conv((const wxChar*)(m_pInterface->GetPQencodingToChar()(m_pInterface->GetPQclientEncoding()((PGconn*)m_pDatabase))));
    SetEncoding(&conv);

    return true;
}
Esempio n. 10
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;
  }
}
Esempio n. 11
0
void wxOdbcDatabase::BeginTransaction()
{
   ResetErrorCodes();

   SQLRETURN nRet = m_pInterface->GetSQLSetConnectAttr()((SQLHDBC)m_sqlHDBC, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_OFF, 0);
   if ( nRet != SQL_SUCCESS )
   {
     InterpretErrorCodes( nRet );
     ThrowDatabaseException();
   }
}
Esempio n. 12
0
int TdsPreparedStatement::GetParameterCount()
{
  ResetErrorCodes();

  int nReturn = 0;
  // It would probably be better to iterate through the query string to make sure that
  //  none of the '?' are in string literals
  //fprintf(stderr, "Freq of '%s' = %d\n", m_strOriginalQuery, m_strOriginalQuery.Freq('?'));
  nReturn = m_strOriginalQuery.Freq('?');

  return nReturn;
}
void wxMysqlPreparedStatementResultSet::Close()
{
  ResetErrorCodes();

  CloseMetaData();

  MYSQL_RES* pResultMetadata = m_pInterface->GetMysqlStmtResultMetadata()(m_pStatement);
  if (!pResultMetadata)
  {
    SetErrorCode(wxMysqlDatabase::TranslateErrorCode(m_pInterface->GetMysqlStmtErrno()(m_pStatement)));
    SetErrorMessage(ConvertFromUnicodeStream(m_pInterface->GetMysqlStmtError()(m_pStatement)));
    ThrowDatabaseException();
  }
  else
  {
    int nParameters = m_pInterface->GetMysqlNumFields()(pResultMetadata);
    for (int i=0; i<nParameters; i++)
    {
//      int nType = m_pResultBindings[i].buffer_type;
//      if (nType == MYSQL_TYPE_STRING || nType == MYSQL_TYPE_VAR_STRING || nType == MYSQL_TYPE_BLOB
//          || nType == MYSQL_TYPE_TINY_BLOB || nType == MYSQL_TYPE_MEDIUM_BLOB || nType == MYSQL_TYPE_LONG_BLOB)
//      {
//      void* pData = m_pResultBindings[i].buffer;
//      if (pData != NULL)
//      {
//        free(m_pResultBindings[i].buffer);
//        m_pResultBindings[i].buffer = NULL;
//      }
//      }
    }
    m_pInterface->GetMysqlFreeResult()(pResultMetadata);
  }

  IntToMysqlParameterMap::iterator start = m_BindingWrappers.begin();
  IntToMysqlParameterMap::iterator stop = m_BindingWrappers.end();

  while (start != stop)
  {
    wxDELETE((*start).second);
    start++;
  }
  m_BindingWrappers.clear();

  wxDELETEA(m_pResultBindings);

  if (m_pStatement != NULL)
  {
    m_pInterface->GetMysqlStmtFreeResult()(m_pStatement);
    if (m_bManageStatement)
      m_pInterface->GetMysqlStmtClose()(m_pStatement);
    m_pStatement = NULL;
  }
}
int wxSqlitePreparedStatement::GetParameterCount()
{
  ResetErrorCodes();

  int nReturn = 0;
  wxSqliteStatementVector::iterator start = m_Statements.begin();
  wxSqliteStatementVector::iterator stop = m_Statements.end();
  while (start != stop)
  {
    nReturn += sqlite3_bind_parameter_count((sqlite3_stmt*)(*start));
    start++;
  }
  return nReturn;
}
Esempio n. 15
0
void* wxOdbcDatabase::allocStmth()
{
    ResetErrorCodes();

    SQLHANDLE handle = NULL;
        
    SQLRETURN nRet = m_pInterface->GetSQLAllocHandle()(SQL_HANDLE_STMT, (SQLHDBC)m_sqlHDBC, &handle);
    if ( nRet != SQL_SUCCESS )
    {
        InterpretErrorCodes( nRet );
        ThrowDatabaseException();
    }
    return handle;
}
Esempio n. 16
0
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;
}
Esempio n. 17
0
// ctor()
wxOdbcDatabase::wxOdbcDatabase(): wxDatabase()
{
   m_bIsConnected = false;
   ResetErrorCodes();

   m_pInterface = new wxOdbcInterface();
   if (!m_pInterface->Init())
   {
     SetErrorCode(wxDATABASE_ERROR_LOADING_LIBRARY);
     SetErrorMessage(wxT("Error loading ODBC library"));
     ThrowDatabaseException();
     return;
   }

   SQLHENV sqlEnvHandle = (SQLHENV)m_sqlEnvHandle;
   SQLRETURN nRet = m_pInterface->GetSQLAllocHandle()(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlEnvHandle);
   m_sqlEnvHandle = sqlEnvHandle;
   if ( nRet != SQL_SUCCESS )
   {
     InterpretErrorCodes( nRet );
     ThrowDatabaseException();
   }

   nRet = m_pInterface->GetSQLSetEnvAttr()((SQLHENV)m_sqlEnvHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
   if ( nRet != SQL_SUCCESS )
   {
     InterpretErrorCodes( nRet );
     ThrowDatabaseException();
   }

   SQLHDBC sqlHDBC = (SQLHDBC)m_sqlHDBC;
   nRet = m_pInterface->GetSQLAllocHandle()(SQL_HANDLE_DBC, (SQLHENV)m_sqlEnvHandle, &sqlHDBC);
   m_sqlHDBC = sqlHDBC;
   if ( nRet != SQL_SUCCESS )
   {
     InterpretErrorCodes( nRet );
     ThrowDatabaseException();
   }

   m_strDSN = wxEmptyString;
   m_strUser = wxEmptyString;
   m_strPassword = wxEmptyString;
   m_strConnection = wxEmptyString;
#if wxUSE_GUI
   m_bPrompt = false;
   m_pParent = NULL;
#endif
}
void wxSqlitePreparedStatement::SetParamBlob(int nPosition, const void* pData, long nDataLength)
{
  ResetErrorCodes();

  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
  if (nIndex > -1)
  {
    sqlite3_reset(m_Statements[nIndex]);
    int nReturn = sqlite3_bind_blob(m_Statements[nIndex], nPosition, (const void*)pData, nDataLength, SQLITE_STATIC);
    if (nReturn != SQLITE_OK)
    {
      SetErrorCode(wxSqliteDatabase::TranslateErrorCode(nReturn));
      SetErrorMessage(ConvertFromUnicodeStream(sqlite3_errmsg(m_pDatabase)));
      ThrowDatabaseException();
    }
  }
}
Esempio n. 19
0
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;
  }
}
void wxSqlitePreparedStatement::SetParamDouble(int nPosition, double dblValue)
{
  ResetErrorCodes();

  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
  if (nIndex > -1)
  {
    sqlite3_reset(m_Statements[nIndex]);
    int nReturn = sqlite3_bind_double(m_Statements[nIndex], nPosition, dblValue);
    if (nReturn != SQLITE_OK)
    {
      SetErrorCode(wxSqliteDatabase::TranslateErrorCode(nReturn));
      SetErrorMessage(ConvertFromUnicodeStream(sqlite3_errmsg(m_pDatabase)));
      ThrowDatabaseException();
    }
  }
}
Esempio n. 21
0
int TdsPreparedStatement::RunQuery()
{
  ResetErrorCodes();

  //fprintf(stderr, "Running statement\n");
  //fprintf(stderr, "m_pDatabase %d\n", m_pDatabase);
  //fprintf(stderr, "m_pStatement %d\n", m_pStatement);
  //fprintf(stderr, "m_pParameters %d\n", m_pParameters);
  // 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 DATABASE_LAYER_QUERY_RESULT_ERROR;
  }
  FreeAllocatedResultSets();
  tds_free_input_params(m_pStatement);
  m_pStatement->params = m_pParameters;

  // Execute the query
  nReturn = tds_submit_execute(m_pDatabase, m_pStatement);
  if (nReturn != TDS_SUCCEED)
  {
    //fprintf(stderr, "tds_submit_execute() failed for statement '%s'\n", m_pStatement->query);
    SetErrorInformationFromDatabaseLayer();
    FreeAllocatedResultSets();
    ThrowDatabaseException();
  }
  //fprintf(stderr, "Statement executed.  Freeing results\n");
  FreeAllocatedResultSets();

  return DATABASE_LAYER_QUERY_RESULT_ERROR;
}
Esempio n. 22
0
PreparedStatement* FirebirdDatabaseLayer::PrepareStatement(const wxString& strQuery)
{
  ResetErrorCodes();
  
  FirebirdPreparedStatement* pStatement = FirebirdPreparedStatement::CreateStatement(m_pInterface, m_pDatabase, m_pTransaction, strQuery, GetEncoding());
  if (pStatement && (pStatement->GetErrorCode() != DATABASE_LAYER_OK))
  {
    SetErrorCode(pStatement->GetErrorCode());
    SetErrorMessage(pStatement->GetErrorMessage());
    wxDELETE(pStatement); // This sets the pointer to NULL after deleting it

    ThrowDatabaseException();
    return NULL;
  }

  LogStatementForCleanup(pStatement);
  return pStatement;
}
void wxSqlitePreparedStatement::SetParamString(int nPosition, const wxString& strValue)
{
  ResetErrorCodes();

  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
  if (nIndex > -1)
  {
    sqlite3_reset(m_Statements[nIndex]);
    wxCharBuffer valueBuffer = ConvertToUnicodeStream(strValue);
    int nReturn = sqlite3_bind_text(m_Statements[nIndex], nPosition, valueBuffer, -1, SQLITE_TRANSIENT);
    if (nReturn != SQLITE_OK)
    {
      SetErrorCode(wxSqliteDatabase::TranslateErrorCode(nReturn));
      SetErrorMessage(ConvertFromUnicodeStream(sqlite3_errmsg(m_pDatabase)));
      ThrowDatabaseException();
    }
  }
}
Esempio n. 24
0
void wxOdbcDatabase::RollBack()
{
   ResetErrorCodes();

   SQLRETURN nRet = m_pInterface->GetSQLEndTran()(SQL_HANDLE_DBC, (SQLHDBC)m_sqlHDBC, SQL_ROLLBACK);
   if ( nRet != SQL_SUCCESS )
   {
     InterpretErrorCodes( nRet );
     ThrowDatabaseException();
   }
   
   nRet = m_pInterface->GetSQLSetConnectAttr()((SQLHDBC)m_sqlHDBC, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, SQL_IS_INTEGER);
   if ( nRet != SQL_SUCCESS )
   {
     InterpretErrorCodes( nRet );
     ThrowDatabaseException();
   }
}
Esempio n. 25
0
// set parameter
void TdsPreparedStatement::SetParamInt(int nPosition, int nValue)
{
  //fprintf(stderr, "Setting param %d to int %d\n", nPosition, nValue);
  ResetErrorCodes();

  AllocateParameter(nPosition);
  TDSCOLUMN* curcol = m_pParameters->columns[nPosition-1];
  curcol->column_type = SYBINTN;
  curcol->on_server.column_type = SYBINTN;
  curcol->column_size = sizeof(TDS_INT);
  curcol->on_server.column_size = sizeof(TDS_INT);
  curcol->column_varint_size = 1;
  curcol->column_cur_size = sizeof(TDS_INT);

  //tds_alloc_param_data(m_pParameters, curcol);
  tds_alloc_param_data(curcol);
  memcpy(curcol->column_data, &nValue, sizeof(nValue));
}
Esempio n. 26
0
void TdsPreparedStatement::SetParamDouble(int nPosition, double dblValue)
{
  //fprintf(stderr, "Setting param %d to double %f\n", nPosition, dblValue);
  ResetErrorCodes();

  AllocateParameter(nPosition);
  TDSCOLUMN* curcol = m_pParameters->columns[nPosition-1];
  curcol->column_type = SYBFLTN;
  curcol->on_server.column_type = SYBFLTN;
  curcol->column_size = sizeof(TDS_FLOAT);
  curcol->on_server.column_size = sizeof(TDS_FLOAT);
  curcol->column_varint_size = 1;
  curcol->column_cur_size = sizeof(TDS_FLOAT);

  //tds_alloc_param_data(m_pParameters, curcol);
  tds_alloc_param_data(curcol);
  memcpy(curcol->column_data, &dblValue, sizeof(dblValue));
}
Esempio n. 27
0
void TdsPreparedStatement::SetParamBool(int nPosition, bool bValue)
{
  //fprintf(stderr, "Setting param %d to boolean %d\n", nPosition, bValue);
  ResetErrorCodes();

  AllocateParameter(nPosition);
  TDSCOLUMN* curcol = m_pParameters->columns[nPosition-1];
  curcol->column_type = SYBBITN;
  curcol->on_server.column_type = SYBBITN;
  curcol->column_size = sizeof(bool);
  curcol->on_server.column_size = sizeof(bool);
  curcol->column_varint_size = 1;
  curcol->column_cur_size = sizeof(TDS_DATETIME);

  //tds_alloc_param_data(m_pParameters, curcol);
  tds_alloc_param_data(curcol);
  memcpy(curcol->column_data, &bValue , sizeof(bool));
}
Esempio n. 28
0
PreparedStatement* TdsDatabaseLayer::PrepareStatement(const wxString& strQuery)
{
  ResetErrorCodes();

  if (m_pDatabase != NULL)
  {
    wxString sql = RemoveLastSemiColon(strQuery);
    /*
    wxCharBuffer sqlBuffer = ConvertToUnicodeStream(sql);
    TDSDYNAMIC* pStatement = NULL;
    TDSPARAMINFO* pParameters = NULL;
    int nReturn = tds_submit_prepare(m_pDatabase, sqlBuffer, NULL, &pStatement, pParameters);
    if (nReturn != TDS_SUCCEED)
    {
      //fprintf(stderr, "tds_submit_prepare() failed for query '%s'\n", strQuery.c_str());

      if (pStatement != NULL)
        tds_free_dynamic(m_pDatabase, pStatement);
      if (pParameters != NULL)
        tds_free_param_results(pParameters);

      FreeAllocatedResultSets();
      ThrowDatabaseException();
      return NULL;
    }
    FreeAllocatedResultSets();

    TdsPreparedStatement* pReturnStatement = new TdsPreparedStatement(m_pDatabase, pStatement, strQuery);
    if (pReturnStatement)
      pReturnStatement->SetEncoding(GetEncoding());
    */
    TdsPreparedStatement* pReturnStatement = new TdsPreparedStatement(m_pDatabase, sql);
    if (pReturnStatement)
      pReturnStatement->SetEncoding(GetEncoding());

    if (pReturnStatement != NULL)
      LogStatementForCleanup(pReturnStatement);
    return pReturnStatement;
  }
  else
  {
    return NULL;
  }
}
Esempio n. 29
0
void TdsPreparedStatement::SetParamBlob(int nPosition, const void* pData, long nDataLength)
{
  //fprintf(stderr, "Setting param %d to blob\n", nPosition);
  ResetErrorCodes();

  AllocateParameter(nPosition);
/*
  TDSCOLUMN* curcol = m_pParameters->columns[nPosition-1];
  curcol->column_type = SYBIMAGE;
  curcol->on_server.column_type = SYBIMAGE;
  curcol->column_size = 40;
  //curcol->on_server.column_size = 40;
  curcol->column_varint_size = 1;
  curcol->column_cur_size = 40;
  fprintf(stderr, "Setting blob column size to %d\n", nDataLength);

  tds_alloc_param_data(m_pParameters, curcol);

  BCPCOLDATA* pBcpColData = tds_alloc_bcp_column_data(nDataLength);
  curcol->bcp_column_data = pBcpColData;
  curcol->bcp_column_data->datalen = nDataLength;
  memcpy(curcol->bcp_column_data->data, pData, nDataLength);
*/
  CONV_RESULT cr;
  //fprintf(stderr, "data length = %ld\n", nDataLength);
  int ret = tds_convert(this->m_pDatabase->tds_ctx, SYBBINARY, (TDS_CHAR*)pData, nDataLength, SYBVARBINARY, &cr);
  //fprintf(stderr, "tds_convert returned %d, data length = %ld\n", ret, nDataLength);
  TDSCOLUMN* curcol = m_pParameters->columns[nPosition-1];
  curcol->column_type = SYBVARBINARY;
  curcol->on_server.column_type = SYBVARBINARY;
  curcol->column_size = ret;
  curcol->on_server.column_size = ret;
  curcol->column_varint_size = 1;
  curcol->column_cur_size = ret;

  //tds_alloc_param_data(m_pParameters, curcol);
  tds_alloc_param_data(curcol);

  //fprintf(stderr, "Ready for memcpy of %d bytes\n", ret);
  memcpy(curcol->column_data, cr.ib, ret);
  //fprintf(stderr, "Memcpy completed\n");
}
Esempio n. 30
0
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;
}