Oracle 11.2 introduced support for REF CURSOR prefetching
oci_new_cursor
(PHP 5, PECL OCI8 >= 1.1.0)
oci_new_cursor — Alloue un nouveau curseur Oracle
Description
Alloue un nouveau curseur Oracle sur la connexion spécifiée.
Liste de paramètres
- connection
-
Un identifiant de connexion Oracle, retourné par la fonction oci_connect() ou la fonction oci_pconnect().
Valeurs de retour
Retourne un nouveau gestionnaire de connexion, ou FALSE si une erreur survient.
Exemples
Exemple #1 Utiliser un REF CURSOR issu d'une procédure enregistrée
<?php
// supposons que votre procédure stockée info.output retourne un curseur de référence dans :data
$conn = oci_connect("scott", "tiger");
$curs = oci_new_cursor($conn);
$stmt = oci_parse($conn, "begin info.output(:data); end;");
oci_bind_by_name($stmt, "data", $curs, -1, OCI_B_CURSOR);
oci_execute($stmt);
oci_execute($curs);
while ($data = oci_fetch_row($curs)) {
var_dump($data);
}
oci_free_statement($stmt);
oci_free_statement($curs);
oci_close($conn);
?>
Exemple #2 Utiliser un REF CURSOR issu d'une commande SELECT
<?php
echo "<html><body>";
$conn = oci_connect("scott", "tiger");
$count_cursor = "CURSOR(select count(empno) num_emps from emp " .
"where emp.deptno = dept.deptno) as EMPCNT from dept";
$stmt = oci_parse($conn, "select deptno,dname,$count_cursor");
oci_execute($stmt);
echo "<table border=\"1\">";
echo "<tr>";
echo "<th>DEPT NAME</th>";
echo "<th>DEPT #</th>";
echo "<th># EMPLOYEES</th>";
echo "</tr>";
while ($data = oci_fetch_assoc($stmt)) {
echo "<tr>";
$dname = $data["DNAME"];
$deptno = $data["DEPTNO"];
echo "<td>$dname</td>";
echo "<td>$deptno</td>";
oci_execute($data["EMPCNT"]);
while ($subdata = oci_fetch_assoc($data["EMPCNT"])) {
$num_emps = $subdata["NUM_EMPS"];
echo "<td>$num_emps</td>";
}
echo "</tr>";
}
echo "</table>";
echo "</body></html>";
oci_free_statement($stmt);
oci_close($conn);
?>
Notes
Note:
Dans les versions de PHP antérieures à la version 5.0.0, vous devez utiliser la fonction ocinewcursor(). Cet ancien nom est toujours utilisable : un alias a été fait vers la fonction oci_new_collection(), pour assurer la compatibilité ascendante. Toutefois, il est recommandé de ne plus l'utiliser.
Because OCI8 uses "prefetching" to greatly improve returning query results, but Oracle doesn't support prefetching for REF CURSORs, application performance using REF CURSORs can be greatly improved by writing a PL/SQL function that pulls data from the REF CURSOR and PIPEs the output. The new function can be queried in a SELECT as if it were a table. See http://blogs.oracle.com/opal/2008/11/
converting_ref_cursor_to_pipe.html
