问题
I have a Oracle Query, which returns the below output:-
stdClass Object
(
[START_TIME] => 2015/01/04 06:03:07
[END_TIME] => 2015/01/04 06:27:27
[STATUS] => Error
[NODE] => DEVSERVER1
[CTRL_GROUP] => DEV_ORA
[SERVER_NUMBER] => 1001
[JOB_NAME] => Oracle Process
[STEP_INFORMATION] => OCI-Lob Object
(
[descriptor] => Resource id #165
)
)
In this, the step information is coming from a getClobVal() function which present inside the Oracle Query. I have extracted the XML contents by running the below code for the step information -
$stmt = oci_parse($this->oraConn, $query);
oci_execute($stmt, OCI_DEFAULT);
$r = oci_fetch_array($stmt, OCI_RETURN_NULLS+OCI_RETURN_LOBS);
echo "<pre>";
echo htmlentities($r["STEP_INFORMATION"]);
echo "</pre>";
exit;
I have got below XML -
<orastep number="9">
<ora_name>oracle_load_script</ora_name>
<ora_selection>10089</ora_selection>
<ora_selection_ind>3</ora_selection_ind>
</orastep>
I am working on a PHP Unit test in which I need to pass the input as same as the Oracle output, but without using the Oracle query or connecting to any database. I have tried using JSON as well as creating object in PHP to mock the input but I couldn't represent the OCI-Lob object.
I want to know how can I use the above XML to create the OCI-Lob object as below, So that I can mock that in input and pass it in unit test.
[STEP_INFORMATION] => OCI-Lob Object
(
[descriptor] => Resource id #165
)
I have looked for almost all the threads related to OCI-Lob in PHP but didn't get the information which I am looking for.
Hope you understand my question. Any suggestions on this would be appreciated.
Thank you.
回答1:
After doing plenty of investigation on this, finally I have come to a point of understanding this. We can not directly convert a XML to OCI-Lob Object directly. Because
"The Oracle Call Interface (OCI) is an application programming interface (API) that allows applications written in C to interact with one or more Oracle Servers. OCI gives your programs the capability to perform the full range of database operations that are possible with Oracle9i database, including SQL statement processing and object manipulation."
Let it be like If we have any XMLAGG function with getClobVal() in side the query which we are storing as "Val" which returns OCI-Lob Object as below
$Val = OCI-Lob Object
(
[descriptor] => Resource id #130
)
the below function will give us the XML Contents of OCI-Lob Object [CLOB DataType], Which I have mentioned in the Questions.
$this->$conn = oci_connect('user', 'password', 'connectionString');
$query = "Some SELECT Query";
$stmt = oci_parse($this->oraConn, $query);
oci_execute($stmt);
while($row = oci_fetch_assoc($stmt)){
if($row['Val'] != false){
printVar($row['Val']->load());
break;
}
}
Now If I remove load() from printVar($row['Val']->load()) and do printVar($row['Val']) it will again return me the OCI-Lob Object.
To get OCI-Lob Object We do need Oracle Select Query in addition we can pass XMLAttributes/Elements with getClobVal() or any function. But We can't parse XML and convert it to OCI-Lob Object as it always interact with the Oracle Database/Server.
So to run the PHP Unit test what I did was, I took the XML and Converted that to an OCIMockObject, Which Will have all the XML Values and parse it to the function.
It Worked!
This link is useful to learn more about Oracle Call Interface(OCI)
Thanks.
来源:https://stackoverflow.com/questions/29589826/how-to-use-xml-to-create-oci-lob-object