home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / SQLDPN.ZIP / SQLDPND.SQC < prev   
Text File  |  1991-06-27  |  13KB  |  382 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*  MODULE NAME:        SQLDPND.SQC                                         */
  4. /*                                                                          */
  5. /*  DESCRIPTIVE NAME:   Display Dependencies for an SQL Statement           */
  6. /*                                                                          */
  7. /*  DEPENDENCIES:                                                           */
  8. /*     Program must be precompiled with the following options (creating     */
  9. /*     .C and .BND files):                                                  */
  10. /*                                                                          */
  11. /*         sqlprep sqldpnd.sqc dbname /b                                    */
  12. /*                                                                          */
  13. /*     To run sqldpnd.exe, the .BND file from the pre-compilation must      */
  14. /*     be in the same subdirectory as sqldpnd.exe.                          */
  15. /*                                                                          */
  16. /*  Changes:                                                                */
  17. /*   -  5/23/89:  Add the clause 'and dcreator = user' to select against    */
  18. /*                the system catalog 'sysibm.sysplandep'                    */
  19. /*                                                                          */
  20. /****************************************************************************/
  21.  
  22. /****************************************************************************/
  23. /*  INCLUDE FILES                                                           */
  24. /****************************************************************************/
  25.  
  26. #include <malloc.h>             /* Include files for C built-in functions    */
  27. #include <memory.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31.  
  32. /* The following #define says "include base os/2 calls" -- used in os2.h     */
  33. #define INCL_BASE
  34. #include <doscalls.h>           /* Include file for OS/2 function calls      */
  35. #include <os2.h>
  36.  
  37. #include <sql.h>                /* Database Manager System constants         */
  38. #include <sqlca.h>              /* sqlca                                     */
  39. #include <sqlcodes.h>           /* SQL return code constants                 */
  40. #include <sqlenv.h>             /* Database Environment Command interfaces   */
  41.  
  42. /****************************************************************************/
  43. /*  EXTERNAL DECLARATIONS                                                   */
  44. /****************************************************************************/
  45.  
  46. EXEC SQL INCLUDE SQLCA;         /* Define an sqlca for SQL return codes      */
  47.  
  48. EXEC SQL BEGIN DECLARE SECTION;
  49.   char sql_stmt[2000];          /* String to hold SQL statements             */
  50.   struct {
  51.            short int length;
  52.            char data[20];
  53.          } bname;
  54.   char bcreator[9];
  55.   char btype;
  56.   short int bname_len;
  57. EXEC SQL END DECLARE SECTION;
  58.  
  59. int rc = 0;                             /* Initialize return code to 0       */
  60. int ok;                                 /* flag for prompting for SQL stmt   */
  61. unsigned short usrc = 0;                /* Unsigned short return code        */
  62. int numrows;                            /* # of rows read from the table     */
  63. char dbname[20];                        /* Database name to connect to       */
  64. char getpswd[9];                        /* String to retrieve password       */
  65. char pw_ptr[9] = "\0\0\0\0\0\0\0\0\0";  /* Database password; init to null   */
  66. char stmtfile[20];                      /* file to contain SQL statement     */
  67. FILE *ptr_output;                       /* output file pointer               */
  68.  
  69. unsigned char pgmname[30];              /* Parameters for sqlabind call      */
  70.  
  71. unsigned char objnamebuf[20];           /* Parameters for DosExecPgm         */
  72. unsigned int objbuflen = 80;
  73. unsigned int execflags = 0;
  74. unsigned char arg_string[40];
  75. unsigned char pgm_string[13];
  76. unsigned char env[20];
  77. struct rtnstruct {
  78.          unsigned short termcode;
  79.          unsigned short exitcode;
  80.        };
  81. struct rtnstruct returncodes;
  82.  
  83. char deptype[10];                       /* String to hold 'dependency type'  */
  84. char blanks[25]="                        ";  /* blanks for output            */
  85. short int numblanks;                         /* number of blanks to output   */
  86.  
  87. #define BUFSIZE 512                     /* Buffer size for error messages    */
  88. unsigned char msgbuf[BUFSIZE];          /* Message buffer                    */
  89.  
  90. #define LONG_DISP_LEN 4000;             /* String Length used to display
  91.                                                 Long Varchar                 */
  92.  
  93. /****************************************************************************/
  94. /*  FUNCTION DECLARATIONS                                                   */
  95. /****************************************************************************/
  96.  
  97. int main();
  98. static int fetch(void);
  99.  
  100. /****************************************************************************/
  101. /*  MAIN PROCEDURE                                                          */
  102. /****************************************************************************/
  103. main()
  104.  
  105. {
  106. /*
  107. **  Install Ctrl+Break signal handler which ensures Ctrl+Break is
  108. **  processed correctly.
  109. */
  110.    sqleisig(&sqlca);
  111.  
  112. /*
  113. ** Prompt for Database name and Database Password
  114. */
  115.    printf("Enter database name: ");
  116.    gets(dbname);
  117.  
  118.    printf("Enter database password; (null 'Enter' for none): ");
  119.    gets(pw_ptr);
  120.  
  121. /*
  122. **  Start Using Database
  123. **  If there was an error, print a message and exit program.
  124. */
  125.    sqlestrd(dbname, pw_ptr, 'S', &sqlca);
  126.  
  127.    if (sqlca.sqlcode != 0)
  128.      {
  129.         printf("\nStart DB Error:  sqlcode = %ld.", sqlca.sqlcode);
  130.  
  131.         rc = sqlaintp(msgbuf,BUFSIZE,0,&sqlca);
  132.  
  133.         if (rc < 0)               /* message retrieve err*/
  134.         {
  135.             printf ("\nSQLAINTP ERROR. Return code = %d",rc);
  136.         }
  137.  
  138.         if (rc > 0)                /* error message return*/
  139.         {
  140.             printf ("%s\n",msgbuf);
  141.         }
  142.      }
  143.  
  144. /*
  145. **  No errors from "Start Using Database".
  146. */
  147.    else
  148.      {
  149. /*
  150. **  Bind this program to the user's database
  151. **  (Assume that the .bnd file for this program is in the current directory)
  152. */
  153.       strcpy ( pgmname, "sqldpnd.bnd" );
  154.       rc = sqlabind ( pgmname, dbname, pw_ptr, "$$err.msg","0", &sqlca );
  155.       if (rc < 0)
  156.       {
  157.          printf ("\nError binding program sqldpnd.bnd.  Sqlcode = %ld\n",
  158.                   sqlca.sqlcode);
  159.          goto exit;
  160.       }
  161.  
  162. /*
  163. **   Prompt for the first SQL statement
  164. */
  165.  
  166.      do
  167.      {
  168.          ok = 1;
  169.          printf("\n\nSQL stmt: ");
  170.          gets(sql_stmt);
  171.          if ( sql_stmt[0] != 's' && sql_stmt[0] != 'S' && sql_stmt[0] != 0 )
  172.          {
  173.             printf("SQL statement must be a SELECT -- try again.");
  174.             ok = 0;
  175.          }
  176.      } while ( ok != 1 );
  177.  
  178. /*
  179. **   Go into a loop where an SQL statement is prepared
  180. **   and executed and another statement is read in.
  181. **   Loop as long as the user keys in more SQL statements.
  182. */
  183.      while (sql_stmt[0] != 0)
  184.        {
  185.  
  186. /*
  187. **   Write the SQL statement into a file  -- in the form of a
  188. **   DECLARE CURSOR statement.
  189. */
  190.          strcpy ( stmtfile, "$$stmt$$.sqc" );
  191.          if ((ptr_output = fopen(stmtfile,"w")) == NULL)
  192.          {
  193.            printf ("\nUnable to open output file.");
  194.            goto exit;
  195.          }
  196.  
  197.          fprintf (ptr_output,
  198.                   "EXEC "
  199.                   "SQL "
  200.                   "DECLARE C1 CURSOR FOR \n"
  201.                   " %s;\n",
  202.                   sql_stmt );
  203.  
  204.          fclose (ptr_output);
  205.  
  206. /*
  207. **  Precompile the "stmt" file:
  208. **  - set up the parameters for DosExecPgm
  209. **  - execute the 'sqlprep' command
  210. */
  211.  
  212.          objnamebuf[0] = '\0';
  213.          strcpy ( arg_string, "sqlprep.exe" );
  214.          arg_string[11] = '\0';
  215.          sprintf ( &arg_string[12],
  216.                    "$$STMT$$.SQC %s",
  217.                    dbname );
  218.          env[0] = '\0';
  219.          strcpy ( pgm_string, "sqlprep.exe" );
  220.  
  221.          usrc = DosExecPgm ( objnamebuf,
  222.                             objbuflen,
  223.                             execflags,
  224.                             (char *) arg_string,
  225.                             env,
  226.                             (PRESULTCODES) &returncodes,
  227.                             pgm_string );
  228.          if (usrc < 0 || returncodes.termcode != 0)
  229.          {
  230.            printf ("\nError from DosExecPgm:"
  231.                    "\n rc = %d\n termcode = %d\n resultcode = %d",
  232.                     usrc,
  233.                     returncodes.termcode,
  234.                     returncodes.exitcode );
  235.            goto exit;
  236.          }
  237.  
  238. /*
  239. **   Retrieve the dependences for this SQL statement from the
  240. **   SYSIBM.SYSPLANDEP table.
  241. */
  242.          EXEC SQL
  243.             DECLARE depcur CURSOR FOR
  244.             SELECT BCREATOR, BNAME, BTYPE, LENGTH(BNAME)
  245.             FROM SYSIBM.SYSPLANDEP
  246.             WHERE DNAME = '$$STMT$$'
  247.                   AND DCREATOR = USER;
  248.  
  249.          printf("\nThe SQL statement is dependent on:");
  250.          printf("\n\nAuthid    Object_Name         Object Type");
  251.          printf("\n---------------------------------------");
  252.  
  253.          numrows = 0;
  254.          EXEC SQL OPEN depcur;
  255.  
  256.          if (sqlca.sqlcode == 0)
  257.          {
  258.             for (sqlca.sqlcode = 0; sqlca.sqlcode == 0; )
  259.             {
  260.                EXEC SQL
  261.                FETCH depcur INTO :bcreator, :bname, :btype, :bname_len;
  262.  
  263.                if (sqlca.sqlcode == 0)
  264.                {
  265.                   switch (btype)
  266.                   {
  267.                      case 'I':
  268.                        strcpy (deptype, "Index");
  269.                        break;
  270.                     case 'T':
  271.                        strcpy (deptype, "Table");
  272.                        break;
  273.                     case 'V':
  274.                        strcpy (deptype, "View");
  275.                        break;
  276.                   }
  277.  
  278.                   numrows = numrows + 1;
  279.  
  280.                   numblanks = 18 - bname_len;
  281.                   printf( "\n%-.8s  %-.*s%-.*s   %s",
  282.                            bcreator,
  283.                            bname.length, bname.data,
  284.                            numblanks, blanks,
  285.                            deptype );
  286.  
  287.                } /* end: no error from fetch  */
  288.             }  /* end:  fetch loop  */
  289.          }  /* end: no error from open cursor  */
  290.  
  291.          if (sqlca.sqlcode == SQL_RC_W100)
  292.          {
  293.              if(numrows == 0)
  294.              {
  295.                printf( "\nSorry Charlie ... there are no records "
  296.                         "that satisfy this query.\n");
  297.              }
  298.              EXEC SQL CLOSE depcur;
  299.          }
  300. /*
  301. **   If the return code from executing the SQL statement
  302. **   indicated an error, print a message.
  303. */
  304.          if (sqlca.sqlcode != 0)
  305.          {
  306.            printf("\nError:  sqlcode = %ld.\n", sqlca.sqlcode);
  307.            rc = sqlaintp(msgbuf,BUFSIZE,0,&sqlca);
  308.  
  309.            if (rc < 0)               /* message retrieve err*/
  310.            {
  311.                printf ("SQLAINTP ERROR. Return code = %d\n",rc);
  312.            }
  313.  
  314.            if (rc > 0)                /* error message return*/
  315.            {
  316.                printf ("%s\n",msgbuf);
  317.            }
  318.          }
  319.  
  320. /*
  321. **  Commit the above actions to release locks on access plan name.
  322. */
  323.          EXEC SQL COMMIT;
  324.          if (sqlca.sqlcode != 0)
  325.          {
  326.             printf("\nError on COMMIT -- sqlcode = %ld", sqlca.sqlcode);
  327.          }
  328.  
  329. /*
  330. **  Prompt for another SQL statement.
  331. */
  332.          do
  333.          {
  334.             ok = 1;
  335.             printf("\n\n\nSQL stmt: ");
  336.             gets(sql_stmt);
  337.             if ( sql_stmt[0] != 's' && sql_stmt[0] != 'S' && sql_stmt[0] != 0 )
  338.             {
  339.                printf("SQL statement must be a SELECT -- try again.");
  340.                ok = 0;
  341.             }
  342.          } while ( ok != 1 && sql_stmt[0] !=0 );
  343.  
  344. /*
  345. ** End WHILE(another SQL statement) loop
  346. */
  347.        }
  348.  
  349. /*
  350. **  Drop the $$stmt$$ program and delete the
  351. **  $$stmt$$.sqc, $$stmt$$.c and $$err.msg files.
  352. */
  353.        EXEC SQL DROP PROGRAM $$STMT$$;
  354.        if (sqlca.sqlcode != 0)
  355.        {
  356.           printf("\nCould not drop access plan '$$stmt$$' - sqlcode = %ld",
  357.                   sqlca.sqlcode );
  358.        }
  359.  
  360.        if ((rc = unlink("$$stmt$$.sqc")) != 0 )
  361.        {
  362.           printf("\nCould not delete file:  $$stmt$$.sqc");
  363.        }
  364.        if ((rc = unlink("$$stmt$$.c")) != 0 )
  365.        {
  366.           printf("\nCould not delete file:  $$stmt$$.c");
  367.        }
  368.  
  369.  
  370. /*
  371. **  Exit program if error.
  372. */
  373.  
  374. exit:
  375. /*
  376. **  Stop Using Database and return.
  377. */
  378.      sqlestpd(&sqlca);
  379.      }
  380.   return(rc);
  381. }
  382.