bool OdbcCommand::Execute(const tstring & szSQL) { if (!Open()) return false; #ifdef _DEBUG OutputDebugString((szSQL + _T("\n")).c_str()); #endif if (!BindParameters()) return false; if (!SQL_SUCCEEDED(SQLExecDirect(m_hStmt, (SQLTCHAR *)szSQL.c_str(), szSQL.length()))) { if (m_odbcConnection != NULL) m_szError = m_odbcConnection->ReportSQLError(SQL_HANDLE_STMT, m_hStmt, (TCHAR *)szSQL.c_str(), _T("Failed to execute statement.")); else m_szError = OdbcConnection::GetSQLError(SQL_HANDLE_STMT, m_hStmt); Close(); return false; } if (!MoveNext()) MoveNextSet(); return true; }
bool OdbcCommand::Execute(const tstring & szSQL) { if (!Open()) return false; #ifdef USE_SQL_TRACE TRACE((szSQL + _T("\n")).c_str()); #endif if (!BindParameters()) return false; SQLRETURN result = SQLExecDirect(m_hStmt, (SQLTCHAR *)szSQL.c_str(), szSQL.length()); if (!(result == SQL_SUCCESS || result == SQL_SUCCESS_WITH_INFO || result == SQL_NO_DATA)) { if (m_odbcConnection != nullptr) m_szError = m_odbcConnection->ReportSQLError(SQL_HANDLE_STMT, m_hStmt, (TCHAR *)szSQL.c_str(), _T("Failed to execute statement.")); else m_szError = OdbcConnection::GetSQLError(SQL_HANDLE_STMT, m_hStmt); Close(); return false; } if (!MoveNext()) MoveNextSet(); return true; }
BOOL SFStatement::CreateStatement(SQLHANDLE hStmt) { if(m_hStmt != SQL_NULL_HANDLE) return FALSE; m_hStmt = hStmt; m_usBindParam = 0; m_usBindCol = 0; SetQuery(); m_sqlReturn = ::SQLPrepare(m_hStmt, m_szQuery, SQL_NTS); if(!SH_SQL_SUCCESS(m_sqlReturn)) return FALSE; return (BindParameters() && BindColumns()); }
bool OdbcCommand::Prepare(const tstring & szSQL) { if (!Open()) return false; #ifdef USE_SQL_TRACE TRACE((szSQL + _T("\n")).c_str()); #endif if (!SQL_SUCCEEDED(SQLPrepare(m_hStmt, (SQLTCHAR *)szSQL.c_str(), szSQL.length()))) { if (m_odbcConnection != nullptr) m_szError = m_odbcConnection->ReportSQLError(SQL_HANDLE_STMT, m_hStmt, _T("SQLPrepare"), _T("Failed to prepare statement.")); else m_szError = OdbcConnection::GetSQLError(SQL_HANDLE_STMT, m_hStmt); Close(); return false; } if (!BindParameters()) return false; SQLRETURN result = SQLExecute(m_hStmt); if (!(result == SQL_SUCCESS || result == SQL_SUCCESS_WITH_INFO || result == SQL_NO_DATA)) { if (m_odbcConnection != nullptr) m_szError = m_odbcConnection->ReportSQLError(SQL_HANDLE_STMT, m_hStmt, (TCHAR *)szSQL.c_str(), _T("Failed to execute prepared statement.")); else m_szError = OdbcConnection::GetSQLError(SQL_HANDLE_STMT, m_hStmt); Close(); return false; } // If there's no rows to move through, skip to the next result set. if (!MoveNext()) MoveNextSet(); ClearParameters(); return true; }
bool OdbcCommand::Prepare(const tstring & szSQL) { if (!Open()) return false; #ifdef _DEBUG OutputDebugString((szSQL + _T("\n")).c_str()); #endif if (!SQL_SUCCEEDED(SQLPrepare(m_hStmt, (SQLTCHAR *)szSQL.c_str(), szSQL.length()))) { if (m_odbcConnection != NULL) m_szError = m_odbcConnection->ReportSQLError(SQL_HANDLE_STMT, m_hStmt, _T("SQLPrepare"), _T("Failed to prepare statement.")); else m_szError = OdbcConnection::GetSQLError(SQL_HANDLE_STMT, m_hStmt); Close(); return false; } if (!BindParameters()) return false; if (!SQL_SUCCEEDED(SQLExecute(m_hStmt))) { if (m_odbcConnection != NULL) m_szError = m_odbcConnection->ReportSQLError(SQL_HANDLE_STMT, m_hStmt, (TCHAR *)szSQL.c_str(), _T("Failed to execute prepared statement.")); else m_szError = OdbcConnection::GetSQLError(SQL_HANDLE_STMT, m_hStmt); Close(); return false; } // If there's no rows to move through, skip to the next result set. if (!MoveNext()) MoveNextSet(); ClearParameters(); return true; }
/** * Execute already prepared query with data provided in data.frame-like data * structure. * * If query fetches data it may be read using sqlFetchMore(). * * @param chan R ODBC handle * @param data data.frame-like structure with query data (columns refer * to query parameters, rows to query executions) * @param row number of row in data to copy values from * @param vtest debug level: * 0-no debug, * 1-verbose, * 2-verbose with no query execution * @retval 1 on success, -1 on error */ SEXP RODBCExecute(SEXP chan, SEXP data, SEXP nrows) { pRODBCHandle thisHandle = R_ExternalPtrAddr(chan); int rows, row, stat = 1; SQLRETURN res = 0; /* Clear error list */ errorFree(thisHandle->msglist); thisHandle->msglist = NULL; /* Bind Query parameters */ res = BindParameters(thisHandle, data); if(res != 1){ return ScalarInteger(-1); } if(0 == LENGTH(data)){ res = SQLExecute(thisHandle->hStmt); SQL_RESULT_CHECK(res, thisHandle, _("[RODBCext] Error: SQLExecute failed"), ScalarInteger(-1)); } else{ rows = LENGTH(VECTOR_ELT(data, 0)); for(row = 0; row < rows; row++) { /* Discard any pending data from previous query executions */ SQLCloseCursor(thisHandle->hStmt); CopyParameters(thisHandle->ColData, data, row); res = SQLExecute(thisHandle->hStmt); SQL_RESULT_CHECK(res, thisHandle, _("[RODBCext] Error: SQLExecute failed"), ScalarInteger(-1)); } } /* Prepare result for fetching */ stat = cachenbind(thisHandle, asInteger(nrows)); return ScalarInteger(stat); }