C Examples: DbiTruncateBlob

<< Click to Display Table of Contents >>

Navigation:  »No topics above this level«

C Examples: DbiTruncateBlob

Return to chapter overview

Copy a table from the specified cursor and empty all the blob fields.

Packing the newly created table will free up space. This example uses the following input:
         fDbiTruncateBlob(hPXBlobCur, "NEWBIO", &hTmpCur)

DBIResult fDbiTruncateBlob(hDBICur hTmpCur, pCHAR NewTblName, phDBICur phTmpCur)

{

   DBIResult    rslt;

   hDBIDb       hTmpDb;

   CURProps     CurProps;

   pFLDDesc     pFldDesc;

   UINT16       FldCount;

   pBYTE        pRecBuf;

 

   rslt = Chk(DbiGetObjFromObj(hTmpCur, objDATABASE, &hTmpDb));

   if (rslt != DBIERR_NONE)

      return rslt;

   rslt = Chk(DbiGetCursorProps(hTmpCur, &CurProps));

   if (rslt != DBIERR_NONE)

      return rslt;

   rslt = Chk(DbiCopyTable(hTmpDb, TRUE, CurProps.szName, CurProps.szTableType, NewTblName));

   if (rslt != DBIERR_NONE)

      return rslt;

   rslt = Chk(DbiOpenTable(hTmpDb, NewTblName, CurProps.szTableType, NULL, NULL, 0,

                  dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL, phTmpCur));

   if (rslt != DBIERR_NONE)

      return rslt;

   pFldDesc = (pFLDDesc)malloc(CurProps.iFields * sizeof(FLDDesc));

   pRecBuf = (pBYTE)malloc(CurProps.iRecBufSize * sizeof(BYTE));

   rslt = Chk(DbiGetFieldDescs(*phTmpCur, pFldDesc));

   if (rslt != DBIERR_NONE)

   {

      free(pFldDesc); free(pRecBuf);

      return rslt;

   }

   while (DbiGetNextRecord(*phTmpCur, dbiWRITELOCK, pRecBuf, NULL) == DBIERR_NONE)

   {

      for (FldCount = 0; FldCount < CurProps.iFields; FldCount++)

      {

         if (pFldDesc[FldCount].iFldType == fldBLOB)

         {

            rslt = Chk(DbiOpenBlob(*phTmpCur, pRecBuf, pFldDesc[FldCount].iFldNum,

                                   dbiREADWRITE));

            if (rslt != DBIERR_NONE)

            {

               free(pFldDesc); free(pRecBuf);

               return rslt;

            }

            rslt = Chk(DbiTruncateBlob(*phTmpCur, pRecBuf, pFldDesc[FldCount].iFldNum, 0));

            if (rslt != DBIERR_NONE)

            {

               free(pFldDesc); free(pRecBuf);

               return rslt;

            }

            rslt = Chk(DbiModifyRecord(*phTmpCur, pRecBuf, TRUE));

            if (rslt != DBIERR_NONE)

            {

               free(pFldDesc); free(pRecBuf);

               return rslt;

            }

            rslt = Chk(DbiFreeBlob(*phTmpCur, pRecBuf, pFldDesc[FldCount].iFldNum));

            if (rslt != DBIERR_NONE)

            {

               free(pFldDesc); free(pRecBuf);

               return rslt;

            }

         }

      }

   }

   rslt = Chk(DbiSetToBegin(*phTmpCur));

   free(pFldDesc); free(pRecBuf);

   return rslt;

}