1
0
mirror of https://github.com/danog/ext-pq.git synced 2024-12-02 09:18:02 +01:00

merge danglind Statement->descAsync patch

This commit is contained in:
Michael Wallner 2013-02-06 16:42:06 +01:00
commit 6abdd023da
6 changed files with 198 additions and 44 deletions

8
.gitignore vendored
View File

@ -2,9 +2,9 @@
# / # /
*~ *~
/*.tgz /*.tgz
/.deps .deps
/*.lo *.lo
/*.la *.la
/config.[^m]* /config.[^m]*
/configure* /configure*
/lib* /lib*
@ -14,7 +14,7 @@
/Make* /Make*
/mk* /mk*
/missing /missing
/.libs .libs
/build /build
/include /include
/modules /modules

1
TODO
View File

@ -1,5 +1,4 @@
* parse arrays out of PQgetvalue * parse arrays out of PQgetvalue
* listenAsync & notifyAsync
* COPY: getAsync & putAsync * COPY: getAsync & putAsync
* pq\Exception * pq\Exception
* pq\Event\Result\Destroy, * pq\Event\Result\Destroy,

View File

@ -35,5 +35,6 @@ if test "$PHP_PQ" != "no"; then
PQ_SRC="src/php_pq.c" PQ_SRC="src/php_pq.c"
PHP_NEW_EXTENSION(pq, $PQ_SRC, $ext_shared) PHP_NEW_EXTENSION(pq, $PQ_SRC, $ext_shared)
PHP_ADD_BUILD_DIR($ext_builddir/src, 1) PHP_ADD_BUILD_DIR($ext_builddir/src, 1)
PHP_ADD_EXTENSION_DEP(pq, raphf)
fi fi

View File

@ -73,6 +73,12 @@ http://pear.php.net/dtd/package-2.0.xsd">
<pearinstaller> <pearinstaller>
<min>1.4.0</min> <min>1.4.0</min>
</pearinstaller> </pearinstaller>
<package>
<name>raphf</name>
<channel>pecl.php.net</channel>
<min>1.0.0</min>
<providesextension>raphf</providesextension>
</package>
</required> </required>
</dependencies> </dependencies>
<providesextension>pq</providesextension> <providesextension>pq</providesextension>

View File

@ -522,17 +522,52 @@ static void php_pq_object_delref(void *o TSRMLS_DC)
zend_objects_store_del_ref_by_handle_ex(obj->zv.handle, obj->zv.handlers TSRMLS_CC); zend_objects_store_del_ref_by_handle_ex(obj->zv.handle, obj->zv.handlers TSRMLS_CC);
} }
static int apply_unlisten(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
{
php_pqconn_object_t *obj = va_arg(argv, php_pqconn_object_t *);
char *quoted_channel = PQescapeIdentifier(obj->intern->conn, key->arKey, key->nKeyLength - 1);
if (quoted_channel) {
PGresult *res;
char *cmd;
spprintf(&cmd, 0, "UNLISTEN %s", quoted_channel);
if ((res = PQexec(obj->intern->conn, cmd))) {
PHP_PQclear(res);
}
efree(cmd);
PQfreemem(quoted_channel);
}
return ZEND_HASH_APPLY_REMOVE;
}
static void php_pqconn_notice_ignore(void *p, const PGresult *res)
{
}
static void php_pqconn_object_free(void *o TSRMLS_DC) static void php_pqconn_object_free(void *o TSRMLS_DC)
{ {
php_pqconn_object_t *obj = o; php_pqconn_object_t *obj = o;
if (obj->intern) { if (obj->intern) {
php_pqconn_event_data_t *evdata = PQinstanceData(obj->intern->conn, php_pqconn_event); php_pqconn_event_data_t *evdata;
PGresult *res;
if (evdata) { if ((evdata = PQinstanceData(obj->intern->conn, php_pqconn_event))) {
memset(evdata, 0, sizeof(*evdata));
efree(evdata); efree(evdata);
PQsetInstanceData(obj->intern->conn, php_pqconn_event, NULL); PQsetInstanceData(obj->intern->conn, php_pqconn_event, NULL);
} }
PQsetNoticeReceiver(obj->intern->conn, php_pqconn_notice_ignore, NULL);
/* clean up async results */
while ((res = PQgetResult(obj->intern->conn))) {
PHP_PQclear(res);
}
zend_hash_apply_with_arguments(&obj->intern->listeners TSRMLS_CC, apply_unlisten, 1, obj);
php_resource_factory_handle_dtor(&obj->intern->factory, obj->intern->conn TSRMLS_CC); php_resource_factory_handle_dtor(&obj->intern->factory, obj->intern->conn TSRMLS_CC);
php_resource_factory_dtor(&obj->intern->factory); php_resource_factory_dtor(&obj->intern->factory);
@ -1584,6 +1619,7 @@ static STATUS php_pqconn_update_socket(zval *this_ptr, php_pqconn_object_t *obj
if ((CONNECTION_BAD != PQstatus(obj->intern->conn)) if ((CONNECTION_BAD != PQstatus(obj->intern->conn))
&& (-1 < (socket = PQsocket(obj->intern->conn))) && (-1 < (socket = PQsocket(obj->intern->conn)))
&& (stream = php_stream_fopen_from_fd(socket, "r+b", NULL))) { && (stream = php_stream_fopen_from_fd(socket, "r+b", NULL))) {
stream->flags |= PHP_STREAM_FLAG_NO_CLOSE;
php_stream_to_zval(stream, zsocket); php_stream_to_zval(stream, zsocket);
retval = SUCCESS; retval = SUCCESS;
} else { } else {
@ -1618,20 +1654,24 @@ static int apply_event(void *p, void *a TSRMLS_DC)
return ZEND_HASH_APPLY_KEEP; return ZEND_HASH_APPLY_KEEP;
} }
static void php_pqconn_event_connreset(PGEventConnReset *event, php_pqconn_event_data_t *data) static void php_pqconn_event_connreset(PGEventConnReset *event)
{ {
zval **evhs; php_pqconn_event_data_t *data = PQinstanceData(event->conn, php_pqconn_event);
TSRMLS_DF(data);
if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("reset"), (void *) &evhs)) { if (data) {
zval *args, *connection = NULL; zval **evhs;
TSRMLS_DF(data);
MAKE_STD_ZVAL(args); if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("reset"), (void *) &evhs)) {
array_init(args); zval *args, *connection = NULL;
php_pq_object_to_zval(data->obj, &connection TSRMLS_CC);
add_next_index_zval(args, connection); MAKE_STD_ZVAL(args);
zend_hash_apply_with_argument(Z_ARRVAL_PP(evhs), apply_event, args TSRMLS_CC); array_init(args);
zval_ptr_dtor(&args); php_pq_object_to_zval(data->obj, &connection TSRMLS_CC);
add_next_index_zval(args, connection);
zend_hash_apply_with_argument(Z_ARRVAL_PP(evhs), apply_event, args TSRMLS_CC);
zval_ptr_dtor(&args);
}
} }
} }
@ -1655,31 +1695,35 @@ static zval *result_instance_zval(PGresult *res TSRMLS_DC)
return rid; return rid;
} }
static void php_pqconn_event_resultcreate(PGEventResultCreate *event, php_pqconn_event_data_t *data) static void php_pqconn_event_resultcreate(PGEventResultCreate *event)
{ {
zval **evhs; php_pqconn_event_data_t *data = PQinstanceData(event->conn, php_pqconn_event);
TSRMLS_DF(data);
/* event listener */ if (data) {
if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("result"), (void *) &evhs)) { zval **evhs;
zval *args, *connection = NULL, *res = result_instance_zval(event->result TSRMLS_CC); TSRMLS_DF(data);
MAKE_STD_ZVAL(args); /* event listener */
array_init(args); if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("result"), (void *) &evhs)) {
php_pq_object_to_zval(data->obj, &connection TSRMLS_CC); zval *args, *connection = NULL, *res = result_instance_zval(event->result TSRMLS_CC);
add_next_index_zval(args, connection);
add_next_index_zval(args, res);
zend_hash_apply_with_argument(Z_ARRVAL_PP(evhs), apply_event, args TSRMLS_CC);
zval_ptr_dtor(&args);
}
/* async callback */ MAKE_STD_ZVAL(args);
if (data->obj->intern->onevent.fci.size > 0) { array_init(args);
zval *res = result_instance_zval(event->result TSRMLS_CC); php_pq_object_to_zval(data->obj, &connection TSRMLS_CC);
add_next_index_zval(args, connection);
add_next_index_zval(args, res);
zend_hash_apply_with_argument(Z_ARRVAL_PP(evhs), apply_event, args TSRMLS_CC);
zval_ptr_dtor(&args);
}
zend_fcall_info_argn(&data->obj->intern->onevent.fci TSRMLS_CC, 1, &res); /* async callback */
zend_fcall_info_call(&data->obj->intern->onevent.fci, &data->obj->intern->onevent.fcc, NULL, NULL TSRMLS_CC); if (data->obj->intern->onevent.fci.size > 0) {
zval_ptr_dtor(&res); zval *res = result_instance_zval(event->result TSRMLS_CC);
zend_fcall_info_argn(&data->obj->intern->onevent.fci TSRMLS_CC, 1, &res);
zend_fcall_info_call(&data->obj->intern->onevent.fci, &data->obj->intern->onevent.fcc, NULL, NULL TSRMLS_CC);
zval_ptr_dtor(&res);
}
} }
} }
@ -1687,10 +1731,10 @@ static int php_pqconn_event(PGEventId id, void *e, void *data)
{ {
switch (id) { switch (id) {
case PGEVT_CONNRESET: case PGEVT_CONNRESET:
php_pqconn_event_connreset(e, data); php_pqconn_event_connreset(e);
break; break;
case PGEVT_RESULTCREATE: case PGEVT_RESULTCREATE:
php_pqconn_event_resultcreate(e, data); php_pqconn_event_resultcreate(e);
break; break;
default: default:
break; break;
@ -1755,6 +1799,29 @@ static php_resource_factory_ops_t php_pqconn_resource_factory_ops = {
php_pqconn_resource_factory_dtor php_pqconn_resource_factory_dtor
}; };
static void php_pqconn_persistent_resource_factory_dtor(void *opaque, void *handle TSRMLS_DC)
{
PGresult *res;
/* clean up */
if ((res = PQexec(handle, "ROLLBACK; RESET ALL;"))) {
PHP_PQclear(res);
}
/* release to the pool, if the connection is alive */
if (CONNECTION_OK == PQstatus(handle)) {
php_persistent_handle_release(opaque, handle TSRMLS_CC);
} else {
PQfinish(handle);
}
}
static php_resource_factory_ops_t php_pqconn_persistent_resource_factory_ops = {
(php_resource_factory_handle_ctor_t) php_persistent_handle_acquire,
NULL,
php_pqconn_persistent_resource_factory_dtor
};
ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct, 0, 0, 1) ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct, 0, 0, 1)
ZEND_ARG_INFO(0, dsn) ZEND_ARG_INFO(0, dsn)
ZEND_ARG_INFO(0, async) ZEND_ARG_INFO(0, async)
@ -1778,7 +1845,7 @@ static PHP_METHOD(pqconn, __construct) {
if (flags & PHP_PQCONN_PERSISTENT) { if (flags & PHP_PQCONN_PERSISTENT) {
php_persistent_handle_factory_t *phf = php_persistent_handle_concede(NULL, ZEND_STRL("pq\\Connection"), dsn_str, dsn_len TSRMLS_CC); php_persistent_handle_factory_t *phf = php_persistent_handle_concede(NULL, ZEND_STRL("pq\\Connection"), dsn_str, dsn_len TSRMLS_CC);
php_resource_factory_init(&obj->intern->factory, php_persistent_handle_get_resource_factory_ops(), phf, (void (*)(void*)) php_persistent_handle_abandon); php_resource_factory_init(&obj->intern->factory, &php_pqconn_persistent_resource_factory_ops, phf, (void (*)(void*)) php_persistent_handle_abandon);
} else{ } else{
php_resource_factory_init(&obj->intern->factory, &php_pqconn_resource_factory_ops, NULL, NULL); php_resource_factory_init(&obj->intern->factory, &php_pqconn_resource_factory_ops, NULL, NULL);
} }
@ -1788,7 +1855,7 @@ static PHP_METHOD(pqconn, __construct) {
obj->intern->conn = php_resource_factory_handle_ctor(&obj->intern->factory, &rfdata TSRMLS_CC); obj->intern->conn = php_resource_factory_handle_ctor(&obj->intern->factory, &rfdata TSRMLS_CC);
PQregisterEventProc(obj->intern->conn, php_pqconn_event, "ext-pq", evdata); PQregisterEventProc(obj->intern->conn, php_pqconn_event, "ext-pq", NULL);
/* the connection might be persistent, so reset event_proc instance data */ /* the connection might be persistent, so reset event_proc instance data */
PQsetInstanceData(obj->intern->conn, php_pqconn_event, evdata); PQsetInstanceData(obj->intern->conn, php_pqconn_event, evdata);
PQsetNoticeReceiver(obj->intern->conn, php_pqconn_notice_recv, evdata); PQsetNoticeReceiver(obj->intern->conn, php_pqconn_notice_recv, evdata);
@ -1875,7 +1942,7 @@ static PHP_METHOD(pqconn, listen) {
PGresult *res; PGresult *res;
char *cmd; char *cmd;
spprintf(&cmd, 0, "LISTEN %s", channel_str); spprintf(&cmd, 0, "LISTEN %s", quoted_channel);
res = PQexec(obj->intern->conn, cmd); res = PQexec(obj->intern->conn, cmd);
efree(cmd); efree(cmd);
@ -1905,6 +1972,51 @@ static PHP_METHOD(pqconn, listen) {
} }
} }
ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen_async, 0, 0, 0)
ZEND_ARG_INFO(0, channel)
ZEND_ARG_INFO(0, callable)
ZEND_END_ARG_INFO();
static PHP_METHOD(pqconn, listenAsync) {
char *channel_str = NULL;
int channel_len = 0;
php_pq_callback_t listener;
if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sf", &channel_str, &channel_len, &listener.fci, &listener.fcc)) {
php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
obj->intern->poller = PQconsumeInput;
if (obj->intern) {
char *quoted_channel = PQescapeIdentifier(obj->intern->conn, channel_str, channel_len);
if (quoted_channel) {
char *cmd;
obj->intern->poller = PQconsumeInput;
spprintf(&cmd, 0, "LISTEN %s", channel_str);
if (PQsendQuery(obj->intern->conn, cmd)) {
php_pqconn_add_listener(obj, channel_str, channel_len, &listener TSRMLS_CC);
RETVAL_TRUE;
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not install listener (%s)", PHP_PQerrorMessage(obj->intern->conn));
RETVAL_FALSE;
}
efree(cmd);
PQfreemem(quoted_channel);
php_pqconn_notify_listeners(obj TSRMLS_CC);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not escape channel identifier (%s)", PHP_PQerrorMessage(obj->intern->conn));
}
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
RETVAL_FALSE;
}
}
}
ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify, 0, 0, 2) ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify, 0, 0, 2)
ZEND_ARG_INFO(0, channel) ZEND_ARG_INFO(0, channel)
ZEND_ARG_INFO(0, message) ZEND_ARG_INFO(0, message)
@ -1943,6 +2055,38 @@ static PHP_METHOD(pqconn, notify) {
} }
} }
ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify_async, 0, 0, 2)
ZEND_ARG_INFO(0, channel)
ZEND_ARG_INFO(0, message)
ZEND_END_ARG_INFO();
static PHP_METHOD(pqconn, notifyAsync) {
char *channel_str, *message_str;
int channel_len, message_len;
if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &channel_str, &channel_len, &message_str, &message_len)) {
php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
if (obj->intern) {
char *params[2] = {channel_str, message_str};
obj->intern->poller = PQconsumeInput;
if (PQsendQueryParams(obj->intern->conn, "select pg_notify($1, $2)", 2, NULL, (const char *const*) params, NULL, NULL, 0)) {
RETVAL_TRUE;
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not notify listeners (%s)", PHP_PQerrorMessage(obj->intern->conn));
RETVAL_FALSE;
}
php_pqconn_notify_listeners(obj TSRMLS_CC);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
RETVAL_FALSE;
}
}
}
ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_poll, 0, 0, 0) ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_poll, 0, 0, 0)
ZEND_END_ARG_INFO(); ZEND_END_ARG_INFO();
static PHP_METHOD(pqconn, poll) { static PHP_METHOD(pqconn, poll) {
@ -2714,7 +2858,9 @@ static zend_function_entry php_pqconn_methods[] = {
PHP_ME(pqconn, prepare, ai_pqconn_prepare, ZEND_ACC_PUBLIC) PHP_ME(pqconn, prepare, ai_pqconn_prepare, ZEND_ACC_PUBLIC)
PHP_ME(pqconn, prepareAsync, ai_pqconn_prepare_async, ZEND_ACC_PUBLIC) PHP_ME(pqconn, prepareAsync, ai_pqconn_prepare_async, ZEND_ACC_PUBLIC)
PHP_ME(pqconn, listen, ai_pqconn_listen, ZEND_ACC_PUBLIC) PHP_ME(pqconn, listen, ai_pqconn_listen, ZEND_ACC_PUBLIC)
PHP_ME(pqconn, listenAsync, ai_pqconn_listen_async, ZEND_ACC_PUBLIC)
PHP_ME(pqconn, notify, ai_pqconn_notify, ZEND_ACC_PUBLIC) PHP_ME(pqconn, notify, ai_pqconn_notify, ZEND_ACC_PUBLIC)
PHP_ME(pqconn, notifyAsync, ai_pqconn_notify_async, ZEND_ACC_PUBLIC)
PHP_ME(pqconn, getResult, ai_pqconn_get_result, ZEND_ACC_PUBLIC) PHP_ME(pqconn, getResult, ai_pqconn_get_result, ZEND_ACC_PUBLIC)
PHP_ME(pqconn, quote, ai_pqconn_quote, ZEND_ACC_PUBLIC) PHP_ME(pqconn, quote, ai_pqconn_quote, ZEND_ACC_PUBLIC)
PHP_ME(pqconn, quoteName, ai_pqconn_quote_name, ZEND_ACC_PUBLIC) PHP_ME(pqconn, quoteName, ai_pqconn_quote_name, ZEND_ACC_PUBLIC)

View File

@ -9,11 +9,13 @@ echo "Test\n";
include "_setup.inc"; include "_setup.inc";
$c = new pq\Connection(PQ_DSN); $c = new pq\Connection(PQ_DSN);
$c->exec("DROP TABLE IF EXISTS test");
new pq\Event($c, pq\Event::NOTICE, function($c, $notice) { new pq\Event($c, pq\Event::NOTICE, function($c, $notice) {
echo "Got notice: $notice\n"; echo "Got notice: $notice\n";
}); });
$t = new pq\Transaction($c); $t = new pq\Transaction($c);
$c->exec("DROP TABLE IF EXISTS test; CREATE TABLE test (id serial, data text)"); $c->exec("DROP TABLE IF EXISTS test");
$c->exec("CREATE TABLE test (id serial, data text)");
$s = $c->prepare("test_insert", "INSERT INTO test (data) VALUES (\$1)", array((new pq\Types($c))["text"]->oid)); $s = $c->prepare("test_insert", "INSERT INTO test (data) VALUES (\$1)", array((new pq\Types($c))["text"]->oid));
$s->exec(array("a")); $s->exec(array("a"));
$s->exec(array("b")); $s->exec(array("b"));