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

  1. // BDE32 3.x - (C) Copyright 1996 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;           // Index pointer
  37.     int         i;                  // 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"
  68.                " databases");
  69.         CloseDbAndExit(&hDb);
  70.         Screen("\r\n*** End of Example ***");
  71.         return;
  72.     }
  73.  
  74.     // Open a Standard database for the local table
  75.     rslt = DbiOpenDatabase(NULL, NULL, dbiREADWRITE, dbiOPENSHARED,
  76.                            NULL, 0, NULL, NULL, &hDbPX);
  77.     if (ChkRslt(rslt, "OpenDatabase") != DBIERR_NONE)
  78.     {
  79.         CloseDbAndExit(&hDb);
  80.         Screen("\r\n*** End of Example ***");
  81.         return;
  82.     }
  83.  
  84.     Screen("    Setting the database directory for the Standard database...");
  85.     rslt = DbiSetDirectory(hDbPX, (pCHAR) szTblDirectory);
  86.     ChkRslt(rslt, "SetDirectory");
  87.  
  88.     Screen("    Open the %s %s table which will be copied to the\r\n"
  89.            "        %s server...", szUPTblName, szUPTblType,
  90.            BatTblDesc.szTblType);
  91.  
  92.     rslt = DbiOpenTable(hDbPX, (pCHAR) szUPTblName, (pCHAR) szUPTblType,
  93.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  94.                         xltFIELD, FALSE, NULL, &hCurPX);
  95.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  96.     {
  97.         DbiCloseDatabase(&hDbPX);
  98.         CloseDbAndExit(&hDb);
  99.         Screen("\r\n*** End of Example ***");
  100.         return;
  101.     }
  102.  
  103.     rslt = DbiSetToBegin(hCurPX);
  104.     ChkRslt(rslt, "SetToBegin");
  105.  
  106.     Screen("\r\n    Copying the %s table to the server...",
  107.            szUPTblName);
  108.  
  109.     BatTblDesc.hDb = hDb;
  110.     strcpy(BatTblDesc.szTblName, (pCHAR)szSVTblName);
  111.     
  112.     rslt = DbiBatchMove(NULL, hCurPX, &BatTblDesc, NULL, batCOPY, NULL,
  113.                         NULL, NULL, NULL, NULL, NULL, NULL,
  114.                         NULL, NULL, NULL, NULL, TRUE, TRUE,
  115.                         NULL, TRUE);
  116.     if(ChkRslt(rslt, "Batch Move") != DBIERR_NONE)
  117.     {
  118.         rslt = DbiCloseCursor(&hCurPX);
  119.         ChkRslt(rslt, "CloseCur");
  120.         DbiCloseDatabase(&hDbPX);
  121.         CloseDbAndExit(&hDb);
  122.         Screen("\r\n*** End of Example ***");
  123.         return;
  124.     }
  125.  
  126.     Screen("    Open the %s %s table...", szSVTblName, BatTblDesc.szTblType);
  127.     rslt = DbiOpenTable(hDb, (pCHAR) szSVTblName,
  128.                         (pCHAR)BatTblDesc.szTblType, NULL, NULL, 0,
  129.                         dbiREADWRITE, dbiOPENEXCL, xltFIELD, FALSE, NULL,
  130.                         &hCur);             
  131.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  132.     {
  133.         rslt = DbiDeleteTable(hDb,(pCHAR) szSVTblName,
  134.                               (pCHAR)BatTblDesc.szTblType);
  135.         ChkRslt(rslt, "DeleteTable");
  136.         rslt = DbiCloseCursor(&hCurPX);
  137.         ChkRslt(rslt, "CloseCur");
  138.         DbiCloseDatabase(&hDbPX);
  139.         CloseDbAndExit(&hDb);
  140.         Screen("\r\n*** End of Example ***");
  141.         return;
  142.     }
  143.  
  144.     //  Allocate a buffer for the index descriptors.
  145.     rslt = DbiGetCursorProps(hCurPX, &TblProps);
  146.     ChkRslt(rslt, "GetCursorProps");
  147.  
  148.     pIdxDesc = (pIDXDesc)malloc(TblProps.iIndexes * sizeof(IDXDesc));
  149.     if ((pIdxDesc == NULL) && (TblProps.iIndexes != 0))
  150.     {
  151.         rslt = DbiCloseCursor(&hCur);
  152.         ChkRslt(rslt, "CloseCur");
  153.         rslt = DbiDeleteTable(hDb,(pCHAR) szSVTblName,
  154.                               (pCHAR)BatTblDesc.szTblType);
  155.         ChkRslt(rslt, "DeleteTable");
  156.         rslt = DbiCloseCursor(&hCurPX);
  157.         ChkRslt(rslt, "CloseCur");
  158.         DbiCloseDatabase(&hDbPX);
  159.         CloseDbAndExit(&hDb);
  160.         Screen("\r\n*** End of Example ***");
  161.         return;
  162.     }
  163.  
  164.     // Check if there are indexes on the table.
  165.     if (TblProps.iIndexes != 0)
  166.     {
  167.         rslt = DbiGetIndexDescs(hCurPX, pIdxDesc);
  168.         ChkRslt(rslt, "Get Index Descs");
  169.     }
  170.  
  171.     Screen("    Add the Unique indexes found in the original table to "
  172.            "the %s table...", szSVTblName);
  173.  
  174.     for ( i = 0; i < TblProps.iIndexes; i++)
  175.     {
  176.         // Only add the index if it is unique - such as a Paradox primary
  177.         // index or certain dBASE indexes.
  178.         if (pIdxDesc[i].bUnique == TRUE)
  179.         {
  180.             // Initialize index descriptor
  181.             pIdxDesc[i].bPrimary = FALSE;
  182.             pIdxDesc[i].bUnique = TRUE;
  183.             pIdxDesc[i].bDescending = FALSE;
  184.             pIdxDesc[i].bMaintained = TRUE;
  185.             pIdxDesc[i].bSubset = FALSE;
  186.             pIdxDesc[i].bExpIdx = FALSE;
  187.             pIdxDesc[i].iBlockSize = 0;
  188.             pIdxDesc[i].iRestrNum = 0;
  189.             pIdxDesc[i].iIndexId = iIndexNum;
  190.             strcpy(pIdxDesc[i].szTagName, "");
  191.             memset(szIndex, 0, DBIMAXNAMELEN);
  192.             wsprintf(szIndex, "Index%u",iIndexNum);
  193.             strcpy(pIdxDesc[i].szName, szIndex);
  194.             
  195.             rslt = DbiAddIndex(hDb, hCur, (pCHAR)szSVTblName,
  196.                                (pCHAR)BatTblDesc.szTblType, &pIdxDesc[i],
  197.                                NULL);
  198.             ChkRslt(rslt, "Add Index");
  199.             iIndexNum++;
  200.         }
  201.     }
  202.  
  203.     memset(&TblProps, 0, sizeof(CURProps));
  204.     rslt = DbiGetCursorProps(hCur, &TblProps);
  205.     ChkRslt(rslt, "GetCursorProps");
  206.  
  207.     rslt = DbiSetToBegin(hCur);
  208.     ChkRslt(rslt, "SetToBegin");
  209.  
  210.     Screen("\r\n    Displaying the first 10 records of the %s %s table...",
  211.            szSVTblName, BatTblDesc.szTblType);
  212.     DisplayTable(hCur, 10);
  213.  
  214.     Screen("\r\n    Close the %s table...", szUPTblName);
  215.     rslt = DbiCloseCursor(&hCurPX);
  216.     ChkRslt(rslt, "CloseCur");
  217.  
  218.     Screen("    Close the %s table...", szSVTblName);
  219.     rslt = DbiCloseCursor(&hCur);
  220.     ChkRslt(rslt, "CloseCur");
  221.  
  222.     Screen("    Delete the %s table...", szSVTblName);
  223.     rslt = DbiDeleteTable(hDb,(pCHAR) szSVTblName,
  224.                           (pCHAR)BatTblDesc.szTblType);
  225.     ChkRslt(rslt, "DeleteTable");
  226.  
  227.     Screen("    Close the databases and exit IDAPI...");
  228.     DbiCloseDatabase(&hDbPX);
  229.     CloseDbAndExit(&hDb);
  230.  
  231.     free(pIdxDesc);
  232.  
  233.     Screen("\r\n*** End of Example ***");
  234. }
  235.