Share via


Returning Column Ordinals

For purposes of identification, columns in a rowset are identified by a column ID, which is a value of type DBID in the DBCOLUMNINFO structure. Although this is sufficient to uniquely identify the column, some providers might need to refer to the column by its position, or ordinal value.

The MapColumnIDs method returns column ordinals for all column IDs provided in the rgColumnIDs array. Column ordinals do not change during the life of the rowset, but might change between different instances of the rowset. The source code for IColumnsInfo::MapColumnIDs follows.

// CImpIColumnsInfo::MapColumnIDs --------------------------------------------
//
// @mfunc Returns an array of ordinals of the columns in a rowset that are
// identified by the specified column IDs.
//
// @rdesc HRESULT
//      @flag S_OK                      | The method succeeded
//      @flag E_INVALIDARG              | cColumnIDs was not 0 and rgColumnIDs was NULL,
//                                        rgColumns was NULL
//      @flag DB_E_COLUMNUNAVAILABLE    | An element of rgColumnIDs was invalid
//
STDMETHODIMP CImpIColumnsInfo::MapColumnIDs
(
    DBORDINAL   cColumnIDs,     //@parm IN | Number of Column IDs to map
    const DBID   rgColumnIDs[],  //@parm IN | Column IDs to map
    DBORDINAL   rgColumns[]     //@parm OUT | Ordinal values
)
{
   DBORDINAL cCols = 0;
   DBORDINAL  ulError = 0;

   //
   // Asserts
    //
   assert(m_pObj);

    //
   // NO-OP if cColumnIds is 0
   //
   if( cColumnIDs == 0 )
        return S_OK;

    //
    // Check in-params and NULL out-params in case of error
    //
    if( !rgColumnIDs || !rgColumns )
        return E_INVALIDARG;

   //
   // Get the Column count
   //
   if( m_pObj->GetBaseObjectType() == BOT_COMMAND )
   {
      HRESULT hr = E_FAIL;
      CFileIO   * pFileio = NULL;
      
      //
      // Asserts
      //
      assert(((CCommand *) m_pObj)->m_pCSession);
      assert(((CCommand *) m_pObj)->m_pCSession->m_pCDataSource);

      //
      // Check that a command has been set
      //
      if( !((CCommand *) m_pObj)->IsCommandSet() )
         return DB_E_NOCOMMAND;

      //
      // Open the File and get the column count
      //
      hr = ((CCommand *) m_pObj)->m_pCSession->m_pCDataSource->OpenFile(
                     ((CCommand *) m_pObj)->GetCommandText(), &pFileio);
      if( FAILED(hr) )
         return hr;
      
      cCols = pFileio->GetColumnCnt();
      SAFE_DELETE(pFileio);
   }
   else
   {
      if( m_pObj->GetBaseObjectType() == BOT_ROWSET )
         cCols = ((CRowset *) m_pObj)->m_cCols;
      else
         cCols = ((CRow *) m_pObj)->GetFileObj()->GetColumnCnt();
   }

    //
   // Walk the Column ID structs and determine the ordinal value
   //
    for (DBORDINAL i=0; i < cColumnIDs; i++)
    {   
      if( m_pObj->GetBaseObjectType() == BOT_ROW &&
         IsEqualDBID(&rgColumnIDs[i], &DBROWCOL_DEFAULTSTREAM) )
      {
         rgColumns[i] = cCols + DEFAULT_STREAM_ORDINAL;
      }
        else if( (rgColumnIDs[i].eKind != DBKIND_GUID_PROPID) ||
             (rgColumnIDs[i].uGuid.guid != GUID_NULL)     ||
             (rgColumnIDs[i].uName.ulPropid < 1)          ||
             (rgColumnIDs[i].uName.ulPropid > cCols) )
        {
            rgColumns[i] = DB_INVALIDCOLUMN;
            ulError++;
        }
      else
            rgColumns[i] = rgColumnIDs[i].uName.ulPropid;
    }

   //
   // Return the correct HResult
   //
   return ulError ? (ulError < cColumnIDs) ? 
         DB_S_ERRORSOCCURRED : DB_E_ERRORSOCCURRED : S_OK;
}

See Also

Tasks

Writing an OLE DB Provider: An Introduction