home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / 32SNIPIT.PAK / IDXPDOX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  16.1 KB  |  447 lines

  1. // BDE32 3.x - (C) Copyright 1996 by Borland International
  2.  
  3. // idxpdox.c
  4. #include "snipit.h"
  5.  
  6. #define NAMELEN  10 // The length of the name fields
  7. #define PLACELEN 20 // The length of the POB field
  8. #define DATELEN  11 // The display length of a date field: mm\dd\yyyy
  9.  
  10. static const char szTblName[] = "People";
  11. static const char szTblType[] = szPARADOX;
  12.  
  13. // Field descriptor used in creating a table.
  14. static SNIPFAR FLDDesc fldDesc[] = {
  15.     { // Field 1 - First Name
  16.         1,            // Field number
  17.         "First Name", // Field name
  18.         fldZSTRING,   // Field type
  19.         fldUNKNOWN,   // Field subtype
  20.         NAMELEN,      // Field size ( 1 or 0, except
  21.                       //     BLOb or CHAR field )
  22.         0,            // Decimal places ( 0 )
  23.                       //     computed
  24.         0,            // Offset in record ( 0 )
  25.         0,            // Length in bytes  ( 0 )
  26.         0,            // For Null bits    ( 0 )
  27.         fldvNOCHECKS, // Validity checks   ( 0 )
  28.         fldrREADWRITE // Rights
  29.     },
  30.     { // Field 2 - Middle Name
  31.         2, "Middle Name", fldZSTRING, fldUNKNOWN,
  32.         NAMELEN, 0, 0, 0, 0,
  33.         fldvNOCHECKS, fldrREADWRITE
  34.     },
  35.     { // Field 3 - Last Name
  36.         3, "Last Name", fldZSTRING, fldUNKNOWN,
  37.         NAMELEN, 0, 0, 0, 0,
  38.         fldvNOCHECKS, fldrREADWRITE
  39.     },
  40.     { // Field 4 - Date of Birth
  41.         4, "DOB", fldDATE, fldUNKNOWN,
  42.         0, 0, 0, 0, 0,
  43.         fldvNOCHECKS, fldrREADWRITE
  44.     },
  45.     { // Field 5 - Place of Birth
  46.         5, "POB", fldZSTRING, fldUNKNOWN,
  47.         PLACELEN, 0, 0, 0, 0,
  48.         fldvNOCHECKS, fldrREADWRITE
  49.     }};  // Array of field descriptors
  50.  
  51. // Index descriptor - describes the index associated with the
  52. // table.
  53. static SNIPFAR IDXDesc idxDesc[] = {
  54.     { // Primary Index - Full Name
  55.         "Full Name",  // Name
  56.         1,            // Number
  57.         { NULL },     // Tag Name ( for dBase )
  58.         { NULL },     // Optional Format ( BTREE,
  59.                       // HASH, etc )
  60.          TRUE,         // Primary?
  61.          TRUE,         // Unique?
  62.          FALSE,        // Descending?
  63.          TRUE,         // Maintained?
  64.          FALSE,        // SubSet?
  65.          FALSE,        // Expression index?
  66.          NULL,         // for QBE only
  67.          3,            // Fields in key
  68.          1,            // Length in bytes
  69.          FALSE,        // Index out of date?
  70.          0,            // Key type of expression
  71.          { 1,2,3 },    // Array of field numbers
  72.          { 0 },        // Key expression
  73.          { 0 },        // Key condition
  74.          FALSE,        // Case insensitive
  75.          0,            // Block size in bytes
  76.          0             // Restructure number
  77.      },
  78.      {  // Secondary Index 1 - single-field - maintained,
  79.         // Case insensitive
  80.          "Last Name", 2, { NULL }, { NULL }, FALSE, FALSE, FALSE,
  81.          TRUE, FALSE, FALSE, NULL, 1, 1, FALSE, 0, { 3 }, { 0 },
  82.          { 0 }, TRUE, 0, 0
  83.      },
  84.      { // Secondary Index 2 - multi-field - maintained
  85.          "Name", 3, { NULL }, { NULL }, FALSE, FALSE, FALSE, TRUE,
  86.          FALSE, FALSE, NULL, 2, 1, FALSE, 0, { 3,1 }, { 0 }, { 0 },
  87.          FALSE, 0, 0
  88.      }};
  89.  
  90. // Index descriptor - Describes the indexes associated with the
  91. // table.  This index will be added to the table after the table
  92. // has been created.
  93. static SNIPFAR IDXDesc idxDesc1[] = {
  94.     {  // Secondary Index 3 - single-field - not maintained
  95.         "POB", 4, { NULL }, { NULL }, FALSE, FALSE, FALSE, FALSE,
  96.         FALSE, FALSE, NULL, 1, 1, FALSE, 0, { 5 }, { 0 }, { 0 },
  97.         FALSE, 0, 0
  98.     }};
  99.  
  100. // The number of fields in the table.
  101. static const UINT16 uNumFields = sizeof(fldDesc) / sizeof (fldDesc[0]);
  102.  
  103. // Number of indexes to be created when the table is created.
  104. static const UINT16 uNumIndexes = sizeof(idxDesc) / sizeof(idxDesc[0]);
  105.  
  106. static DBIResult InitTable(phDBIDb);
  107. static DBIResult AddRecord(phDBICur, pCHAR, pCHAR, pCHAR, UINT16, UINT16, UINT16,
  108.                            pCHAR);
  109.  
  110. //=====================================================================
  111. //  Function:
  112. //          IndexPDox();
  113. //
  114. //  Description:
  115. //          This example shows how to create and add Paradox indexes.
  116. //          This includes creating primary indexes and managing
  117. //          non-maintained secondary indexes.
  118. //=====================================================================
  119. void
  120. IndexPDox (void)
  121. {
  122.     hDBIDb      hDb = 0;    // Database handle
  123.     hDBICur     hCur = 0;   // Cursor handle
  124.     DBIResult   rslt;       // Return value from IDAPI functions
  125.     CURProps    TblProps;   // Table properties
  126.     IDXDesc     *MyDesc;    // Index descriptor
  127.     int         i;          // Loop variable
  128.  
  129.     Screen("*** Paradox Index Manipulation Example ***\r\n");
  130.  
  131.     BREAK_IN_DEBUGGER();
  132.  
  133.     Screen("    Initializing IDAPI...");
  134.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  135.     {                                       
  136.         Screen("\r\n*** End of Example ***");
  137.         return;
  138.     }
  139.  
  140.     Screen("    Setting the database directory...");
  141.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  142.     ChkRslt(rslt, "SetDirectory");
  143.  
  144.     // Create the table and fill with data.
  145.     if (InitTable(&hDb))
  146.     {
  147.         CloseDbAndExit(&hDb);
  148.         Screen("\r\n*** End of Example ***");
  149.         return;
  150.     }
  151.  
  152.     // Add an index to the table.  Note that the index is added to the
  153.     // table before the table is opened - the table must either be closed
  154.     // or be opened in exclusive mode to add an index.
  155.     Screen("        Add an index to the table...");
  156.     rslt = DbiAddIndex(hDb, NULL, (pCHAR) szTblName, (pCHAR) szTblType,
  157.                        idxDesc1, NULL);
  158.     ChkRslt(rslt, "AddIndex");
  159.  
  160.  
  161.     Screen("        Open the %s table...", szTblName);
  162.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  163.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  164.                         xltFIELD, FALSE, NULL, &hCur);
  165.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  166.     {
  167.         CloseDbAndExit(&hDb);
  168.         Screen("\r\n*** End of Example ***");
  169.         return;
  170.     }
  171.  
  172.     Screen("    Get the number of indexes on the %s table...", szTblName);
  173.     rslt = DbiGetCursorProps(hCur, &TblProps);
  174.     ChkRslt(rslt, "GetCursorProps");
  175.  
  176.     Screen("    There are %d indexes on the table", TblProps.iIndexes);
  177.  
  178.     // Allocate space for the index descriptor.
  179.     MyDesc = (IDXDesc *) malloc(sizeof(IDXDesc) * TblProps.iIndexes);
  180.     if (MyDesc == NULL)
  181.     {
  182.         Screen("    Error - Out of memory");
  183.         CloseDbAndExit(&hDb);
  184.         Screen("\r\n*** End of Example ***");
  185.         return;
  186.     }
  187.  
  188.     Screen("    Get and display information about the indexes");
  189.     Screen("\r\n        Name\t\t   ID\tPrimary Maintained "
  190.            "Fields Case_Insensitive");
  191.     Screen("        =================================================="
  192.            "==========");
  193.     for (i = 0;i < TblProps.iIndexes; i++)
  194.     {
  195.         rslt = DbiGetIndexDesc(hCur, (UINT16)(i+1), &(MyDesc[i]));
  196.         ChkRslt(rslt, "GetIndexDesc");
  197.  
  198.         Screen("        %-15s\t%5d \t%s\t%2s\t %4d\t   %2s",
  199.                (strcmp(MyDesc[i].szName, "") ? MyDesc[i].szName :
  200.                 "*Primary Index*"), // Primary indexes don't have a name
  201.                 MyDesc[i].iIndexId, // The ID of the Index.
  202.                 // Display "TRUE" or "FALSE" as opposed to 1 or 0...
  203.                 (MyDesc[i].bPrimary ? "TRUE" : "FALSE"),
  204.                 (MyDesc[i].bMaintained ? "TRUE" : "FALSE"),
  205.                 MyDesc[i].iFldsInKey,
  206.                 (MyDesc[i].bCaseInsensitive ? "TRUE" : "FALSE"));
  207.     }
  208.  
  209.     rslt = DbiSetToBegin(hCur);
  210.     ChkRslt(rslt, "SetToBegin");
  211.  
  212.     // Display the records in the table. The order of the records depends
  213.     // on the active index - in this case, it is the primary index.
  214.     Screen("\r\n    Display the records in the table in the order of the"
  215.            " primary \r\n    index - First, Middle, Last");
  216.     DisplayTable(hCur, 0);
  217.  
  218.     // Switch the index that the table is opened on - only need to
  219.     // provide one of the following: IndexName, TagName, or IndexId.
  220.     Screen("\r\n    Change the table to be opened on the \"Last Name\""
  221.            " index...");
  222.     rslt = DbiSwitchToIndex(&hCur, idxDesc[1].szName, NULL, NULL, FALSE);
  223.     ChkRslt(rslt, "SwitchToIndex");
  224.  
  225.     // Go to the beginning of the table
  226.     rslt = DbiSetToBegin(hCur);
  227.     ChkRslt(rslt, "SetToBegin");
  228.  
  229.     Screen("    Display the records in the table in the order of the"
  230.            " \"Last Name\" index...");
  231.     DisplayTable(hCur, 0);
  232.  
  233.     Screen("\r\n    Add a record to the table in order to invalidate"
  234.            " non-maintained indexes...");
  235.     AddRecord(&hCur, "Jerry", "Joel", "Raccah", 5, 22, 1958,
  236.               "Lake Tahoe");
  237.  
  238.     // Switch the index that the table is opened on - only need to
  239.     // provide one of the following: IndexName, TagName, or IndexId.
  240.     // This function is passed an ID of 5 which is the id of the
  241.     // single field that it is indexing on.
  242.     Screen("\r\n    Change the table to be opened on the \"POB\""
  243.            " index (which is non-maintained)...");
  244.     Screen("\r\n        Error expected - the \"POB\" index is"
  245.            " not maintained");
  246.  
  247.     rslt = DbiSwitchToIndex(&hCur, NULL, NULL, MyDesc[1].iIndexId, FALSE);
  248.     ChkRslt(rslt, "SwitchToIndex");
  249.  
  250.     Screen("\r\n    Steps to regenerate the \"POB\" index...");
  251.     Screen("        Close the table - the table must be open in"
  252.            " exclusive mode to regenerate \r\n        the indexes...");
  253.  
  254.     rslt = DbiCloseCursor(&hCur);
  255.     ChkRslt(rslt, "CloseCursor");                 
  256.  
  257.     Screen("        Open the table in exclusive mode...");
  258.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  259.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENEXCL,
  260.                         xltFIELD, FALSE, NULL, &hCur);
  261.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  262.     {
  263.         CloseDbAndExit(&hDb);
  264.         Screen("\r\n*** End of Example ***");
  265.         return;
  266.     }
  267.  
  268.     Screen("        Regenerate the index...");
  269.     rslt = DbiRegenIndex(hDb, hCur, (pCHAR) szTblName, (pCHAR) szTblType,
  270.                          MyDesc[1].szName, NULL, NULL);
  271.     ChkRslt(rslt, "RegenIndexes");
  272.  
  273.     // Switch the index that the table is opened on - only need to
  274.     // provide one of the following: IndexName, TagName, or IndexId
  275.     // (parameter 2, 3, or 4 to DbiSwitchToIndex).
  276.     Screen("\r\n    Change the table to be opened on the \"POB\""
  277.            " index (which is non-maintained)...");
  278.     rslt = DbiSwitchToIndex(&hCur, MyDesc[1].szName, NULL, NULL, FALSE);
  279.     ChkRslt(rslt, "SwitchToIndex");
  280.  
  281.     rslt = DbiSetToBegin(hCur);
  282.     ChkRslt(rslt, "SetToBegin");
  283.  
  284.     Screen("    Display the records in the table in the order of the"
  285.            " \"POB\" (Place of Birth)\r\n    index...");
  286.     DisplayTable(hCur, 0);
  287.  
  288.     Screen("\r\n    Close the %s table...", szTblName);
  289.     rslt = DbiCloseCursor(&hCur);
  290.     ChkRslt(rslt, "CloseCursor");
  291.  
  292.     // Note that the table has to be opened in exclusive mode or closed
  293.     // to delete an index.
  294.     Screen("    Delete the \"Last Name\" index...");
  295.  
  296.     rslt = DbiDeleteIndex(hDb, NULL, (pCHAR) szTblName, (pCHAR) szTblType,
  297.                           "Last Name", "", NULL);
  298.     ChkRslt(rslt, "DeleteIndex");
  299.  
  300.     Screen("    Delete the %s table and remaining indexes...", szTblName);
  301.     rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
  302.     ChkRslt(rslt, "DeleteTable");
  303.  
  304.     free(MyDesc);
  305.  
  306.     Screen("    Close the database and exit IDAPI...");
  307.     CloseDbAndExit(&hDb);
  308.  
  309.     Screen("\r\n*** End of Example ***");
  310. }
  311.  
  312. //=====================================================================
  313. //  Function:
  314. //          InitTable(phDb);
  315. //
  316. //  Input:  phDb    - Pointer to the database handle.
  317. //
  318. //  Return: result of the table initialization.
  319. //
  320. //  Description:
  321. //          This function will create a table and fill it
  322. //          with a number of records.  
  323. //=====================================================================
  324. DBIResult
  325. InitTable (phDBIDb phDb)
  326. {
  327.     hDBICur     hCur = 0;           // Cursor handle for the table that is
  328.                                     // created
  329.     DBIResult   rslt;               // Value returned from IDAPI functions
  330.     CRTblDesc   crTblDsc;           // Table descriptor
  331.     BOOL        bOverWrite = TRUE;  // Overwrite, yes/no flag
  332.  
  333.     // Initialize the table create descriptor.
  334.     memset(&crTblDsc, 0, sizeof(CRTblDesc));
  335.     strcpy(crTblDsc.szTblName, szTblName);
  336.     strcpy(crTblDsc.szTblType, szTblType);
  337.     crTblDsc.iFldCount     = uNumFields;
  338.     crTblDsc.pfldDesc      = fldDesc;
  339.     crTblDsc.iIdxCount     = uNumIndexes;
  340.     crTblDsc.pidxDesc      = idxDesc;
  341.  
  342.     Screen("    Creating the %s table...", szTblName);
  343.  
  344.     rslt = DbiCreateTable(*phDb, bOverWrite, &crTblDsc);
  345.     if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
  346.     {
  347.         return rslt;
  348.     }
  349.  
  350.     rslt = DbiOpenTable(*phDb, (pCHAR) szTblName, (pCHAR) szTblType,
  351.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  352.                         xltFIELD, FALSE, NULL, &hCur);
  353.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  354.     {
  355.         return rslt;
  356.     }
  357.  
  358.     // Add records to the table.
  359.     Screen("    Add records to the table...");
  360.     AddRecord(&hCur, "Klaus", "John", "Lockwood", 7, 28, 1968,
  361.               "Chicago");
  362.     AddRecord(&hCur, "Tracy", "Dunvan", "Aoki", 12, 27, 1969,
  363.               "Hermosa Beach");
  364.     AddRecord(&hCur, "John", "David", "Krull", 2, 7, 1954,
  365.               "Ohmaha");
  366.     AddRecord(&hCur, "Goliath", "Joel", "Raccah", 4, 13, 1966,
  367.               "Tel Aviv");
  368.  
  369.     rslt = DbiCloseCursor(&hCur);
  370.     ChkRslt(rslt, "CloseCursor");
  371.  
  372.     return rslt;
  373. }
  374.  
  375. //=====================================================================
  376. //  Function:
  377. //          AddRecord(hCur, pszFirst, pszMiddle, pszLast, uMonth,
  378. //                    uDay, uYear, pszPOB);
  379. //
  380. //  Input:  hCur        - Pointer to the cursor handle
  381. //          pszFirst    - First name
  382. //          pszMiddle   - Middle name
  383. //          pszLast     - Last name
  384. //          uMonth      - Month of birth
  385. //          uDay        - Day of birth
  386. //          uYear       - Year of birth
  387. //          pszPOB      - Place of birth
  388. //
  389. //  Return: Result of adding the record to the table
  390. //
  391. //  Description:
  392. //          This function will add a record to the given table.
  393. //=====================================================================
  394. DBIResult
  395. AddRecord (phDBICur hCur, pCHAR pszFirst, pCHAR pszMiddle, pCHAR pszLast,
  396.            UINT16 uMonth, UINT16 uDay, UINT16 uYear, pCHAR pszPOB)
  397. {
  398.     DBIDATE     Date;       // Date structure
  399.     DBIResult   rslt;       // Value returned from IDAPI functions
  400.     CURProps    TblProps;   // Table properties
  401.     pBYTE       pRecBuf;    // Record buffer
  402.  
  403.     //  Allocate a record buffer,
  404.     rslt = DbiGetCursorProps(*hCur, &TblProps);
  405.     ChkRslt(rslt, "GetCursorProps");
  406.  
  407.     pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE));
  408.     if (pRecBuf == NULL)
  409.     {
  410.         return DBIERR_NOMEMORY;
  411.     }
  412.  
  413.     //  Make sure we're starting with a clean record buffer.
  414.     rslt = DbiInitRecord(*hCur, pRecBuf);
  415.     ChkRslt(rslt, "InitRecord");
  416.  
  417.     // First Name.
  418.     rslt = DbiPutField(*hCur, 1, pRecBuf, (pBYTE) pszFirst);
  419.     ChkRslt(rslt, "PutField");
  420.  
  421.     // Middle Name.
  422.     rslt = DbiPutField(*hCur, 2, pRecBuf, (pBYTE) pszMiddle);
  423.     ChkRslt(rslt, "PutField");
  424.  
  425.     // Last Name.
  426.     rslt = DbiPutField(*hCur, 3, pRecBuf, (pBYTE) pszLast);
  427.     ChkRslt(rslt, "PutField");
  428.  
  429.     // DOB.
  430.     rslt = DbiDateEncode(uMonth, uDay, uYear, &Date);
  431.     ChkRslt(rslt, "DateEncode");
  432.  
  433.     rslt = DbiPutField(*hCur, 4,  pRecBuf, (pBYTE) &Date);
  434.     ChkRslt(rslt, "PutField");
  435.  
  436.     // Place of Birth.
  437.     rslt = DbiPutField(*hCur, 5,  pRecBuf, (pBYTE) pszPOB);
  438.     ChkRslt(rslt, "PutField");
  439.  
  440.     rslt = DbiInsertRecord(*hCur, dbiNOLOCK, pRecBuf),
  441.     ChkRslt(rslt, "InsertRecord");
  442.  
  443.     free(pRecBuf);
  444.  
  445.     return rslt;
  446. }
  447.