home *** CD-ROM | disk | FTP | other *** search
/ Total C++ 2 / TOTALCTWO.iso / borland / 32snipit.pak / REFINTEG.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  12KB  |  327 lines

  1. // BDE32 3.x - (C) Copyright 1996 by Borland International
  2.  
  3. // refinteg.c
  4. #include "snipit.h"
  5.  
  6. // Function used to add a record to the table
  7. static DBIResult AddRecord(hDBICur hCur, DFLOAT fStatus, DFLOAT fOrder,
  8.                            CHAR *szDateSent, CHAR *szDateDelivered);
  9.  
  10. static const char szTblName[] = "RefInteg";
  11. static const char szTblType[] = szPARADOX;
  12.  
  13. // Field descriptor used in creating a table
  14. static SNIPFAR FLDDesc fldDesc[] = {
  15.               { // Field 1 - AUTOINC
  16.                 1,            // Field number
  17.                 "Status ID",    // Field name
  18.                 fldFLOAT,     // Field type
  19.                 fldUNKNOWN, // Field subtype
  20.                 0,            // Field size
  21.                 0,            // Decimal places ( 0 )
  22.                               //     computed
  23.                 0,            // Offset in record ( 0 )
  24.                 0,            // Length in bytes  ( 0 )
  25.                 0,            // For Null bits    ( 0 )
  26.                 fldvNOCHECKS, // Validity checks   ( 0 )
  27.                 fldrREADWRITE // Rights
  28.               },
  29.               { // Field 2 - ALPHA
  30.                 2, "Order No", fldFLOAT, fldUNKNOWN,
  31.                 0, 0, 0, 0, 0,
  32.                 fldvNOCHECKS, fldrREADWRITE
  33.               },
  34.               { // Field 4 - DATE
  35.                 3, "Date Sent", fldDATE, fldUNKNOWN,
  36.                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  37.               },
  38.               { // Field 4 - DATE
  39.                 4, "Date Received", fldDATE, fldUNKNOWN,
  40.                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  41.               }
  42.              }; // Array of field descriptors
  43.  
  44. // The number of fields in the table
  45. static const UINT16 uNumFields = sizeof(fldDesc) / sizeof(fldDesc[0]);
  46.  
  47. // Array of referential integrity rules
  48. static SNIPFAR RINTDesc rintDesc[] = {
  49.                 {
  50.                     1,                  // Ref integrity number
  51.                     "Order No",         // Name for this integrity
  52.                     rintDEPENDENT,      // Whether master or dependent
  53.                     "orders.db",        // Other table name
  54.                     rintCASCADE,        // Modify qualifier
  55.                     rintRESTRICT,       // Delete modifier
  56.                     1,                  // Fields in foreign key
  57.                     {2},                // Fields in this table
  58.                     {1}                 // Fields in other table
  59.                 }
  60.             };
  61.  
  62. // Number of referential integrity rules
  63. static const INT16 iRintCount = sizeof(rintDesc) / sizeof(rintDesc[0]);
  64.  
  65. // Index Descriptor - describes the indexes associated with the
  66. // table. This index is going to be added to the table when the
  67. // table is created. 
  68. static SNIPFAR IDXDesc idxDesc[] = {
  69.             { // Primary Index
  70.                "",          // Name
  71.                1,           // Number
  72.                { NULL },    // Tag Name ( for dBase )
  73.                { NULL },    // Optional Format ( BTREE,
  74.                             // HASH, etc )
  75.                TRUE,        // Primary?
  76.                TRUE,        // Unique?
  77.                FALSE,       // Descending?
  78.                TRUE,        // Maintained?
  79.                FALSE,       // SubSet?
  80.                FALSE,       // Expression index?
  81.                NULL,        // for QBE only
  82.                2,           // Fields in key
  83.                1,           // Length in bytes
  84.                FALSE,       // Index out of date?
  85.                0,           // Key type of expression
  86.                { 1,2 },     // Array of field numbers
  87.                { 0 },       // Key expression
  88.                { 0 },       // Key condition
  89.                FALSE,       // Case insensitive
  90.                0,           // Block size in bytes
  91.                0            // Restructure number
  92.             },
  93.             {  // Secondary Index 1 - Single-Field - Maintained,
  94.                // Case insensitive
  95.                "Order No", 2, { NULL }, { NULL }, FALSE, FALSE, FALSE,
  96.                TRUE, FALSE, FALSE, NULL, 1, 1, FALSE, 0, { 2 }, { 0 },
  97.                { 0 }, FALSE, 0, 0
  98.             }
  99.         };
  100.  
  101. // Number of indexes to be created when the table is created.
  102. static const UINT16 uNumIndexes = sizeof(idxDesc) / sizeof(idxDesc[0]);
  103.                               
  104. //=====================================================================
  105. //  Function:
  106. //          RefInteg();
  107. //
  108. //  Description:
  109. //          This function shows how to create and use referential
  110. //          integrity.
  111. //=====================================================================
  112. void
  113. RefInteg (void)
  114. {
  115.     hDBIDb      hDb;                // Handle to the database
  116.     hDBICur     hCur;               // Handle to the table
  117.     CRTblDesc   TblDesc;            // Create table descriptor
  118.     UINT16      uDispNumRecs = 10 ; // Number of records to add and
  119.                                     // display.
  120.     DBIResult   rslt;               // Return value from IDAPI functions
  121.     CROpType    crOpType[] = { crADD }; // Define the operation on the table.            
  122.  
  123.     Screen("*** Referential Integrity Example ***\r\n");
  124.  
  125.     BREAK_IN_DEBUGGER();
  126.  
  127.     Screen("    Initializing IDAPI...");
  128.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  129.     {
  130.         Screen("\r\n*** End of Example ***");
  131.         return;
  132.     }
  133.  
  134.     Screen("    Setting the default database directory...");
  135.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  136.     ChkRslt(rslt, "SetDirectory");
  137.  
  138.     Screen("    Initializing the table descriptor...");
  139.     memset((void *)&TblDesc, 0, sizeof(CRTblDesc));
  140.     lstrcpy(TblDesc.szTblName, szTblName);
  141.     lstrcpy(TblDesc.szTblType, szTblType);
  142.     TblDesc.iFldCount   = uNumFields;
  143.     TblDesc.pfldDesc    = fldDesc;
  144.     TblDesc.iIdxCount   = uNumIndexes;
  145.     TblDesc.pidxDesc    = idxDesc;
  146.     TblDesc.iRintCount  = iRintCount;
  147.     TblDesc.pecrRintOp  = crOpType;
  148.     TblDesc.printDesc   = rintDesc;
  149.  
  150.     Screen("    Creating the %s table...", szTblName);
  151.     rslt = DbiCreateTable(hDb, TRUE, &TblDesc);
  152.     if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
  153.     {
  154.         CloseDbAndExit(&hDb);
  155.         Screen("\r\n*** End of Example ***");
  156.         return;
  157.     }
  158.  
  159.     Screen("    Open the \"%s\" table...", szTblName);
  160.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  161.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  162.                         xltFIELD, FALSE, NULL, &hCur);
  163.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  164.     {
  165.         rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
  166.         ChkRslt(rslt, "DeleteTable");
  167.         CloseDbAndExit(&hDb);
  168.         Screen("\r\n*** End of Example ***");
  169.         return;
  170.     }
  171.  
  172.     // This add will fail - Order Id of 900 does not exist within the
  173.     // orders table.
  174.     Screen("    Add a record to the table.");
  175.     Screen("        Error Expected - no record in the orders table has an"
  176.            " ID of 900...");
  177.     AddRecord(hCur, 1, 900, "07/28/1996", "08/12/1996");
  178.  
  179.     // This add will succeed - Order Id of 1004 exists within the orders
  180.     // table.
  181.     Screen("\r\n    Add a record to the table with a valid Order Id...");
  182.     AddRecord(hCur, 1, 1004, "07/28/1996", "08/12/1996");
  183.  
  184.     rslt = DbiSetToBegin(hCur);
  185.     ChkRslt(rslt, "SetToBegin");
  186.  
  187.     Screen("    Display the \"%s\" table...", szTblName);
  188.     DisplayTable(hCur, uDispNumRecs);
  189.  
  190.     Screen("\r\n    Close the table...");
  191.     rslt = DbiCloseCursor(&hCur);
  192.     ChkRslt(rslt, "CloseCursor");
  193.  
  194.     Screen("    Deleting the table...");
  195.     rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
  196.     ChkRslt(rslt, "DeleteTable");
  197.  
  198.     Screen("    Close the database and exit IDAPI...");
  199.     CloseDbAndExit(&hDb);
  200.  
  201.     Screen("\r\n*** End of Example ***");
  202. }
  203.  
  204. //=====================================================================
  205. //  Funciton:
  206. //          AddRecord (hDBICur hCur, DFLOAT fStatus, DFLOAT fOrder,
  207. //                     CHAR *DateOrdered, CHAR *DateDelivered)
  208. //
  209. //  Input:  hCur            - Cursor to which the record will be added
  210. //          fStatus         - Value to write to the Status ID field
  211. //          fOrder          - Value to write to the Order ID field
  212. //          DateSent        - Value to write to the Date Sent field
  213. //          DateDelivered   - Value to write to the Date Received field
  214. //
  215. //  Return: Success of the function.
  216. //
  217. //  Description:
  218. //          This function is used to add a record to the table.
  219. //=====================================================================
  220. DBIResult
  221. AddRecord (hDBICur hCur, DFLOAT fStatus, DFLOAT fOrder, CHAR *szDateSent,
  222.            CHAR *szDateDelivered)
  223. {
  224.     DBIResult   rslt;               // Return value from IDAPI functions
  225.     pBYTE       pRecBuf;            // Record buffer
  226.     CURProps    TblProps;           // The properties of the table
  227.     UINT16      uMonth;             // Contains the month portion of the
  228.                                     //   date
  229.     UINT16      uDay;               // Contains the day portion of the
  230.                                     //   date
  231.     INT16       iYear;              // Contains the year portion of the
  232.                                     //   date
  233.     CHAR        szDate[30];         // Pointer to be allocated off the
  234.                                     //   heap
  235.     CHAR        *pszTemp;           // Temporary string used in parsing
  236.                                     //   the date
  237.     DBIDATE     Date;               // Date structure - used in
  238.                                     //   DbiPutField.
  239.  
  240.     // Allocate a record buffer.
  241.     rslt = DbiGetCursorProps(hCur, &TblProps);
  242.     ChkRslt(rslt, "GetCursorProps");
  243.  
  244.     pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(pBYTE));
  245.     if (pRecBuf == NULL)
  246.     {
  247.         Screen("    Error - Out of Memory");
  248.         return DBIERR_NOMEMORY;
  249.     }
  250.  
  251.     //  Make sure we're starting with a clean record buffer
  252.     rslt = DbiInitRecord(hCur, pRecBuf);
  253.     ChkRslt(rslt, "InitRecord");
  254.  
  255.     // Add the Status ID to the record buffer
  256.     rslt = DbiPutField(hCur, 1, pRecBuf, (pBYTE) &fStatus);
  257.     ChkRslt(rslt, "PutField");
  258.  
  259.     // Add the Order ID to the record buffer
  260.     rslt = DbiPutField(hCur, 2, pRecBuf, (pBYTE) &fOrder);
  261.     ChkRslt(rslt, "PutField");
  262.  
  263.     strcpy(szDate, szDateSent);
  264.  
  265.     // Need to convert the string to a Date structure
  266.     pszTemp = strtok(szDate, "/");
  267.     if (pszTemp)
  268.     {
  269.         uMonth = (UINT16)atoi(pszTemp); // uMonth set to 0 if invalid date.
  270.         pszTemp = strtok(NULL, "/");
  271.         if (pszTemp)
  272.         {
  273.             uDay = (UINT16)atoi(pszTemp); // uDay set to 0 if invalid date.
  274.             pszTemp = strtok(NULL, "/");
  275.             if (pszTemp)
  276.             {
  277.                 iYear = (UINT16)atoi(pszTemp); // iYear set to 0 if
  278.                                                //   invalid date.
  279.             }
  280.         }
  281.     }
  282.  
  283.     // Encode the date
  284.     rslt = DbiDateEncode(uMonth, uDay, iYear, &Date);
  285.     ChkRslt(rslt, "DateEncode");
  286.  
  287.     // Put the data into the record buffer
  288.     rslt = DbiPutField(hCur, 3, pRecBuf, (pBYTE) &Date);
  289.     ChkRslt(rslt, "PutField");
  290.  
  291.     strcpy(szDate, szDateDelivered);
  292.  
  293.     // Need to convert the string to a Date structure
  294.     pszTemp = strtok(szDate, "/");
  295.     if (pszTemp)
  296.     {
  297.         uMonth = (UINT16)atoi(pszTemp); // uMonth set to 0 if invalid date.
  298.         pszTemp = strtok(NULL, "/");
  299.         if (pszTemp)
  300.         {
  301.             uDay = (UINT16)atoi(pszTemp); // uDay set to 0 if invalid date.
  302.             pszTemp = strtok(NULL, "/");
  303.             if (pszTemp)
  304.             {
  305.                 iYear =(UINT16)atoi(pszTemp);   // iYear set to 0 if
  306.                                                 //   invalid date.
  307.             }
  308.         }
  309.     }
  310.  
  311.     // Encode the date
  312.     rslt = DbiDateEncode(uMonth, uDay, iYear, &Date);
  313.     ChkRslt(rslt, "DateEncode");
  314.  
  315.     // Put the data into the record buffer
  316.     rslt = DbiPutField(hCur, 4, pRecBuf, (pBYTE) &Date);
  317.     ChkRslt(rslt, "PutField");
  318.  
  319.     // Insert the record into the table
  320.     rslt = DbiInsertRecord(hCur, dbiNOLOCK, pRecBuf);
  321.     ChkRslt(rslt, "InsertRecord");
  322.  
  323.     free(pRecBuf);
  324.  
  325.     return rslt;
  326. }
  327.