home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / iapp300.zip / SAMPLES / DB2SQL.C next >
C/C++ Source or Header  |  1995-03-01  |  6KB  |  171 lines

  1. /************************************************************************/
  2. /*                    DB2/2 SQL Sample Program                                            */
  3. /************************************************************************/
  4. /* Function        Display the names of all DB2/2 system tables.                 */
  5. /* Author        (c) Copyright Infoline AG 1995                                    */
  6. /*                    Schaffhauserstrasse 121                                                */
  7. /*                    CH-8302 Kloten - Switzerland                                        */
  8. /*                    Phone: +41 1 803 07 06 / Fax: +41 1 881 17 55                */
  9. /* History        V3.00 01/Mar/1995 Andy Brunner    Initial program version    */
  10. /************************************************************************/
  11.  
  12. #define    INCL_BASE
  13. #define    USER_OS2
  14.  
  15. #include "user.h"                                /* Include the necessary files    */
  16. #include "iapp.h"                                /* Include IAPP user file            */
  17.  
  18. /*----------------------------------------------------------------------*/
  19. /*    Function prototypes                                                                    */
  20. /*----------------------------------------------------------------------*/
  21.  
  22. VOID TerminateProgram(PUCHAR pszErrorMessage);
  23.  
  24. /*======================================================================*/
  25. /*    Function: main                                                                             */ 
  26. /*======================================================================*/
  27.  
  28. SHORT main(SHORT sArgCounter, PUCHAR *pszArgVariables)
  29. {
  30.     /*-------------------------------------------------------------------*/
  31.     /* Automatic variables                                                                */
  32.     /*-------------------------------------------------------------------*/
  33.  
  34.     typedef struct
  35.     {
  36.         UCHAR        szCreator[9];
  37.         UCHAR        szName[20];
  38.         UCHAR        szType[2];
  39.         UCHAR        szRemarks[41];
  40.     } RESULT_TABLE;
  41.  
  42.     typedef RESULT_TABLE FAR *P_RESULT_TABLE;
  43.  
  44.     P_RESULT_TABLE        pxResultTable                        = NULL;
  45.  
  46.     PUCHAR                pszSqlMessage                        = NULL;
  47.     PUCHAR                pszSqlStatement                    = NULL;
  48.  
  49.     ULONG                    ulRowsProcessed                    = 0L;
  50.  
  51.     /*-------------------------------------------------------------------*/
  52.     /* Display program title                                                            */
  53.     /*-------------------------------------------------------------------*/
  54.  
  55.     printf("DB2/2 SQL Sample Program Version 3.00 - (c) Copyright Infoline AG 1995\n");
  56.     printf("──────────────────────────────────────────────────────────────────────\n");
  57.     printf("\n");
  58.  
  59.     /*-------------------------------------------------------------------*/
  60.     /* Check program arguments                                                            */
  61.     /*-------------------------------------------------------------------*/
  62.  
  63.     if (sArgCounter != 2)
  64.         TerminateProgram("Usage: DB2SQL DatabaseName");
  65.  
  66.     /*-------------------------------------------------------------------*/
  67.     /* Allocate memory for data structure                                            */
  68.     /*-------------------------------------------------------------------*/
  69.  
  70.     pszSqlMessage        = IAppAllocMemory(512, 0);
  71.     pszSqlStatement    = IAppAllocMemory(32768, 0);
  72.     pxResultTable        = IAppAllocMemory(sizeof(RESULT_TABLE), 0);
  73.  
  74.     /*-------------------------------------------------------------------*/
  75.     /* Open the database                                                                    */
  76.     /*-------------------------------------------------------------------*/
  77.  
  78.     if (IAppSqlOpen(pszArgVariables[1]) != 0L)
  79.         TerminateProgram(IAppSqlMessage(pszSqlMessage));
  80.  
  81.     /*-------------------------------------------------------------------*/
  82.     /* Display column title and prepare dynamic SQL statement                */
  83.     /*-------------------------------------------------------------------*/
  84.  
  85.     printf("Creator  Name               Type  Remarks\n");
  86.     printf("──────────────────────────────────────────────────────────────────────────\n");
  87.  
  88.     strcpy(pszSqlStatement, "SELECT Creator, Name, Type, SUBSTR(Remarks,1,40) "\
  89.                                     "FROM Sysibm.Systables "\
  90.                                     "WHERE Type = 'T' "\
  91.                                     "ORDER BY Creator, Name");
  92.  
  93.     /*-------------------------------------------------------------------*/
  94.     /* Execute SQL statement to display all DB2/2 system tables                */
  95.     /*-------------------------------------------------------------------*/
  96.  
  97.     for (;;)
  98.     {
  99.         /*----------------------------------------------------------------*/
  100.         /* Fetch next row in result table                                            */
  101.         /*----------------------------------------------------------------*/
  102.  
  103.         if (IAppSqlExec(1, pszSqlStatement, pxResultTable, 1L, &ulRowsProcessed) != 0L)
  104.             TerminateProgram(IAppSqlMessage(pszSqlMessage));
  105.  
  106.         if (ulRowsProcessed == 0L)
  107.             break;
  108.  
  109.         /*----------------------------------------------------------------*/
  110.         /* Format and display the fetched row                                        */
  111.         /*----------------------------------------------------------------*/
  112.  
  113.         printf("%-8s %-18s %-5s %-40s\n",
  114.             pxResultTable->szCreator,
  115.             pxResultTable->szName,
  116.             (pxResultTable->szType[0] == 'T') ? "Table" : "View",
  117.             pxResultTable->szRemarks);
  118.     }
  119.  
  120.     printf("\n");
  121.  
  122.     /*-------------------------------------------------------------------*/
  123.     /* Commit the transaction                                                            */
  124.     /*-------------------------------------------------------------------*/
  125.  
  126.     if (IAppSqlCommit() != 0L)
  127.         TerminateProgram(IAppSqlMessage(pszSqlMessage));
  128.  
  129.     /*-------------------------------------------------------------------*/
  130.     /* Close the database                                                                */
  131.     /*-------------------------------------------------------------------*/
  132.  
  133.     if (IAppSqlClose() != 0L)
  134.         TerminateProgram(IAppSqlMessage(pszSqlMessage));
  135.  
  136.     /*-------------------------------------------------------------------*/
  137.     /* Free the memory of the data structures                                        */
  138.     /*-------------------------------------------------------------------*/
  139.  
  140.     IAppFreeMemory(pszSqlMessage);
  141.     IAppFreeMemory(pszSqlStatement);
  142.     IAppFreeMemory(pxResultTable);
  143.  
  144.     pszSqlMessage        = NULL;
  145.     pszSqlStatement    = NULL;
  146.     pxResultTable        = NULL;
  147.  
  148.     /*-------------------------------------------------------------------*/
  149.     /* Return to the caller                                                                */
  150.     /*-------------------------------------------------------------------*/
  151.  
  152.     return (0);
  153. }
  154.  
  155. /*======================================================================*/
  156. /*    Function: TerminateProgram                                                             */ 
  157. /*======================================================================*/
  158.  
  159. VOID TerminateProgram(PUCHAR pszErrorMessage)
  160. {
  161.     /*-------------------------------------------------------------------*/
  162.     /* Display error message and terminate program                                */
  163.     /*-------------------------------------------------------------------*/
  164.  
  165.     puts(pszErrorMessage);
  166.  
  167.     exit(4);
  168. }
  169.  
  170.  
  171.