Selecting into an XMLType
After successfully connecting to the database and opening a session, you can submit the query that was passed in on the command line. This is the select section of the select_and_query() function:sb4 ociapi::select_and_query( text* select_stmt, text *xpathexpr)
{
ub4 xmlsize = 0;
sword status = 0;
OCIDefine *defnp = (OCIDefine *) 0;
OCIBind *bndhp = (OCIBind *) 0;
OCIXMLType *xml = (OCIXMLType *) 0;
OCIType *xmltdo = (OCIType *) 0;
OCIInd ind = OCI_IND_NULL;
OCIInd *xml_ind = &ind;
printf("\n=> \"%select_stmt\" as an XMLType instance......\n", row);
/* get the tdo for the xmltype */
if (status = OCITypeByName(envhp, errhp, svchp, (const text *) SCHEMA,
(ub4) strlen((char *)SCHEMA), (const text *) TYPE,
(ub4) strlen((char *)TYPE), (CONST text *) 0,
(ub4) 0, OCI_DURATION_SESSION, OCI_TYPEGET_HEADER,
(OCIType **) &xmltdo))
{
printf("FAILED: OCITypeByName()\n");
checkerr( errhp, status);
return OCI_ERROR;
}
if (!xmltdo)
{
printf("NULL tdo returned\n");
return OCI_ERROR;
}
/* create a new xmltype instance */
if (status = OCIObjectNew( (OCIEnv*)envhp, (OCIError*)errhp,
(CONST OCISvcCtx *)svchp,
(OCITypeCode) OCI_TYPECODE_OPAQUE,
(OCIType *) xmltdo, (dvoid *) 0,
(OCIDuration) OCI_DURATION_SESSION, FALSE,
(dvoid **) &xml))
{
printf("FAILED: OCIObjectNew()\n");
checkerr( errhp, status);
return OCI_ERROR;
}
if(status = OCIStmtPrepare( stmthp, errhp, (OraText *)select_stmt,
(ub4)strlen((char*)select_stmt),
(ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT))
{
printf("FAILED: OCIStmtPrepare()\n");
checkerr( errhp, status);
return OCI_ERROR;
}
/* define it to an xmltype instance */
if(status = OCIDefineByPos( stmthp, &defnp, errhp, (ub4) 1, (dvoid *) 0,
(sb4) 0, SQLT_NTY, (dvoid *) 0, (ub2 *)0,
(ub2 *)0, (ub4) OCI_DEFAULT))
{
printf("FAILED: OCIDefineByPos()\n");
checkerr( errhp, status);
return OCI_ERROR;
}
if(status = OCIDefineObject( defnp, errhp, (OCIType *) xmltdo,
(dvoid **) &xml, &xmlsize,
(dvoid **) &xml_ind, (ub4 *) 0))
{
printf("FAILED: OCIDefineObject()\n");
checkerr(errhp, status);
return OCI_ERROR;
}
if(status = OCIStmtExecute( svchp, stmthp, errhp, (ub4) 1, (ub4) 0,
(CONST OCISnapshot*) 0, (OCISnapshot*) 0,
(ub4) OCI_DEFAULT))
{
printf("FAILED: OCIStmtExecute()\n");
checkerr(errhp, status);
return OCI_ERROR;
}
At this point, assuming the statement execution was successful, you have an OCI context, xml, populated with an XMLType XOB corresponding to the result set in XML format. You now need to initialize the XDK context and bind it to the XOB so you can use the C++ unified XDK APIs on the XOB.