/****************************************************************************
** (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: tbreorg.sqC
**
** SAMPLE: How to reorganize a table and update its statistics
**
** DB2 APIs USED:
**         db2Reorg -- Reorganize a Table or Index
**         db2Runstats -- Runstats
**
** SQL STATEMENT USED:
**         SELECT
**
**                           
*****************************************************************************
**
** 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, 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 <db2ApiDf.h>
#include "utilemb.h"
#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 tableName[129];
  char schemaName[129];
EXEC SQL END DECLARE SECTION;

class TbReorg
{
  public:
    int TbReorganize();
  private: //support function
    int SchemaNameGet();
};

int TbReorg::SchemaNameGet()
{
  struct sqlca sqlca;

  // get table schema name
  EXEC SQL SELECT tabschema INTO :schemaName
    FROM syscat.tables
    WHERE tabname = :tableName;
  EMB_SQL_CHECK("table schema name -- get");

  // get rid of spaces from the end of schemaName
  strtok(schemaName, " ");

  return 0;
} //TbReorg::SchemaNameGet

int TbReorg::TbReorganize()
{
  int rc = 0;
  struct sqlca sqlca;
  char fullTableName[258];
  db2ReorgStruct paramStruct;
  db2Uint32 versionNumber = db2Version970;
  db2RunstatsData runStatData;


  cout << "\n-----------------------------------------------------------";
  cout << "\nUSE THE DB2 APIs:" << endl;
  cout << "  db2Reorg -- Reorganize a Table or Index" << endl;
  cout << "  db2Runstats -- Runstats" << endl;
  cout << "TO REORGANIZE A TABLE AND UPDATE ITS STATISTICS." << endl;

  // get fully qualified name of the table
  strcpy(tableName, "EMP_RESUME");
  rc = SchemaNameGet();
  if (rc != 0)
  {
    return rc;
  }

  strcpy(fullTableName, schemaName);
  strcat(fullTableName, ".");
  strcat(fullTableName, tableName);

  cout << "  Reorganize the table: " << fullTableName << endl;

  /* setup parameters */
  memset(&paramStruct, '\0', sizeof(paramStruct));
  paramStruct.reorgObject.tableStruct.pTableName = fullTableName;
  paramStruct.reorgObject.tableStruct.pOrderByIndex = NULL;
  paramStruct.reorgObject.tableStruct.pSysTempSpace = NULL;
  paramStruct.reorgType = DB2REORG_OBJ_TABLE_OFFLINE;
  paramStruct.reorgFlags = DB2REORG_LONGLOB;
  paramStruct.nodeListFlag = DB2_ALL_NODES;
  paramStruct.numNodes = 0;
  paramStruct.pNodeList = NULL;

  // reorganize table
  rc = db2Reorg(versionNumber, &paramStruct, &sqlca);
  DB2_API_CHECK("table -- reorganize");

  cout << "  Update statistics for the table: " << fullTableName << endl;

  // runstats table
  runStatData.iSamplingOption = 0;
  runStatData.piTablename = ( unsigned char *) fullTableName;
  runStatData.piColumnList = NULL;
  runStatData.piColumnDistributionList = NULL;
  runStatData.piColumnGroupList = NULL;
  runStatData.piIndexList = NULL;
  runStatData.iRunstatsFlags = DB2RUNSTATS_ALL_INDEXES;

  runStatData.iRunstatsFlags |= DB2RUNSTATS_SAMPLING_SYSTEM;
  runStatData.iSamplingOption = 20; // each page has a 20% chance of being
                                    // included in the sample
  
  runStatData.iRunstatsFlags |= DB2RUNSTATS_SAMPLING_REPEAT;
  runStatData.iSamplingRepeatable = 23; // seed to keep results consistent

  runStatData.iNumColumns = 0;
  runStatData.iNumColdist = 0;
  runStatData.iNumColGroups = 0;
  runStatData.iNumIndexes = 0;
  runStatData.iParallelismOption = 0;
  runStatData.iTableDefaultFreqValues = 0;
  runStatData.iTableDefaultQuantiles = 0;
  runStatData.iUtilImpactPriority      = 100;

  db2Runstats (versionNumber, &runStatData, &sqlca);
  DB2_API_CHECK("table -- runstats");

  cout << "  Make sure to rebind all packages that use this table." << endl;

  return rc;
} //TbReorg::TbReorganize

int main(int argc, char *argv[])
{
  int rc = 0;
  CmdLineArgs check;
  TbReorg reorg;
  DbEmb db;

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

  cout << "\nTHIS SAMPLE SHOWS ";
  cout << "HOW TO REORGANIZE A TABLE AND UPDATE ITS STATISTICS." << endl;

  // connect to database
  rc = db.Connect();
  if (rc != 0)
  {
    return rc;
  }

  rc = reorg.TbReorganize();

  // disconnect from the database
  rc = db.Disconnect();
  if (rc != 0)
  {
    return rc;
  }

  return 0;
} //main