/****************************************************************************
** (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: utilemb.sqC
**
** SAMPLE: Checks for and prints to the screen SQL warnings and errors
**
**         This utility file is compiled and linked in as an object module 
**         with embedded SQL sample programs by the supplied makefile and 
**         build files.
**
** SQL STATEMENTS USED:
**         BEGIN DECLARE SECTION
**         END DECLARE SECTION
**   �     ROLLBACK
**         CONNECT
**
** CLASS FUNCTIONS:
**         DbEmb::TransRollback() - rolls back the transaction
**         DbEmb::Connect() - connects to the database
**         DbEmb::Disconnect() - disconnects from the database
**
*****************************************************************************
**
** 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 <sql.h>
#include <sqlenv.h>
#include <sqlda.h>
#include <sqlca.h>
#include "utilemb.h"
#if ( defined( DB2NT ) )
#include "utilapi.cxx"
#else //UNIX
#include "utilapi.C"
#endif
#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;
  char dbAlias[15];
  char dbUser[129];
  char dbPswd[15];
EXEC SQL END DECLARE SECTION;

void DbEmb::TransRollback()
{
  struct sqlca sqlca;

  // rollback the transaction
  cout << "\n  Rolling back the transaction..." << endl;

  EXEC SQL ROLLBACK;
  SqlInfo::SqlInfoPrint("ROLLBACK", &sqlca, __LINE__, __FILE__);
  if (sqlca.sqlcode == 0)
  {
    cout << "  The transaction was rolled back." << endl;
  }

} //DbEmb::TransRollback

int DbEmb::Connect()
{
  struct sqlca sqlca;

  strcpy(dbAlias, alias);
  strcpy(dbUser, user);
  strcpy(dbPswd, pswd);

  cout << "\n  Connecting to '";
  cout << alias << "' database..." << endl;
  if (strlen(dbUser) == 0)
  {
    EXEC SQL CONNECT TO :dbAlias;
    EMB_SQL_CHECK("CONNECT");
  }
  else
  {
    EXEC SQL CONNECT TO :dbAlias USER :dbUser USING :dbPswd;
    EMB_SQL_CHECK("CONNECT");
  }
  cout << "  Connected to '" << dbAlias << "' database." << endl;

  return 0;

} //DbEmb::Connect

int DbEmb::Disconnect()
{
  struct sqlca sqlca;

  cout << "\n  Disconnecting from '";
  cout << alias << "' database..." << endl;

  EXEC SQL CONNECT RESET;
  EMB_SQL_CHECK("CONNECT RESET");

  cout << "  Disconnected from '";
  cout << alias << "' database." << endl;

  return 0;

} //DbEmb::Disconnect