home *** CD-ROM | disk | FTP | other *** search
- // BDE - (C) Copyright 1995 by Borland International
-
- // tbrstrct.c
- #include "snipit.h"
-
- static const char szTblName[] = "contacts";
- static const char szRstrctTblName[] = "rstrct";
- static const char szTblType[] = szPARADOX;
-
- #define NUMOFFIELDS 4
-
- static DBIResult getFieldDescs(hDBICur hCur, UINT16 *uNumFields,
- FLDDesc **fldDesc);
- static DBIResult changeFieldDescs(UINT16 *uNumFields, FLDDesc *fldDesc,
- FLDDesc *fldDescriptor, CROpType *ecrFldOp);
-
- //=====================================================================
- // Function:
- // TBRestructure();
- //
- // Description:
- // This example shows how to change the characteristics of a
- // table, adding, dropping and changing fields.
- //
- // The following steps are followed:
- // Getting the existing field descriptors
- // Modifying the field descriptors to the new table structure
- // Initializing the CRTblDesc (create table descriptor)
- // Restructuring the table
- //=====================================================================
- void
- TBRestructure (void)
- {
- hDBIDb hDb; // Handle to the database
- hDBICur hCur; // Handle to the table
- CRTblDesc crTblDesc; // Table descriptor
- UINT16 uNumFields; // Number of fields
- UINT16 uNumOfRecs = 5; // Nunmber of records to display
- FLDDesc *fldDesc; // Pointer to the field descriptors
- FLDDesc fldDescriptor[NUMOFFIELDS]; // Field descriptor
- CROpType ecrFldOp[NUMOFFIELDS]; // Field operations
- DBIResult rslt; // Return value from IDAPI functions
- DBIPATH szKeyViol = "KEYVIOL"; // Name for the key violation table
-
- Screen("*** Restructure Example ***\r\n");
-
- BREAK_IN_DEBUGGER();
-
- Screen(" Initializing IDAPI...");
- if (InitAndConnect(&hDb) != DBIERR_NONE)
- {
- Screen("*** End of Example ***");
- return;
- }
-
- Screen(" Setting the database directory... ");
- rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
- ChkRslt(rslt, "SetDirectory");
-
- Screen(" Open the %s table....", szTblName);
- rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
- NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
- xltFIELD, FALSE, NULL, &hCur);
- if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
- {
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- rslt = DbiSetToBegin(hCur);
- ChkRslt(rslt, "SetToBegin");
-
- Screen(" Display the first %u records of the %s table...",
- uNumOfRecs, szTblName);
- DisplayTable(hCur, uNumOfRecs);
-
- // Get the field descriptors for the existing table.
- if (getFieldDescs(hCur, &uNumFields, &fldDesc) != DBIERR_NONE)
- {
- rslt = DbiCloseCursor(&hCur);
- ChkRslt(rslt, "CloseCursor");
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- Screen("\r\n Close the %s table...", szTblName);
- DbiCloseCursor(&hCur);
-
- Screen(" Initialize the table descriptor...");
- memset(&crTblDesc, 0, sizeof(CRTblDesc));
- strcpy(crTblDesc.szTblName, szTblName);
- strcpy(crTblDesc.szTblType, szTblType);
- crTblDesc.bPack = TRUE;
- changeFieldDescs(&uNumFields, fldDesc, fldDescriptor, ecrFldOp);
- crTblDesc.iFldCount = uNumFields;
- crTblDesc.pecrFldOp = ecrFldOp;
- crTblDesc.pfldDesc = fldDescriptor;
-
- Screen(" Restructure the %s table to the %s table...",
- szTblName, szRstrctTblName);
- rslt = DbiDoRestructure(hDb, 1, &crTblDesc, (pCHAR) szRstrctTblName,
- szKeyViol, NULL, FALSE);
- if (ChkRslt(rslt, "DoRestructure") != DBIERR_NONE)
- {
- free(fldDesc);
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- Screen(" Open the %s table...", szRstrctTblName);
- rslt = DbiOpenTable(hDb, (pCHAR) szRstrctTblName, (pCHAR) szTblType,
- NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
- xltFIELD, FALSE, NULL, &hCur);
- if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
- {
- free(fldDesc);
- rslt = DbiDeleteTable(hDb, (pCHAR) szRstrctTblName, (pCHAR) szTblType);
- ChkRslt(rslt, "DeleteTable");
- CloseDbAndExit(&hDb);
- Screen("\r\n*** End of Example ***");
- return;
- }
-
- rslt = DbiSetToBegin(hCur);
- ChkRslt(rslt, "SetToBegin");
-
- Screen(" Display the first %u records of the %s table...",
- uNumOfRecs, szRstrctTblName);
- DisplayTable(hCur, uNumOfRecs);
-
- Screen("\r\n Close the %s table...", szRstrctTblName);
- rslt = DbiCloseCursor(&hCur);
- ChkRslt(rslt, "CloseCursor");
-
- Screen(" Delete the %s table...", szRstrctTblName);
- rslt = DbiDeleteTable(hDb, (pCHAR) szRstrctTblName, (pCHAR) szTblType);
- ChkRslt(rslt, "DeleteTable");
-
- free(fldDesc);
-
- Screen(" Close the database and exit IDAPI...");
- CloseDbAndExit(&hDb);
-
- Screen("\r\n*** End of Example ***");
- }
-
- //===============================================================
- // Function:
- // getFieldDescs(hCur, *uNumFields, **fldDesc)
- //
- // Input: hCur - Handle to the cursor
- // uNumFields - Number of fields in the field descriptor
- // (Returned)
- // fldDesc - Existing field descriptor (Input)
- //
- // Return: IDAPI return code
- //
- // Description:
- // This function gets the field descriptor for an existing cursor.
- //================================================================
- DBIResult
- getFieldDescs (hDBICur hCur, UINT16 *uNumFields, FLDDesc **fldDesc)
- {
- DBIResult rslt; // Return value from IDAPI functions
- CURProps curProps; // Properties of the table
-
- // Change the translation mode to xltNONE to get the physical
- // field descriptors.
- rslt = DbiSetProp(hCur, curXLTMODE, xltNONE);
- ChkRslt(rslt, "SetProp");
-
- rslt = DbiGetCursorProps(hCur, &curProps);
- if (ChkRslt(rslt, "GetCursorProps") != DBIERR_NONE)
- {
- return rslt;
- }
-
- *uNumFields = curProps.iFields;
-
- *fldDesc = (FLDDesc *) malloc(curProps.iFields * sizeof(FLDDesc));
- if (fldDesc == NULL)
- {
- return DBIERR_NOMEMORY;
- }
-
- rslt = DbiGetFieldDescs(hCur, *fldDesc);
- if (ChkRslt(rslt, "GetFIeldDescs") != DBIERR_NONE)
- {
- return rslt;
- }
-
- // Change the translation mode back to xltFIELD.
- rslt = DbiSetProp(hCur, curXLTMODE, xltFIELD);
- ChkRslt(rslt, "SetProp");
-
- return DBIERR_NONE;
- }
-
- //===============================================================
- // Function:
- // changeFieldDescs(UINT16 *uNumFields, FLDDesc *fldDesc,
- // FLDDesc *fldDescriptor, CROpType *ecrFldOp);
- //
- // Input: uNumFields - Number of fields in the existing field
- // descriptor. Also returns the number of
- // fields in the new field descriptor
- // (Input and Returned)
- // fldDesc - Existing field descriptor (Input)
- // fldDescriptor - New field descriptor to use (Returned)
- // ecrFldOp - Array of operations on the fields in
- // fldDescriptor (Returned)
- //
- // Return: IDAPI return code
- //
- // Description:
- // This function sets up the field structure for the new table.
- //================================================================
- DBIResult
- changeFieldDescs (UINT16 *uNumFields, FLDDesc *fldDesc,
- FLDDesc *fldDescriptor, CROpType *ecrFldOp)
- {
- UINT16 i; // Loop Counter
- UINT16 uField; // Number of the field
-
- uField = 0;
-
- for (i=0; i < *uNumFields; i++)
- {
- // Keep the "Last Name" field as is.
- if (! strcmp(fldDesc[i].szName, "Last Name"))
- {
- fldDescriptor[uField] = fldDesc[i];
- ecrFldOp[uField] = crNOOP;
- uField++;
- }
-
- // Change the "First Name" field.
- if (! strcmp(fldDesc[i].szName, "First Name"))
- {
- fldDescriptor[uField] = fldDesc[i];
- strcpy(fldDescriptor[uField].szName, "First");
- ecrFldOp[uField] = crMODIFY;
- uField++;
- }
-
- // Move the "Phone" Field.
- if (! strcmp(fldDesc[i].szName, "Phone"))
- {
- fldDescriptor[uField] = fldDesc[i];
- ecrFldOp[uField] = crCOPY;
- uField++;
- }
- }
-
- // Make certain that there is enough space in the field descriptor
- // for a new field.
- if (uField < *uNumFields)
- {
- // Add the "Age" field.
- fldDescriptor[uField].iFldNum = 0;
- strcpy(fldDescriptor[uField].szName, "Age");
- fldDescriptor[uField].iFldType = fldPDXSHORT;
- fldDescriptor[uField].iSubType = 0;
- fldDescriptor[uField].iUnits1 = 0;
- fldDescriptor[uField].iUnits2 = 0;
- fldDescriptor[uField].iOffset = 0;
- fldDescriptor[uField].iLen = 0;
- fldDescriptor[uField].iNullOffset = 0;
- fldDescriptor[uField].efldvVchk = fldvNOCHECKS;
- fldDescriptor[uField].efldrRights = fldrREADWRITE;
-
- ecrFldOp[uField] = crADD;
-
- uField++;
- }
-
- *uNumFields = uField;
-
- return DBIERR_NONE;
- }
-