/**************************************************************************** ** (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: dbinspec.sqC ** ** SAMPLE: How to check architectural integrity with the DB2 API db2Inspect ** ** This program will create files ResultOutput.txt and ** ResultOutput2.txt in the DB2 diagnostic directory. To obtain ** this value, the db2CfgGet API is used to query SQLF_KTN_DIAGPATH. ** If a value is not obtained, the default directory is used: ** (X\sqllib\DB2\ in the case of Windows or X/sqllib/db2dump/ in the ** case of UNIX, where X is the directory where DB2 is installed). ** The files FormattedOutput.txt and FormattedOutput2.txt are the ** formatted versions of ResultOutput.txt and ResultOutput2.txt and ** are created in the current working directory. ** ** DB2 API USED: ** db2CfgGet ** db2Inspect -- Check architectural integrity ** ** STRUCTURES USED: ** db2InspectStruct ** sqlca ** ** ***************************************************************************** ** ** 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 <string.h> #include <sqlenv.h> #include <sqlutil.h> #include <stdlib.h> #include <db2ApiDf.h> #include "utilemb.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 tableName[128]; char schemaName[128]; EXEC SQL END DECLARE SECTION; class DbInspec { public: int CheckTableIntegrity(); int CheckDatabaseIntegrity(); char *diagpath; }; // This function demonstrates how to check the achitectural integrity of the // STAFF table int DbInspec::CheckTableIntegrity() { int rc = 0; struct sqlca sqlca; // Declare and initialize a 'db2InspectStruct' structure 'inspectstruct' db2InspectStruct inspectStruct = {0}; char cmd1[256]; sprintf(cmd1, "db2inspf \"%sResultOutput.txt\" FormattedOutput.txt", diagpath); cout << "\n DEMONSTRATE HOW TO CHECK THE ARCHITECTURAL INTEGRITY OF THE" << " TABLE 'STAFF'\n" << "\n CREATE A db2InspectStruct STRUCTURE 'inspectStruct' AND" << " INITIALIZE ITS" << "\n MEMBERS TO ZERO" << endl; // Assign values to the members of the structure 'inspectStruct' cout << "\n ASSIGN VALUES TO THE MEMBERS OF THE db2InspectStruct" << " STRUCTURE 'inspectStruct'"; inspectStruct.iAction = DB2INSPECT_ACT_CHECK_TABLE; inspectStruct.piTableName = "STAFF"; EXEC SQL SELECT tabschema INTO :schemaName FROM syscat.tables WHERE tabname = 'STAFF'; EMB_SQL_CHECK("Table schema name -- Get"); inspectStruct.piSchemaName = schemaName; inspectStruct.iBeginCheckOption = DB2INSPECT_BEGIN_FROM_START; inspectStruct.iAllNodeFlag = DB2_ALL_NODES; inspectStruct.piResultsName = "ResultOutput.txt"; inspectStruct.piDataFileName = NULL; inspectStruct.iObjectErrorState = DB2INSPECT_ERROR_STATE_ALL; inspectStruct.iLevelObjectData = DB2INSPECT_LEVEL_NORMAL; inspectStruct.iLevelObjectBlkMap = DB2INSPECT_LEVEL_NORMAL; inspectStruct.iLevelObjectIndex = DB2INSPECT_LEVEL_NORMAL; inspectStruct.iLevelObjectLong = DB2INSPECT_LEVEL_NORMAL; inspectStruct.iLevelObjectLOB = DB2INSPECT_LEVEL_NORMAL; inspectStruct.iLevelExtentMap = DB2INSPECT_LEVEL_NORMAL; inspectStruct.iKeepResultfile = DB2INSPECT_RESFILE_KEEP_ALWAYS; inspectStruct.iLimitErrorReported = DB2INSPECT_LIMIT_ERROR_DEFAULT; cout << "\n CALL THE DB2 API 'db2Inspect' TO CHECK THE ARCHITECTURAL" << " INTEGRITY" << "\n OF THE TABLE 'STAFF' WHICH STORES THE RESULTS IN THE FILE" << " 'ResultOutput.txt'" << endl; // Use the DB2 API db2Inspect db2Inspect(db2Version970, (void *)&inspectStruct, &sqlca); DB2_API_CHECK("DB -- INSPECT"); // Invoke the db2inspf command to format the result file 'ResultOutput.txt' cout << "\n FORMAT THE UNFORMATTED RESULT FILE 'ResultOutput.txt' AND" << "\n SAVE IT IN A FILE 'FormattedOutput.txt' BY ISSUING THE COMMAND:\n\n " << cmd1; rc = system(cmd1); return 0; } // CheckTableIntegrity // This function demonstrates how to check the achitectural integrity of the // SAMPLE database int DbInspec::CheckDatabaseIntegrity() { int rc = 0; struct sqlca sqlca; // Declare and initialize a 'db2InspectStruct' structure 'inspectstruct' db2InspectStruct inspectStruct = {0}; char cmd1[256]; sprintf(cmd1, "db2inspf \"%sResultOutput2.txt\" FormattedOutput2.txt", diagpath); cout << "\n\n DEMONSTRATE HOW TO CHECK THE ARCHITECTURAL INTEGRITY OF THE" << " DATABASE 'SAMPLE'\n" << "\n CREATE A db2InspectStruct STRUCTURE 'inspectStruct' AND" << " INITIALIZE ITS" << "\n MEMEBRS TO ZERO" << endl; // Assign values to the members of the structure 'inspectStruct' cout << "\n ASSIGN VALUES TO THE MEMBERS OF THE db2InspectStruct" << " STRUCTURE 'inspectStruct'"; inspectStruct.iAction = DB2INSPECT_ACT_CHECK_DB; inspectStruct.iBeginCheckOption = DB2INSPECT_BEGIN_FROM_START; inspectStruct.iAllNodeFlag = DB2_ALL_NODES; inspectStruct.piResultsName = "ResultOutput2.txt"; inspectStruct.piDataFileName = NULL; inspectStruct.iObjectErrorState = DB2INSPECT_ERROR_STATE_ALL; inspectStruct.iLevelObjectData = DB2INSPECT_LEVEL_NORMAL; inspectStruct.iLevelObjectBlkMap = DB2INSPECT_LEVEL_NORMAL; inspectStruct.iLevelObjectIndex = DB2INSPECT_LEVEL_NORMAL; inspectStruct.iLevelObjectLong = DB2INSPECT_LEVEL_NORMAL; inspectStruct.iLevelObjectLOB = DB2INSPECT_LEVEL_NORMAL; inspectStruct.iLevelExtentMap = DB2INSPECT_LEVEL_NORMAL; inspectStruct.iKeepResultfile = DB2INSPECT_RESFILE_KEEP_ALWAYS; inspectStruct.iLimitErrorReported = DB2INSPECT_LIMIT_ERROR_DEFAULT; cout << "\n CALL THE DB2 API 'db2Inspect' TO CHECK THE ARCHITECTURAL" << " INTEGRITY" << "\n OF THE DATABASE 'SAMPLE' WHICH STORES THE RESULTS IN THE FILE" << "\n 'ResultOutput2.txt'" << endl; // Use the DB2 API db2Inspect db2Inspect(db2Version970, (void *)&inspectStruct, &sqlca); DB2_API_CHECK("DB -- INSPECT"); // Invoke the db2inspf command to format the result file // 'ResultOutput2.txt' cout << "\n FORMAT THE UNFORMATTED RESULT FILE 'ResultOutput2.txt' AND" << "\n SAVE IT IN A FILE 'FormattedOutput2.txt' BY ISSUING THE COMMAND:\n\n " << cmd1 << endl; rc = system(cmd1); return 0; } // CheckDatabaseIntegrity int main(int argc, char *argv[]) { int rc = 0; struct sqlca sqlca; CmdLineArgs check; DbInspec dbIns; DbEmb db; char nodeName[SQL_INSTNAME_SZ + 1]; char dbAlias[SQL_ALIAS_SZ + 1]; char user[USERID_SZ + 1]; char pswd[PSWD_SZ + 1]; char cmd1[256]; char cmd2[256]; char cmd3[256]; char cmd4[256]; #if (defined(DB2NT)) char *db2path; // path where db2 is installed char *db2instance; #else char *home; // instance owners home directory #endif // The following variables are for querying the dbm cfg for diagpath db2CfgParam cfgParameters[1]; db2Cfg cfgStruct; // check the command line arguments rc = check.CmdLineArgsCheck1(argc, argv, db); if (rc != 0) { return rc; } cout.setf(ios::fixed, ios::floatfield); cout << "\n THIS SAMPLE SHOWS HOW TO CHECK ARCHITECTURAL INTEGRITY USING" << "\n THE DB2 API db2Inspect" << endl; // connect to database rc = db.Connect(); if (rc != 0) { return rc; } // Initialize cfgParameters cfgParameters[0].flags = 0; cfgParameters[0].token = SQLF_KTN_DIAGPATH; cfgParameters[0].ptrvalue = (char *)malloc(sizeof(char) * (SQL_FFDCPATH_SZ + 1)); // Initialize cfgStruct cfgStruct.numItems = 1; cfgStruct.paramArray = cfgParameters; cfgStruct.flags = db2CfgDatabaseManager; cfgStruct.dbname = dbAlias; // Try getting the diagpath from db2CfgGet db2CfgGet(db2Version970, (void *)&cfgStruct, &sqlca); DB2_API_CHECK("DBM Config. Defaults -- Get"); dbIns.diagpath = cfgStruct.paramArray[0].ptrvalue; // If diagpath wasn't obtainable from db2CfgGet, set diagpath to the // default value (operating system dependent) if(dbIns.diagpath[0] == '\0') { #if(defined(DB2NT)) db2path = getenv("DB2PATH"); db2instance = getenv("DB2INSTANCE"); if(db2path == NULL || db2instance == NULL) { cout << "\nError getting the diagpath, exiting."; return 1; } sprintf(dbIns.diagpath, "%s\\%s\\", db2path, db2instance); #else home = getenv("HOME"); if(home == NULL) { cout << "\nError getting the diagpath, exiting."; return 1; } sprintf(dbIns.diagpath, "%s/sqllib/db2dump/", home); #endif } #if (defined(DB2NT)) sprintf(cmd1, "del \"%sResultOutput.txt\"", dbIns.diagpath); sprintf(cmd2, "del \"%sResultOutput2.txt\"", dbIns.diagpath); sprintf(cmd3, "del FormattedOutput.txt"); sprintf(cmd4, "del FormattedOutput2.txt"); #else sprintf(cmd1, "rm -f %sResultOutput.txt", dbIns.diagpath); sprintf(cmd2, "rm -f %sResultOutput2.txt", dbIns.diagpath); sprintf(cmd3, "rm -f FormattedOutput.txt"); sprintf(cmd4, "rm -f FormattedOutput2.txt"); #endif // Delete the files ResultOutput.txt, ResultOutput2.txt, // FormattedOutput.txt and FormattedOutput2.txt if they already exist #if (defined(DB2NT)) cout << "\n DELETE FILES 'ResultOutput.txt' AND 'ResultOutput2.txt'" << " FROM THE DIAGNOSTIC" << "\n DATA DIRECTORY (" << dbIns.diagpath << "), AND FILES" << " 'FormattedOutput.txt'" << "\n AND 'FormattedOutput2.txt' FROM THE CURRENT WORKING" << " DIRECTORY, IF THEY EXIST" << endl; #else // UNIX cout << "\n DELETE FILES 'ResultOutput.txt' AND 'ResultOutput2.txt'" << " FROM THE DIAGNOSTIC" << "\n DATA DIRECTORY (" << dbIns.diagpath << "), AND FILES" << " 'FormattedOutput.txt'" << "\n AND 'FormattedOutput2.txt' FROM THE CURRENT WORKING" << " DIRECTORY, IF THEY EXIST" << endl; #endif rc = system(cmd1); rc = system(cmd2); rc = system(cmd3); rc = system(cmd4); rc = dbIns.CheckTableIntegrity(); rc = dbIns.CheckDatabaseIntegrity(); // disconnect from the database rc = db.Disconnect(); if (rc != 0) { return rc; } return 0; } // main