Perl Cd Bookshelf [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Perl Cd Bookshelf [Electronic resources] - نسخه متنی

Mark V. Scardina, Ben ChangandJinyu Wang

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






Retrieving a DOM of the Record List via OCI

Since the application queries a set of relational tables and then encapsulates the data returned in XML as dictated by the SQL/XML query, we don’t want to return the XML directly but rather return it as an XOB-based DOM so that it can be directly accessed. However, first you need to connect to the database as follows:

/*--------------------------------------------------------*/
/* Attach to server, set attributes, and begin session----*/
/*--------------------------------------------------------*/
sb4 connect_server(srvhp, errhp, svchp, authp, user, password)
OCIServer *srvhp;
OCIError *errhp;
OCISvcCtx *svchp;
OCISession *authp;
text *user;
text *password;
{
sword status = 0;
/* attach to server */
if (status = OCIServerAttach((OCIServer *) srvhp, (OCIError *) errhp,
(text *) conn, (sb4) strlen(conn), (ub4) OCI_DEFAULT))
{
printf("FAILED: OCIServerAttach() on srvhp\n");
checkerr(errhp, status);
return OCI_ERROR;
}
/* set server attribute to service context */
if (status = OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX,
(dvoid *) srvhp, (ub4) 0, (ub4) OCI_ATTR_SERVER,
(OCIError *) errhp))
{
printf("FAILED: OCIAttrSet() on svchp\n");
checkerr(errhp, status);
return OCI_ERROR;
}
/* set user attribute to session */
if (status = OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,
(dvoid *) user, (ub4) strlen((char *)user),
(ub4) OCI_ATTR_USERNAME, (OCIError *) errhp))
{
printf("FAILED: OCIAttrSet() on authp for user\n");
checkerr(errhp, status);
return OCI_ERROR;
}
/* set password attribute to session */
if (status = OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,
(dvoid *) password, (ub4) strlen((char *)password),
(ub4) OCI_ATTR_PASSWORD, (OCIError *) errhp))
{
printf("FAILED: OCIAttrSet() on authp for password\n");
checkerr(errhp, status);
return OCI_ERROR;
}
/* Begin a session */
if (status = OCISessionBegin((OCISvcCtx *) svchp, (OCIError *) errhp,
(OCISession *) authp, (ub4) OCI_CRED_RDBMS,
(ub4) OCI_DEFAULT))
{
printf("FAILED: OCISessionBegin()\n");
checkerr(errhp, status);
return OCI_ERROR;
}
/* set session attribute to service context */
if (status = OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX,
(dvoid *) authp, (ub4) 0, (ub4) OCI_ATTR_SESSION,
(OCIError *) errhp))
{
printf("FAILED: OCIAttrSet() on svchp\n");
checkerr(errhp, status);
return OCI_ERROR;
}
return OCI_SUCCESS;
}

After successfully connecting to the database and opening a session, you can submit the query that was passed in on the command line as follows:

/*--------------------------------------------------------------*/
/* select an xml column from the table into an xmltype instance */
/*--------------------------------------------------------------*/
sb4 select_xml(envhp, svchp, errhp, stmthp, row, selstmt,
xpathexpr, value)
OCIEnv *envhp;
OCISvcCtx *svchp;
OCIError *errhp;
OCIStmt *stmthp;
ub4 row;
text *selstmt;
text *xpathexpr;
text *value;
{
ub4 colc = row;
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=> Selecting row %d by defining to 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 *)selstmt,
(ub4)strlen((char *)selstmt),
(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 you have an OCI context, the OCIXMLType member called xml, populated with an XMLType XOB corresponding to the result set in XML format. However, it is not quite ready to be accessed, because you need to initialize one more context and perform a cast as follows:

/*--------------------------------------------------------*/
/* Initialize XML context and call DOM APIs---------------*/
/*--------------------------------------------------------*/
sb4 do_xml(envhp, svchp, errhp, xml, xpathexpr, value)
OCIEnv *envhp;
OCISvcCtx *svchp;
OCIError *errhp;
OCIXMLType *xml;
text *xpathexpr;
text *value;
{
boolean result = 0;
OCIXMLType *newxml = (OCIXMLType *) 0;
OCIXMLType *xmltran = (OCIXMLType *) 0;
sword status = 0;
/* For XML DOM APIs */
OCIStmt *stmthp = (OCIStmt *)0;
OCIDuration dur = OCI_DURATION_SESSION;
struct xmlctx *xctx = (xmlctx *)0;
xmldocnode *doc = (xmldocnode *)0;
ocixmldbparam params[1];

/* Get an XML context */
params[0].name_ocixmldbparam = XCTXINIT_OCIDUR;
params[0].value_ocixmldbparam = &dur;
xctx = OCIXmlDbInitXmlCtx(envhp, svchp, errhp, params, 1);
/* cast the xmltype value to an xmldomctx */
doc = (xmldocnode *) xml;
/* test xml unified C APIs */
if(update_xml((xmlctx *)xctx, doc, xpathexpr, value)) {
printf("FAILED: update_xml()\n");
return OCI_ERROR;
}
/* free the statement handle */
if (stmthp) {
OCIHandleFree((dvoid *)stmthp, OCI_HTYPE_STMT);
}
/* free the xmlctx */
OCIXmlDbFreeXmlCtx(xctx);
/* free xml instances using OCIObjectFree */
if(newxml &&
(status = OCIObjectFree(envhp, errhp, (dvoid *) newxml, (ub2)
OCI_OBJECTFREE_FORCE))) {
printf("FAILED: OCIObjectFree()\n");
checkerr(errhp, status);
return OCI_ERROR;
}
return OCI_SUCCESS;
}

Note the pair of functions called OCIXmlDbInitXmlCtx() and OCIXmlDbFreeXmlCtx(). These set up and free the XML context that will be used by the DOM APIs. These are also only used when you are working with an XOB, as distinct from an XDK DOM. Additionally, there is an important cast of xml context to an xmldocnode, doc. This is equivalent to providing the XMLDocument from the parse operation for DOM access.

/ 218