/******************************************************************************
**
** Source File Name = dbstat.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 program uses embedded SQL calls to connect to an existing
**       database. It then creates a temporary table to work with.
**
**    STRUCTURES USED :
**       sqlca
**
**    APIs USED :
**       REORGANIZE TABLE           sqlureot()
**       RUN STATISTICS             sqlustat()
**
**    FUNCTIONS DECLARED :
**       'C' COMPILER LIBRARY :
**          stdio.h  -  printf
**          string.h -  strncpy
**
**       DBMS LIBRARY :
**          sqlenv.h -  see "APIs USED" above
**
**       OTHER :
**          external :
**             check_error :     Checks for SQLCODE error, and prints out any
**             [in UTIL.C]       related information available.
**
**    EXTERNAL DEPENDANCIES :
**       - Ensure existence of database for precompile purposes.
**       - Precompile with the SQL precompiler (PREP in DB2)
**       - Bind to a database (BIND in DB2)
**       - Compile and link 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>
#include <sqlenv.h>
#include <sqlutil.h>
#include "utilemb.h"



EXEC SQL INCLUDE SQLCA;

int main (int argc, char *argv[]) {
   char testIndex[]="sample.testind";
   char qualifierTable[80] = "";
   char *tarray = testIndex;

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


   if (argc == 3) { 
      strcpy (userid, argv[1]);
      strcpy (passwd, argv[2]);
      EXEC SQL CONNECT TO sample USER :userid USING :passwd; 
      EMB_SQL_CHECK("CONNECT TO SAMPLE");
   }
   else {
      printf ("\nUSAGE: dbstat userid passwd\n\n"); 
      return 1;
   } /* endif */
   
   EXEC SQL DROP INDEX sample.testind;

   printf("CREATE INDEX\n");
   EXEC SQL CREATE INDEX sample.testind ON staff (salary);
   EMB_SQL_CHECK("CREATE INDEX");

   strcpy (qualifierTable, argv[1]);
   strcat (qualifierTable, ".staff");

   printf("REORGanizing the TABLE\n");
   /****************************************\
   * REORGANIZE TABLE STATISTICS API called *
   \****************************************/
   sqlureot (qualifierTable, "sample.testind", NULL, &sqlca);
   EMB_SQL_CHECK("reorganizing the STAFF table");

   printf("RUNning STATISTICS\n");
   /***************************\
   * RUN STATISTICS API called *
   \***************************/
   sqlustat (qualifierTable, 1, &tarray, SQL_STATS_TABLE,
      SQL_STATS_CHG, &sqlca);
   EMB_SQL_CHECK("stats");

   printf("CONNECT RESETting\n");
   EXEC SQL CONNECT RESET;
   EMB_SQL_CHECK("CONNECT RESET");
   return 0;
}