Initializing the OCI Application
The first step when creating an OCI application is to perform all of the necessary OCI initializations and allocations. These steps are required for all OCI applications, as the following code illustrates:/*--------------------------------------------------------*/
/* Initialize the environment and allocate handles */
/*--------------------------------------------------------*/
sb4 init_env_handle(envhp, svchp, errhp, srvhp, authp, stmthp)
OCIEnv **envhp;
OCISvcCtx **svchp;
OCIError **errhp;
OCIServer **srvhp;
OCISession **authp;
OCIStmt **stmthp;
{
sword status = 0;
/* Environment initialization and creation */
if (OCIEnvCreate((OCIEnv **) envhp, (ub4) OCI_OBJECT, (dvoid *) 0,
(dvoid * (*)(dvoid *,size_t)) 0,
(dvoid * (*)(dvoid *, dvoid *, size_t)) 0,
(void (*)(dvoid *, dvoid *)) 0, (size_t) 0, (dvoid **) 0))
{
printf("FAILED: OCIEnvCreate()\n");
return OCI_ERROR;
}
/* allocate error handle */
if (OCIHandleAlloc((dvoid *) *envhp, (dvoid **) errhp,
(ub4) OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0))
{
printf("FAILED: OCIHandleAlloc() on errhp\n");
return OCI_ERROR;
}
/* allocate server handle */
if (status = OCIHandleAlloc((dvoid *) *envhp, (dvoid **) srvhp,
(ub4) OCI_HTYPE_SERVER, (size_t) 0, (dvoid **) 0))
{
printf("FAILED: OCIHandleAlloc() on srvhp\n");
checkerr(*errhp, status);
return OCI_ERROR;
}
/* allocate service context handle */
if (status = OCIHandleAlloc((dvoid *) *envhp, (dvoid **) svchp,
(ub4) OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **) 0))
{
printf("FAILED: OCIHandleAlloc() on svchp\n");
checkerr(*errhp, status);
return OCI_ERROR;
}
/* allocate session handle */
if (status = OCIHandleAlloc((dvoid *) *envhp, (dvoid **) authp,
(ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0))
{
printf("FAILED: OCIHandleAlloc() on authp\n");
checkerr(*errhp, status);
return OCI_ERROR;
}
/* Allocate statement handle */
if (status = OCIHandleAlloc((dvoid *) *envhp, (dvoid **) stmthp,
(ub4) OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0))
{
printf("FAILED: OCIHandleAlloc() on stmthp\n");
checkerr(*errhp, status);
return OCI_ERROR;
}
return OCI_SUCCESS;
}
/*----------------------Subfunctions ---------------------*/
/* Return corresponding messages in different cases-------*/
/*--------------------------------------------------------*/
void checkerr(errhp, status)
OCIError *errhp;
sword status;
{
text msgbuf[512];
sb4 errcode = 0;
memset((void *) msgbuf, (int)'\0', (size_t)512);
switch (status)
{
case OCI_SUCCESS: break;
case OCI_SUCCESS_WITH_INFO:
printf("status = OCI_SUCCESS_WITH_INFO\n");
OCIErrorGet((dvoid *) errhp, (ub4) 1, (text *) NULL, &errcode,
msgbuf, (ub4) sizeof(msgbuf), (ub4) OCI_HTYPE_ERROR);
printf("ERROR CODE = %d\n", errcode);
printf("%.*s\n", 512, msgbuf);
if (errcode == 436 || errcode == 437 || errcode == 438 ||
errcode == 439) exit(1);
break;
case OCI_NEED_DATA:
printf("status = OCI_NEED_DATA\n");
break;
case OCI_NO_DATA:
printf("status = OCI_NO_DATA\n");
break;
case OCI_ERROR:
printf("status = OCI_ERROR\n");
OCIErrorGet((dvoid *) errhp, (ub4) 1, (text *) NULL, &errcode,
msgbuf, (ub4) sizeof(msgbuf), (ub4) OCI_HTYPE_ERROR);
printf("ERROR CODE = %d\n", errcode);
printf("%.*s\n", 512, msgbuf);
if (errcode == 436 || errcode == 437 || errcode == 438
|| errcode == 439)
exit(1);
break;
case OCI_INVALID_HANDLE:
printf("status = OCI_INVALID_HANDLE\n");
break;
case OCI_STILL_EXECUTING:
printf("status = OCI_STILL_EXECUTE\n");
break;
case OCI_CONTINUE:
printf("status = OCI_CONTINUE\n");
break;
default:
break;
}
return;
}
/*--------------------------------------------------------*/
/* Free the envhp whenever there is an error--------------*/
/*--------------------------------------------------------*/
void cleanup(envhp)
OCIEnv *envhp;
{
if (envhp) {
OCIHandleFree((dvoid *)envhp, OCI_HTYPE_ENV);
}
return;
}
No part of this code is specific to an application, thus it can be pasted into your OCI applications without modification.