/****************************************************************************
** (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: dbmcon.sqC 
**    
** SAMPLE: How to use multiple databases
**
**         This sample program requires a second database 
**         that has to be created as follows:
**         - locally:
**            db2 create db sample2
**         - remotely:
**            db2 attach to node_name
**            db2 create db sample2
**            db2 detach
**            db2 catalog db sample2 as sample2 at node node_name
**         If another name is used for the second database,
**         it can be specified in the command line arguments
**         as follows:
**            dbmcon [dbAlias1 dbAlias2 [user1 pswd1 [user2 pswd2]]
**         The second database can be dropped as follows:
**         - locally:
**            db2 drop db sample2
**         - remotely:
**            db2 attach to node_name
**            db2 drop db sample2
**            db2 detach
**            db2 uncatalog db sample2
**
**         This sample also requires that the TCPIP listener is running. To
**         ensure this, do the following:
**         1. Set the environment variable DB2COMM to TCPIP as follows: 
**         "db2set DB2COMM=TCPIP"
**         2. Update the database manager configuration file with the TCP/IP 
**         service name as specified in the services file:
**         "db2 update dbm cfg using SVCENAME <TCP/IP service name>"
**         You must do a "db2stop" and "db2start" for this setting to take
**         effect.
**           
** DB2 API USED:
**          sqlesetc -- SET CLIENT	 
**
** SQL STATEMENTS USED:
**          CONNECT
**          SET CONNECTION
**          DISCONNECT
**
**                           
*****************************************************************************
**
** For more information on the sample programs, see the README file.
**
** For information on developing C++ applications, see the Application
** Development Guide.
**
** For information on using SQL statements, see the SQL Reference.
**
** For information on DB2 APIs, see the Administrative API 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 "utilemb.h"
#include "dbmcon1.h"
#include "dbmcon2.h"
#if ((__cplusplus >= 199711L) && !defined DB2HP && !defined DB2AIX) || \
    (DB2LINUX && (__LP64__ || (__GNUC__ >= 3)) )
   #include <iomanip>
   #include <iostream>
   using namespace std; 
#else
   #include <iomanip.h>
   #include <iostream.h>
#endif

EXEC SQL BEGIN DECLARE SECTION;
  char dbAlias1[15];
  char dbAlias2[15];
  char user1[129];
  char user2[129];
  char pswd1[15];
  char pswd2[15];
EXEC SQL END DECLARE SECTION;

class DbMCon
{
  public:
    int TwoDbConnectType1();
    int TwoDbConnectType2OnePhaseCommit();
};

int DbMCon::TwoDbConnectType1()
{
  int rc = 0;
  struct sqlca sqlca;
  struct sqle_conn_setting clientAppInfo[2];
  DbMCon1 firstDb;
  DbMCon2 secondDb;

  cout << "\n-----------------------------------------------------------";
  cout << "\nUSE THE DB2 API:" << endl;
  cout << "  sqlesetc -- SET CLIENT" << endl;
  cout << "AND THE SQL STATEMENTS:" << endl;
  cout << "  CONNECT" << endl;
  cout << "  COMMIT" << endl;
  cout << "  CONNECT RESET" << endl;
  cout << "TO CONNECT 'TYPE1' TO TWO DATABASES." << endl;

  // set client connection attributes
  cout << "\n  Set the Client Connection Attributes to the values:" << endl;
  cout << "    SQL_CONNECT_TYPE = SQL_CONNECT_1" << endl;
  cout << "    SQL_DISCONNECT   = SQL_DISCONNECT_EXPL" << endl;

  clientAppInfo[0].type = SQL_CONNECT_TYPE;
  clientAppInfo[0].value = SQL_CONNECT_1;

  clientAppInfo[1].type = SQL_DISCONNECT;
  clientAppInfo[1].value = SQL_DISCONNECT_EXPL;

  // set client information
  sqlesetc(&clientAppInfo[0], 2, &sqlca);
  DB2_API_CHECK("Client Connection Attributes -- set");

  // work with databases

  /* ------------------------------------- */

  cout << "\n  CONNECT TO " << dbAlias1 << endl;
  if (strlen(user1) > 0)
  {
    EXEC SQL CONNECT TO :dbAlias1 USER :user1 USING :pswd1;
  }
  else
  {
    EXEC SQL CONNECT TO :dbAlias1;
  }
  EMB_SQL_CHECK("first database -- connect");

  rc = firstDb.CreateTable();

  cout << "    COMMIT" << endl;

  EXEC SQL COMMIT;
  EMB_SQL_CHECK("transaction -- commit");

  cout << "  CONNECT RESET" << endl;

  EXEC SQL CONNECT RESET;
  EMB_SQL_CHECK("connection -- reset");

  /* ------------------------------------- */

  cout << "\n  CONNECT TO " << dbAlias2 << endl;
  if (strlen(user2) > 0)
  {
    EXEC SQL CONNECT TO :dbAlias2 USER :user2 USING :pswd2;
  }
  else
  {
    EXEC SQL CONNECT TO :dbAlias2;
  }
  EMB_SQL_CHECK("second database -- connect");

  rc = secondDb.CreateTable();

  cout << "    COMMIT" << endl;

  EXEC SQL COMMIT;
  EMB_SQL_CHECK("transaction -- commit");

  cout << "  CONNECT RESET" << endl;

  EXEC SQL CONNECT RESET;
  EMB_SQL_CHECK("connection -- reset");

  /* ------------------------------------- */

  cout << "\n  CONNECT TO " << dbAlias1 << endl;
  if (strlen(user1) > 0)
  {
    EXEC SQL CONNECT TO :dbAlias1 USER :user1 USING :pswd1;
  }
  else
  {
    EXEC SQL CONNECT TO :dbAlias1;
  }
  EMB_SQL_CHECK("first database -- connect");

  rc = firstDb.DropTable();

  cout << "    COMMIT" << endl;

  EXEC SQL COMMIT;
  EMB_SQL_CHECK("transaction -- commit");

  cout << "  CONNECT RESET" << endl;

  EXEC SQL CONNECT RESET;
  EMB_SQL_CHECK("connection -- reset");

  /* ------------------------------------- */

  cout << "\n  CONNECT TO " << dbAlias2 << endl;
  if (strlen(user2) > 0)
  {
    EXEC SQL CONNECT TO :dbAlias2 USER :user2 USING :pswd2;
  }
  else
  {
    EXEC SQL CONNECT TO :dbAlias2;
  }
  EMB_SQL_CHECK("first database -- connect");

  rc = secondDb.DropTable();

  cout << "    COMMIT" << endl;

  EXEC SQL COMMIT;
  EMB_SQL_CHECK("transaction -- commit");

  cout << "  CONNECT RESET" << endl;

  EXEC SQL CONNECT RESET;
  EMB_SQL_CHECK("connection -- reset");

  return 0;
} //DbMCon::TwoDbConnectType1

int DbMCon::TwoDbConnectType2OnePhaseCommit()
{
  int rc = 0;
  struct sqlca sqlca;
  struct sqle_conn_setting clientAppInfo[3];
  DbMCon1 firstDb;
  DbMCon2 secondDb;

  cout << "\n-----------------------------------------------------------";
  cout << "\nUSE THE DB2 API:" << endl;
  cout << "  sqlesetc -- SET CLIENT" << endl;
  cout << "AND THE SQL STATEMENTS:" << endl;
  cout << "  CONNECT" << endl;
  cout << "  SET CONNECTION" << endl;
  cout << "  COMMIT" << endl;
  cout << "  DISCONNECT" << endl;
  cout << "TO CONNECT 'TYPE2 - ONE PHASE' TO TWO DATABASES." << endl;

  // set client connection attributes
  cout << "\n  Set the Client Connection Attributes to the values:" << endl;
  cout << "    SQL_CONNECT_TYPE = SQL_CONNECT_2" << endl;
  cout << "    SQL_DISCONNECT   = SQL_DISCONNECT_EXPL" << endl;
  cout << "    SQL_SYNCPOINT    = SQL_SYNC_ONEPHASE" << endl;

  clientAppInfo[0].type = SQL_CONNECT_TYPE;
  clientAppInfo[0].value = SQL_CONNECT_2;

  clientAppInfo[1].type = SQL_DISCONNECT;
  clientAppInfo[1].value = SQL_DISCONNECT_EXPL;

  clientAppInfo[2].type = SQL_SYNCPOINT;
  clientAppInfo[2].value = SQL_SYNC_ONEPHASE;

  // set client information
  sqlesetc(&clientAppInfo[0], 3, &sqlca);
  DB2_API_CHECK("Client Connection Attributes -- set");

  // work with databases

  /* ------------------------------------- */

  cout << "\n  CONNECT TO " << dbAlias1 << endl;
  if (strlen(user1) > 0)
  {
    EXEC SQL CONNECT TO :dbAlias1 USER :user1 USING :pswd1;
  }
  else
  {
    EXEC SQL CONNECT TO :dbAlias1;
  }
  EMB_SQL_CHECK("first database -- connect");

  cout << "\n  CONNECT TO " << dbAlias2 << endl;
  if (strlen(user2) > 0)
  {
    EXEC SQL CONNECT TO :dbAlias2 USER :user2 USING :pswd2;
  }
  else
  {
    EXEC SQL CONNECT TO :dbAlias2;
  }
  EMB_SQL_CHECK("second database -- connect");

  /* ------------------------------------- */

  cout << "\n  SET CONNECTION " << dbAlias1 << endl;
  EXEC SQL SET CONNECTION :dbAlias1;
  EMB_SQL_CHECK("first connection -- activate");

  rc = firstDb.CreateTable();

  cout << "    COMMIT" << endl;

  EXEC SQL COMMIT;
  EMB_SQL_CHECK("transaction -- commit");

  /* ------------------------------------- */

  cout << "\n  SET CONNECTION " << dbAlias2 << endl;

  EXEC SQL SET CONNECTION :dbAlias2;
  EMB_SQL_CHECK("second connection -- activate");

  rc = secondDb.CreateTable();

  cout << "    COMMIT" << endl;

  EXEC SQL COMMIT;
  EMB_SQL_CHECK("transaction -- commit");

  /* ------------------------------------- */

  cout << "\n  SET CONNECTION " << dbAlias1 << endl;

  EXEC SQL SET CONNECTION :dbAlias1;
  EMB_SQL_CHECK("first connection -- activate");

  rc = firstDb.DropTable();

  cout << "    COMMIT" << endl;

  EXEC SQL COMMIT;
  EMB_SQL_CHECK("transaction -- commit");

  /* ------------------------------------- */

  cout << "\n  SET CONNECTION " << dbAlias2 << endl;

  EXEC SQL SET CONNECTION :dbAlias2;
  EMB_SQL_CHECK("second connection -- activate");

  rc = secondDb.DropTable();

  cout << "    COMMIT" << endl;

  EXEC SQL COMMIT;
  EMB_SQL_CHECK("transaction -- commit");

  /* ------------------------------------- */

  cout << "\n  DISCONNECT ALL" << endl;

  EXEC SQL DISCONNECT ALL;
  EMB_SQL_CHECK("all connections -- disconnect");

  return 0;
} //DbMCon::TwoDbConnectType2OnePhaseCommit

int main(int argc, char *argv[])
{
  int rc = 0;
  CmdLineArgs check;
  DbMCon con;
  DbEmb db1;
  DbEmb db2;

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

  strcpy(dbAlias1, db1.getAlias());
  strcpy(user1, db1.getUser());
  strcpy(pswd1, db1.getPswd());

  strcpy(dbAlias2, db2.getAlias());
  strcpy(user2, db2.getUser());
  strcpy(pswd2, db2.getPswd());

  cout << "\nTHIS SAMPLE SHOWS HOW TO USE MULTIPLE DATABASES." << endl;

  rc = con.TwoDbConnectType1();
  rc = con.TwoDbConnectType2OnePhaseCommit();

  return 0;
} //main