************************************************************************* 
      ** 
      ** Source File Name = dbcat.cbl 
      ** 
      ** Licensed Materials - Property of IBM 
      ** 
      ** (C) COPYRIGHT International Business Machines Corp. 1995, 2000  
      ** All Rights Reserved. 
      ** 
      ** US Government Users Restricted Rights - Use, duplication or 
      ** disclosure restricted by GSA ADP Schedule Contract with 
      ** 
      **    PURPOSE : 
      **       an example showing how to use DATABASE CATALOG APIs in order to: 
      **          - catalog a database 
      **          - list a directory of databases (showing what was created) 
      **          - uncatalog the database 
      ** 
      **    APIs USED : 
      **       CATALOG DATABASE                       sqlgcadb() 
      **       OPEN DATABASE DIRECTORY SCAN           sqlgdosd() 
      **       GET NEXT DATABASE DIRECTORY ENTRY      sqlgdgne() 
      **       CLOSE DATABASE DIRECTORY SCAN          sqlgdcls() 
      **       UNCATALOG DATABASE                     sqlguncd() 
      **       DEREFERENCE ADDRESS                    sqlgdref() 
      ** 
      ** 
      ** For more information about these samples see the README file. 
      ** 
      ** For more information on Programming in COBOL, see the: 
      **    -  "Programming in COBOL" section of the Application Development Guide. 
      ** 
      ** For more information on Building COBOL Applications, see the: 
      **    - "Building COBOL Applications" section of the Application Building Guide. 
      ** 
      ** For more information on the SQL language see the SQL Reference. 
      ** 
      ************************************************************************* 

       Identification Division.
       Program-Id. "dbcat".

       Data Division.
       Working-Storage Section.

       copy "sqlenv.cbl".
       copy "sqlutil.cbl".
       copy "sqlca.cbl".

      * Local Variables 
       77 rc                  pic s9(9) comp-5.
       77 idx                 pic 9(4) comp-5.
       77 errloc              pic x(80).

      * Variables for the CATALOG/UNCATALOG DATABASE APIs 
       77 dce-prin-len      pic 9(4) comp-5.
       77 comment-len       pic 9(4) comp-5.
       77 path-len          pic 9(4) comp-5.
       77 nname-len         pic 9(4) comp-5.
       77 alias-len         pic 9(4) comp-5.
       77 db-len            pic 9(4) comp-5.
       77 authentication    pic 9(4) comp-5.
       77 dce-prin          pic x(21).
       77 cbl-comment       pic x(31).
       77 path              pic x(1025).
       77 nname             pic x(9).
       77 loc-type          pic x.
       77 alias             pic x(9).
       77 database          pic x(9).

      * Variables for OPEN/CLOSE DATABASE DIRECTORY APIs. 
       77 dbCount           pic 9(4) comp-5.
       77 dbHandle          pic 9(4) comp-5.

      * Variables for GET NEXT DATABASE DIRECTORY ENTRY API. 
       01 buffer            pointer.
       77 sqledinfo-sz      pic 9(4) comp-5 value 560.
       77 disp-drive        pic x(50).

       Procedure Division.
       dbcat-pgm section.
           display "Sample COBOL Program : dbcat.cbl".

           move 0 to dce-prin-len.

           move "this is a test database" to cbl-comment.
           move 23 to comment-len.

           move 0 to path-len.
           move 0 to nname-len.
           move SQL-AUTHENTICATION-SERVER to authentication.
           move SQL-INDIRECT to loc-type.
           move "newalias" to alias.
           move 8 to alias-len.
           move "newdata" to database.
           move 7 to db-len.

           display "cataloging the new database".

      ******************************* 
      * CATALOG DATABASE API called * 
      ******************************* 
           call "sqlgcadb" using
                                 by value       dce-prin-len
                                 by value       comment-len
                                 by value       path-len
                                 by value       nname-len
                                 by value       alias-len
                                 by value       db-len
                                 by reference   sqlca
                                 by reference   dce-prin
                                 by value       authentication
                                 by reference   cbl-comment
                                 by reference   path
                                 by reference   nname
                                 by value       loc-type
                                 by reference   alias
                                 by reference   database
                           returning rc.
           move "CATALOG DATABASE" to errloc.
           call "checkerr" using SQLCA errloc.

           display "listing all databases...".
           display "========================".
           perform list-db.

           display "UNCATALOGing the database that was created".

      ********************************* 
      * UNCATALOG DATABASE API called * 
      ********************************* 
           call "sqlguncd" using
                                 by value       alias-len
                                 by reference   sqlca
                                 by reference   alias
                           returning rc.
           move "UNCATALOG DATABASE" to errloc.
           call "checkerr" using SQLCA errloc.

           display "Listing all databases [after UNCATALOG]".
           display "=======================================".
           perform list-db.
       end-dbcat. stop run.

       list-db Section.

      ******************************************* 
      * OPEN DATABASE DIRECTORY SCAN API called * 
      ******************************************* 
           call "sqlgdosd" using
                                 by value      path-len
                                 by reference  sqlca
                                 by reference  dbCount
                                 by reference  dbHandle
                                 by reference  path
                           returning rc.
           move "OPEN DATABASE DIRECTORY SCAN" to errloc.
           call "checkerr" using SQLCA errloc.

           perform get-db-entry thru end-get-db-entry
              varying idx from 0 by 1 until idx equal dbCount.

      ******************************************** 
      * CLOSE DATABASE DIRECTORY SCAN API called * 
      ******************************************** 
           call "sqlgdcls" using
                                 by value      dbHandle
                                 by reference  sqlca
                           returning rc.
           move "CLOSE DATABASE DIRECTORY SCAN" to errloc.
           call "checkerr" using SQLCA errloc.
       end-list-db. exit.

       get-db-entry section.

      ************************************************ 
      * GET NEXT DATABASE DIRECTORY ENTRY API called * 
      ************************************************ 
           call "sqlgdgne" using
                                 by value      dbHandle
                                 by reference  buffer
                                 by reference  sqlca
                           returning rc.

      ********************************** 
      * DEREFERENCE ADDRESS API called * 
      ********************************** 
           call "sqlgdref" using
                                 by value      sqledinfo-sz
                                 by reference  SQLEDINFO
                                 by reference  buffer
                           returning rc.

      * Displaying the contents of the SQLEDINFO structure. 
      * The SQLEDINFO structure is found in the "sqlenv.cbl" which is copied 
      * into this program.  The "sqlenv.cbl" file can be found in the 
      * "sqllib/include" directory. 
           display "alias :                 ", SQL-ALIAS.
           display "database name :         ", SQL-DBNAME.
      * The "SQL-DRIVE" field of the SQLEDINFO is truncated to 50 characters 
      * for display purposes in this program.  The actual length of this 
      * field is 215. 
           move SQL-DRIVE TO disp-drive.
           display "database drive :        ", disp-drive.
           display "node name :             ", SQL-NODENAME.
           display "database release type : ", SQL-DBTYPE.
           display "database comment :      ", SQL-COMMENT
                                               of SQLEDINFO.
           display "database entry type :   ", SQL-TYPE.
           display "authentication :        ", SQL-AUTHENTICATION.

           if SQL-AUTHENTICATION equal SQL-AUTHENTICATION-SERVER
              display "authentication :        SERVER".

           if SQL-AUTHENTICATION equal SQL-AUTHENTICATION-CLIENT
              display "authentication :        CLIENT".

           if SQL-AUTHENTICATION equal SQL-AUTHENTICATION-DCS
              display "authentication :        DCS".

           display " ".

       end-get-db-entry. exit.