//  
//  Source File Name = cursor.sqC  
//  
//  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 IBM Corp. 
//  
//  
//  PURPOSE: This sample program demonstrates the use of a CURSOR. 
//           The CURSOR is processed using static SQL.  This program 
//           obtains all managers in the STAFF table of the 
//           SAMPLE database. 
//  
//  The file "utilemb.sqC" contains functions for error-checking and 
//  rolling back a transaction in case of error. This file must be 
//  compiled and its object file linked in to the "cursor" program. 
//  
//  EXTERNAL DEPENDENCIES : 
//        - Existing database for precompile purposes. 
//        - Precompile with the SQL precompiler (PREP in DB2) 
//        - Binding to a database (BIND in DB2) 
//        - Compiling and linking with the IBM Cset++ compiler (AIX and OS/2) 
//          or the Microsoft Visual C++ compiler (Windows) 
//          or the compiler supported on your platform. 
//  
//  For more information about these samples see the README file. 
//  
//  For more information on programming in C++, see the 
//     -  "Programming in C and C++" section of the Application Development Guide 
//  
//  For more information on Building C++ Applications, see the: 
//     - "Building C++ Applications" section of the Application Building Guide. 
//  
//  For more information on the SQL language see the SQL Reference. 

#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sqlca.h>
#include "utilemb.h"

EXEC SQL INCLUDE SQLCA;


class Cursor {
   public:
      Fetch ();

   private:
      EXEC SQL BEGIN DECLARE SECTION;
         char   pname[10];
         short  dept;
      EXEC SQL END DECLARE SECTION;
};


class CNT {
   public:
      void Connect();
      void Connect(char *, char *);

      void Disconnect();

   private:
      EXEC SQL BEGIN DECLARE SECTION;
         char userid[9];
         char passwd[19];
      EXEC SQL END DECLARE SECTION;
};


void CNT::Connect () {
   cout <<"Connecting to database SAMPLE with default userid and password\n";
   EXEC SQL CONNECT TO sample;
   EMB_SQL_CHECK("CONNECT TO SAMPLE") ;
   cout << "Connected to database SAMPLE \n";
}

void CNT::Connect (char *uid, char *pswd) {
   strcpy (userid, uid);
   strcpy (passwd, pswd);

   cout <<  "Connecting to database SAMPLE... \n";
   EXEC SQL CONNECT TO sample USER :userid USING :passwd;
   EMB_SQL_CHECK("CONNECT TO SAMPLE") ;
   cout <<  "Connected to database SAMPLE \n";
}

void CNT::Disconnect () {
   //  Disconnect from Remote Database 
   cout <<  "Disconnecting from database SAMPLE... \n";
   EXEC SQL CONNECT RESET;
   EMB_SQL_CHECK("CONNECT RESET");
   cout <<  "Disconnected from database SAMPLE \n";
}


Cursor::Fetch () {
   EXEC SQL DECLARE c1 CURSOR FOR 
            SELECT name, dept FROM staff WHERE job='Mgr';
   EMB_SQL_CHECK("DECLARE CURSOR") ;

   EXEC SQL OPEN c1;  
   EMB_SQL_CHECK("OPEN CURSOR") ;

   cout << endl;
   do {
      EXEC SQL FETCH c1 INTO :pname, :dept; 
      if (SQLCODE != 0) break;

      cout << pname << " in dept. " << dept << " is a manager\n";
   } while ( 1 );

   EXEC SQL CLOSE c1; 
   EMB_SQL_CHECK("CLOSE CURSOR") ;

   return 0;
}


int main(int argc, char *argv[]) 
{
   cout << "Sample C++ program: CURSOR \n";
   CNT cnt;
   Cursor doit; 

   if (argc == 1) {
     cnt.Connect(); 
   } else if (argc == 3) {
      cnt.Connect (argv[1], argv[2]);
   } else {
      cout << "\nUSAGE: cursor userid passwd\n\n";
   } //  end if 


   doit.Fetch();

   return 0;
} //  end of program : cursor.sqC