C Examples: DbiDoRestructure

<< Click to Display Table of Contents >>

Navigation:  »No topics above this level«

C Examples: DbiDoRestructure

Return to chapter overview

Example 1: Change first field type to fldINT16 and save table as NEW_CUST PX table to dBASE table.

This example uses the following input:
         fDbiDoRestructure1(hDb, hCur, "CUSTOMER.DB");

DBIResult fDbiDoRestructure1(hDBIDb hDb, hDBICur hXCur, char *TblName)

{

   DBIResult   rslt;

   CRTblDesc   TblDesc;

   CURProps    CurProps;

   pFLDDesc    fldDesc;

   pCROpType   OpType;

 

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

 

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

   rslt = Chk(DbiGetFieldDescs(hXCur, fldDesc));

 

   fldDesc[0].iFldType = fldINT16;

 

   memset((void *) &TblDesc, 0, sizeof(CRTblDesc));

   lstrcpy(TblDesc.szTblName, TblName);

   lstrcpy(TblDesc.szTblType, szDBASE);

   TblDesc.pfldDesc = fldDesc;

   OpType = (pCROpType)malloc((CurProps.iFields + 1) * sizeof(CROpType));

   memset(OpType, crNOOP, (CurProps.iFields + 1) * sizeof(CROpType));

   OpType[CurProps.iFields] = crMODIFY;

   TblDesc.pecrFldOp = OpType;

   rslt = Chk(DbiDoRestructure(hDb, 1, &TblDesc, "NEW_CUST", NULL,

                               NULL, FALSE));

   return rslt;

}

Example 2: Pack a Paradox table.

This example uses the following input:
         fDbiDoRestructure2(hDb, "CUSTOMER");

DBIResult fDbiDoRestructure2(hDBIDb hDb, char *TblName)

{

   DBIResult   rslt;

   CRTblDesc   TblDesc;

 

   memset((void *) &TblDesc, 0, sizeof(CRTblDesc));

   lstrcpy(TblDesc.szTblName, TblName);

   lstrcpy(TblDesc.szTblType, szPARADOX);

   TblDesc.bPack = TRUE;

   rslt = Chk(DbiDoRestructure(hDb, 1, &TblDesc, NULL, NULL,

                               NULL, FALSE));

   if(rslt == DBIERR_NONE)

      ShowMessage("Successful Pack");

 

   return rslt;

}

Example 3: Add validity checks and referential integrity to a table.

This example uses the following input:
         fDbiDoRestructure3(hDb, "CUSTOMER");

DBIResult fDbiDoRestructure3(hDBIDb hDb, char *TblName)

{

  DBIResult   rslt;

  CRTblDesc   TblDesc;

  RINTDesc    pRintDesc[] = {{1, "Order No", rintDEPENDENT, "orders.db",

                               rintCASCADE, rintRESTRICT, 1, {2}, {1}}};

  VCHKDesc    pVchkDesc[] = {{1, TRUE, TRUE, FALSE, FALSE, 1000.00,

                              NULL, NULL, NULL, lkupNONE, NULL},

             // Setting the first field required with minimum value 1000.00

                             {2, TRUE, TRUE, FALSE, FALSE, NULL, NULL,

                               NULL, NULL, lkupNONE, NULL}};

             // Setting second field required.

  memset((void *) &TblDesc, 0, sizeof(CRTblDesc));

  lstrcpy(TblDesc.szTblName, TblName);

  lstrcpy(TblDesc.szTblType, szPARADOX);

  TblDesc.pvchkDesc = pVchkDesc;

  TblDesc.printDesc = pRintDesc;

  rslt = Chk(DbiDoRestructure(hDb, 1, &TblDesc, NULL, NULL,

                              NULL, FALSE));

  return rslt;

}

Example 4: Add a default value to an existing field in a Paradox table.

This example uses the following input:
         fDbiDoRestructure4(hDb, "STOCK", 2, &DefValue);

DBIResult fDbiDoRestructure4(hDBIDb hTmpDb, pCHAR TblName, UINT16 Field, pVOID Value)

{

   DBIResult      rslt;

   CRTblDesc      TblDesc;

   VCHKDesc       VCHK;

   CROpType       Operation = crADD;

 

   memset(&VCHK, 0, sizeof(VCHK));

   VCHK.iFldNum = Field;

   VCHK.bHasDefVal = TRUE;

   memcpy(&VCHK.aDefVal, Value, sizeof(Value));

 

   memset(&TblDesc, 0, sizeof(TblDesc));

   strcpy(TblDesc.szTblName, TblName);

   strcpy(TblDesc.szTblType, szPARADOX);

   TblDesc.iValChkCount = 1;

   TblDesc.pecrValChkOp = &Operation;

   TblDesc.pvchkDesc = &VCHK;

 

   rslt = Chk(DbiDoRestructure(hTmpDb, 1, &TblDesc, NULL, NULL, NULL, FALSE));

   return rslt;

}

Example 5: Add a new field to a Paradox or dBASE table

This example uses the following input:
         fDbiDoRestructure5(hDb, "STOCK.DB", NewFld, "Stock Add.DB");

Note:        A field descriptor must be setup prior to calling this function. You must fill in szName, iFLdType, iSubType (optional), iUnits1 (optional), iUnits2 (optional).

DBIResult fDbiDoRestructure5(hDBIDb hTmpDb, pCHAR TblName, FLDDesc FldDesc, pCHAR NewTblName)

{

   DBIResult      rslt;

   CRTblDesc      TblDesc;

   pCROpType      AddOp;

   UINT16         i;

   pFLDDesc       pFldDesc;

   hDBICur        hCur;

   CURProps       Props;

 

   // Get an existing cursor on the source table.

   rslt = Chk(DbiGetCursorForTable(hTmpDb, TblName, NULL, &hCur));

   if (rslt != DBIERR_NONE)

      return rslt;

 

   // Get the amount of fields in the source table.

   rslt = Chk(DbiGetCursorProps(hCur, &Props));

   if (rslt != DBIERR_NONE)

      return rslt;

 

   // Get the existing field descriptor.

   pFldDesc = (pFLDDesc)malloc((Props.iFields + 1) * sizeof(FLDDesc));

   rslt = Chk(DbiGetFieldDescs(hCur, pFldDesc));

   if (rslt != DBIERR_NONE)

   {

      free(pFldDesc); return rslt;

   }

 

   // Close the source table so the restructure can occur.

   rslt = Chk(DbiCloseCursor(&hCur));

   if (rslt != DBIERR_NONE)

   {

      free(pFldDesc); return rslt;

   }

 

   // Move the new field descriptor to the end of the source field descriptor.

   memcpy(&pFldDesc[Props.iFields], &FldDesc, sizeof(FLDDesc));

 

   // Put a crADD at the same position ad the new field descriptor

   AddOp = (pCROpType)malloc((Props.iFields + 1) * sizeof(CROpType));

   memset(AddOp, crNOOP, (Props.iFields + 1) * sizeof(CROpType));

   AddOp[Props.iFields] = crADD;

 

   memset(&TblDesc, 0, sizeof(TblDesc));

   strcpy(TblDesc.szTblName, TblName);

   TblDesc.iFldCount = (UINT16)(Props.iFields + 1);

   TblDesc.pecrFldOp = AddOp;

   TblDesc.pfldDesc = pFldDesc;

 

   // Resync the field numbers in order.

   for (i = 0; i < Props.iFields; i++)

                         pFldDesc[i].iFldNum = (UINT16)(i + 1);

 

   rslt = Chk(DbiDoRestructure(hTmpDb, 1, &TblDesc, NewTblName, NULL, NULL, FALSE));

   free(AddOp);

   free(pFldDesc);

   return rslt;

}

Example 6: Remove a validity descriptor on the specified field.

This example uses the following input:
         fDbiDoRestructure6(hDb, "STOCK", 3);

DBIResult fDbiDoRestructure6(hDBIDb hTmpDb, pCHAR TblName, UINT16 Field)

{

   DBIResult      rslt;

   CRTblDesc      TblDesc;

   CROpType       Operation = crDROP;

   VCHKDesc       VCHK;

 

   memset(&VCHK, 0, sizeof(VCHK));

   VCHK.iFldNum = Field;

 

   memset(&TblDesc, 0, sizeof(TblDesc));

   strcpy(TblDesc.szTblName, TblName);

   strcpy(TblDesc.szTblType, szPARADOX);

   TblDesc.iValChkCount = 1;

   TblDesc.pecrValChkOp = &Operation;

   TblDesc.pvchkDesc = &VCHK;

 

   rslt = Chk(DbiDoRestructure(hTmpDb, 1, &TblDesc, NULL, NULL, NULL, FALSE));

   return rslt;

}