home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / DBREOR.ZIP / DBMREORG.CMD
OS/2 REXX Batch file  |  1991-09-21  |  12KB  |  368 lines

  1. /************************************************************************/
  2. /*                    OS/2 DBM Reorganization Utility                                     */
  3. /************************************************************************/
  4. /* Function        This procedure reorganizes all tables in the specified    */
  5. /*                    database. The RUNSTATS command is also executed.            */
  6. /* Author        (c) Copyright Infoline AG 1991                                    */
  7. /*                    Unterrietstrasse 6                                                    */
  8. /*                    CH-8152 Glattbrugg - Switzerland                                    */
  9. /*                    Phone: ++41 1 810 97 77                                                */
  10. /*                    Fax:   ++41 1 810 97 79                                                */
  11. /* History        V1.00 21/Sep/1991 Andy Brunner    Initial program version    */
  12. /************************************************************************/
  13.  
  14. /*======================================================================*/
  15. /*    Initialize variables                                                                      */
  16. /*======================================================================*/
  17.  
  18. tempfile = 'DBMReorg.$$$'
  19. temppath = 'C:\'
  20.  
  21. /*======================================================================*/
  22. /*    Start function                                                                              */
  23. /*======================================================================*/
  24.  
  25. StartFunction:
  26.  
  27.     /*-------------------------------------------------------------------*/
  28.     /* Write title lines                                                                    */
  29.     /*-------------------------------------------------------------------*/
  30.  
  31.     say
  32.     say 'OS/2 DBM Reorganization Utility V1.00 (c) Copyright Infoline AG 1991'
  33.      say '--------------------------------------------------------------------'
  34.     say
  35.  
  36.     /*-------------------------------------------------------------------*/
  37.     /* Get command line arguments                                                        */
  38.     /*-------------------------------------------------------------------*/
  39.  
  40.     parse arg dbname argx
  41.  
  42.     if dbname = '' | argx <> '' then do
  43.         say 'Usage: DBMReorg DatabaseName'
  44.         signal ExitProgram
  45.     end
  46.  
  47.     /*-------------------------------------------------------------------*/
  48.     /* Open database                                                                        */
  49.     /*-------------------------------------------------------------------*/
  50.  
  51.     call OpenDatabase dbname
  52.  
  53.     /*-------------------------------------------------------------------*/
  54.     /* Write temporary file with tablename and indexname                         */
  55.     /*-------------------------------------------------------------------*/
  56.  
  57.     '@if exist' tempfile 'del' tempfile
  58.  
  59.     call OpenCursor 1, 'SELECT creator, name FROM sysibm.systables WHERE type = ''T'' AND creator <> ''SYSIBM'' ORDER BY creator, name'
  60.  
  61.     do while (FetchCursor(1, ':tablecreator, :tablename') = 0)
  62.  
  63.         /*----------------------------------------------------------------*/
  64.         /* Locate all corresponding indexes for the found tables                */
  65.         /*----------------------------------------------------------------*/
  66.         
  67.         call OpenCursor 2, 'SELECT creator, name, uniquerule FROM sysibm.sysindexes WHERE tbcreator = ''' || strip(tablecreator) || ''' AND tbname = ''' || strip(tablename) || ''' ORDER BY uniquerule'
  68.     
  69.         do while (FetchCursor(2, ':indexcreator, :indexname, :indextype') = 0)
  70.  
  71.             if indextype = 'P' then
  72.                 leave
  73.  
  74.             if indextype = 'U' then
  75.                 leave
  76.         end
  77.  
  78.         select
  79.             when indextype = 'P' then
  80.                 indexrule = 'Primary'
  81.             when indextype = 'U' then
  82.                 indexrule = 'Unique'
  83.             when indextype = 'D' then
  84.                 indexrule = 'Duplicates'
  85.             otherwise
  86.                 indexrule = 'N/A'
  87.         end
  88.  
  89.         call CloseCursor 2 
  90.  
  91.         call lineout tempfile, tablename tablecreator indexrule indexname indexcreator
  92.  
  93.     end
  94.  
  95.     call CloseCursor 1 
  96.  
  97.     call lineout tempfile
  98.  
  99.     /*-------------------------------------------------------------------*/
  100.     /* Commit SELECT statements                                                        */
  101.     /*-------------------------------------------------------------------*/
  102.  
  103.     call ExecSQL 'COMMIT'
  104.  
  105.     /*-------------------------------------------------------------------*/
  106.     /* Read temporary file and reorganize tables and perform RUNSTATS        */
  107.     /*-------------------------------------------------------------------*/
  108.  
  109.     say 'TableName          Creator  IndexRule      IndexName          Creator'
  110.     say '---------------------------------------------------------------------'
  111.  
  112.     do while lines(tempfile) > 0
  113.  
  114.         inputline = linein(tempfile)
  115.  
  116.         parse var inputline tablename tablecreator indexrule indexname indexcreator
  117.  
  118.         say substr(tablename, 1, 18) substr(tablecreator, 1, 8) substr(indexrule, 1, 14) substr(indexname, 1, 18) substr(indexcreator, 1, 8)
  119.  
  120.         call ExecSQL 'LOCK TABLE' strip(tablecreator) || '.' || strip(tablename) 'IN EXCLUSIVE MODE'
  121.  
  122.         if indexrule = 'N/A' then
  123.             call ExecDBS 'REORG TABLE' strip(tablecreator) || '.' || strip(tablename) 'IN' dbname 'USE' temppath
  124.         else
  125.             call ExecDBS 'REORG TABLE' strip(tablecreator) || '.' || strip(tablename) 'IN' dbname 'INDEX' strip(indexcreator) || '.' || strip(indexname) 'USE' temppath
  126.  
  127.         if indexrule = 'N/A' then
  128.             call ExecDBS 'RUNSTATS ON TABLE' strip(tablecreator) || '.' || strip(tablename)
  129.         else
  130.             call ExecDBS 'RUNSTATS ON TABLE' strip(tablecreator) || '.' || strip(tablename) 'AND INDEXES ALL SHRLEVEL REFERENCE'
  131.  
  132.         call ExecSQL 'COMMIT'
  133.  
  134.     end
  135.  
  136.     call lineout tempfile
  137.  
  138.  
  139.     '@del' tempfile
  140.  
  141.     /*-------------------------------------------------------------------*/
  142.     /* Close database                                                                        */
  143.     /*-------------------------------------------------------------------*/
  144.  
  145.     call CloseDatabase
  146.  
  147. ExitProgram:
  148.  
  149.     /*-------------------------------------------------------------------*/
  150.     /* Terminate program                                                                    */
  151.     /*-------------------------------------------------------------------*/
  152.  
  153.     exit
  154.     
  155. SqlError:
  156.  
  157.     /*-------------------------------------------------------------------*/
  158.     /* Display SQL error message                                                        */
  159.     /*-------------------------------------------------------------------*/
  160.  
  161.     say SQLMSG
  162.     signal ExitProgram
  163.  
  164. /*======================================================================*/
  165. /*    Open the database                                                                          */
  166. /*======================================================================*/
  167.  
  168. OpenDatabase:
  169.  
  170.     /*-------------------------------------------------------------------*/
  171.     /* Add the REXX SQL interface functions                                          */
  172.     /*-------------------------------------------------------------------*/
  173.  
  174.     if RxFuncQuery('SQLDBS') <> 0 then do
  175.         if RxFuncAdd('SQLDBS','SQLAR','SQLDBS') <> 0 then do
  176.             say 'Error: Unable to load REXX SQL interface function SQLDBS.'
  177.             signal ExitProgram
  178.         end
  179.     end
  180.  
  181.     if RxFuncQuery('SQLEXEC') <> 0 then do
  182.         if RxFuncAdd('SQLEXEC','SQLAR','SQLEXEC') then do
  183.             say 'Error: Unable to load REXX SQL interface function SQLEXEC.'
  184.             signal ExitProgram
  185.             end
  186.         end
  187.  
  188.     /*-------------------------------------------------------------------*/
  189.     /* Start Database Manager                                                            */
  190.     /*-------------------------------------------------------------------*/
  191.  
  192.     call SQLDBS 'START DATABASE MANAGER'
  193.  
  194.     if SQLCA.SQLCODE <> -1026 & SQLCA.SQLCODE <> 0 then
  195.         signal SqlError
  196.  
  197.     /*-------------------------------------------------------------------*/
  198.     /* Open the database                                                                    */
  199.     /*-------------------------------------------------------------------*/
  200.  
  201.     do forever
  202.  
  203.         call SQLDBS 'START USING DATABASE' arg(1)
  204.  
  205.         if SQLCA.SQLCODE <> 0 & SQLCA.SQLCODE <> -1015 then
  206.             signal SqlError
  207.  
  208.         if SQLCA.SQLCODE = 0 then
  209.             leave
  210.  
  211.         if SQLCA.SQLCODE = -1015 then do
  212.  
  213.             call SQLDBS 'RESTART DATABASE' arg(1)
  214.  
  215.             if SQLCA.SQLCODE <> 0 then
  216.                 signal SqlError
  217.     
  218.             iterate
  219.  
  220.         end
  221.  
  222.     end
  223.  
  224.     return result
  225.         
  226. /*======================================================================*/
  227. /*    Close the database                                                                       */
  228. /*======================================================================*/
  229.  
  230. CloseDatabase:
  231.  
  232.     /*-------------------------------------------------------------------*/
  233.     /* Close the database                                                                */
  234.     /*-------------------------------------------------------------------*/
  235.  
  236.     call SQLDBS 'STOP USING DATABASE'
  237.  
  238.     if SQLCA.SQLCODE <> 0 then
  239.         signal SqlError
  240.  
  241.     /*-------------------------------------------------------------------*/
  242.     /* Stop Database Manager                                                            */
  243.     /*-------------------------------------------------------------------*/
  244.  
  245.     call SQLDBS 'STOP DATABASE MANAGER'
  246.  
  247.     if SQLCA.SQLCODE <> -1025 & SQLCA.SQLCODE <> 0 then
  248.         signal SqlError
  249.  
  250.     /*-------------------------------------------------------------------*/
  251.     /* Drop the REXX SQL interface functions                                        */
  252.     /*-------------------------------------------------------------------*/
  253.  
  254.     if RxFuncQuery('SQLDBS') == 0 then do
  255.         if RxFuncDrop('SQLDBS') <> 0 then do
  256.             say 'Error: Unable to drop REXX SQL interface function SQLDBS.'
  257.             signal ExitProgram
  258.         end
  259.     end
  260.  
  261.     if RxFuncQuery('SQLEXEC') == 0 then do
  262.         if RxFuncDrop('SQLEXEC') <> 0 then do
  263.             say 'Error: Unable to drop REXX SQL interface function SQLEXEC.'
  264.             signal ExitProgram
  265.             end
  266.         end
  267.  
  268.     return result
  269.         
  270. /*======================================================================*/
  271. /*    Execute Database Services command                                                   */
  272. /*======================================================================*/
  273.  
  274. ExecDBS:
  275.  
  276.     call SQLDBS arg(1)
  277.  
  278.     if SQLCA.SQLCODE <> 0 then
  279.         signal SqlError
  280.  
  281.     return result
  282.  
  283. /*======================================================================*/
  284. /*    Execute SQL statement (Non-SELECT)                                                  */
  285. /*======================================================================*/
  286.  
  287. ExecSQL:
  288.  
  289.     SqlStmt = arg(1)
  290.  
  291.     call SQLEXEC 'EXECUTE IMMEDIATE :SqlStmt'
  292.  
  293.     if SQLCA.SQLCODE <> 0 then
  294.         signal SqlError
  295.  
  296.     return result
  297.  
  298. /*======================================================================*/
  299. /*    Open SQL cursor                                                                           */
  300. /*======================================================================*/
  301.  
  302. OpenCursor:
  303.  
  304.     /*-------------------------------------------------------------------*/
  305.     /* Declare the cursor                                                                */
  306.     /*-------------------------------------------------------------------*/
  307.  
  308.     call SQLEXEC 'DECLARE c' || arg(1) 'CURSOR FOR s' || arg(1)
  309.  
  310.     if SQLCA.SQLCODE <> 0 then
  311.         signal SqlError
  312.  
  313.     /*-------------------------------------------------------------------*/
  314.     /* Prepare the SQL statement                                                        */
  315.     /*-------------------------------------------------------------------*/
  316.  
  317.     SqlStmt = arg(2)
  318.  
  319.     call SQLEXEC 'PREPARE s' || arg(1) 'FROM :SqlStmt'
  320.  
  321.     if SQLCA.SQLCODE <> 0 then
  322.         signal SqlError
  323.  
  324.     /*-------------------------------------------------------------------*/
  325.     /* Open the cursor                                                                    */
  326.     /*-------------------------------------------------------------------*/
  327.  
  328.     call SQLEXEC 'OPEN c' || arg(1)
  329.  
  330.     if SQLCA.SQLCODE <> 0 then
  331.         signal SqlError
  332.  
  333.     return result
  334.  
  335. /*======================================================================*/
  336. /*    Fetch SQL cursor                                                                           */
  337. /*======================================================================*/
  338.  
  339. FetchCursor:
  340.  
  341.     call SQLEXEC 'FETCH c' || arg(1) 'INTO' arg(2)
  342.     
  343.     if SQLCA.SQLCODE <> 0 & SQLCA.SQLCODE <> 100 then 
  344.         signal SqlError
  345.  
  346.     if SQLCA.SQLCODE = 0 then
  347.         return 0
  348.     else
  349.         return 1
  350.  
  351. /*======================================================================*/
  352. /*    Close SQL cursor                                                                           */
  353. /*======================================================================*/
  354.  
  355. CloseCursor:
  356.  
  357.     call SQLEXEC 'CLOSE c' || arg(1)
  358.  
  359.     if SQLCA.SQLCODE <> 0 then
  360.         signal SqlError
  361.  
  362.     return result
  363.  
  364. /************************************************************************/
  365. /*    End of procedure                                                                        */
  366. /************************************************************************/
  367.  
  368.