/****************************************************************************
** (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: dtlob.sqC 
**    
** SAMPLE: How to use the LOB data type 
**
** SQL STATEMENTS USED:
**         SELECT
**         INSERT
**         DELETE
**         DECLARE CURSOR
**         OPEN
**         FETCH
**         CLOSE
**         VALUES
**         FREE LOCATOR 
**
**                           
*****************************************************************************
**
** 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 using SQL statements, see the SQL Reference.
**
** For the latest information on programming, compiling, and running DB2
** applications, visit the DB2 Information Center at
**     http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
****************************************************************************/

#include <string.h>
#include <sqlenv.h>
#include <sqlutil.h>
#include <sql.h>
#include "utilemb.h"
#if ((__cplusplus >= 199711L) && !defined DB2HP && !defined DB2AIX) || \
    (DB2LINUX && (__LP64__ || (__GNUC__ >= 3)) )
   #include <iostream>
   using namespace std; 
#else
   #include <iostream.h>
#endif

EXEC SQL BEGIN DECLARE SECTION;
  SQL TYPE IS BLOB_FILE blobFilePhoto;
  char photoFormat[10];
  char empno[7];
  SQL TYPE IS CLOB(5 K) clobResume;
  SQL TYPE IS CLOB_FILE clobFileResume;
  SQL TYPE IS CLOB_LOCATOR clobLocResume;
  sqlint32 posBeginDeptInfo;
  sqlint32 posBeginEducation;
  SQL TYPE IS CLOB(5 K) clobDeptInfo;
  SQL TYPE IS CLOB_LOCATOR clobLocShortResume;
  SQL TYPE IS CLOB_LOCATOR clobLocNewResume;
  short lobind;
EXEC SQL END DECLARE SECTION;

class DtLob
{
  public:
    int BlobFileUse();
    int ClobUse();
    int ClobFileUse();
    int ClobLocatorUse();
};

int DtLob::BlobFileUse()
{
  struct sqlca sqlca;
  char fileName[15];

  cout << "\n-----------------------------------------------------------";
  cout << "\nUSE THE SQL STATEMENTS:" << endl;
  cout << "  SELECT INTO" << endl;
  cout << "  INSERT" << endl;
  cout << "  DELETE" << endl;
  cout << "TO SHOW HOW TO USE A BLOB FILE." << endl;

#if (defined(DB2NT))
  strcpy(photoFormat, "bitmap");
  strcpy(fileName, "photo.BMP");
#else //UNIX
  strcpy(photoFormat, "gif");
  strcpy(fileName, "photo.GIF");
#endif

  // initialize blobFile
  strcpy(blobFilePhoto.name, fileName);
  blobFilePhoto.name_length = strlen(blobFilePhoto.name);

  // read BLOB data
  cout << "\n  Read BLOB data in the file '" << fileName << "'." << endl;
  blobFilePhoto.file_options = SQL_FILE_OVERWRITE;

  EXEC SQL SELECT picture INTO :blobFilePhoto :lobind
    FROM emp_photo
    WHERE photo_format = :photoFormat AND
          empno = '000130';
  EMB_SQL_CHECK("BLOB data -- read");

  if (lobind < 0)
  {
    cout << "  NULL LOB indicated." << endl;
    return 1;
  }

  // write BLOB data
  cout << "  Write BLOB data from the file '" << fileName << "'." << endl;
  blobFilePhoto.file_options = SQL_FILE_READ;

  EXEC SQL INSERT INTO emp_photo(empno, photo_format, picture)
    VALUES('200340', :photoFormat, :blobFilePhoto);
  EMB_SQL_CHECK("BLOB data -- write");

  // delete new record
  cout << "  Delete the new record from the database." << endl;

  EXEC SQL DELETE FROM emp_photo WHERE empno = '200340';
  EMB_SQL_CHECK("new record -- delete");

  return 0;
} //DtLob::BlobFileUse

int DtLob::ClobUse()
{
  struct sqlca sqlca;
  int charNb;
  int lineNb;

  cout << "\n-----------------------------------------------------------";
  cout << "\nUSE THE SQL STATEMENTS:" << endl;
  cout << "  DECLARE CURSOR" << endl;
  cout << "  OPEN" << endl;
  cout << "  FETCH" << endl;
  cout << "  CLOSE" << endl;
  cout << "TO SHOW HOW TO USE THE CLOB DATA TYPE." << endl;

  cout << "\n  READ THE CLOB DATA:" << endl;

  EXEC SQL DECLARE c1 CURSOR FOR
    SELECT empno, resume
      FROM emp_resume
      WHERE resume_format = 'ascii' AND empno = '000130';

  EXEC SQL OPEN c1;
  EMB_SQL_CHECK("cursor -- open");

  EXEC SQL FETCH c1 INTO :empno, :clobResume :lobind;
  EMB_SQL_CHECK("cursor -- fetch");

  while (sqlca.sqlcode != 100)
  {
    if (lobind < 0)
    {
      cout << "  NULL LOB indicated." << endl;
    }
    else
    {
      cout << "\n    Empno: " << empno << endl;
      cout << "    Resume length: " << clobResume.length << endl;
      cout << "    First 15 lines of the resume:" << endl;

      for (charNb = 0, lineNb = 0;
           lineNb < 15 && charNb < clobResume.length; charNb++)
      {
        cout << clobResume.data[charNb];
        if (clobResume.data[charNb] == '\n')
        {
          cout << "       ";
          lineNb++;
        }
      }
    }

    EXEC SQL FETCH c1 INTO :empno, :clobResume:lobind;
    EMB_SQL_CHECK("cursor -- fetch");
  }

  EXEC SQL CLOSE c1;
  EMB_SQL_CHECK("cursor -- close");

  return 0;
} //DtLob::ClobUse

int DtLob::ClobFileUse()
{
  struct sqlca sqlca;
  char fileName[15];

  cout << "\n-----------------------------------------------------------";
  cout << "\nUSE THE SQL STATEMENT:" << endl;
  cout << "  SELECT INTO" << endl;
  cout << "TO SHOW HOW TO USE A CLOB FILE." << endl;

  strcpy(fileName, "resume.TXT");

  // initialize clobFile
  strcpy(clobFileResume.name, fileName);
  clobFileResume.name_length = strlen(clobFileResume.name);

  // read CLOB data
  cout << "\n  Read CLOB data in the file '" << fileName << "'." << endl;
  clobFileResume.file_options = SQL_FILE_OVERWRITE;

  EXEC SQL SELECT resume INTO :clobFileResume:lobind
    FROM emp_resume
    WHERE resume_format = 'ascii' AND empno = '000130';
  EMB_SQL_CHECK("CLOB data -- read");

  if (lobind < 0)
  {
    cout << "  NULL LOB indicated." << endl;
    return 1;
  }

  return 0;
} //DtLob::ClobFileUse

int DtLob::ClobLocatorUse()
{
  struct sqlca sqlca;
  int charNb;

  cout << "\n-----------------------------------------------------------";
  cout << "\nUSE THE SQL STATEMENTS:" << endl;
  cout << "  SELECT INTO" << endl;
  cout << "  INSERT" << endl;
  cout << "  DELETE" << endl;
  cout << "  VALUES" << endl;
  cout << "  FREE LOCATOR" << endl;
  cout << "TO SHOW HOW TO USE A CLOB LOCATOR." << endl;

  cout << "\n  **************************************************" << endl;
  cout << "           ORIGINAL RESUME -- VIEW" << endl;
  cout << "  **************************************************" << endl;

  EXEC SQL SELECT resume INTO :clobResume
    FROM emp_resume
    WHERE empno = '000130' AND resume_format = 'ascii';
  EMB_SQL_CHECK("old resume -- read");

  for (charNb = 0; charNb < clobResume.length; charNb++)
  {
    cout << clobResume.data[charNb];
  }

  cout << "\n  ********************************************" << endl;
  cout << "       NEW RESUME -- CREATE" << endl;
  cout << "  ********************************************" << endl;

  EXEC SQL SELECT resume INTO :clobLocResume
    FROM emp_resume
    WHERE empno = '000130' AND resume_format = 'ascii';
  EMB_SQL_CHECK("resume -- read");

  EXEC SQL VALUES(POSSTR(:clobLocResume, 'Department Information'))
    INTO :posBeginDeptInfo;
  EMB_SQL_CHECK("Department Info position -- get");

  EXEC SQL VALUES(POSSTR(:clobLocResume, 'Education'))
    INTO :posBeginEducation;
  EMB_SQL_CHECK("Education position -- get");

  cout << "\n    Create short resume without Department Info." << endl;

  EXEC SQL VALUES(SUBSTR(:clobLocResume, 1, :posBeginDeptInfo - 1) ||
                  SUBSTR(:clobLocResume, :posBeginEducation))
    INTO :clobLocShortResume;
  EMB_SQL_CHECK("short resume -- create");

  EXEC SQL VALUES(SUBSTR(:clobLocResume, :posBeginDeptInfo,
                         :posBeginEducation - :posBeginDeptInfo))
    INTO :clobDeptInfo;
  EMB_SQL_CHECK("Department Info -- create");

  cout << "  Append Department Info at the end of Short resume." << endl;

  EXEC SQL VALUES(:clobLocShortResume || :clobDeptInfo)
    INTO :clobLocNewResume;
  EMB_SQL_CHECK("new resume -- create");

  cout << "  Insert the new resume in the database." << endl;

  EXEC SQL INSERT INTO emp_resume(empno, resume_format, resume)
    VALUES('200340', 'ascii', :clobLocNewResume);
  EMB_SQL_CHECK("new resume -- write");

  EXEC SQL FREE LOCATOR :clobLocResume,
                        :clobLocShortResume,
                        :clobLocNewResume;
  EMB_SQL_CHECK("locators -- free");

  cout << "\n  *************************************" << endl;
  cout << "      NEW RESUME -- VIEW" << endl;
  cout << "  *************************************" << endl;

  EXEC SQL SELECT resume INTO :clobResume
    FROM emp_resume
    WHERE empno = '200340';
  EMB_SQL_CHECK("new resume -- read");

  for (charNb = 0; charNb < clobResume.length; charNb++)
  {
    cout << clobResume.data[charNb];
  }

  cout << "\n  **************************************" << endl;
  cout << "      NEW RESUME -- DELETE" << endl;
  cout << "  **************************************" << endl;

  EXEC SQL DELETE FROM emp_resume WHERE empno = '200340';
  EMB_SQL_CHECK("new resume -- delete");

  return 0;
} //DtLob::LobLocatorUse

int main(int argc, char *argv[])
{
  int rc = 0;
  CmdLineArgs check;
  DtLob lob;
  DbEmb db;

  // check the command line arguments
  rc = check.CmdLineArgsCheck1(argc, argv, db);
  if (rc != 0)
  {
    return rc;
  }

  cout << "\nTHIS SAMPLE SHOWS HOW TO USE THE LOB DATA TYPE." << endl;

  // connect to the database
  rc = db.Connect();
  if (rc != 0)
  {
    return rc;
  }

  rc = lob.BlobFileUse();
  rc = lob.ClobUse();
  rc = lob.ClobFileUse();
  rc = lob.ClobLocatorUse();

  // disconnect from the database
  rc = db.Disconnect();
  if (rc != 0)
  {
    return rc;
  }

  return 0;
} //main