************************************************************************* 
      ** 
      ** Source File Name = prepbind.sqb  1.4 
      ** 
      ** Licensed Materials - Property of IBM 
      ** 
      ** (C) COPYRIGHT International Business Machines Corp. 1995, 1999  
      ** All Rights Reserved. 
      ** 
      ** US Government Users Restricted Rights - Use, duplication or 
      ** disclosure restricted by GSA ADP Schedule Contract with 
      ** 
      **    PURPOSE : 
      **         This program shows how the PRECOMPILE and BIND APIs are used 
      **         to prepare an Embedded SQL program to a specified database. 
      ** 
      **    Note: The output ".cbl" that is produced by "prepbind" must 
      **          be compiled and linked to avoid timestamp conflicts. 
      ** 
      **    APIs USED : 
      **       START DATABASE MANAGER                 sqlgpstart() 
      **       PRECOMPILE PROGRAM                     sqlgprep() 
      **       BIND                                   sqlgbndx() 
      **       STOP DATABASE MANAGER                  sqlgpstp() 
      ** 
      ** 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. "prepbind".

       Data Division.
       Working-Storage Section.

       copy "sqlenv.cbl".
       copy "sqlca.cbl".
       copy "sql.cbl".

           EXEC SQL BEGIN DECLARE SECTION END-EXEC.
       01 userid.
         49 userid-length        pic s9(4) comp-5 value 0.
         49 userid-name          pic x(9).

       01 passwd.
         49 passwd-length        pic s9(4) comp-5 value 0.
         49 passwd-name          pic x(19).

       01 database.
         49 database-length      pic s9(4) comp-5 value 0.
         49 database-name        pic x(9).

       01 prog.
         49 program-length       pic s9(4) comp-5 value 0.
         49 program-name         pic x(20).

           EXEC SQL END DECLARE SECTION END-EXEC.

       01 prpmsg.
         49 prpmsg-file-length   pic s9(4) comp-5 value 11.
         49 prpmsg-file          pic x(11) value "outprep.msg".

       01 bndmsg.
         49 bndmsg-file-length   pic s9(4) comp-5 value 11.
         49 bndmsg-file          pic x(12) value "outbind.msg".

       01 prepbind.
         05 prepbind-options occurs 6 times pic s9(9) comp-5.

       01 bndfile.
         05 bndfile-length       pic s9(4) comp-5 value +0.
         05 bndfile-file         pic x(20).

      * Local Variables 
       77 rc                     pic s9(9) comp-5.
       77 errloc                 pic x(80).
       77 user-response          pic x(3).

       Procedure Division.
       prepbind-pgm section.

           display "Sample COBOL Program : PREPBIND.CBL".

           display "starting the database manager".
      ************************************* 
      * START DATABASE MANAGER API called * 
      ************************************* 
           call "sqlgpstart" using
                                 by value     0
                                 by reference sqlca
                         returning rc.

           display "Enter the name of the database : " with no advancing.
           accept database-name.
           inspect database-name tallying database-length for characters
              before initial " ".
           display " ".

           display "Enter program name with extension to precompile : "
              with no advancing.
           accept program-name.
           inspect program-name tallying program-length for characters
              before initial " ".
           display " ".

           display "Enter your user id (default none): " 
              with no advancing.
           accept userid-name.

           if userid-name = spaces then           
	       EXEC SQL CONNECT TO sample END-EXEC
           else
             display "Enter your password : " with no advancing.
             accept passwd-name.
             inspect passwd-name tallying passwd-length for characters
                before initial " ".
             display " ".

             display "connect to database : ", database-name.
             EXEC SQL CONNECT to :database USER :userid USING :passwd
                END-EXEC.

           move "CONNECT to database" to errloc.
           call "checkerr" using SQLCA errloc.

      * setup prep and bind options 
      * (1) - number of options allocated 
      * (2) - number of options used 
      * (3)-(6) - options/lengths 
           move 2            to prepbind-options(1).
           move 2            to prepbind-options(2).
           move SQL-BIND-OPT to prepbind-options(3).
           move 0            to prepbind-options(4).
           move SQL-PKG-OPT  to prepbind-options(5).
           move 0            to prepbind-options(6).

      ************************* 
      * PRECOMPILE API called * 
      ************************* 
           call "sqlgprep" using
                                 by value     prpmsg-file-length
                                 by value     program-length
                                 by reference sqlca
                                 by reference prepbind
                                 by reference prpmsg-file
                                 by reference program-name
                         returning rc.

           move "PRECOMPILE PROGRAM" to errloc.
           call "checkerr" using SQLCA errloc.

           move SQL-NO-OPT to prepbind-options(3).
           move SQL-NO-OPT to prepbind-options(5).

           string program-name delimited by ".", ".bnd" delimited by
              size into bndfile-file.
           inspect bndfile-file tallying bndfile-length for characters
              before initial " ".

      ******************* 
      * BIND API called * 
      ******************* 
           call "sqlgbndx" using
                                 by value     bndmsg-file-length
                                 by value     bndfile-length
                                 by reference sqlca
                                 by reference prepbind
                                 by reference bndmsg-file
                                 by reference bndfile-file
                           returning rc.
           move "binding the application" to errloc.
           call "checkerr" using SQLCA errloc.

           display "BIND was successful on ", bndfile-file.
           display "BIND output has been placed in file ", bndmsg-file.

           display "connect reseting".
           EXEC SQL CONNECT RESET END-EXEC.

           move "CONNECT RESET" to errloc.
           call "checkerr" using SQLCA errloc.

           display "Would you like to stop the Database Manager (y/n)"
              with no advancing.
           accept user-response.

           if user-response not equal to "y" and not equal to "Y"
           then stop run.

      * setup stop options structure SQLEDBSTOPOPT 
           move 0                   to SQL-ISPROFILE of SQLEDBSTOPOPT.
           move " "                 to SQL-PROFILE of SQLEDBSTOPOPT.
           move 0                   to SQL-ISNODENUM of SQLEDBSTOPOPT.
           move 0                   to SQL-NODENUM of SQLEDBSTOPOPT.
           move SQLE-NONE           to SQL-OPTION of SQLEDBSTOPOPT.
           move SQLE-DROP           to SQL-CALLERAC of SQLEDBSTOPOPT.


      ********************************* 
      * STOP DATABASE MANAGER API called * 
      ********************************* 
           call "sqlgpstp" using
                                 by reference   SQLEDBSTOPOPT 
                                 by reference   sqlca
                           returning rc.


           move "STOPPING DATABASE MANAGER" to errloc.
           call "checkerr" using SQLCA errloc.

       end-prepbind. stop run.