📄 cpl_odbc.cpp
字号:
if( cbDataLen == SQL_NULL_DATA ) { m_papszColValues[iCol] = NULL; m_panColValueLengths[iCol] = 0; } // assume big result: should check for state=SQLSATE 01004. else if( nRetCode == SQL_SUCCESS_WITH_INFO ) { if( cbDataLen > (_SQLLEN)(sizeof(szWrkData)-1) ) { cbDataLen = (_SQLLEN)(sizeof(szWrkData)-1); if (nFetchType == SQL_C_CHAR) while ((cbDataLen > 1) && (szWrkData[cbDataLen - 1] == 0)) --cbDataLen; // trimming the extra terminators: bug 990 } m_papszColValues[iCol] = (char *) CPLMalloc(cbDataLen+1); memcpy( m_papszColValues[iCol], szWrkData, cbDataLen ); m_papszColValues[iCol][cbDataLen] = '\0'; m_panColValueLengths[iCol] = cbDataLen; while( TRUE ) { _SQLLEN nChunkLen; nRetCode = SQLGetData( m_hStmt, (SQLUSMALLINT) iCol+1, nFetchType, szWrkData, sizeof(szWrkData)-1, &cbDataLen ); if( nRetCode == SQL_NO_DATA ) break; if( Failed( nRetCode ) ) { CPLError( CE_Failure, CPLE_AppDefined, m_poSession->GetLastError() ); return FALSE; } if( cbDataLen > (int) (sizeof(szWrkData) - 1) || cbDataLen == SQL_NO_TOTAL ) { nChunkLen = sizeof(szWrkData)-1; if (nFetchType == SQL_C_CHAR) while ( (nChunkLen > 1) && (szWrkData[nChunkLen - 1] == 0) ) --nChunkLen; // trimming the extra terminators } else nChunkLen = cbDataLen; szWrkData[nChunkLen] = '\0'; m_papszColValues[iCol] = (char *) CPLRealloc( m_papszColValues[iCol], m_panColValueLengths[iCol] + nChunkLen + 1 ); memcpy( m_papszColValues[iCol] + m_panColValueLengths[iCol], szWrkData, nChunkLen ); m_panColValueLengths[iCol] += nChunkLen; m_papszColValues[iCol][m_panColValueLengths[iCol]] = '\0'; } } else { m_panColValueLengths[iCol] = cbDataLen; m_papszColValues[iCol] = (char *) CPLMalloc(cbDataLen+1); memcpy( m_papszColValues[iCol], szWrkData, cbDataLen ); m_papszColValues[iCol][cbDataLen] = '\0'; } // Trim white space off end, if there is any. if( nFetchType == SQL_C_CHAR && m_papszColValues[iCol] != NULL ) { char *pszTarget = m_papszColValues[iCol]; size_t iEnd = strlen(pszTarget); while ( iEnd > 0 && pszTarget[iEnd - 1] == ' ' ) pszTarget[--iEnd] = '\0'; } } return TRUE;}/************************************************************************//* GetColData() *//************************************************************************//** * Fetch column data. * * Fetches the data contents of the requested column for the currently loaded * row. The result is returned as a string regardless of the column type. * NULL is returned if an illegal column is given, or if the actual column * is "NULL". * * @param iCol the zero based column to fetch. * * @param pszDefault the value to return if the column does not exist, or is * NULL. Defaults to NULL. * * @return pointer to internal column data or NULL on failure. */const char *CPLODBCStatement::GetColData( int iCol, const char *pszDefault ){ if( iCol < 0 || iCol >= m_nColCount ) return pszDefault; else if( m_papszColValues[iCol] != NULL ) return m_papszColValues[iCol]; else return pszDefault;}/************************************************************************//* GetColData() *//************************************************************************//** * Fetch column data. * * Fetches the data contents of the requested column for the currently loaded * row. The result is returned as a string regardless of the column type. * NULL is returned if an illegal column is given, or if the actual column * is "NULL". * * @param pszColName the name of the column requested. * * @param pszDefault the value to return if the column does not exist, or is * NULL. Defaults to NULL. * * @return pointer to internal column data or NULL on failure. */const char *CPLODBCStatement::GetColData( const char *pszColName, const char *pszDefault ){ int iCol = GetColId( pszColName ); if( iCol == -1 ) return pszDefault; else return GetColData( iCol, pszDefault );}/************************************************************************//* GetColDataLength() *//************************************************************************/int CPLODBCStatement::GetColDataLength( int iCol ){ if( iCol < 0 || iCol >= m_nColCount ) return 0; else if( m_papszColValues[iCol] != NULL ) return (int)m_panColValueLengths[iCol]; else return 0;}/************************************************************************//* GetColId() *//************************************************************************//** * Fetch column index. * * Gets the column index corresponding with the passed name. The * name comparisons are case insensitive. * * @param pszColName the name to search for. * * @return the column index, or -1 if not found. */int CPLODBCStatement::GetColId( const char *pszColName ){ for( SQLSMALLINT iCol = 0; iCol < m_nColCount; iCol++ ) if( EQUAL(pszColName, m_papszColNames[iCol]) ) return iCol; return -1;}/************************************************************************//* ClearColumnData() *//************************************************************************/void CPLODBCStatement::ClearColumnData(){ if( m_nColCount > 0 ) { for( int iCol = 0; iCol < m_nColCount; iCol++ ) { if( m_papszColValues[iCol] != NULL ) { CPLFree( m_papszColValues[iCol] ); m_papszColValues[iCol] = NULL; } } }}/************************************************************************//* Failed() *//************************************************************************/int CPLODBCStatement::Failed( int nResultCode ){ if( m_poSession != NULL ) return m_poSession->Failed( nResultCode, m_hStmt ); return TRUE;}/************************************************************************//* Append(const char *) *//************************************************************************//** * Append text to internal command. * * The passed text is appended to the internal SQL command text. * * @param pszText text to append. */void CPLODBCStatement::Append( const char *pszText ){ size_t nTextLen = strlen(pszText); if( m_nStatementMax < m_nStatementLen + nTextLen + 1 ) { m_nStatementMax = (m_nStatementLen + nTextLen) * 2 + 100; if( m_pszStatement == NULL ) { m_pszStatement = (char *) VSIMalloc(m_nStatementMax); m_pszStatement[0] = '\0'; } else { m_pszStatement = (char *) VSIRealloc(m_pszStatement, m_nStatementMax); } } strcpy( m_pszStatement + m_nStatementLen, pszText ); m_nStatementLen += nTextLen;}/************************************************************************//* AppendEscaped(const char *) *//************************************************************************//** * Append text to internal command. * * The passed text is appended to the internal SQL command text after * escaping any special characters so it can be used as a character string * in an SQL statement. * * @param pszText text to append. */void CPLODBCStatement::AppendEscaped( const char *pszText ){ size_t iIn, iOut ,nTextLen = strlen(pszText); char *pszEscapedText = (char *) VSIMalloc(nTextLen*2 + 1); for( iIn = 0, iOut = 0; iIn < nTextLen; iIn++ ) { switch( pszText[iIn] ) { case '\'': case '\\': pszEscapedText[iOut++] = '\\'; pszEscapedText[iOut++] = pszText[iIn]; break; default: pszEscapedText[iOut++] = pszText[iIn]; break; } } pszEscapedText[iOut] = '\0'; Append( pszEscapedText ); CPLFree( pszEscapedText );}/************************************************************************//* Append(int) *//************************************************************************//** * Append to internal command. * * The passed value is formatted and appended to the internal SQL command text. * * @param nValue value to append to the command. */void CPLODBCStatement::Append( int nValue ){ char szFormattedValue[100]; sprintf( szFormattedValue, "%d", nValue ); Append( szFormattedValue );}/************************************************************************//* Append(double) *//************************************************************************//** * Append to internal command. * * The passed value is formatted and appended to the internal SQL command text. * * @param dfValue value to append to the command. */void CPLODBCStatement::Append( double dfValue ){ char szFormattedValue[100]; sprintf( szFormattedValue, "%24g", dfValue ); Append( szFormattedValue );}/************************************************************************//* Appendf() *//************************************************************************//** * Append to internal command. * * The passed format is used to format other arguments and the result is * appended to the internal command text. Long results may not be formatted * properly, and should be appended with the direct Append() methods. * * @param pszFormat printf() style format string. * * @return FALSE if formatting fails dueto result being too large. */int CPLODBCStatement::Appendf( const char *pszFormat, ... ){ va_list args; char szFormattedText[8000]; int bSuccess; va_start( args, pszFormat );#if defined(HAVE_VSNPRINTF) bSuccess = vsnprintf( szFormattedText, sizeof(szFormattedText)-1, pszFormat, args ) < (int) sizeof(szFormattedText)-1;#else vsprintf( szFormattedText, pszFormat, args ); bSuccess = TRUE;#endif va_end( args ); if( bSuccess ) Append( szFormattedText ); return bSuccess;} /************************************************************************//* Clear() *//************************************************************************//** * Clear internal command text and result set definitions. */void CPLODBCStatement::Clear(){ ClearColumnData(); if( m_pszStatement != NULL ) { CPLFree( m_pszStatement ); m_pszStatement = NULL; } m_nStatementLen = 0; m_nStatementMax = 0; if( m_papszColNames ) { CPLFree( m_panColType ); m_panColType = NULL; CSLDestroy( m_papszColTypeNames ); m_papszColTypeNames = NULL; CPLFree( m_panColSize ); m_panColSize = NULL; CPLFree( m_panColPrecision ); m_panColPrecision = NULL; CPLFree( m_panColNullable ); m_panColNullable = NULL; CSLDestroy( m_papszColNames ); m_papszColNames = NULL; CPLFree( m_papszColValues ); m_papszColValues = NULL; CPLFree( m_panColValueLengths );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -