home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bde / snipit.pak / UPSIZE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  8.2 KB  |  234 lines

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // Upsize.C
  4. #include "snipit.h"
  5.  
  6. static const char szUPTblName[] = "customer";
  7. static const char szUPTblType[] = szPARADOX;
  8. static const char szSVTblName[] = "SrvrCustomer";
  9.  
  10. //=====================================================================
  11. //  Function:
  12. //          UpSize();
  13. //
  14. //  Description:
  15. //          This example shows how to copy a local table to an SQL server.
  16. //          This is done using the DbiBatchMove function.
  17. //
  18. //          The example will request that the user supply a server name
  19. //          and password, and will then proceed to copy the "Customer"
  20. //          Paradox table (and its primary index) to the table.
  21. //
  22. //  WARNING:
  23. //          You must have write access to your server in order for this
  24. //          example to work.
  25. //=====================================================================
  26. void
  27. UpSize (void)
  28. {
  29.     hDBIDb      hDb;                // Handle to the server database
  30.     hDBICur     hCur;               // Server cursor handle
  31.     hDBIDb      hDbPX;              // Handle to the Paradox database
  32.     hDBICur     hCurPX;             // Paradox cursor handle
  33.     DBIResult   rslt;               // Return value of a given function call.
  34.     UINT16      iLen;               // Length of the returned property
  35.     CURProps    TblProps;           // Table properties
  36.     pIDXDesc    pIdxDesc = NULL;    // Index pointer
  37.     UINT16      i=0;                // Index counter
  38.     CHAR        szIndex[DBIMAXNAMELEN]; // Temporary index name
  39.     UINT16      iIndexNum = 1;      // Index number on the server table
  40.  
  41.     BATTblDesc  BatTblDesc = {
  42.                             NULL,
  43.                             "",
  44.                             "",
  45.                             { NULL },
  46.                             { NULL }
  47.                          };
  48.  
  49.     Screen("*** Copying a local table to an SQL Server ***\r\n");
  50.  
  51.     BREAK_IN_DEBUGGER();
  52.  
  53.     Screen("    Initializing IDAPI...");
  54.     if (InitAndConnect2(&hDb) != DBIERR_NONE)
  55.     {
  56.         Screen("\r\n*** End of Example ***");
  57.         return;
  58.     }
  59.  
  60.     // Get the type of the server.
  61.     rslt = DbiGetProp((hDBIObj)hDb, dbDATABASETYPE,
  62.                       (pCHAR)BatTblDesc.szTblType, sizeof(DBINAME), &iLen);
  63.     ChkRslt(rslt, "Get Prop");
  64.  
  65.     if (! strcmp(BatTblDesc.szTblType, "STANDARD"))
  66.     {
  67.         Screen("    This example only works on remote SQL databases");
  68.         CloseDbAndExit(&hDb);
  69.         Screen("\r\n*** End of Example ***");
  70.         return;
  71.     }
  72.  
  73.     // Open a standard database for the local table.
  74.     rslt = DbiOpenDatabase(NULL, NULL, dbiREADWRITE, dbiOPENSHARED,
  75.                            NULL, 0, NULL, NULL, &hDbPX);
  76.     if (ChkRslt(rslt, "OpenDatabase") != DBIERR_NONE)
  77.     {
  78.         CloseDbAndExit(&hDb);
  79.         Screen("\r\n*** End of Example ***");
  80.         return;
  81.     }
  82.  
  83.     Screen("    Setting the database directory for the standard database...");
  84.     rslt = DbiSetDirectory(hDbPX, (pCHAR) szTblDirectory);
  85.     ChkRslt(rslt, "SetDirectory");
  86.  
  87.     Screen("    Open the %s %s table which will be copied to the\r\n"
  88.            "        %s server...", szUPTblName, szUPTblType,
  89.            BatTblDesc.szTblType);
  90.  
  91.     rslt = DbiOpenTable(hDbPX, (pCHAR) szUPTblName, (pCHAR) szUPTblType,
  92.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  93.                         xltFIELD, FALSE, NULL, &hCurPX);
  94.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  95.     {
  96.         DbiCloseDatabase(&hDbPX);
  97.         CloseDbAndExit(&hDb);
  98.         Screen("\r\n*** End of Example ***");
  99.         return;
  100.     }
  101.  
  102.     rslt = DbiSetToBegin(hCurPX);
  103.     ChkRslt(rslt, "SetToBegin");
  104.  
  105.     Screen("\r\n    Copying the %s table to the server...",
  106.            szUPTblName);
  107.  
  108.     BatTblDesc.hDb = hDb;
  109.     strcpy(BatTblDesc.szTblName, (pCHAR)szSVTblName);
  110.     
  111.     rslt = DbiBatchMove(NULL, hCurPX, &BatTblDesc, NULL, batCOPY, NULL,
  112.                         NULL, NULL, NULL, NULL, NULL, NULL,
  113.                         NULL, NULL, NULL, NULL, TRUE, TRUE,
  114.                         NULL, TRUE);
  115.     if(ChkRslt(rslt, "Batch Move") != DBIERR_NONE)
  116.     {
  117.         rslt = DbiCloseCursor(&hCurPX);
  118.         ChkRslt(rslt, "CloseCur");
  119.         DbiCloseDatabase(&hDbPX);
  120.         CloseDbAndExit(&hDb);
  121.         Screen("\r\n*** End of Example ***");
  122.         return;
  123.     }
  124.  
  125.     Screen("    Open the %s %s table...", szSVTblName, BatTblDesc.szTblType);
  126.     rslt = DbiOpenTable(hDb, (pCHAR) szSVTblName,
  127.                         (pCHAR)BatTblDesc.szTblType, NULL, NULL, 0,
  128.                         dbiREADWRITE, dbiOPENEXCL, xltFIELD, FALSE, NULL,
  129.                         &hCur);             
  130.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  131.     {
  132.         rslt = DbiDeleteTable(hDb,(pCHAR) szSVTblName,
  133.                               (pCHAR)BatTblDesc.szTblType);
  134.         ChkRslt(rslt, "DeleteTable");
  135.         rslt = DbiCloseCursor(&hCurPX);
  136.         ChkRslt(rslt, "CloseCur");
  137.         DbiCloseDatabase(&hDbPX);
  138.         CloseDbAndExit(&hDb);
  139.         Screen("\r\n*** End of Example ***");
  140.         return;
  141.     }
  142.  
  143.     //  Allocate a buffer for the index descriptors.
  144.     rslt = DbiGetCursorProps(hCurPX, &TblProps);
  145.     ChkRslt(rslt, "GetCursorProps");
  146.  
  147.     pIdxDesc = (pIDXDesc)malloc(TblProps.iIndexes * sizeof(IDXDesc));
  148.     if ((pIdxDesc == NULL) && (TblProps.iIndexes != 0))
  149.     {
  150.         rslt = DbiCloseCursor(&hCur);
  151.         ChkRslt(rslt, "CloseCur");
  152.         rslt = DbiDeleteTable(hDb,(pCHAR) szSVTblName,
  153.                               (pCHAR)BatTblDesc.szTblType);
  154.         ChkRslt(rslt, "DeleteTable");
  155.         rslt = DbiCloseCursor(&hCurPX);
  156.         ChkRslt(rslt, "CloseCur");
  157.         DbiCloseDatabase(&hDbPX);
  158.         CloseDbAndExit(&hDb);
  159.         Screen("\r\n*** End of Example ***");
  160.         return;
  161.     }
  162.  
  163.     // Check if there are indexes on the table.
  164.     if (TblProps.iIndexes != 0)
  165.     {
  166.         rslt = DbiGetIndexDescs(hCurPX, pIdxDesc);
  167.         ChkRslt(rslt, "Get Index Descs");
  168.     }
  169.  
  170.     Screen("    Add the Unique indexes found in the original table to "
  171.            "the %s table...", szSVTblName);
  172.  
  173.     for ( i = 0; i < TblProps.iIndexes; i++)
  174.     {
  175.         // Only add the index if it is unique - such as a Paradox primary
  176.         //   index or certain dBASE indexes.
  177.         if (pIdxDesc[i].bUnique == TRUE)
  178.         {
  179.             // Initialize index descriptor.
  180.             pIdxDesc[i].bPrimary = FALSE;
  181.             pIdxDesc[i].bUnique = TRUE;
  182.             pIdxDesc[i].bDescending = FALSE;
  183.             pIdxDesc[i].bMaintained = TRUE;
  184.             pIdxDesc[i].bSubset = FALSE;
  185.             pIdxDesc[i].bExpIdx = FALSE;
  186.             pIdxDesc[i].iBlockSize = 0;
  187.             pIdxDesc[i].iRestrNum = 0;
  188.             pIdxDesc[i].iIndexId = iIndexNum;
  189.             strcpy(pIdxDesc[i].szTagName, "");
  190.             memset(szIndex, 0, DBIMAXNAMELEN);
  191.             wsprintf(szIndex, "Index%u",iIndexNum);
  192.             strcpy(pIdxDesc[i].szName, szIndex);
  193.             
  194.             rslt = DbiAddIndex(hDb, hCur, (pCHAR)szSVTblName,
  195.                                (pCHAR)BatTblDesc.szTblType, &pIdxDesc[i],
  196.                                NULL);
  197.             ChkRslt(rslt, "Add Index");
  198.             iIndexNum++;
  199.         }
  200.     }
  201.  
  202.     memset(&TblProps, 0, sizeof(CURProps));
  203.     rslt = DbiGetCursorProps(hCur, &TblProps);
  204.     ChkRslt(rslt, "GetCursorProps");
  205.  
  206.     rslt = DbiSetToBegin(hCur);
  207.     ChkRslt(rslt, "SetToBegin");
  208.  
  209.     Screen("\r\n    Displaying the first 10 records of the %s %s table...",
  210.            szSVTblName, BatTblDesc.szTblType);
  211.     DisplayTable(hCur, 10);
  212.  
  213.     Screen("\r\n    Close the %s table...", szUPTblName);
  214.     rslt = DbiCloseCursor(&hCurPX);
  215.     ChkRslt(rslt, "CloseCur");
  216.  
  217.     Screen("    Close the %s table...", szSVTblName);
  218.     rslt = DbiCloseCursor(&hCur);
  219.     ChkRslt(rslt, "CloseCur");
  220.  
  221.     Screen("    Delete the %s table...", szSVTblName);
  222.     rslt = DbiDeleteTable(hDb,(pCHAR) szSVTblName,
  223.                           (pCHAR)BatTblDesc.szTblType);
  224.     ChkRslt(rslt, "DeleteTable");
  225.  
  226.     Screen("    Close the databases and exit IDAPI...");
  227.     DbiCloseDatabase(&hDbPX);
  228.     CloseDbAndExit(&hDb);
  229.  
  230.     free(pIdxDesc);
  231.  
  232.     Screen("\r\n*** End of Example ***");
  233. }
  234.