/**************************************************************************** ** (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: dbpkg.sqC ** ** SAMPLE: How to work with packages ** ** DB2 APIs USED: ** sqlaprep -- PRECOMPILE PROGRAM ** sqlabndx -- BIND ** sqlarbnd -- REBIND PACKAGE ** ** SQL STATEMENT USED: ** DROP PACKAGE ** ** ***************************************************************************** ** ** 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 <sql.h> #include <sqlenv.h> #include <sqlutil.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 class DbPkg { public: int BndFileCreate(char *); int PackageCreate(char *); int PackageRefresh(char *); int PackageDrop(); }; int DbPkg::BndFileCreate(char *sourceFileBaseName) { struct sqlca sqlca; char sourceFileName[8 + 1 + 3 + 1]; char msgFileName[8 + 1 + 3 + 1]; struct sqlopt *pPrecompileOptions; struct sqloptions *optionsArray; cout << "\n-----------------------------------------------------------"; cout << "\nUSE THE DB2 API:" << endl; cout << " sqlaprep -- PRECOMPILE PROGRAM" << endl; cout << "TO CREATE A BIND FILE." << endl; // create the package strcpy(sourceFileName, sourceFileBaseName); #if (defined(DB2NT)) strcat(sourceFileName, ".sqx"); #else // UNIX strcat(sourceFileName, ".sqC"); #endif strcpy(msgFileName, sourceFileBaseName); msgFileName[6] = '\0'; // truncate to 6 characters strcat(msgFileName, "_p.msg"); // _p = precompile cout << "\n Create the bind file." << endl; cout << " source file name : " << sourceFileName << endl; cout << " bind file name : " << sourceFileBaseName << ".bnd" << endl; cout << " message file name: " << msgFileName << endl; // for 1 option no more struct sqloptions are needed because // struct sqlopt already contains one pPrecompileOptions = new sqlopt[1]; pPrecompileOptions->header.allocated = 1; // number of options pPrecompileOptions->header.used = 1; optionsArray = pPrecompileOptions->option; optionsArray[0].type = SQL_BIND_OPT; optionsArray[0].val = 0; // create the default bnd file // procompile program sqlaprep(sourceFileName, msgFileName, pPrecompileOptions, &sqlca); DB2_API_CHECK("Bnd File -- Create"); // release the memory allocated delete [] pPrecompileOptions; return 0; } //DbPkg::BndFileCreate int DbPkg::PackageCreate(char *sourceFileBaseName) { struct sqlca sqlca; char bndFileName[8 + 1 + 3 + 1]; char msgFileName[8 + 1 + 3 + 1]; struct sqlopt *pBindOptions; struct sqloptions *optionsArray; cout << "\n-----------------------------------------------------------"; cout << "\nUSE THE DB2 API:" << endl; cout << " sqlabndx -- BIND" << endl; cout << "TO CREATE A PACKAGE." << endl; // create the package strcpy(bndFileName, sourceFileBaseName); strcat(bndFileName, ".bnd"); strcpy(msgFileName, sourceFileBaseName); msgFileName[6] = '\0'; // truncate to 6 characters strcat(msgFileName, "_b.msg"); // _b = bind cout << "\n Create the package." << endl; cout << " bind file name : " << bndFileName << endl; cout << " package name : " << sourceFileBaseName << endl; cout << " message file name: " << msgFileName << endl; // for 2 options one more struct sqloptions is needed because // struct sqlopt already contains one pBindOptions = new sqlopt[2]; pBindOptions->header.allocated = 2; // number of options pBindOptions->header.used = 2; optionsArray = pBindOptions->option; optionsArray[0].type = SQL_BLOCK_OPT; optionsArray[0].val = SQL_BL_ALL; // cursor blocking = block all optionsArray[1].type = SQL_ISO_OPT; optionsArray[1].val = SQL_CURSOR_STAB; // isol. level = cursor stab. // invoke the bind utility sqlabndx(bndFileName, msgFileName, pBindOptions, &sqlca); DB2_API_CHECK("Package -- Create"); // release the memory allocated delete [] pBindOptions; return 0; } //DbPkg::PackageCreate int DbPkg::PackageRefresh(char *sourceFileBaseName) { struct sqlca sqlca; char packageName[8 + 1]; struct sqlopt *pRebindOptions; cout << "\n-----------------------------------------------------------"; cout << "\nUSE THE DB2 API:" << endl; cout << " sqlarbnd -- REBIND PACKAGE" << endl; cout << "TO REFRESH A PACKAGE." << endl; // refresh the package pRebindOptions = NULL; strcpy(packageName, sourceFileBaseName); cout << "\n Refresh the package: " << packageName << endl; // rebind - recreate a package stored in the database without the // need for a bind file sqlarbnd(packageName, &sqlca, pRebindOptions); DB2_API_CHECK("Package -- Refresh"); return 0; } //DbPkg::PackageRefresh int DbPkg::PackageDrop() { struct sqlca sqlca; cout << "\n-----------------------------------------------------------"; cout << "\nUSE THE SQL STATEMENT:" << endl; cout << " DROP" << endl; cout << "TO DROP A PACKAGE." << endl; // drop the package cout << "\n Execute the statement" << endl; cout << " DROP PACKAGE dbconn" << endl; EXEC SQL DROP PACKAGE dbconn; EMB_SQL_CHECK("Package -- Drop"); return 0; } //DbPkg::PackageDrop int main(int argc, char *argv[]) { int rc = 0; CmdLineArgs check; DbPkg pkg; DbEmb db; char sourceFileBaseName[8 + 1]; // check the command line arguments rc = check.CmdLineArgsCheck1(argc, argv, db); if (rc != 0) { return rc; } strcpy(sourceFileBaseName, "dbconn"); cout << "\nTHIS SAMPLE SHOWS HOW TO WORK WITH PACKAGES." << endl; // connect to the database rc = db.Connect(); if (rc != 0) { return rc; } rc = pkg.BndFileCreate(sourceFileBaseName); rc = pkg.PackageCreate(sourceFileBaseName); rc = pkg.PackageRefresh(sourceFileBaseName); rc = pkg.PackageDrop(); // disconnect from the database rc = db.Disconnect(); if (rc != 0) { return rc; } return 0; } //main