home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / makeview.zip / MAKEVIEW.CMD next >
OS/2 REXX Batch file  |  1995-07-10  |  8KB  |  278 lines

  1. /*
  2.     MAKEVIEW.CMD
  3.     ------------
  4.     
  5.     Usage:
  6.         MAKEVIEW -dDatabase [-oViewOwner] [-sSystemOwner] [-q] [-r]
  7.     
  8.     Copyright (c) 1994 by Watcom International Corporation
  9.     All Rights Reserved.
  10. */
  11.     debug = 0
  12.     
  13.     /*  Read table configuration
  14.     */
  15.     tables = charin( "tables.dat", ,chars( "tables.dat" ) );
  16.     if( tables = "" ) then signal BadTablesDat
  17.     tables = translate( tables, ';;', '0d0a'x );
  18.     interpret tables
  19.  
  20.     /*  Parse command line
  21.     */
  22.     viewowner = "SYSIBM"
  23.     systemowner = "SYSTEM"
  24.     dbname = ""
  25.     verbose = 1
  26.     makeview = 1
  27.     
  28.     if( arg() = 0 ) then signal Usage
  29.     do j = 1 to arg()
  30.         do i = 1 to Words( arg(j) )
  31.             parse upper value Word( arg(j), i ) with parm
  32.             select
  33.                 when( Left( parm, 2 ) = "-D" ) then dbname = SubStr( parm, 3 )
  34.                 when( Left( parm, 2 ) = "-O" ) then viewowner = SubStr( parm, 3 )
  35.                 when( Left( parm, 2 ) = "-Q" ) then verbose = 0
  36.                 when( Left( parm, 2 ) = "-R" ) then makeview = 0
  37.                 when( Left( parm, 2 ) = "-S" ) then systemowner = SubStr( parm, 3 )
  38.             otherwise
  39.                 say "Invalid argument:" parm
  40.                 say
  41.                 signal Usage
  42.             end
  43.         end
  44.     end
  45.     if( dbname = "" ) then signal Usage
  46.     if( verbose ) then pipeto = ">con"
  47.     else pipeto = ">nul"
  48.     
  49.    
  50.     /*  Load REXX API
  51.     */
  52.     call RxFuncDrop 'SQLEXEC'
  53.     call RxFuncAdd 'SQLEXEC', 'SQLAR', 'SQLEXEC'
  54.     
  55.     call RxFuncDrop 'SQLDBS'
  56.     call RxFuncAdd 'SQLDBS', 'SQLAR', 'SQLDBS'
  57.  
  58.  
  59.     /*  Start the database manager
  60.     */ 
  61.     if( verbose ) then do
  62.         say "Starting database manager..."
  63.     end
  64.     call SQLDBS 'START DATABASE MANAGER'
  65.     if( RESULT \= 0 ) then signal BadStartDBM
  66.     if( SQLCA.SQLCODE \= 0 & SQLCA.SQLCODE \= -1026 & SQLCA.SQLCODE \= -1063 ) then signal BadStartDBM
  67.     
  68.     
  69.     /*  Report settings
  70.     */
  71.     if( verbose ) then do
  72.         say "Database is" dbname
  73.         say "ViewOwner is" viewowner
  74.         say "SystemOwner is" systemowner
  75.     end
  76.  
  77.  
  78.     /*  Connect
  79.     */
  80.     if( verbose ) then do
  81.         say "Connecting..."
  82.     end
  83.     call SQLEXEC "CONNECT TO" dbname
  84.     if( SQLCA.SQLCODE <> 0 ) then signal BadConnect
  85.     
  86.     
  87.     /*  Create or drop system views...
  88.     */
  89.     
  90.     /*  SYSTABLES
  91.     */
  92.     if( makeview ) then do
  93.         stmt = "CREATE VIEW" viewowner || "." || "SYSTABLES"
  94.         stmt = stmt "( CREATOR, NAME, TYPE, REMARKS )"
  95.         stmt = stmt "AS SELECT"
  96.         stmt = stmt systables.col.creator ","
  97.         stmt = stmt systables.col.name ","
  98.         stmt = stmt systables.col.type ","
  99.         stmt = stmt systables.col.remarks
  100.         stmt = stmt "FROM" systemowner || "." || systables.table
  101.         if( verbose ) then do
  102.             say "Creating systables..."
  103.         end
  104.     end; else do
  105.         stmt = "DROP VIEW" viewowner || "." || "SYSTABLES"
  106.         if( verbose ) then do
  107.             say "Dropping systables..."
  108.         end
  109.     end
  110. if( debug ) then do
  111.     say stmt
  112. end; else do
  113.     call SQLEXEC "EXECUTE IMMEDIATE :stmt"
  114.     if( SQLCA.SQLCODE <> 0 ) then call BadCreateorDropView
  115. end
  116.     
  117.     /*  SYSCOLUMNS
  118.     */
  119.     if( makeview ) then do
  120.         stmt = "CREATE VIEW" viewowner || "." || "SYSCOLUMNS"
  121.         stmt = stmt "( TBCREATOR, TBNAME, NAME, REMARKS, COLTYPE, LENGTH, SCALE, NULLS, COLNO, KEYSEQ )"
  122.         stmt = stmt "AS SELECT"
  123.         stmt = stmt syscolumns.col.tbcreator ","
  124.         stmt = stmt syscolumns.col.tbname ","
  125.         stmt = stmt syscolumns.col.name ","
  126.         stmt = stmt syscolumns.col.remarks ","
  127.         stmt = stmt syscolumns.col.coltype ","
  128.         stmt = stmt syscolumns.col.length ","
  129.         stmt = stmt syscolumns.col.scale ","
  130.         stmt = stmt syscolumns.col.nulls ","
  131.         stmt = stmt syscolumns.col.colno ","
  132.         stmt = stmt syscolumns.col.keyseq
  133.         stmt = stmt "FROM" systemowner || "." || syscolumns.table
  134.         if( verbose ) then do
  135.             say "Creating syscolumns..."
  136.         end
  137.     end; else do
  138.         stmt = "DROP VIEW" viewowner || "." || "SYSCOLUMNS"
  139.         if( verbose ) then do
  140.             say "Dropping syscolumns..."
  141.         end
  142.     end
  143. if( debug ) then do
  144.     say stmt
  145. end; else do
  146.     call SQLEXEC "EXECUTE IMMEDIATE :stmt"
  147.     if( SQLCA.SQLCODE <> 0 ) then call BadCreateorDropView
  148. end
  149.     
  150.  
  151.     /*  SYSRELS
  152.     */
  153.     if( sysrels.table \= "" ) then do
  154.         if( makeview ) then do
  155.             stmt = "CREATE VIEW" viewowner || "." || "SYSRELS"
  156.             stmt = stmt "( CREATOR, TBNAME, REFTBCREATOR, REFTBNAME, RELNAME, FKCOLNAMES, PKCOLNAMES )"
  157.             stmt = stmt "AS SELECT"
  158.             stmt = stmt sysrels.col.creator ","
  159.             stmt = stmt sysrels.col.tbname ","
  160.             stmt = stmt sysrels.col.reftbcreator ","
  161.             stmt = stmt sysrels.col.reftbname ","
  162.             stmt = stmt sysrels.col.rename ","
  163.             stmt = stmt sysrels.col.fkcolnames ","
  164.             stmt = stmt sysrels.col.pkcolnames
  165.             stmt = stmt "FROM" systemowner || "." || sysrels.table
  166.             if( verbose ) then do
  167.                 say "Creating sysrels..."
  168.             end
  169.         end; else do
  170.             stmt = "DROP VIEW" viewowner || "." || "SYSRELS"
  171.             if( verbose ) then do
  172.                 say "Dropping sysrels..."
  173.             end
  174.         end
  175. if( debug ) then do
  176.         say stmt
  177. end; else do
  178.         call SQLEXEC "EXECUTE IMMEDIATE :stmt"
  179.         if( SQLCA.SQLCODE <> 0 ) then call BadCreateorDropView
  180. end
  181.     end
  182.  
  183.     /*  SYSINDEXES
  184.     */
  185.     if( sysindexes.table \= "" ) then do
  186.         if( makeview ) then do
  187.             stmt = "CREATE VIEW" viewowner || "." || "SYSINDEXES"
  188.             stmt = stmt "( CREATOR, NAME, COLNAMES, UNIQUERULE, TBCREATOR, TBNAME )"
  189.             stmt = stmt "AS SELECT"
  190.             stmt = stmt sysindexes.col.creator ","
  191.             stmt = stmt sysindexes.col.name ","
  192.             stmt = stmt sysindexes.col.colnames ","
  193.             stmt = stmt sysindexes.col.uniquerule ","
  194.             stmt = stmt sysindexes.col.tbcreator ","
  195.             stmt = stmt sysindexes.col.tbname
  196.             stmt = stmt "FROM" systemowner || "." || sysindexes.table
  197.             if( verbose ) then do
  198.                 say "Creating sysindexes..."
  199.             end
  200.         end; else do
  201.             stmt = "DROP VIEW" viewowner || "." || "SYSINDEXES"
  202.             if( verbose ) then do
  203.                 say "Dropping sysindexes..."
  204.             end
  205.         end
  206. if( debug ) then do
  207.         say stmt
  208. end; else do
  209.         call SQLEXEC "EXECUTE IMMEDIATE :stmt"
  210.         if( SQLCA.SQLCODE <> 0 ) then call BadCreateorDropView
  211. end
  212.     end
  213.    
  214.     
  215.     /*  Disconnect
  216.     */
  217.     if( verbose ) then do
  218.         say "Disconnecting..."
  219.     end
  220.     call SQLEXEC "CONNECT RESET"
  221.     
  222.     if( verbose ) then do
  223.         if( makeview ) then say "System views created successfully."
  224.         else say "System views dropped."
  225.     end
  226.  
  227.  
  228.     /*  Normal termination
  229.     */
  230.     exit
  231.     
  232.     /*  Error messages
  233.     */
  234. Usage:
  235.     say "Create system views"
  236.     say "MAKEVIEW -dDatabase [-oViewOwner] [-sSystemOwner] [-q] [-r]"
  237.     say "where:"
  238.     say "    Database       the name of a database"
  239.     say "    ViewOwner      qualifier of the created views"
  240.     say "    SystemOwner    qualifier of the current system tables"
  241.     say "                   (this is the collection name for SQL/400)"
  242.     say "    -q             operate quietly"
  243.     say "    -r             drop views"
  244.     say
  245.     say "By default, ViewOwner is SYSIBM and SystemOwner is SYSTEM"
  246.     exit
  247.     
  248. BadTablesDat:
  249.     say "Error: Unable to read tables.dat"
  250.     exit
  251.     
  252. BadStartDBM:
  253.     say "Error: Could not start the database manager."
  254.     say SQLMSG
  255.     exit 
  256.     
  257. BadConnect:
  258.     say "Error: Unable to connect to database."
  259.     say SQLMSG
  260.     exit
  261.  
  262. BadCreateorDropView:
  263.     if( makeview ) then do
  264.         
  265.         /*  Terminate now if we could not create a view
  266.         */
  267.         say "Error: Unable to create view."
  268.         say SQLMSG
  269.         call SQLEXEC "CONNECT RESET"
  270.         exit
  271.     end; else do
  272.     
  273.         /*  Keep going -- attempt to remove all views
  274.         */
  275.         say "Warning: Unable to drop view, continuing."
  276.         return
  277.     end
  278.