X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-pq;a=blobdiff_plain;f=src%2Fphp_pq.c;h=2ac9b196edcd996c3bd72925de4ffd3414f9c477;hp=4f0483d23b30f98905861b47d6259d32cc21e9af;hb=168d1f68510e87b77af42fa1e7087ae3c06d5eac;hpb=ac08c194e71fee0f68559c1ec43f323ecc334019 diff --git a/src/php_pq.c b/src/php_pq.c index 4f0483d..2ac9b19 100644 --- a/src/php_pq.c +++ b/src/php_pq.c @@ -6,7 +6,7 @@ | modification, are permitted provided that the conditions mentioned | | in the accompanying LICENSE file are met. | +--------------------------------------------------------------------+ - | Copyright (c) 2013, Michael Wallner | + | Copyright (c) 2013, Michael Wallner | +--------------------------------------------------------------------+ */ @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -106,9 +107,13 @@ typedef struct php_pq_object { void *intern; } php_pq_object_t; +#define PHP_PQCONN_ASYNC 0x01 +#define PHP_PQCONN_PERSISTENT 0x02 + typedef struct php_pqconn { PGconn *conn; int (*poller)(PGconn *); + php_resource_factory_t factory; HashTable listeners; HashTable eventhandlers; php_pq_callback_t onevent; @@ -157,6 +162,7 @@ typedef struct php_pqres_iterator { typedef struct php_pqres { PGresult *res; php_pqres_iterator_t *iter; + HashTable bound; } php_pqres_t; typedef struct php_pqres_object { @@ -169,6 +175,7 @@ typedef struct php_pqres_object { typedef struct php_pqstm { php_pqconn_object_t *conn; char *name; + HashTable bound; } php_pqstm_t; typedef struct php_pqstm_object { @@ -187,6 +194,7 @@ typedef enum php_pqtxn_isolation { typedef struct php_pqtxn { php_pqconn_object_t *conn; php_pqtxn_isolation_t isolation; + unsigned savepoint; unsigned readonly:1; unsigned deferrable:1; } php_pqtxn_t; @@ -335,16 +343,24 @@ static STATUS php_pqres_iterator_valid(zend_object_iterator *i TSRMLS_DC) return SUCCESS; } -static zval *php_pqres_row_to_zval(PGresult *res, unsigned row, php_pqres_fetch_t fetch_type TSRMLS_DC) +static zval *php_pqres_row_to_zval(PGresult *res, unsigned row, php_pqres_fetch_t fetch_type, zval **data_ptr TSRMLS_DC) { - zval *data; + zval *data = NULL; int c, cols; - MAKE_STD_ZVAL(data); - if (PHP_PQRES_FETCH_OBJECT == fetch_type) { - object_init(data); - } else { - array_init(data); + if (data_ptr) { + data = *data_ptr; + } + if (!data) { + MAKE_STD_ZVAL(data); + if (PHP_PQRES_FETCH_OBJECT == fetch_type) { + object_init(data); + } else { + array_init(data); + } + if (data_ptr) { + *data_ptr = data; + } } for (c = 0, cols = PQnfields(res); c < cols; ++c) { @@ -393,7 +409,7 @@ static void php_pqres_iterator_current(zend_object_iterator *i, zval ***data_ptr if (iter->current_val) { zval_ptr_dtor(&iter->current_val); } - iter->current_val = php_pqres_row_to_zval(obj->intern->res, iter->index, iter->fetch_type TSRMLS_CC); + iter->current_val = php_pqres_row_to_zval(obj->intern->res, iter->index, iter->fetch_type, NULL TSRMLS_CC); *data_ptr = &iter->current_val; } @@ -436,6 +452,18 @@ static zend_object_iterator_funcs php_pqres_iterator_funcs = { NULL }; +static int php_pqres_count_elements(zval *object, long *count TSRMLS_DC) +{ + php_pqres_object_t *obj = zend_object_store_get_object(object TSRMLS_CC); + + if (obj->intern) { + *count = (long) PQntuples(obj->intern->res); + return SUCCESS; + } else { + return FAILURE; + } +} + static STATUS php_pqres_success(PGresult *res TSRMLS_DC) { switch (PQresultStatus(res)) { @@ -494,12 +522,55 @@ 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); } +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) { php_pqconn_object_t *obj = o; if (obj->intern) { - PQfinish(obj->intern->conn); + php_pqconn_event_data_t *evdata; + PGresult *res; + + if ((evdata = PQinstanceData(obj->intern->conn, php_pqconn_event))) { + memset(evdata, 0, sizeof(*evdata)); + efree(evdata); + 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_dtor(&obj->intern->factory); php_pq_callback_dtor(&obj->intern->onevent); zend_hash_destroy(&obj->intern->listeners); zend_hash_destroy(&obj->intern->eventhandlers); @@ -547,6 +618,8 @@ static void php_pqres_object_free(void *o TSRMLS_DC) obj->intern->iter = NULL; } + zend_hash_destroy(&obj->intern->bound); + efree(obj->intern); obj->intern = NULL; } @@ -561,6 +634,7 @@ static void php_pqstm_object_free(void *o TSRMLS_DC) if (obj->intern) { php_pq_object_delref(obj->intern->conn TSRMLS_CC); efree(obj->intern->name); + zend_hash_destroy(&obj->intern->bound); efree(obj->intern); obj->intern = NULL; } @@ -1545,6 +1619,7 @@ static STATUS php_pqconn_update_socket(zval *this_ptr, php_pqconn_object_t *obj if ((CONNECTION_BAD != PQstatus(obj->intern->conn)) && (-1 < (socket = PQsocket(obj->intern->conn))) && (stream = php_stream_fopen_from_fd(socket, "r+b", NULL))) { + stream->flags |= PHP_STREAM_FLAG_NO_CLOSE; php_stream_to_zval(stream, zsocket); retval = SUCCESS; } else { @@ -1565,17 +1640,6 @@ static STATUS php_pqconn_update_socket(zval *this_ptr, php_pqconn_object_t *obj # define TSRMLS_CF(d) #endif -static void php_pqconn_event_register(PGEventRegister *event, php_pqconn_event_data_t *data) -{ - PQsetInstanceData(event->conn, php_pqconn_event, data); -} - -static void php_pqconn_event_conndestroy(PGEventConnDestroy *event, php_pqconn_event_data_t *data) -{ - PQsetInstanceData(event->conn, php_pqconn_event, NULL); - efree(data); -} - static int apply_event(void *p, void *a TSRMLS_DC) { zval **evh = p; @@ -1590,20 +1654,24 @@ static int apply_event(void *p, void *a TSRMLS_DC) 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; - TSRMLS_DF(data); + php_pqconn_event_data_t *data = PQinstanceData(event->conn, php_pqconn_event); - if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("reset"), (void *) &evhs)) { - zval *args, *connection = NULL; + if (data) { + zval **evhs; + TSRMLS_DF(data); - MAKE_STD_ZVAL(args); - array_init(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); + if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("reset"), (void *) &evhs)) { + zval *args, *connection = NULL; + + MAKE_STD_ZVAL(args); + array_init(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); + } } } @@ -1616,6 +1684,7 @@ static zval *result_instance_zval(PGresult *res TSRMLS_DC) MAKE_STD_ZVAL(rid); r->res = res; + ZEND_INIT_SYMTABLE(&r->bound); rid->type = IS_OBJECT; rid->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC); @@ -1626,48 +1695,46 @@ static zval *result_instance_zval(PGresult *res TSRMLS_DC) 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; - TSRMLS_DF(data); - - /* event listener */ - if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("result"), (void *) &evhs)) { - zval *args, *connection = NULL, *res = result_instance_zval(event->result TSRMLS_CC); - - MAKE_STD_ZVAL(args); - array_init(args); - 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); - } + php_pqconn_event_data_t *data = PQinstanceData(event->conn, php_pqconn_event); + + if (data) { + zval **evhs; + TSRMLS_DF(data); + + /* event listener */ + if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("result"), (void *) &evhs)) { + zval *args, *connection = NULL, *res = result_instance_zval(event->result TSRMLS_CC); + + MAKE_STD_ZVAL(args); + array_init(args); + 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); + } - /* async callback */ - if (data->obj->intern->onevent.fci.size > 0) { - zval *res = result_instance_zval(event->result TSRMLS_CC); + /* async callback */ + if (data->obj->intern->onevent.fci.size > 0) { + 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); + 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); + } } } static int php_pqconn_event(PGEventId id, void *e, void *data) { switch (id) { - case PGEVT_REGISTER: - php_pqconn_event_register(e, data); - break; - case PGEVT_CONNDESTROY: - php_pqconn_event_conndestroy(e, data); - break; case PGEVT_CONNRESET: - php_pqconn_event_connreset(e, data); + php_pqconn_event_connreset(e); break; case PGEVT_RESULTCREATE: - php_pqconn_event_resultcreate(e, data); + php_pqconn_event_resultcreate(e); break; default: break; @@ -1705,6 +1772,56 @@ static void php_pqconn_notice_recv(void *p, const PGresult *res) } } +typedef struct php_pqconn_resource_factory_data { + char *dsn; + long flags; +} php_pqconn_resource_factory_data_t; + +static void *php_pqconn_resource_factory_ctor(void *data, void *init_arg TSRMLS_DC) +{ + php_pqconn_resource_factory_data_t *o = init_arg; + + if (o->flags & PHP_PQCONN_ASYNC) { + return PQconnectStart(o->dsn); + } else { + return PQconnectdb(o->dsn); + } +} + +static void php_pqconn_resource_factory_dtor(void *opaque, void *handle TSRMLS_DC) +{ + PQfinish(handle); +} + +static php_resource_factory_ops_t php_pqconn_resource_factory_ops = { + php_pqconn_resource_factory_ctor, + NULL, + 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_ARG_INFO(0, dsn) ZEND_ARG_INFO(0, async) @@ -1713,28 +1830,35 @@ static PHP_METHOD(pqconn, __construct) { zend_error_handling zeh; char *dsn_str = ""; int dsn_len = 0; - zend_bool async = 0; + long flags = 0; zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sb", &dsn_str, &dsn_len, &async)) { + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &dsn_str, &dsn_len, &flags)) { php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - php_pqconn_event_data_t *data = php_pqconn_event_data_init(obj TSRMLS_CC); + php_pqconn_event_data_t *evdata = php_pqconn_event_data_init(obj TSRMLS_CC); + php_pqconn_resource_factory_data_t rfdata = {dsn_str, flags}; obj->intern = ecalloc(1, sizeof(*obj->intern)); zend_hash_init(&obj->intern->listeners, 0, NULL, (dtor_func_t) zend_hash_destroy, 0); zend_hash_init(&obj->intern->eventhandlers, 0, NULL, ZVAL_PTR_DTOR, 0); - - if (async) { - obj->intern->conn = PQconnectStart(dsn_str); + 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_resource_factory_init(&obj->intern->factory, &php_pqconn_persistent_resource_factory_ops, phf, (void (*)(void*)) php_persistent_handle_abandon); + } else{ + php_resource_factory_init(&obj->intern->factory, &php_pqconn_resource_factory_ops, NULL, NULL); + } + if (flags & PHP_PQCONN_ASYNC) { obj->intern->poller = (int (*)(PGconn*)) PQconnectPoll; - } else { - obj->intern->conn = PQconnectdb(dsn_str); } - PQsetNoticeReceiver(obj->intern->conn, php_pqconn_notice_recv, data); - PQregisterEventProc(obj->intern->conn, php_pqconn_event, "ext-pq", data); + obj->intern->conn = php_resource_factory_handle_ctor(&obj->intern->factory, &rfdata TSRMLS_CC); + + PQregisterEventProc(obj->intern->conn, php_pqconn_event, "ext-pq", NULL); + /* the connection might be persistent, so reset event_proc instance data */ + PQsetInstanceData(obj->intern->conn, php_pqconn_event, evdata); + PQsetNoticeReceiver(obj->intern->conn, php_pqconn_notice_recv, evdata); if (SUCCESS != php_pqconn_update_socket(getThis(), obj TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connection failed (%s)", PHP_PQerrorMessage(obj->intern->conn)); @@ -1818,7 +1942,7 @@ static PHP_METHOD(pqconn, listen) { PGresult *res; char *cmd; - spprintf(&cmd, 0, "LISTEN %s", channel_str); + spprintf(&cmd, 0, "LISTEN %s", quoted_channel); res = PQexec(obj->intern->conn, cmd); efree(cmd); @@ -1848,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_ARG_INFO(0, channel) ZEND_ARG_INFO(0, message) @@ -1886,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_END_ARG_INFO(); static PHP_METHOD(pqconn, poll) { @@ -1933,6 +2134,7 @@ static PHP_METHOD(pqconn, exec) { php_pqres_t *r = ecalloc(1, sizeof(*r)); r->res = res; + ZEND_INIT_SYMTABLE(&r->bound); return_value->type = IS_OBJECT; return_value->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC); } @@ -1959,6 +2161,7 @@ static PHP_METHOD(pqconn, getResult) { php_pqres_t *r = ecalloc(1, sizeof(*r)); r->res = res; + ZEND_INIT_SYMTABLE(&r->bound); return_value->type = IS_OBJECT; return_value->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC); } else { @@ -2138,6 +2341,7 @@ static PHP_METHOD(pqconn, execParams) { php_pqres_t *r = ecalloc(1, sizeof(*r)); r->res = res; + ZEND_INIT_SYMTABLE(&r->bound); return_value->type = IS_OBJECT; return_value->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC); } @@ -2277,6 +2481,7 @@ static PHP_METHOD(pqconn, prepare) { php_pq_object_addref(obj TSRMLS_CC); stm->conn = obj; stm->name = estrdup(name_str); + ZEND_INIT_SYMTABLE(&stm->bound); return_value->type = IS_OBJECT; return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC); @@ -2345,6 +2550,7 @@ static PHP_METHOD(pqconn, prepareAsync) { php_pq_object_addref(obj TSRMLS_CC); stm->conn = obj; stm->name = estrdup(name_str); + ZEND_INIT_SYMTABLE(&stm->bound); return_value->type = IS_OBJECT; return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC); @@ -2652,7 +2858,9 @@ static zend_function_entry php_pqconn_methods[] = { PHP_ME(pqconn, prepare, ai_pqconn_prepare, 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, listenAsync, ai_pqconn_listen_async, 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, quote, ai_pqconn_quote, ZEND_ACC_PUBLIC) PHP_ME(pqconn, quoteName, ai_pqconn_quote_name, ZEND_ACC_PUBLIC) @@ -2718,7 +2926,7 @@ static PHP_METHOD(pqtypes, refresh) { int r, rows; for (r = 0, rows = PQntuples(res); r < rows; ++r) { - zval *row = php_pqres_row_to_zval(res, r, PHP_PQRES_FETCH_OBJECT TSRMLS_CC); + zval *row = php_pqres_row_to_zval(res, r, PHP_PQRES_FETCH_OBJECT, NULL TSRMLS_CC); long oid = atol(PQgetvalue(res, r, 0 )); char *name = PQgetvalue(res, r, 1); @@ -2747,8 +2955,9 @@ static zend_function_entry php_pqtypes_methods[] = { {0} }; -static zval *php_pqres_iteration(zval *this_ptr, php_pqres_object_t *obj, php_pqres_fetch_t fetch_type, zval ***row TSRMLS_DC) +static STATUS php_pqres_iteration(zval *this_ptr, php_pqres_object_t *obj, php_pqres_fetch_t fetch_type, zval ***row TSRMLS_DC) { + STATUS rv; php_pqres_fetch_t orig_fetch; if (!obj) { @@ -2761,13 +2970,146 @@ static zval *php_pqres_iteration(zval *this_ptr, php_pqres_object_t *obj, php_pq } orig_fetch = obj->intern->iter->fetch_type; obj->intern->iter->fetch_type = fetch_type; - if (SUCCESS == obj->intern->iter->zi.funcs->valid((zend_object_iterator *) obj->intern->iter TSRMLS_CC)) { + if (SUCCESS == (rv = obj->intern->iter->zi.funcs->valid((zend_object_iterator *) obj->intern->iter TSRMLS_CC))) { obj->intern->iter->zi.funcs->get_current_data((zend_object_iterator *) obj->intern->iter, row TSRMLS_CC); obj->intern->iter->zi.funcs->move_forward((zend_object_iterator *) obj->intern->iter TSRMLS_CC); } obj->intern->iter->fetch_type = orig_fetch; - return *row ? **row : NULL; + return rv; +} + +typedef struct php_pqres_col { + char *name; + int num; +} php_pqres_col_t; + +static STATUS column_nn(php_pqres_object_t *obj, zval *zcol, php_pqres_col_t *col TSRMLS_DC) +{ + long index = -1; + char *name = NULL; + + switch (Z_TYPE_P(zcol)) { + default: + convert_to_string(zcol); + /* no break */ + + case IS_STRING: + if (!is_numeric_string(Z_STRVAL_P(zcol), Z_STRLEN_P(zcol), &index, NULL, 0)) { + name = Z_STRVAL_P(zcol); + } + break; + + case IS_LONG: + index = Z_LVAL_P(zcol); + break; + } + + if (name) { + col->name = name; + col->num = PQfnumber(obj->intern->res, name); + } else { + col->name = PQfname(obj->intern->res, index); + col->num = index; + } + + if (!col->name) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to find column at index %ld", index); + return FAILURE; + } + if (col->num == -1) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to find column with name '%s'", name); + return FAILURE; + } + return SUCCESS; +} + +static int compare_index(const void *lptr, const void *rptr TSRMLS_DC) +{ + const Bucket *l = *(const Bucket **) lptr; + const Bucket *r = *(const Bucket **) rptr; + + if (l->h < r->h) { + return -1; + } + if (l->h > r->h) { + return 1; + } + return 0; +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqres_bind, 0, 0, 2) + ZEND_ARG_INFO(0, col) + ZEND_ARG_INFO(1, ref) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqres, bind) { + zval *zcol, *zref; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/z", &zcol, &zref)) { + php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + php_pqres_col_t col; + + if (SUCCESS == column_nn(obj, zcol, &col TSRMLS_CC)) { + Z_ADDREF_P(zref); + if (SUCCESS == zend_hash_index_update(&obj->intern->bound, col.num, (void *) &zref, sizeof(zval *), NULL)) { + zend_hash_sort(&obj->intern->bound, zend_qsort, compare_index, 0 TSRMLS_CC); + RETVAL_TRUE; + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to bind column %s@%d", col.name, col.num); + RETVAL_FALSE; + } + } else { + RETVAL_FALSE; + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Result not initialized"); + RETVAL_FALSE; + } + } +} + +static int apply_bound(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key) +{ + zval **zvalue, **zbound = p; + zval **zrow = va_arg(argv, zval **); + + if (SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(zrow), key->h, (void *) &zvalue)) { + zval_dtor(*zbound); + ZVAL_COPY_VALUE(*zbound, *zvalue); + ZVAL_NULL(*zvalue); + zval_ptr_dtor(zvalue); + Z_ADDREF_P(*zbound); + *zvalue = *zbound; + return ZEND_HASH_APPLY_KEEP; + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to find column ad index %lu", key->h); + return ZEND_HASH_APPLY_STOP; + } +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqres_fetch_bound, 0, 0, 0) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqres, fetchBound) { + zend_error_handling zeh; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + if (SUCCESS == zend_parse_parameters_none()) { + php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + zval **row = NULL; + + if (SUCCESS == php_pqres_iteration(getThis(), obj, PHP_PQRES_FETCH_ARRAY, &row TSRMLS_CC)) { + if (row) { + zend_hash_apply_with_arguments(&obj->intern->bound TSRMLS_CC, apply_bound, 1, row); + RETVAL_ZVAL(*row, 1, 0); + } + } + } + } + zend_restore_error_handling(&zeh TSRMLS_CC); } ZEND_BEGIN_ARG_INFO_EX(ai_pqres_fetch_row, 0, 0, 0) @@ -2776,18 +3118,23 @@ ZEND_END_ARG_INFO(); static PHP_METHOD(pqres, fetchRow) { zend_error_handling zeh; php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - long fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : PHP_PQRES_FETCH_ARRAY; + long fetch_type = -1; zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &fetch_type)) { - zval **row = NULL; + if (obj->intern) { + zval **row = NULL; - php_pqres_iteration(getThis(), obj, fetch_type, &row TSRMLS_CC); + if (fetch_type == -1) { + fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : PHP_PQRES_FETCH_ARRAY; + } + php_pqres_iteration(getThis(), obj, fetch_type, &row TSRMLS_CC); - if (row) { - RETVAL_ZVAL(*row, 1, 0); + if (row) { + RETVAL_ZVAL(*row, 1, 0); + } } else { - RETVAL_FALSE; + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Result not initialized"); } } zend_restore_error_handling(&zeh TSRMLS_CC); @@ -2821,29 +3168,203 @@ static PHP_METHOD(pqres, fetchCol) { zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &fetch_col)) { php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - zval **row = NULL; - php_pqres_iteration(getThis(), obj, obj->intern->iter ? obj->intern->iter->fetch_type : 0, &row TSRMLS_CC); + if (obj->intern) { + zval **row = NULL; + + php_pqres_iteration(getThis(), obj, obj->intern->iter ? obj->intern->iter->fetch_type : 0, &row TSRMLS_CC); - if (row) { - zval **col = column_at(*row, fetch_col TSRMLS_CC); + if (row) { + zval **col = column_at(*row, fetch_col TSRMLS_CC); - if (col) { - RETVAL_ZVAL(*col, 1, 0); + if (col) { + RETVAL_ZVAL(*col, 1, 0); + } + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Result not initialized"); + } + } + zend_restore_error_handling(&zeh TSRMLS_CC); +} + +static int apply_to_col(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key) +{ + zval **c = p; + php_pqres_object_t *obj = va_arg(argv, php_pqres_object_t *); + php_pqres_col_t *col, **cols = va_arg(argv, php_pqres_col_t **); + STATUS *rv = va_arg(argv, STATUS *); + + col = *cols; + if (SUCCESS == column_nn(obj, *c, col TSRMLS_CC)) { + *rv = SUCCESS; + } else { + *rv = FAILURE; + return ZEND_HASH_APPLY_STOP; + } + ++*cols; + + return ZEND_HASH_APPLY_KEEP; +} + +static php_pqres_col_t *php_pqres_convert_to_cols(php_pqres_object_t *obj, HashTable *ht TSRMLS_DC) +{ + php_pqres_col_t *tmp, *cols = ecalloc(zend_hash_num_elements(ht), sizeof(*cols)); + STATUS rv = SUCCESS; + + tmp = cols; + zend_hash_apply_with_arguments(ht TSRMLS_CC, apply_to_col, 2, obj, &tmp, &rv); + + if (SUCCESS == rv) { + return cols; + } else { + efree(cols); + return NULL; + } +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqres_map, 0, 0, 0) + ZEND_ARG_INFO(0, keys) + ZEND_ARG_INFO(0, vals) + ZEND_ARG_INFO(0, fetch_type) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqres, map) { + zend_error_handling zeh; + zval *zkeys = 0, *zvals = 0; + long fetch_type = -1; + + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z/!z/!l", &zkeys, &zvals, &fetch_type)) { + php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + int ks = 0, vs = 0; + php_pqres_col_t def = {PQfname(obj->intern->res, 0), 0}, *keys = NULL, *vals = NULL; + + if (zkeys) { + convert_to_array(zkeys); + + if ((ks = zend_hash_num_elements(Z_ARRVAL_P(zkeys)))) { + keys = php_pqres_convert_to_cols(obj, Z_ARRVAL_P(zkeys) TSRMLS_CC); + } else { + ks = 1; + keys = &def; + } } else { - RETVAL_FALSE; + ks = 1; + keys = &def; + } + if (zvals) { + convert_to_array(zvals); + + if ((vs = zend_hash_num_elements(Z_ARRVAL_P(zvals)))) { + vals = php_pqres_convert_to_cols(obj, Z_ARRVAL_P(zvals) TSRMLS_CC); + } + } + + if (fetch_type == -1) { + fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : PHP_PQRES_FETCH_ARRAY; + } + + if (keys) { + int rows, r; + zval **cur; + + switch (fetch_type) { + case PHP_PQRES_FETCH_ARRAY: + case PHP_PQRES_FETCH_ASSOC: + array_init(return_value); + break; + case PHP_PQRES_FETCH_OBJECT: + object_init(return_value); + break; + } + for (r = 0, rows = PQntuples(obj->intern->res); r < rows; ++r) { + int k, v; + + cur = &return_value; + for (k = 0; k < ks; ++k) { + char *key = PQgetvalue(obj->intern->res, r, keys[k].num); + int len = PQgetlength(obj->intern->res, r, keys[k].num); + + if (SUCCESS != zend_symtable_find(HASH_OF(*cur), key, len + 1, (void *) &cur)) { + zval *tmp; + + MAKE_STD_ZVAL(tmp); + switch (fetch_type) { + case PHP_PQRES_FETCH_ARRAY: + case PHP_PQRES_FETCH_ASSOC: + array_init(tmp); + break; + case PHP_PQRES_FETCH_OBJECT: + object_init(tmp); + break; + } + if (SUCCESS != zend_symtable_update(HASH_OF(*cur), key, len + 1, (void *) &tmp, sizeof(zval *), (void *) &cur)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create map"); + goto err; + } + } + } + if (vals && vs) { + for (v = 0; v < vs; ++v) { + char *val = PQgetvalue(obj->intern->res, r, vals[v].num); + int len = PQgetlength(obj->intern->res, r, vals[v].num); + + switch (fetch_type) { + case PHP_PQRES_FETCH_ARRAY: + add_index_stringl(*cur, vals[v].num, val, len, 1); + break; + case PHP_PQRES_FETCH_ASSOC: + add_assoc_stringl(*cur, vals[v].name, val, len, 1); + break; + case PHP_PQRES_FETCH_OBJECT: + add_property_stringl(*cur, vals[v].name, val, len, 1); + break; + } + } + } else { + php_pqres_row_to_zval(obj->intern->res, r, fetch_type, cur TSRMLS_CC); + } + } + } + + err: + if (keys && keys != &def) { + efree(keys); + } + if (vals) { + efree(vals); } } else { - RETVAL_FALSE; + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Result not initialized"); } } zend_restore_error_handling(&zeh TSRMLS_CC); +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqres_count, 0, 0, 0) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqres, count) { + if (SUCCESS == zend_parse_parameters_none()) { + long count; + if (SUCCESS == php_pqres_count_elements(getThis(), &count TSRMLS_CC)) { + RETVAL_LONG(count); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Result not initialized"); + } + } } 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) PHP_ME(pqres, fetchRow, ai_pqres_fetch_row, ZEND_ACC_PUBLIC) PHP_ME(pqres, fetchCol, ai_pqres_fetch_col, ZEND_ACC_PUBLIC) + PHP_ME(pqres, count, ai_pqres_count, ZEND_ACC_PUBLIC) + PHP_ME(pqres, map, ai_pqres_map, ZEND_ACC_PUBLIC) {0} }; @@ -2882,6 +3403,7 @@ static PHP_METHOD(pqstm, __construct) { php_pq_object_addref(conn_obj TSRMLS_CC); stm->conn = conn_obj; stm->name = estrdup(name_str); + ZEND_INIT_SYMTABLE(&stm->bound); obj->intern = stm; } } else { @@ -2890,6 +3412,26 @@ static PHP_METHOD(pqstm, __construct) { } zend_restore_error_handling(&zeh TSRMLS_CC); } +ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_bind, 0, 0, 2) + ZEND_ARG_INFO(0, param_no) + ZEND_ARG_INFO(1, param_ref) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqstm, bind) { + long param_no; + zval *param_ref; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz", ¶m_no, ¶m_ref)) { + php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + Z_ADDREF_P(param_ref); + zend_hash_index_update(&obj->intern->bound, param_no, (void *) ¶m_ref, sizeof(zval *), NULL); + zend_hash_sort(&obj->intern->bound, zend_qsort, compare_index, 0 TSRMLS_CC); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Statement not initialized"); + } + } +} ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_exec, 0, 0, 0) ZEND_ARG_ARRAY_INFO(0, params, 1) @@ -2909,9 +3451,12 @@ static PHP_METHOD(pqstm, exec) { HashTable zdtor; PGresult *res; + ZEND_INIT_SYMTABLE(&zdtor); + if (zparams) { - ZEND_INIT_SYMTABLE(&zdtor); count = php_pq_params_to_array(Z_ARRVAL_P(zparams), ¶ms, &zdtor TSRMLS_CC); + } else { + count = php_pq_params_to_array(&obj->intern->bound, ¶ms, &zdtor TSRMLS_CC); } res = PQexecPrepared(obj->intern->conn->intern->conn, obj->intern->name, count, (const char *const*) params, NULL, NULL, 0); @@ -2919,9 +3464,7 @@ static PHP_METHOD(pqstm, exec) { if (params) { efree(params); } - if (zparams) { - zend_hash_destroy(&zdtor); - } + zend_hash_destroy(&zdtor); php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC); @@ -2930,6 +3473,7 @@ static PHP_METHOD(pqstm, exec) { php_pqres_t *r = ecalloc(1, sizeof(*r)); r->res = res; + ZEND_INIT_SYMTABLE(&r->bound); return_value->type = IS_OBJECT; return_value->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC); } @@ -3050,6 +3594,7 @@ static PHP_METHOD(pqstm, desc) { static zend_function_entry php_pqstm_methods[] = { PHP_ME(pqstm, __construct, ai_pqstm_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) + PHP_ME(pqstm, bind, ai_pqstm_bind, ZEND_ACC_PUBLIC) PHP_ME(pqstm, exec, ai_pqstm_exec, ZEND_ACC_PUBLIC) PHP_ME(pqstm, desc, ai_pqstm_desc, ZEND_ACC_PUBLIC) PHP_ME(pqstm, execAsync, ai_pqstm_exec_async, ZEND_ACC_PUBLIC) @@ -3098,6 +3643,62 @@ static PHP_METHOD(pqtxn, __construct) { zend_restore_error_handling(&zeh TSRMLS_CC); } +ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_savepoint, 0, 0, 0) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqtxn, savepoint) { + zend_error_handling zeh; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + if (SUCCESS == zend_parse_parameters_none()) { + php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + char *cmd; + PGresult *res; + + spprintf(&cmd, 0, "SAVEPOINT \"%u\"", ++obj->intern->savepoint); + res = PQexec(obj->intern->conn->intern->conn, cmd); + + if (res) { + php_pqres_success(res TSRMLS_CC); + PHP_PQclear(res); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } + + efree(cmd); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized"); + } + } + zend_restore_error_handling(&zeh TSRMLS_CC); +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_savepoint_async, 0, 0, 0) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqtxn, savepointAsync) { + zend_error_handling zeh; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + if (SUCCESS == zend_parse_parameters_none()) { + php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + char *cmd; + + spprintf(&cmd, 0, "SAVEPOINT \"%u\"", ++obj->intern->savepoint); + if (!PQsendQuery(obj->intern->conn->intern->conn, cmd)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } + + efree(cmd); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized"); + } + } + zend_restore_error_handling(&zeh TSRMLS_CC); +} + ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_commit, 0, 0, 0) ZEND_END_ARG_INFO(); static PHP_METHOD(pqtxn, commit) { @@ -3108,18 +3709,31 @@ static PHP_METHOD(pqtxn, commit) { php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); if (obj->intern) { + PGresult *res; - if (obj->intern->conn->intern) { - PGresult *res = PQexec(obj->intern->conn->intern->conn, "COMMIT"); + if (obj->intern->savepoint) { + char *cmd; + + spprintf(&cmd, 0, "RELEASE SAVEPOINT \"%u\"", obj->intern->savepoint--); + res = PQexec(obj->intern->conn->intern->conn, cmd); if (res) { php_pqres_success(res TSRMLS_CC); PHP_PQclear(res); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not commit transaction (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn)); } + + efree(cmd); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not intialized"); + res = PQexec(obj->intern->conn->intern->conn, "COMMIT"); + + if (res) { + php_pqres_success(res TSRMLS_CC); + PHP_PQclear(res); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to commit transaction (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized"); @@ -3138,14 +3752,21 @@ static PHP_METHOD(pqtxn, commitAsync) { php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); if (obj->intern) { - if (obj->intern->conn->intern) { - obj->intern->conn->intern->poller = PQconsumeInput; + obj->intern->conn->intern->poller = PQconsumeInput; - if (!PQsendQuery(obj->intern->conn->intern->conn, "COMMIT")) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not commit transaction (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + if (obj->intern->savepoint) { + char *cmd; + + spprintf(&cmd, 0, "RELEASE SAVEPOINT \"%u\"", obj->intern->savepoint--); + if (!PQsendQuery(obj->intern->conn->intern->conn, cmd)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn)); } + + efree(cmd); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not intialized"); + if (!PQsendQuery(obj->intern->conn->intern->conn, "COMMIT")) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to commit transaction (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized"); @@ -3164,17 +3785,31 @@ static PHP_METHOD(pqtxn, rollback) { php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); if (obj->intern) { - if (obj->intern->conn->intern) { - PGresult *res = PQexec(obj->intern->conn->intern->conn, "ROLLBACK"); + PGresult *res; + + if (obj->intern->savepoint) { + char *cmd; + + spprintf(&cmd, 0, "ROLLBACK TO SAVEPOINT \"%u\"", obj->intern->savepoint--); + res = PQexec(obj->intern->conn->intern->conn, cmd); if (res) { php_pqres_success(res TSRMLS_CC); PHP_PQclear(res); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not rollback transaction (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn)); } + + efree(cmd); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not intialized"); + res = PQexec(obj->intern->conn->intern->conn, "ROLLBACK"); + + if (res) { + php_pqres_success(res TSRMLS_CC); + PHP_PQclear(res); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to rollback transaction (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized"); @@ -3193,13 +3828,145 @@ static PHP_METHOD(pqtxn, rollbackAsync) { php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); if (obj->intern) { - if (obj->intern->conn->intern) { - obj->intern->conn->intern->poller = PQconsumeInput; - if (!PQsendQuery(obj->intern->conn->intern->conn, "REOLLBACK")) { + obj->intern->conn->intern->poller = PQconsumeInput; + + if (obj->intern->savepoint) { + char *cmd; + + spprintf(&cmd, 0, "ROLLBACK TO SAVEPOINT \"%u\"", obj->intern->savepoint--); + if (!PQsendQuery(obj->intern->conn->intern->conn, cmd)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } + + efree(cmd); + } else { + if (!PQsendQuery(obj->intern->conn->intern->conn, "ROLLBACK")) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not rollback transaction (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); } + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized"); + } + } + zend_restore_error_handling(&zeh TSRMLS_CC); +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_export_snapshot, 0, 0, 0) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqtxn, exportSnapshot) { + zend_error_handling zeh; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + if (SUCCESS == zend_parse_parameters_none()) { + php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + PGresult *res = PQexec(obj->intern->conn->intern->conn, "SELECT pg_export_snapshot()"); + + if (res) { + if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { + RETVAL_STRING(PQgetvalue(res, 0, 0), 1); + } + + PHP_PQclear(res); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not intialized"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to export transaction snapshot (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized"); + } + } + zend_restore_error_handling(&zeh TSRMLS_CC); +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_export_snapshot_async, 0, 0, 0) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqtxn, exportSnapshotAsync) { + zend_error_handling zeh; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + if (SUCCESS == zend_parse_parameters_none()) { + php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + obj->intern->conn->intern->poller = PQconsumeInput; + + if (!PQsendQuery(obj->intern->conn->intern->conn, "SELECT pg_export_snapshot()")) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to export transaction snapshot (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized"); + } + } + zend_restore_error_handling(&zeh TSRMLS_CC); +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_import_snapshot, 0, 0, 1) + ZEND_ARG_INFO(0, snapshot_id) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqtxn, importSnapshot) { + zend_error_handling zeh; + char *snapshot_str; + int snapshot_len; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &snapshot_str, &snapshot_len)) { + php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + if (obj->intern->isolation >= PHP_PQTXN_REPEATABLE_READ) { + char *cmd, *sid = PQescapeLiteral(obj->intern->conn->intern->conn, snapshot_str, snapshot_len); + PGresult *res; + + spprintf(&cmd, 0, "SET TRANSACTION SNAPSHOT %s", sid); + res = PQexec(obj->intern->conn->intern->conn, cmd); + + if (res) { + php_pqres_success(res TSRMLS_CC); + PHP_PQclear(res); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "A transaction must have at least isolation level REPEATABLE READ to be able to import a snapshot"); + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized"); + } + } + zend_restore_error_handling(&zeh TSRMLS_CC); +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_import_snapshot_async, 0, 0, 1) + ZEND_ARG_INFO(0, snapshot_id) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqtxn, importSnapshotAsync) { + zend_error_handling zeh; + char *snapshot_str; + int snapshot_len; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &snapshot_str, &snapshot_len)) { + php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + if (obj->intern->isolation >= PHP_PQTXN_REPEATABLE_READ) { + char *sid = PQescapeLiteral(obj->intern->conn->intern->conn, snapshot_str, snapshot_len); + + if (sid) { + char *cmd; + obj->intern->conn->intern->poller = PQconsumeInput; + + spprintf(&cmd, 0, "SET TRANSACTION SNAPSHOT %s", sid); + if (!PQsendQuery(obj->intern->conn->intern->conn, cmd)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } + efree(cmd); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to quote snapshot identifier (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "A transaction must have at least isolation level REPEATABLE READ to be able to import a snapshot"); } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized"); @@ -3326,6 +4093,12 @@ static zend_function_entry php_pqtxn_methods[] = { PHP_ME(pqtxn, rollback, ai_pqtxn_rollback, ZEND_ACC_PUBLIC) PHP_ME(pqtxn, commitAsync, ai_pqtxn_commit_async, ZEND_ACC_PUBLIC) PHP_ME(pqtxn, rollbackAsync, ai_pqtxn_rollback_async, ZEND_ACC_PUBLIC) + PHP_ME(pqtxn, savepoint, ai_pqtxn_savepoint, ZEND_ACC_PUBLIC) + PHP_ME(pqtxn, savepointAsync, ai_pqtxn_savepoint_async, ZEND_ACC_PUBLIC) + PHP_ME(pqtxn, exportSnapshot, ai_pqtxn_export_snapshot, ZEND_ACC_PUBLIC) + PHP_ME(pqtxn, exportSnapshotAsync, ai_pqtxn_export_snapshot_async, ZEND_ACC_PUBLIC) + PHP_ME(pqtxn, importSnapshot, ai_pqtxn_import_snapshot, ZEND_ACC_PUBLIC) + PHP_ME(pqtxn, importSnapshotAsync, ai_pqtxn_import_snapshot_async, ZEND_ACC_PUBLIC) PHP_ME(pqtxn, openLOB, ai_pqtxn_open_lob, ZEND_ACC_PUBLIC) PHP_ME(pqtxn, createLOB, ai_pqtxn_create_lob, ZEND_ACC_PUBLIC) PHP_ME(pqtxn, unlinkLOB, ai_pqtxn_unlink_lob, ZEND_ACC_PUBLIC) @@ -3964,6 +4737,8 @@ static PHP_MINIT_FUNCTION(pq) zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("POLLING_WRITING"), PGRES_POLLING_WRITING TSRMLS_CC); zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("POLLING_OK"), PGRES_POLLING_OK TSRMLS_CC); + zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("ASYNC"), 0x1 TSRMLS_CC); + zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("PERSISTENT"), 0x2 TSRMLS_CC); memset(&ce, 0, sizeof(ce)); INIT_NS_CLASS_ENTRY(ce, "pq", "Types", php_pqtypes_methods); php_pqtypes_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC); @@ -3992,7 +4767,7 @@ static PHP_MINIT_FUNCTION(pq) php_pqres_class_entry->create_object = php_pqres_create_object; php_pqres_class_entry->iterator_funcs.funcs = &php_pqres_iterator_funcs; php_pqres_class_entry->get_iterator = php_pqres_iterator_init; - zend_class_implements(php_pqres_class_entry TSRMLS_CC, 1, zend_ce_traversable); + zend_class_implements(php_pqres_class_entry TSRMLS_CC, 2, zend_ce_traversable, spl_ce_Countable); memcpy(&php_pqres_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_pqres_object_handlers.read_property = php_pq_object_read_prop; @@ -4000,6 +4775,7 @@ static PHP_MINIT_FUNCTION(pq) php_pqres_object_handlers.clone_obj = NULL; php_pqres_object_handlers.get_property_ptr_ptr = NULL; php_pqres_object_handlers.get_debug_info = php_pq_object_debug_info; + php_pqres_object_handlers.count_elements = php_pqres_count_elements; zend_hash_init(&php_pqres_object_prophandlers, 6, NULL, NULL, 1); @@ -4208,6 +4984,8 @@ static PHP_MINIT_FUNCTION(pq) zend_declare_class_constant_long(php_pqcopy_class_entry, ZEND_STRL("FROM_STDIN"), PHP_PQCOPY_FROM_STDIN TSRMLS_CC); zend_declare_class_constant_long(php_pqcopy_class_entry, ZEND_STRL("TO_STDOUT"), PHP_PQCOPY_TO_STDOUT TSRMLS_CC); + php_persistent_handle_provide(ZEND_STRL("pq\\Connection"), &php_pqconn_resource_factory_ops, NULL, NULL); + /* REGISTER_INI_ENTRIES(); */ @@ -4222,6 +5000,7 @@ static PHP_MSHUTDOWN_FUNCTION(pq) /* uncomment this line if you have INI entries UNREGISTER_INI_ENTRIES(); */ + php_persistent_handle_cleanup(ZEND_STRL("pq\\Connection"), NULL, 0 TSRMLS_CC); return SUCCESS; } /* }}} */ @@ -4230,8 +5009,23 @@ static PHP_MSHUTDOWN_FUNCTION(pq) */ static PHP_MINFO_FUNCTION(pq) { +#ifdef HAVE_PQLIBVERSION + int libpq_v; +#endif + char libpq_version[10] = "pre-9.1"; + php_info_print_table_start(); - php_info_print_table_header(2, "pq support", "enabled"); + php_info_print_table_header(2, "PQ Support", "enabled"); + php_info_print_table_row(2, "Extension Version", PHP_PQ_EXT_VERSION); + php_info_print_table_end(); + + php_info_print_table_start(); + php_info_print_table_header(2, "Used Library", "Version"); +#ifdef HAVE_PQLIBVERSION + libpq_v = PQlibVersion(); + slprintf(libpq_version, sizeof(libpq_version), "%d.%d.%d", libpq_v/10000%100, libpq_v/100%100, libpq_v%100); +#endif + php_info_print_table_row(2, "libpq", libpq_version); php_info_print_table_end(); /* Remove comments if you have entries in php.ini @@ -4244,10 +5038,17 @@ const zend_function_entry pq_functions[] = { {0} }; +static zend_module_dep pq_module_deps[] = { + ZEND_MOD_REQUIRED("raphf") + ZEND_MOD_END +}; + /* {{{ pq_module_entry */ zend_module_entry pq_module_entry = { - STANDARD_MODULE_HEADER, + STANDARD_MODULE_HEADER_EX, + NULL, + pq_module_deps, "pq", pq_functions, PHP_MINIT(pq),