/**************************************************************************** ** (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, 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 "utilemb.h" int BndFileCreate(char *); int PackageCreate(char *); int PackageRefresh(char *); int PackagePrivilegesChange(char *); int PackageDrop(void); int main(int argc, char *argv[]) { int rc = 0; char sourceFileBaseName[8 + 1]; char dbAlias[SQL_ALIAS_SZ]; char user[USERID_SZ + 1]; char pswd[PSWD_SZ + 1]; /* check the command line arguments */ rc = CmdLineArgsCheck1(argc, argv, dbAlias, user, pswd); if (rc != 0) { return rc; } strcpy(sourceFileBaseName, "dbconn"); printf("\nTHIS SAMPLE SHOWS HOW TO WORK WITH PACKAGES.\n"); /* connect to the database */ rc = DbConn(dbAlias, user, pswd); if (rc != 0) { return rc; } rc = BndFileCreate(sourceFileBaseName); rc = PackageCreate(sourceFileBaseName); rc = PackageRefresh(sourceFileBaseName); /* rc = PackagePrivilegesChange(sourceFileBaseName); */ rc = PackageDrop(); /* disconnect from the database */ rc = DbDisconn(dbAlias); if (rc != 0) { return rc; } return 0; } /* end main */ int BndFileCreate(char *sourceFileBaseName) { struct sqlca sqlca; char sourceFileName[8 + 1 + 3 + 1]; char msgFileName[8 + 1 + 3 + 1]; struct sqlopt *pPrecompileOptions; struct sqloptions *optionsArray; printf("\n-----------------------------------------------------------"); printf("\nUSE THE DB2 API:\n"); printf(" sqlaprep -- PRECOMPILE PROGRAM\n"); printf("TO CREATE A BIND FILE.\n"); /* create the package */ strcpy(sourceFileName, sourceFileBaseName); strcat(sourceFileName, ".sqc"); strcpy(msgFileName, sourceFileBaseName); msgFileName[6] = '\0'; /* truncate to 6 characters */ strcat(msgFileName, "_p.msg"); /* _p = precompile */ printf("\n Create the bind file.\n"); printf(" source file name : %s\n", sourceFileName); printf(" bind file name : %s.bnd\n", sourceFileBaseName); printf(" message file name: %s\n", msgFileName); pPrecompileOptions = (struct sqlopt *)malloc(sizeof(struct sqlopt) + (1 - 1) * sizeof(struct sqloptions)); /* for option 1 no more struct sqloptions are needed */ /* because struct sqlopt already contains one */ 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 */ /* precompile program */ sqlaprep(sourceFileName, msgFileName, pPrecompileOptions, &sqlca); DB2_API_CHECK("Bnd File -- Create"); /* free the memory allocated */ free(pPrecompileOptions); return 0; } /* BndFileCreate */ int PackageCreate(char *sourceFileBaseName) { struct sqlca sqlca; char bndFileName[8 + 1 + 3 + 1]; char msgFileName[8 + 1 + 3 + 1]; struct sqlopt *pBindOptions; struct sqloptions *optionsArray; printf("\n-----------------------------------------------------------"); printf("\nUSE THE DB2 API:\n"); printf(" sqlabndx -- BIND\n"); printf("TO CREATE A PACKAGE.\n"); /* 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 */ printf("\n Create the package.\n"); printf(" bind file name : %s\n", bndFileName); printf(" package name : %s\n", sourceFileBaseName); printf(" message file name: %s\n", msgFileName); pBindOptions = (struct sqlopt *)malloc(sizeof(struct sqlopt) + (2 - 1) * sizeof(struct sqloptions)); /* for 2 options one more struct sqloptions is needed because */ /* struct sqlopt already contains one */ 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"); /* free the memory allocated */ free(pBindOptions); return 0; } /* PackageCreate */ int PackageRefresh(char *sourceFileBaseName) { struct sqlca sqlca; char packageName[8 + 1]; struct sqlopt *pRebindOptions; printf("\n-----------------------------------------------------------"); printf("\nUSE THE DB2 API:\n"); printf(" sqlarbnd -- REBIND PACKAGE\n"); printf("TO REFRESH A PACKAGE.\n"); /* refresh the package */ pRebindOptions = NULL; strcpy(packageName, sourceFileBaseName); printf("\n Refresh the package: %s\n", packageName); /* rebind */ sqlarbnd(packageName, &sqlca, pRebindOptions); DB2_API_CHECK("Package -- Refresh"); return 0; } /* PackageRefresh */ int PackageDrop(void) { struct sqlca sqlca; printf("\n-----------------------------------------------------------"); printf("\nUSE THE SQL STATEMENT:\n"); printf(" DROP\n"); printf("TO DROP A PACKAGE.\n"); /* drop the package */ printf("\n Execute the statement\n"); printf(" DROP PACKAGE dbconn\n"); EXEC SQL DROP PACKAGE dbconn; EMB_SQL_CHECK("Package -- Drop"); return 0; } /* PackageDrop */