***********************************************************************
      ** (c) Copyright IBM Corp. 2007 All rights reserved.
      ** 
      ** The following sample of source code ("Sample") is owned by International 
      ** Business Machines Corporation or one of its subsidiaries ("IBM") and is 
      ** copyrighted and licensed, not sold. You may use, copy, modify, and 
      ** distribute the Sample in any form without payment to IBM, for the purpose of 
      ** assisting you in the development of your applications.
      ** 
      ** The Sample code is provided to you on an "AS IS" basis, without warranty of 
      ** any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR 
      ** IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
      ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do 
      ** not allow for the exclusion or limitation of implied warranties, so the above 
      ** limitations or exclusions may not apply to you. IBM shall not be liable for 
      ** any damages you suffer as a result of using, copying, modifying or 
      ** distributing the Sample, even if IBM has been advised of the possibility of 
      ** such damages.
      ***********************************************************************
      **
      ** SOURCE FILE NAME: impexp.sqb 
      **
      ** SAMPLE: Export and import tables with table data
      **
      **         This program will first export a table to a comma-delimited
      **         text file and then import the comma-delimited text file to
      **         a DB2 table.
      **      
      **         This program needs the embedded SQL calls in order to 
      **         connect to an existing database, then to create a temporary
      **         table to work with.
      **
      ** DB2 APIs USED:
      **         db2gExport -- EXPORT
      **         db2gImport -- IMPORT
      **
      ** SQL STATEMENTS USED:
      **         BEGIN DECLARE SECTION 
      **         END DECLARE SECTION
      **         EXECUTE IMMEDIATE
      **         CREATE TABLE
      **         CONNECT
      **         COMMIT
      **
      **                           
      ***********************************************************************
      **
      ** For more information on the sample programs, see the README file. 
      **
      ** For information on developing embedded SQL applications see the Developing Embedded SQL Applications book.
      **
      ** For information on DB2 APIs, see the Administrative API Reference.
      **
      ** For information on using SQL statements, see the SQL Reference. 
      **
      ** For the latest information on programming, compiling, and running
      ** DB2 applications, visit the DB2 Information Center: 
      **     http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
      ***********************************************************************

       Identification Division.
       Program-ID. "impexp".

       Data Division.
       Working-Storage Section.

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

           EXEC SQL BEGIN DECLARE SECTION END-EXEC.

       77 statement         pic x(254).

       01 userid            pic x(8).
       01 passwd.
         49 passwd-length   pic s9(4) comp-5 value 0.
         49 passwd-name     pic x(18).
       01 mysqlchar.
         49 mysqlchar-LENGTH  pic s9(4) comp-5.
         49 mysqlchar-DATA    pic x(100).
       01 mysqllob            usage is SQL type is clob(100).
       77 mycommitcount       pic s9(4) comp-5 value 0.
       77 mysqlmethd          pic s9(4) comp-5.

           EXEC SQL END DECLARE SECTION END-EXEC.

       77 errloc              pic x(80).
       77 rc                  pic s9(9) comp-5.
       77 stmt                pic x(25).
       77 impstmt             pic x(37).

      * variables for import/export APIs
       77 datafile            pic x(12) value "EXPTABLE.IXF".
       77 datafile-len        pic 9(4) comp-5 value 12.
       77 msgfile-x           pic x(10) value "EXPMSG.TXT".
       77 msgfile-x-len       pic 9(4) comp-5 value 10.
       77 msgfile-m           pic x(10) value "IMPMSG.TXT".
       77 msgfile-m-len       pic 9(4) comp-5 value 10.
       77 fileformat          pic x(3) value "DEL".
       77 fileformat-len      pic 9(4) comp-5 value 3.

       Procedure Division.
       impexp Section.
           display "Sample COBOL program: IMPEXP".

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

           display "Enter your password : " with no advancing
           accept passwd-name.
           inspect passwd-name tallying passwd-length for characters
              before initial " ".

           display " ".

           move "select name,id from staff" to stmt.
           move "insert into impexp.imptable (name,id)" to impstmt.

      * need to preset the size of structure field and counts
           move stmt              to mysqllob-DATA.
           move 25                to mysqllob-LENGTH.
           move SQL-METH-D        to mysqlmethd.

      * Prepare the DB2G-EXPORT-STRUCT
           set DB2-PI-DATA-FILE-NAME of DB2G-EXPORT-STRUCT
               to address of datafile.
           move datafile-len to DB2-I-DATA-FILE-NAME-LEN
               of DB2G-EXPORT-STRUCT.
           set DB2-PI-LOB-PATH-LIST of DB2G-EXPORT-STRUCT
               to NULL. 
           set DB2-PI-LOB-FILE-LIST of DB2G-EXPORT-STRUCT
               to NULL.
           set DB2-PI-DATA-DESCRIPTOR of DB2G-EXPORT-STRUCT
              to address of mysqlmethd.
           set DB2-PI-ACTION-STRING of DB2G-EXPORT-STRUCT
              to address of mysqllob.
           set DB2-PI-FILE-TYPE of DB2G-EXPORT-STRUCT
              to address of fileformat.
           set DB2-PI-FILE-TYPE-MOD of DB2G-EXPORT-STRUCT
              to address of SQL-FILETMOD.
           set DB2-PI-MSG-FILE-NAME of DB2G-EXPORT-STRUCT
              to address of msgfile-x.
           move SQLU-INITIAL
             to DB2-I-CALLER-ACTION of DB2G-EXPORT-STRUCT.
           set DB2-PO-EXPORT-INFO-OUT of DB2G-EXPORT-STRUCT
             to address of DB2EXPORT-OUT.
           move datafile-len
             to DB2-I-DATA-FILE-NAME-LEN of DB2G-EXPORT-STRUCT.
           move fileformat-len
             to DB2-I-FILE-TYPE-LEN of DB2G-EXPORT-STRUCT.
           move msgfile-x-len
             to DB2-I-MSG-FILE-NAME-LEN of DB2G-EXPORT-STRUCT.

      * connecting to SAMPLE database.
           EXEC SQL CONNECT TO sample USER :userid USING :passwd
                END-EXEC.
           move "CONNECT TO SAMPLE" to errloc.
           call "checkerr" using SQLCA errloc.

           display "export name,id from staff table into file: ",
                   datafile.

      *********************
      * EXPORT API called *
      *********************
           call "db2gExport" using
                                 by value     DB2VERSION820
                                 by reference DB2G-EXPORT-STRUCT
                                 by reference sqlca
                             returning rc.
           move "exporting table" to errloc.
           call "checkerr" using SQLCA errloc.
      
           display "rows exported : ", DB2-O-ROWS-EXPORTED.

      * drop the table before creating it, just in case it already exists
           move "drop table impexp.imptable" to statement.
           EXEC SQL EXECUTE IMMEDIATE :statement END-EXEC.
           EXEC SQL COMMIT END-EXEC.
           move "COMMIT the DROP TABLE" to errloc.
           call "checkerr" using SQLCA errloc.
      
      * create a temporary table to import into
           EXEC SQL CREATE TABLE impexp.imptable (name varchar(15),
                                                  id int)
                END-EXEC.
           move "CREATE TABLE" to errloc.
           call "checkerr" using SQLCA errloc.
      
      * need to preset the size of structure field and counts
           move impstmt           to mysqlchar-DATA.
           move 37                to mysqlchar-LENGTH.

           set DB2-PI-DATA-FILE-NAME of DB2G-IMPORT-STRUCT
               to address of datafile.
           move datafile-len to DB2-I-DATA-FILE-NAME-LEN
               of DB2G-IMPORT-STRUCT.
           set DB2-PI-LOB-PATH-LIST of DB2G-IMPORT-STRUCT
               to NULL. 
           set DB2-PI-DATA-DESCRIPTOR of DB2G-IMPORT-STRUCT
              to address of mysqlmethd.
           set DB2-PI-ACTION-STRING of DB2G-IMPORT-STRUCT
               to address of mysqlchar.
           set DB2-PI-FILE-TYPE of DB2G-IMPORT-STRUCT
               to address of fileformat.
           set DB2-PI-FILE-TYPE-MOD of DB2G-IMPORT-STRUCT
               to address of SQL-FILETMOD.
           set DB2-PI-MSG-FILE-NAME of DB2G-IMPORT-STRUCT
               to address of msgfile-m.
           move SQLU-INITIAL
               to DB2-I-CALLER-ACTION of DB2G-IMPORT-STRUCT.
           set DB2-PO-IMPORT-INFO-OUT of DB2G-IMPORT-STRUCT
               to address of DB2G-IMPORT-OUT.
           move datafile-len
               to DB2-I-DATA-FILE-NAME-LEN of DB2G-IMPORT-STRUCT.
           move fileformat-len
               to DB2-I-FILE-TYPE-LEN of DB2G-IMPORT-STRUCT.
           move msgfile-m-len
               to DB2-I-MSG-FILE-NAME-LEN of DB2G-IMPORT-STRUCT.

      * need to preset db2gImportIn structure fields
           move 0 to DB2-I-ROWCOUNT of DB2G-IMPORT-IN.
           move 0 to DB2-I-RESTARTCOUNT of DB2G-IMPORT-IN.
           move 0 to DB2-I-SKIPCOUNT of DB2G-IMPORT-IN.
           set DB2-PI-COMMITCOUNT of DB2G-IMPORT-IN to NULL.
           move 0 to DB2-I-WARNINGCOUNT of DB2G-IMPORT-IN.
           move 0 to DB2-I-NO-TIMEOUT of DB2G-IMPORT-IN.
           move 0 to DB2-I-ACCESS-LEVEL of DB2G-IMPORT-IN.
           set DB2-PI-IMPORT-INFO-IN of DB2G-IMPORT-STRUCT
               to address of DB2G-IMPORT-IN.

           display "importing the file ", datafile, " into 'imptable'".
      
      *********************
      * IMPORT API called *
      *********************
           call "db2gImport" using
                                 by value     DB2VERSION820
                                 by reference DB2G-IMPORT-STRUCT
                                 by reference sqlca
                             returning rc.
           move "importing table" to errloc.
           call "checkerr" using SQLCA errloc.
      
           display "rows imported : ", DB2-O-ROWS-INSERTED.
           display "rows committed : ", 
                     DB2-O-ROWS-COMMITTED of DB2G-IMPORT-OUT.

           EXEC SQL CONNECT RESET END-EXEC.
           move "CONNECT RESET" to errloc.
           call "checkerr" using SQLCA errloc.

       End-impexp. stop run.