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; }
wxDatabaseResultSet* wxMysqlPreparedStatementWrapper::RunQueryWithResults() { wxMysqlPreparedStatementResultSet* pResultSet = NULL; MYSQL_BIND* pBoundParameters = m_Parameters.GetMysqlParameterBindings(); if (m_pInterface->GetMysqlStmtBindParam()(m_pStatement, pBoundParameters)) { SetErrorCode(wxMysqlDatabase::TranslateErrorCode(m_pInterface->GetMysqlStmtErrno()(m_pStatement))); SetErrorMessage(ConvertFromUnicodeStream(m_pInterface->GetMysqlStmtError()(m_pStatement))); wxDELETEA(pBoundParameters); ThrowDatabaseException(); return NULL; } else { if (m_pInterface->GetMysqlStmtExecute()(m_pStatement) != 0) { SetErrorCode(wxMysqlDatabase::TranslateErrorCode(m_pInterface->GetMysqlStmtErrno()(m_pStatement))); SetErrorMessage(ConvertFromUnicodeStream(m_pInterface->GetMysqlStmtError()(m_pStatement))); wxDELETEA(pBoundParameters); ThrowDatabaseException(); return NULL; } else { pResultSet = new wxMysqlPreparedStatementResultSet(m_pInterface, m_pStatement); if (pResultSet) pResultSet->SetEncoding(GetEncoding()); } } wxDELETEA(pBoundParameters);; return pResultSet; }
int wxMysqlPreparedStatementWrapper::RunQuery() { MYSQL_BIND* pBoundParameters = m_Parameters.GetMysqlParameterBindings(); int nBindReturn = m_pInterface->GetMysqlStmtBindParam()(m_pStatement, pBoundParameters); if (nBindReturn != 0) { SetErrorCode(wxMysqlDatabase::TranslateErrorCode(m_pInterface->GetMysqlStmtErrno()(m_pStatement))); SetErrorMessage(ConvertFromUnicodeStream(m_pInterface->GetMysqlStmtError()(m_pStatement))); wxDELETEA(pBoundParameters); ThrowDatabaseException(); return wxDATABASE_QUERY_RESULT_ERROR; } else { int nReturn = m_pInterface->GetMysqlStmtExecute()(m_pStatement); if (nReturn != 0) { SetErrorCode(wxMysqlDatabase::TranslateErrorCode(m_pInterface->GetMysqlStmtErrno()(m_pStatement))); SetErrorMessage(ConvertFromUnicodeStream(m_pInterface->GetMysqlStmtError()(m_pStatement))); wxDELETEA(pBoundParameters); ThrowDatabaseException(); return wxDATABASE_QUERY_RESULT_ERROR; } } wxDELETEA(pBoundParameters); return m_pInterface->GetMysqlStmtAffectedRows()(m_pStatement); }
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; }
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; } }
wxArrayString wxOdbcDatabase::GetPKColumns(const wxString& table) { wxArrayString returnArray; // Use SQLColumns SQLHSTMT pStatement = allocStmth(); wxCharBuffer tableBuffer = ConvertToUnicodeStream(table); int tableBufferLength = GetEncodedStreamLength(table); SQLRETURN nRet = m_pInterface->GetSQLPKColumns()(pStatement, NULL, 0, NULL, 0, (SQLTCHAR*)(const char*)tableBuffer, tableBufferLength, NULL, 0); if (nRet != SQL_SUCCESS) { InterpretErrorCodes( nRet ); m_pInterface->GetSQLFreeStmt()(pStatement, SQL_CLOSE); ThrowDatabaseException(); return returnArray; } nRet = m_pInterface->GetSQLFetch()(pStatement); while (nRet == SQL_SUCCESS || nRet == SQL_SUCCESS_WITH_INFO) { SQLPOINTER buff[8192]; memset(buff, 0, 8192*sizeof(SQLTCHAR)); SQLINTEGER col_size = 8192; SQLLEN real_size = 0; int nField = 4; SQLRETURN nGetDataReturn = m_pInterface->GetSQLGetData()( pStatement, nField, SQL_C_CHAR, buff, col_size, &real_size ); if ( nGetDataReturn != SQL_SUCCESS && nGetDataReturn != SQL_SUCCESS_WITH_INFO ) { InterpretErrorCodes(nRet); m_pInterface->GetSQLFreeStmt()(pStatement, SQL_CLOSE); ThrowDatabaseException(); return returnArray; } wxString strColumn = ConvertFromUnicodeStream((const char*)buff); returnArray.Add(strColumn); nRet = m_pInterface->GetSQLFetch()(pStatement); } m_pInterface->GetSQLFreeStmt()(pStatement, SQL_CLOSE); return returnArray; }
// 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 }
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; }
bool wxOdbcResultSet::Next() { m_RetrievedValues.clear(); m_NullValues.clear(); m_BlobMap.clear(); if (m_pOdbcStatement == NULL) m_pOdbcStatement = m_pStatement->GetLastStatement(); long nReturn = m_pInterface->GetSQLFetch()( m_pOdbcStatement ); if ( nReturn != SQL_SUCCESS && nReturn != SQL_SUCCESS_WITH_INFO ) { if ( nReturn == SQL_NO_DATA ) return false; InterpretErrorCodes(nReturn, m_pOdbcStatement); ThrowDatabaseException(); return false; } m_fieldValues.Clear(); for ( int i=0; i<(int)m_FieldLookupMap.size(); i++ ) m_fieldValues.push_back( wxVariant() ); return true; }
FirebirdDatabaseLayer::FirebirdDatabaseLayer(const wxString& strServer, const wxString& strDatabase, const wxString& strUser, const wxString& strPassword) : DatabaseLayer() { m_pDatabase = NULL; m_pTransaction = NULL; m_pStatus = new ISC_STATUS_ARRAY(); #ifndef DONT_USE_DYNAMIC_DATABASE_LAYER_LINKING m_pInterface = new FirebirdInterface(); if (!m_pInterface->Init()) { SetErrorCode(DATABASE_LAYER_ERROR_LOADING_LIBRARY); SetErrorMessage(wxT("Error loading Firebird library")); ThrowDatabaseException(); return; } #endif m_strServer = strServer; m_strUser = strUser; m_strPassword = strPassword; m_strRole = wxEmptyString; Open(strDatabase); }
// close database bool FirebirdDatabaseLayer::Close() { CloseResultSets(); CloseStatements(); if (m_pDatabase) { if (m_pTransaction) { isc_tr_handle pTransaction = (isc_tr_handle)m_pTransaction; m_pInterface->GetIscRollbackTransaction()(*(ISC_STATUS_ARRAY*)m_pStatus, &pTransaction); m_pTransaction = NULL; } isc_db_handle pDatabase = (isc_db_handle)m_pDatabase; int nReturn = m_pInterface->GetIscDetachDatabase()(*(ISC_STATUS_ARRAY*)m_pStatus, &pDatabase); m_pDatabase = NULL; if (nReturn != 0) { InterpretErrorCodes(); ThrowDatabaseException(); return false; } } return true; }
// 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; }
bool SqliteResultSet::Next() { if (m_pSqliteStatement == NULL) m_pSqliteStatement = m_pStatement->GetLastStatement(); int nReturn = sqlite3_step(m_pSqliteStatement); if (nReturn != SQLITE_ROW) sqlite3_reset(m_pSqliteStatement); if ((nReturn != SQLITE_ROW) && (nReturn != SQLITE_DONE)) { wxLogError(_("Error with RunQueryWithResults\n")); SetErrorCode(SqliteDatabaseLayer::TranslateErrorCode(nReturn)); #if SQLITE_VERSION_NUMBER>=3002002 // sqlite3_db_handle wasn't added to the SQLite3 API until version 3.2.2 SetErrorMessage(ConvertFromUnicodeStream(sqlite3_errmsg(sqlite3_db_handle(m_pSqliteStatement)))); #else SetErrorMessage(_("Unknown error advancing result set")); #endif ThrowDatabaseException(); return false; } return (nReturn == SQLITE_ROW); }
wxMysqlPreparedStatementParameter::wxMysqlPreparedStatementParameter(MYSQL_BIND* pBind, MYSQL_FIELD* pField) : wxDatabaseErrorReporter() { m_pBind = pBind; m_pBind->is_null = &m_bIsNull; // Set the binding properties m_pBind->buffer_type = pField->type; m_pBind->buffer_length = pField->length + 1; //int nType = m_pBind->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) //{ //wxLogDebug(_("Allocating %ld bytes in the MYSQL_BIND buffer\n"), pField->length); void* pBuffer = m_Data.bufferValue.GetWriteBuf(m_pBind->buffer_length); if (pBuffer == 0) { SetErrorCode(wxMysqlDatabase::TranslateErrorCode(0)); SetErrorMessage(_("Error allocating buffer")); ThrowDatabaseException(); } m_pBind->buffer = pBuffer; ClearBuffer(); // } // else // { // pCurrentBinding->buffer = malloc(1); // } int nType = m_pBind->buffer_type; if (nType == MYSQL_TYPE_BLOB || nType == MYSQL_TYPE_TINY_BLOB || nType == MYSQL_TYPE_MEDIUM_BLOB || nType == MYSQL_TYPE_LONG_BLOB) m_pBind->length = &m_Data.nBufferLength; }
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; }
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); }
// 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; } }
// ctor OTLResultSet::OTLResultSet(otl_stream *otlstream, bool bManageStatement /*= false*/) { try { m_pResultSet = otlstream; m_bManageStatement = bManageStatement; m_readIt.attach(*m_pResultSet); otl_column_desc* otldesc = m_pResultSet->describe_select(m_colCount); int nCounter = 1; for (int i = 0; i < m_colCount; i++) { otl_column_desc *newItem = new otl_column_desc(); *newItem = otldesc[i]; m_otldesc.push_back(newItem); wxString strField = ConvertFromUnicodeStream(otldesc[i].name); m_FieldLookupMap[strField.Upper()] = nCounter; nCounter++; } } catch (otl_exception& e) { SetErrorCode(OTLDatabaseLayer::TranslateErrorCode(e.code)); SetErrorMessage(ConvertFromUnicodeStream((char*)e.msg)); ThrowDatabaseException(); } }
wxOdbcResultSet::wxOdbcResultSet(wxOdbcInterface* pInterface, wxOdbcPreparedStatement* pStatement, bool bManageStatement, int nCol) : wxDatabaseResultSet() { m_pInterface = pInterface; m_pStatement = pStatement; m_pOdbcStatement = m_pStatement->GetLastStatement(); m_bManageStatement = bManageStatement; // Populate field lookup map SQLTCHAR field_name[ODBC_FIELD_NAME_LEN]; SQLSMALLINT colnamelen; for (int i=0; i<nCol; i++) { UWORD col = i+1; long nReturn = m_pInterface->GetSQLColAttributes()(m_pOdbcStatement, col, SQL_COLUMN_NAME, field_name, ODBC_FIELD_NAME_LEN, &colnamelen, 0); if ( nReturn != SQL_SUCCESS && nReturn != SQL_SUCCESS_WITH_INFO ) { InterpretErrorCodes(nReturn, m_pOdbcStatement); ThrowDatabaseException(); return; } #if wxUSE_UNICODE wxString strField = ConvertFromUnicodeStream((const char*)(wxChar*)field_name); #else wxString strField((wxChar*)field_name); #endif m_FieldLookupMap[strField.Upper()] = i; } }
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; }
bool wxOdbcDatabase::ViewExists(const wxString& view) { bool bReturn = false; // Use SQLTables SQLHSTMT pStatement = allocStmth(); wxCharBuffer viewBuffer = ConvertToUnicodeStream(view); wxString tableType = _("VIEW"); wxCharBuffer tableTypeBuffer = ConvertToUnicodeStream(tableType); int tableTypeBufferLength = GetEncodedStreamLength(tableType); SQLRETURN nRet = m_pInterface->GetSQLTables()(pStatement, NULL, 0, NULL, 0, (SQLTCHAR*)(const char*)viewBuffer, SQL_NTS, (SQLTCHAR*)(const char*)tableTypeBuffer, tableTypeBufferLength); if (nRet != SQL_SUCCESS) { InterpretErrorCodes( nRet ); m_pInterface->GetSQLFreeStmt()(pStatement, SQL_CLOSE); ThrowDatabaseException(); return false; } nRet = m_pInterface->GetSQLFetch()(pStatement); if (nRet == SQL_SUCCESS || nRet == SQL_SUCCESS_WITH_INFO) bReturn = true; m_pInterface->GetSQLFreeStmt()(pStatement, SQL_CLOSE); return bReturn; }
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; }
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; }
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(); } }
// 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; }
wxOdbcDatabase::~wxOdbcDatabase() { Close(); SQLRETURN nRet = m_pInterface->GetSQLFreeHandle()(SQL_HANDLE_DBC, (SQLHDBC)m_sqlHDBC); if ( nRet != SQL_SUCCESS ) { InterpretErrorCodes( nRet ); ThrowDatabaseException(); } nRet = m_pInterface->GetSQLFreeHandle()(SQL_HANDLE_ENV, (SQLHENV)m_sqlEnvHandle); if ( nRet != SQL_SUCCESS ) { InterpretErrorCodes( nRet ); ThrowDatabaseException(); } wxDELETE(m_pInterface); }
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(); } }
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; } }
// open database bool SqliteDatabaseLayer::Open(const wxString& strDatabase, bool mustExist) { if (strDatabase!= _(":memory:") && // :memory: is a special SQLite in-memory database mustExist && !(wxFileName::FileExists(strDatabase))) { SetErrorCode(DATABASE_LAYER_ERROR); SetErrorMessage(_("The specified database file '") + strDatabase + _("' does not exist.")); ThrowDatabaseException(); return false; } return Open(strDatabase); }
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; }