fix fetchCol to return true/false and pass value int pass-by-ref param
authorMichael Wallner <mike@php.net>
Fri, 19 Apr 2013 09:04:56 +0000 (11:04 +0200)
committerMichael Wallner <mike@php.net>
Fri, 19 Apr 2013 09:04:56 +0000 (11:04 +0200)
src/php_pqres.c
tests/basic002.phpt
tests/encoding001.phpt
tests/unbuffered001.phpt

index aee753d..8982b4f 100644 (file)
@@ -614,16 +614,18 @@ static zval **column_at(zval *row, int col TSRMLS_DC)
        return data;
 }
 
-ZEND_BEGIN_ARG_INFO_EX(ai_pqres_fetch_col, 0, 0, 0)
+ZEND_BEGIN_ARG_INFO_EX(ai_pqres_fetch_col, 0, 0, 1)
+       ZEND_ARG_INFO(1, col_val)
        ZEND_ARG_INFO(0, col_num)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(pqres, fetchCol) {
        zend_error_handling zeh;
+       zval *fetch_val;
        long fetch_col = 0;
        STATUS rv;
 
        zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
-       rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &fetch_col);
+       rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &fetch_val, &fetch_col);
        zend_restore_error_handling(&zeh TSRMLS_CC);
 
        if (SUCCESS == rv) {
@@ -636,11 +638,17 @@ static PHP_METHOD(pqres, fetchCol) {
 
                        zend_replace_error_handling(EH_THROW, exce(EX_RUNTIME), &zeh TSRMLS_CC);
                        php_pqres_iteration(getThis(), obj, obj->intern->iter ? obj->intern->iter->fetch_type : 0, &row TSRMLS_CC);
-                       if (row) {
+                       if (!row) {
+                               RETVAL_FALSE;
+                       } else {
                                zval **col = column_at(*row, fetch_col TSRMLS_CC);
 
-                               if (col) {
-                                       RETVAL_ZVAL(*col, 1, 0);
+                               if (!col) {
+                                       RETVAL_FALSE;
+                               } else {
+                                       zval_dtor(fetch_val);
+                                       ZVAL_ZVAL(fetch_val, *col, 1, 1);
+                                       RETVAL_TRUE;
                                }
                        }
                        zend_restore_error_handling(&zeh TSRMLS_CC);
index a6455ab..2afb785 100644 (file)
@@ -13,7 +13,8 @@ $s = $c->prepare("test1", "SELECT \$1",array($t["text"]->oid));
 $r = $s->exec(array("fooo"));
 
 printf("%s\n", $r->errorMessage);
-printf("%s\n", $r->fetchCol());
+$r->fetchCol($val);
+printf("%s\n", $val);
 ?>
 DONE
 --EXPECT--
index 57f24c5..53bcef5 100644 (file)
@@ -11,7 +11,8 @@ $c = new pq\Connection(PQ_DSN);
 var_dump($c->encoding);
 $c->encoding = "utf8";
 var_dump($c->encoding);
-var_dump($c->exec("SELECT 'ßüpä…'")->fetchCol());
+$c->exec("SELECT 'ßüpä…'")->fetchCol($val);
+var_dump($val);
 $tmp = 12345;
 $c->encoding = $tmp;
 var_dump($c->encoding);
index a009b63..11840ad 100644 (file)
@@ -16,7 +16,8 @@ var_dump($c->unbuffered);
 $c->execAsync("SELECT a from generate_series(1,10) a", function($res) {
        switch ($res->status) {
        case pq\Result::SINGLE_TUPLE:
-               printf("%s\n", $res->fetchCol());
+               $res->fetchCol($val);
+               printf("%s\n", $val);
                break;
        case pq\Result::TUPLES_OK:
                printf("-> fetching done\n");