fix Statement::descAsync; add Result::desc()
authorMichael Wallner <mike@php.net>
Tue, 14 May 2013 12:30:53 +0000 (14:30 +0200)
committerMichael Wallner <mike@php.net>
Tue, 14 May 2013 12:30:53 +0000 (14:30 +0200)
package.xml
src/php_pqres.c
src/php_pqstm.c
tests/async007.phpt [new file with mode: 0644]
tests/stm_desc002.phpt [new file with mode: 0644]

index 741cddca9417c0089456a29aaeeb3c27f600946e..4956eda4796cf764324d51a6bda3179b7d5a0a8f 100644 (file)
@@ -90,6 +90,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <file role="test" name="async004.phpt" />
     <file role="test" name="async005.phpt" />
     <file role="test" name="async006.phpt" />
+    <file role="test" name="async007.phpt" />
     <file role="test" name="basic001.phpt" />
     <file role="test" name="basic002.phpt" />
     <file role="test" name="bound001.phpt" />
@@ -114,6 +115,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <file role="test" name="reset001.phpt" />
     <file role="test" name="savepoint001.phpt" />
     <file role="test" name="stm_desc001.phpt" />
+    <file role="test" name="stm_desc002.phpt" />
     <file role="test" name="trans001.phpt" />
     <file role="test" name="trans002.phpt" />
     <file role="test" name="types001.phpt" />
index addeab3c93af153d6735a7d7b42d5caf55c73891..de33fd2d70325949adce56e9cae6f68c1588d7ea 100644 (file)
@@ -883,6 +883,25 @@ static PHP_METHOD(pqres, count) {
        }
 }
 
+ZEND_BEGIN_ARG_INFO_EX(ai_pqres_desc, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqres, desc) {
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               if (!obj->intern) {
+                       throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Result not initialized");
+               } else {
+                       int p, params;
+
+                       array_init(return_value);
+                       for (p = 0, params = PQnparams(obj->intern->res); p < params; ++p) {
+                               add_next_index_long(return_value, PQparamtype(obj->intern->res, p));
+                       }
+               }
+       }
+}
+
 static zend_function_entry php_pqres_methods[] = {
        PHP_ME(pqres, bind, ai_pqres_bind, ZEND_ACC_PUBLIC)
        PHP_ME(pqres, fetchBound, ai_pqres_fetch_bound, ZEND_ACC_PUBLIC)
@@ -891,6 +910,7 @@ static zend_function_entry php_pqres_methods[] = {
        PHP_ME(pqres, fetchAll, ai_pqres_fetch_all, ZEND_ACC_PUBLIC)
        PHP_ME(pqres, count, ai_pqres_count, ZEND_ACC_PUBLIC)
        PHP_ME(pqres, map, ai_pqres_map, ZEND_ACC_PUBLIC)
+       PHP_ME(pqres, desc, ai_pqres_desc, ZEND_ACC_PUBLIC)
        {0}
 };
 
index a0ae249aa7e9d1ef9f864a5378c2b0830363a1d4..6be580cf0607a24a35c94440250a523e6bfa5265 100644 (file)
@@ -113,7 +113,7 @@ static void php_pqstm_object_read_connection(zval *object, void *o, zval *return
 
 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_construct, 0, 0, 3)
        ZEND_ARG_OBJ_INFO(0, Connection, pq\\Connection, 0)
-       ZEND_ARG_INFO(0, type)
+       ZEND_ARG_INFO(0, name)
        ZEND_ARG_INFO(0, query)
        ZEND_ARG_ARRAY_INFO(0, types, 1)
        ZEND_ARG_INFO(0, async)
@@ -293,14 +293,16 @@ static PHP_METHOD(pqstm, desc) {
        }
 }
 
-ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_desc_async, 0, 0, 0)
+ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_desc_async, 0, 0, 1)
+       ZEND_ARG_INFO(0, callable)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(pqstm, descAsync) {
        zend_error_handling zeh;
+       php_pq_callback_t resolver = {{0}};
        STATUS rv;
 
        zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
-       rv = zend_parse_parameters_none();
+       rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f", &resolver.fci, &resolver.fcc);
        zend_restore_error_handling(&zeh TSRMLS_CC);
 
        if (SUCCESS == rv) {
@@ -311,6 +313,11 @@ static PHP_METHOD(pqstm, descAsync) {
                } else if (!PQsendDescribePrepared(obj->intern->conn->intern->conn, obj->intern->name)) {
                        throw_exce(EX_IO TSRMLS_CC, "Failed to describe statement: %s", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
                } else {
+                       php_pq_callback_dtor(&obj->intern->conn->intern->onevent);
+                       if (resolver.fci.size > 0) {
+                               obj->intern->conn->intern->onevent = resolver;
+                               php_pq_callback_addref(&obj->intern->conn->intern->onevent);
+                       }
                        obj->intern->conn->intern->poller = PQconsumeInput;
                        php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC);
                }
diff --git a/tests/async007.phpt b/tests/async007.phpt
new file mode 100644 (file)
index 0000000..c2a59f6
--- /dev/null
@@ -0,0 +1,41 @@
+--TEST--
+async statement
+--SKIPIF--
+<?php include "_skipif.inc"; ?>
+--FILE--
+<?php
+echo "Test\n";
+include "_setup.inc";
+
+function complete($c) {
+       do {
+               while ($c->busy) {
+                       $r = array($c->socket);
+                       $w = $e = null;
+                       if (stream_select($r, $w, $e, null)) {
+                               $c->poll();
+                       }
+               }
+       } while ($c->getResult());
+}
+
+$c = new pq\Connection(PQ_DSN);
+$t = new pq\Types($c);
+$s = new pq\Statement($c, "test1", "SELECT NOW() - \$1", null, true);
+complete($s->connection);
+
+$s->execAsync(array("2012-12-12 12:12:12"));
+complete($s->connection);
+
+$s->descAsync(function($r) use ($t) {
+       list($typeOid) = $r->desc();
+       printf("%s\n", $t[$typeOid]->typname);
+});
+complete($s->connection);
+
+?>
+DONE
+--EXPECT--
+Test
+timestamptz
+DONE
diff --git a/tests/stm_desc002.phpt b/tests/stm_desc002.phpt
new file mode 100644 (file)
index 0000000..32ab8b2
--- /dev/null
@@ -0,0 +1,22 @@
+--TEST--
+desc statement
+--SKIPIF--
+<?php include "_skipif.inc"; ?>
+--FILE--
+<?php
+echo "Test\n";
+include "_setup.inc";
+
+$c = new pq\Connection(PQ_DSN);
+$s = new pq\Statement($c, "test1", "SELECT NOW() - \$1");
+$r = $s->exec(array("2012-12-12 12:12:12"));
+$d = $s->desc();
+
+printf("%s\n", (new pq\Types($c))[$d[0]]->typname);
+
+?>
+DONE
+--EXPECT--
+Test
+timestamptz
+DONE