//
//  Source File Name = client.sqx 
//  
//  Licensed Materials - Property of IBM 
//  
//  (C) COPYRIGHT International Business Machines Corp. 1995, 1997 
//  All Rights Reserved. 
//  
//  US Government Users Restricted Rights - Use, duplication or 
//  disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
//  
//  
//  PURPOSE: This file implement the class Administrator with the 
//  public member functions QueryConnSettings, ModifyConnSettings, 
//  RestoreConnSettings and shows how to use CLIENT APIs 
//  in order to: 
//           - set a client 
//           - and query a client 
//  
//     APIs USED : 
//        SET CLIENT                     sqlesetc 
//        QUERY CLIENT                   sqleqryc 
//  
//  
//  EXTERNAL DEPENDENCIES : 
//        - 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef DB2MAC
#include <malloc.h>
#endif
#include <sqlenv.h>
#include "util.h"

#ifdef DB268K
/* Need to include ASLM for 68K applications */
#include <LibraryManager.h>
#endif

/* the following strings are used in the Administrator::printConnSettings member function */

#define SQL_CONNECT_1_STR "Enforces the rules for Remote Unit of Work (RUOW) from\
\nprevious releases\
\n  TYPE = SQL_1\n\n"

#define SQL_CONNECT_2_STR "Supports the multiple databases per unit of work semantics \
\nDUOW\
\n  TYPE = SQL_2\n\n"

#define SQL_RULES_DB2_STR "Enables the SQL CONNECT statement to switch the current connection to an\
\nestablished (dormant) connection\
\n  TYPE = SQL_DB2\n\n"

#define SQL_RULES_STD_STR "Permits the establishment of a new connection only through SQL CONNECT\
\nstatement.  Under SQL_STD, the SQL SET CONNECTION statement is used to switch\
\nthe current connection to a dormant connection.\
\n  TYPE = SQL_STD\n\n"

#define SQL_DISCONNECT_EXPL_STR "Breaks those connections that have been explicitly marked for release at commit\
\nby the SQL RELEASE statement\
\n  TYPE = SQL_EXPLICIT\n\n"

#define SQL_DISCONNECT_COND_STR "Breaks those connections that have no open WITH HOLD cursors at commit,\
\nand those that have been marked for release by the SQL RELEASE statement\
\n  TYPE = SQL_CONDITIONAL\n\n"

#define SQL_DISCONNECT_AUTO_STR "Breaks all connections at commit\
\n  TYPE = SQL_AUTOMATIC\n\n"

#define SQL_SYNC_TWOPHASE_STR "Requires a Transaction Manager (TM) to coordinate two-phase commits among\
\ndatabases that support this protocol\
\n  TYPE = SQL_TWOPHASE\n\n"

#define SQL_SYNC_ONEPHASE_STR "Uses one-phase commits to commit the work done by each database in multiple\
\ndatabase transactions.  Enforces single updater, multiple read behaviour\
\n  TYPE = SQL_ONEPHASE\n\n"

#define SQL_SYNC_NONE_STR "Does not enforce two-phase commits, or singer updater, multiple\
\nread behaviour.\
\n  TYPE = SQL_NONE\n\n"

#define SQL_MAX_NETBIOS_CONNECTIONS_STR "This specifies the maximum number of concurrent connections that can be\
\nmade using the NETBIOS adapter in an application\
\n  CONNECTIONS = %d\n"


#define  CHECKERR(CE_STR)   check_error (CE_STR, &sqlca)

#define NUMSETTINGS 5


class Administrator {
   public:
      void QueryConnSettings();
      void ModifyConnSettings();
      void RestoreConnSettings();

   private: //  member functions 
      int printConnSettings (struct sqle_conn_setting *);

   private: //  variables 
      struct sqle_conn_setting connSetting[NUMSETTINGS];
      short temp_val1, temp_val2, temp_val3, temp_val4, temp_val5;
};


void Administrator::QueryConnSettings (){
   int rc;
   struct sqlca sqlca;

   connSetting[0].type = SQL_CONNECT_TYPE;
   connSetting[1].type = SQL_RULES;
   connSetting[2].type = SQL_DISCONNECT;
   connSetting[3].type = SQL_SYNCPOINT;
   connSetting[4].type = SQL_MAX_NETBIOS_CONNECTIONS;
   printf ("QUERY CLIENT**************************************************\n");
   /*************************\
   * QUERY CLIENT API called *
   \*************************/
   sqleqryc (&connSetting[0], NUMSETTINGS, &sqlca);
   CHECKERR ("QUERY CLIENT");
   rc = printConnSettings(&connSetting[0]);

} //  QueryConnSettings 


void Administrator::ModifyConnSettings (){
   int rc;
   struct sqlca sqlca;

   /* save the current values */
   temp_val1 = connSetting[0].value;
   temp_val2 = connSetting[1].value;
   temp_val3 = connSetting[2].value;
   temp_val4 = connSetting[3].value;
   temp_val5 = connSetting[4].value;
   /* set new values */
   connSetting[0].value = SQL_CONNECT_2;
   connSetting[1].value = SQL_RULES_STD;
   connSetting[2].value = SQL_DISCONNECT_COND;
   connSetting[3].value = SQL_SYNC_TWOPHASE;
   connSetting[4].value = 254;
   printf ("SET CLIENT****************************************************\n");
   printf ("connect type        = SQL_CONNECT_2\n");
   printf ("rules               = SQL_RULES_STD\n");
   printf ("disconnect          = SQL_DISCONNECT_COND\n");
   printf ("syncpoint           = SQL_SYNC_TWOPHASE\n");
   printf ("max netbios conn.   = 254\n");
   /***********************\
   * SET CLIENT API called *
   \***********************/
   sqlesetc (&connSetting[0], NUMSETTINGS, &sqlca);
   CHECKERR ("SET CLIENT");
   printf ("QUERY CLIENT**************************************************\n");
   /*************************\
   * QUERY CLIENT API called *
   \*************************/
   sqleqryc (&connSetting[0], NUMSETTINGS, &sqlca);
   CHECKERR ("QUERY CLIENT");
   rc = printConnSettings(&connSetting[0]);

} //  ModifyConnSettings 

void Administrator::RestoreConnSettings (){
   int rc;
   struct sqlca sqlca;


   connSetting[0].value = temp_val1;
   connSetting[1].value = temp_val2;
   connSetting[2].value = temp_val3;
   connSetting[3].value = temp_val4;
   connSetting[4].value = temp_val5;
   printf ("SET CLIENT back to original settings *************************\n");
   /***********************\
   * SET CLIENT API called *
   \***********************/
   sqlesetc (&connSetting[0], NUMSETTINGS, &sqlca);
   CHECKERR ("SET CLIENT");

} //  RestoreConnSettings 


int Administrator::printConnSettings (struct sqle_conn_setting *connP) {

   printf ("\n\n\n");
   printf ("SQL CONNECTION TYPE\n");
   printf ("===================\n");
   switch (connP->value){
      case SQL_CONNECT_1:
         printf (SQL_CONNECT_1_STR);
         break;
      case SQL_CONNECT_2:
         printf (SQL_CONNECT_2_STR);
         break;
      default:
         printf ("undefined value = %d\n\n", connP->value);
   } /* endswitch */

   connP++;
   printf ("SQL RULES\n");
   printf ("=========\n");
   switch (connP->value) {
      case SQL_RULES_DB2:
         printf (SQL_RULES_DB2_STR);
         break;
      case SQL_RULES_STD:
         printf (SQL_RULES_STD_STR);
         break;
      default:
         printf ("undefined value = %d\n\n", connP->value);
   } /* endswitch */

   connP++;
   printf ("SQL DISCONNECT\n");
   printf ("==============\n");
   switch (connP->value) {
      case SQL_DISCONNECT_EXPL:
         printf(SQL_DISCONNECT_EXPL_STR);
         break;
      case SQL_DISCONNECT_COND:
         printf(SQL_DISCONNECT_COND_STR);
         break;
      case SQL_DISCONNECT_AUTO:
         printf(SQL_DISCONNECT_AUTO_STR);
         break;
      default:
         printf ("undefined value = %d\n\n", connP->value);
   } /* endswitch */

   connP++;
   printf ("SQL SYNCPOINT\n");
   printf ("=============\n");
   switch (connP->value) {
      case SQL_SYNC_TWOPHASE:
         printf (SQL_SYNC_TWOPHASE_STR);
         break;
      case SQL_SYNC_ONEPHASE:
         printf (SQL_SYNC_ONEPHASE_STR);
         break;
      case SQL_SYNC_NONE:
         printf (SQL_SYNC_NONE_STR);
         break;
      default:
         printf ("undefined value = %d\n\n", connP->value);
   } /* endswitch */

   connP++;
   printf ("SQL MAX NETBIOS CONNECTIONS\n");
   printf ("===========================\n");
   printf (SQL_MAX_NETBIOS_CONNECTIONS_STR, connP->value);

   printf ("\n\n\n");

   return 0;
} //  printConnSettings 




int main (void) {
   Administrator admin;


   #ifdef DB268K
   /* Before making any API calls for 68K environment,
      need to initial the Library Manager */
   InitLibraryManager(0,kCurrentZone,kNormalMemory);
   atexit(CleanupLibraryManager);
   #endif

   admin.QueryConnSettings();
   admin.ModifyConnSettings();
   admin.RestoreConnSettings();

   return 0;
}


//  end of file : client.sqx