/****************************************************************************
** (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: dbconn.sqc 
**    
** SAMPLE: How to connect to and disconnect from a database 
**           
** DB2 API USED:
**         db2DatabaseRestart -- RESTART DATABASE
**         sqlefrce -- FORCE APPLICATION
**
** SQL STATEMENT USED:
**         CONNECT 
**
**                           
*****************************************************************************
**
** 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 information on DB2 APIs, see the Administrative API Reference.
**
** For the latest information on programming, building, and running DB2 
** applications, visit the DB2 Information Center: 
**     http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlenv.h>
#include <sqlutil.h>
#include <db2ApiDf.h>
#include "utilemb.h"

int DbConnect(char *, char *, char *);
int DbRestart(char *, char *, char *);
int DbDisconnect(void);
int AllApplicationsConnectedToAllDatabasesForceOff(void);

int main(int argc, char *argv[])
{
  int rc = 0;
  char nodeName[SQL_INSTNAME_SZ + 1];

  EXEC SQL BEGIN DECLARE SECTION;
    char dbAlias[15];
    char user[128 + 1];
    char pswd[15];
  EXEC SQL END DECLARE SECTION;

  /* check the command line arguments */
  rc = CmdLineArgsCheck3(argc, argv, dbAlias, nodeName, user, pswd);
  if (rc != 0)
  {
    return rc;
  }

  printf("\nTHIS SAMPLE SHOWS HOW TO CONNECT TO/DISCONNECT FROM DATABASES.");
  printf("\n");

  rc = DbConnect(dbAlias, user, pswd);
  rc = DbRestart(dbAlias, user, pswd);
  rc = DbDisconnect();

  /* attach to a local or remote instance */
  rc = InstanceAttach(nodeName, user, pswd);
  if (rc != 0)
  {
    return rc;
  }

  /* The next function will disconnect all the applications from all */
  /* the databases located in the instance you are attached to. */
  /* Uncomment the next function if this is acceptable. */

  /* rc = AllApplicationsConnectedToAllDatabasesForceOff(); */

  /* detach from the local or remote instance */
  rc = InstanceDetach(nodeName);
  if (rc != 0)
  {
    return rc;
  }

  return rc;
} /* end main */

int DbConnect(char dbAlias[], char user[], char pswd[])
{
  struct sqlca sqlca;

  printf("\n-----------------------------------------------------------");
  printf("\nUSE THE SQL STATEMENT:\n");
  printf("  CONNECT TO\n");
  printf("TO CONNECT TO A DATABASE.\n");

  /* connect to a database */
  printf("\n  Execute the statement\n");
  printf("    CONNECT TO %s\n", dbAlias);
  if (strlen(user) > 0)
  {
    EXEC SQL CONNECT TO :dbAlias USER :user USING :pswd;
    EMB_SQL_CHECK("database -- connect with userid and password");
  }
  else
  {

    EXEC SQL CONNECT TO :dbAlias;
    EMB_SQL_CHECK("Database -- Connect");
  }

  return 0;
} /* DbConnect */

int DbRestart(char dbAlias[], char user[], char pswd[])
{
  struct sqlca sqlca;
  struct db2RestartDbStruct dbRestartParam;

  printf("\n-----------------------------------------------------------");
  printf("\nUSE THE DB2 API:\n");
  printf("  db2DatabaseRestart -- RESTART DATABASE\n");
  printf("TO RESTART A DATABASE.\n");

  /* restart a database */
  dbRestartParam.piDatabaseName = dbAlias;
  dbRestartParam.piUserId = user;
  dbRestartParam.piPassword = pswd;
  dbRestartParam.piTablespaceNames = NULL;
  printf("\n  Restart a database.\n");
  printf("    database alias: %s\n", dbAlias);

  /* restart database */
  db2DatabaseRestart(db2Version970, &dbRestartParam, &sqlca);
  DB2_API_CHECK("Database -- Restart");

  return 0;
} /* DbRestart */

int DbDisconnect(void)
{
  struct sqlca sqlca;

  printf("\n-----------------------------------------------------------");
  printf("\nUSE THE SQL STATEMENT:\n");
  printf("  CONNECT RESET");
  printf("\nTO DISCONNECT FROM THE CURRENT DATABASE.\n");

  /* disconnect from the database */
  printf("\n  Execute the statement\n");
  printf("    CONNECT RESET\n");

  EXEC SQL CONNECT RESET;
  EMB_SQL_CHECK("Database -- Disconnect");

  return 0;
} /* DbDisconnect */

int AllApplicationsConnectedToAllDatabasesForceOff(void)
{
  struct sqlca sqlca;

  printf("\n-----------------------------------------------------------");
  printf("\nUSE THE DB2 API:\n");
  printf("  sqlefrce -- FORCE APPLICATION\n");
  printf("TO FORCE OFF ALL THE APPLICATIONS CONNECTED TO ALL DATABASES.\n");

  /* force off all the appl. connected to all databases */
  printf("\n  Force off all applications connected to all databases.\n");

  /* force application */
  sqlefrce(SQL_ALL_USERS, NULL, SQL_ASYNCH, &sqlca);
  DB2_API_CHECK("DBM Config. -- Set");

  return 0;
} /* AllApplicationsConnectedToAllDatabasesForceOff */