]> granicus.if.org Git - php/commitdiff
MFH: fix PECL bug #8816 (issue in php_oci_statement_fetch with more than one piecewis...
authorAntony Dovgal <tony2001@php.net>
Mon, 9 Oct 2006 09:34:55 +0000 (09:34 +0000)
committerAntony Dovgal <tony2001@php.net>
Mon, 9 Oct 2006 09:34:55 +0000 (09:34 +0000)
patch by jeff at badtz-maru dot com

ext/oci8/oci8_statement.c
ext/oci8/tests/pecl_bug8816.phpt [new file with mode: 0644]

index fabfaa8a3b212b3aa3628ae7f080bf59698bdae7..75a2cf3066cb9883ee29eb7b4ee61365676df716 100644 (file)
@@ -155,6 +155,11 @@ int php_oci_statement_set_prefetch(php_oci_statement *statement, ub4 size TSRMLS
 int php_oci_statement_fetch(php_oci_statement *statement, ub4 nrows TSRMLS_DC)
 {
        int i;
+       void *handlepp;
+       ub4 typep, iterp, idxp;
+       ub1 in_outp, piecep;
+       zend_bool piecewisecols = 0;
+
        php_oci_out_column *column;
 
        PHP_OCI_CALL_RETURN(statement->errcode, OCIStmtFetch, (statement->stmt, statement->err, nrows, OCI_FETCH_NEXT, OCI_DEFAULT));
@@ -186,42 +191,63 @@ int php_oci_statement_fetch(php_oci_statement *statement, ub4 nrows TSRMLS_DC)
                column = php_oci_statement_get_column(statement, i + 1, NULL, 0 TSRMLS_CC);
                if (column->piecewise) {
                        column->retlen4 = 0;
+                       piecewisecols = 1;
                }
        }
        
        while (statement->errcode == OCI_NEED_DATA) {
-               for (i = 0; i < statement->ncolumns; i++) {
-                       column = php_oci_statement_get_column(statement, i + 1, NULL, 0 TSRMLS_CC);
-                       if (column->piecewise)  {
-                               if (!column->data) {
-                                       column->data = (text *) emalloc(PHP_OCI_PIECE_SIZE);
-                               } else {
-                                       column->data = erealloc(column->data, column->retlen4 + PHP_OCI_PIECE_SIZE);
+               if (piecewisecols) {
+                       PHP_OCI_CALL_RETURN(statement->errcode,
+                               OCIStmtGetPieceInfo, 
+                                  (
+                                       statement->stmt,
+                                       statement->err,
+                                       &handlepp,
+                                       &typep,
+                                       &in_outp,
+                                       &iterp,
+                                       &idxp,
+                                       &piecep
+                                  )
+                       );
+
+                       /* scan through our columns for a piecewise column with a matching handle */
+                       for (i = 0; i < statement->ncolumns; i++) {
+                               column = php_oci_statement_get_column(statement, i + 1, NULL, 0 TSRMLS_CC);
+                               if (column->piecewise && handlepp == column->oci_define)   {
+                                       if (!column->data) {
+                                               column->data = (text *) ecalloc(1, PHP_OCI_PIECE_SIZE + 1);
+                                       } else {
+                                               column->data = erealloc(column->data, column->retlen4 + PHP_OCI_PIECE_SIZE + 1);
+                                               memset(column->data + column->retlen4, 0, PHP_OCI_PIECE_SIZE + 1);
+                                       }
+                                       column->cb_retlen = PHP_OCI_PIECE_SIZE;
+
+                                       /* and instruct fetch to fetch waiting piece into our buffer */
+                                       PHP_OCI_CALL(OCIStmtSetPieceInfo,
+                                                  (
+                                                       (void *) column->oci_define,
+                                                       OCI_HTYPE_DEFINE,
+                                                       statement->err,
+                                                       ((char*)column->data) + column->retlen4,
+                                                       &(column->cb_retlen),
+                                                       piecep,
+                                                       &column->indicator,
+                                                       &column->retcode
+                                                  )
+                                       );
                                }
-
-                               column->cb_retlen = PHP_OCI_PIECE_SIZE;
-
-                               PHP_OCI_CALL(OCIStmtSetPieceInfo, 
-                                               (
-                                                (void *) column->oci_define,
-                                                OCI_HTYPE_DEFINE,
-                                                statement->err,
-                                                ((char*)column->data) + column->retlen4,
-                                                &(column->cb_retlen),
-                                                OCI_NEXT_PIECE,
-                                                &column->indicator,
-                                                &column->retcode
-                                               )
-                               );
                        }
                }
 
                PHP_OCI_CALL_RETURN(statement->errcode,  OCIStmtFetch, (statement->stmt, statement->err, nrows, OCI_FETCH_NEXT, OCI_DEFAULT));
 
-               for (i = 0; i < statement->ncolumns; i++) {
-                       column = php_oci_statement_get_column(statement, i + 1, NULL, 0 TSRMLS_CC);
-                       if (column && column->piecewise)        {
-                               column->retlen4 += column->cb_retlen;
+               if (piecewisecols) {
+                       for (i = 0; i < statement->ncolumns; i++) {
+                               column = php_oci_statement_get_column(statement, i + 1, NULL, 0 TSRMLS_CC);
+                               if (column && column->piecewise && handlepp == column->oci_define)      {
+                                       column->retlen4 += column->cb_retlen;
+                               }
                        }
                }
        }
diff --git a/ext/oci8/tests/pecl_bug8816.phpt b/ext/oci8/tests/pecl_bug8816.phpt
new file mode 100644 (file)
index 0000000..c369711
--- /dev/null
@@ -0,0 +1,98 @@
+--TEST--
+PECL Bug #8816 (issue in php_oci_statement_fetch with more than one piecewise column)
+--SKIPIF--
+<?php if (!extension_loaded('oci8')) die("skip no oci8 extension"); ?>
+--FILE--
+<?php
+
+require dirname(__FILE__)."/connect.inc";
+
+$create_1 = "CREATE TABLE t1 (id INTEGER, l1 LONG)";
+$create_2 = "CREATE TABLE t2 (id INTEGER, l2 LONG)";
+$drop_1 = "DROP TABLE t1";
+$drop_2 = "DROP TABLE t2";
+
+$s1 = oci_parse($c, $drop_1);
+$s2 = oci_parse($c, $drop_2);
+@oci_execute($s1);
+@oci_execute($s2);
+
+$s1 = oci_parse($c, $create_1);
+$s2 = oci_parse($c, $create_2);
+oci_execute($s1);
+oci_execute($s2);
+
+$values = array("1234567890111111111", "122222222222222", "985456745674567654567654567654", "123456789", "987654321");
+
+$i = 0;
+foreach ($values as $val) {
+       $i++;
+       $insert = "INSERT INTO t1 VALUES($i, ".$val.")";
+       $s = oci_parse($c, $insert);
+       oci_execute($s);
+}
+
+foreach ($values as $val) {
+       $insert = "INSERT INTO t2 VALUES($i, ".$val.")";
+       $s = oci_parse($c, $insert);
+       oci_execute($s);
+       $i--;
+}
+
+$query ="
+SELECT
+  t1.l1, t2.l2
+FROM
+t1, t2
+WHERE 
+t1.id = t2.id 
+ORDER BY t1.id ASC
+";
+
+$sth = oci_parse($c, $query);
+oci_execute($sth);
+
+while ( $row = oci_fetch_assoc($sth) ) {
+       var_dump($row);
+}
+
+$s1 = oci_parse($c, $drop_1);
+$s2 = oci_parse($c, $drop_2);
+@oci_execute($s1);
+@oci_execute($s2);
+
+echo "Done\n";
+
+?>
+--EXPECT--
+array(2) {
+  ["L1"]=>
+  string(19) "1234567890111111111"
+  ["L2"]=>
+  string(9) "987654321"
+}
+array(2) {
+  ["L1"]=>
+  string(15) "122222222222222"
+  ["L2"]=>
+  string(9) "123456789"
+}
+array(2) {
+  ["L1"]=>
+  string(30) "985456745674567654567654567654"
+  ["L2"]=>
+  string(30) "985456745674567654567654567654"
+}
+array(2) {
+  ["L1"]=>
+  string(9) "123456789"
+  ["L2"]=>
+  string(15) "122222222222222"
+}
+array(2) {
+  ["L1"]=>
+  string(9) "987654321"
+  ["L2"]=>
+  string(19) "1234567890111111111"
+}
+Done