+ handler->read(object, obj, return_value TSRMLS_CC);
+ } else {
+ zend_error(E_ERROR, "Cannot access %s properties by reference or array key/index", ancestor(obj->zo.ce)->name);
+ return_value = NULL;
+ }
+ } else {
+ return_value = zend_get_std_object_handlers()->read_property(object, member, type, key TSRMLS_CC);
+ }
+
+ return return_value;
+}
+
+static void php_pq_object_write_prop(zval *object, zval *member, zval *value, const zend_literal *key TSRMLS_DC)
+{
+ php_pq_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
+ php_pq_object_prophandler_t *handler;
+
+ if (SUCCESS == zend_hash_find(obj->prophandler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void *) &handler)) {
+ if (handler->write) {
+ handler->write(object, obj, value TSRMLS_CC);
+ }
+ } else {
+ zend_get_std_object_handlers()->write_property(object, member, value, key TSRMLS_CC);
+ }
+}
+
+static STATUS php_pqconn_update_socket(zval *this_ptr, php_pqconn_object_t *obj TSRMLS_DC)
+{
+ zval *zsocket, zmember;
+ php_stream *stream;
+ STATUS retval;
+ int socket;
+
+ if (!obj) {
+ obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+ }
+
+ INIT_PZVAL(&zmember);
+ ZVAL_STRINGL(&zmember, "socket", sizeof("socket")-1, 0);
+ MAKE_STD_ZVAL(zsocket);
+
+ if ((CONNECTION_BAD != PQstatus(obj->conn))
+ && (-1 < (socket = PQsocket(obj->conn)))
+ && (stream = php_stream_fopen_from_fd(socket, "r+b", NULL))) {
+ php_stream_to_zval(stream, zsocket);
+ retval = SUCCESS;
+ } else {
+ ZVAL_NULL(zsocket);
+ retval = FAILURE;
+ }
+ zend_get_std_object_handlers()->write_property(getThis(), &zmember, zsocket, NULL TSRMLS_CC);
+ zval_ptr_dtor(&zsocket);
+
+ return retval;
+}
+
+#ifdef ZTS
+# define TSRMLS_DF(d) TSRMLS_D = (d)->ts
+# define TSRMLS_CF(d) (d)->ts = TSRMLS_C
+#else
+# define TSRMLS_DF(d)
+# 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 void php_pqconn_event_resultcreate(PGEventResultCreate *event, php_pqconn_event_data_t *data)
+{
+ TSRMLS_DF(data);
+
+ if (data->obj->onevent.fci.size > 0) {
+ zval *res;
+
+ MAKE_STD_ZVAL(res);
+ res->type = IS_OBJECT;
+ res->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, event->result, NULL TSRMLS_CC);
+
+ Z_ADDREF_P(res);
+ PQresultSetInstanceData(event->result, php_pqconn_event, res);
+
+ zend_fcall_info_argn(&data->obj->onevent.fci TSRMLS_CC, 1, &res);
+ zend_fcall_info_call(&data->obj->onevent.fci, &data->obj->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_RESULTCREATE:
+ php_pqconn_event_resultcreate(e, data);
+ break;
+ default:
+ break;
+ }
+
+ return 1;
+}
+
+static php_pqconn_event_data_t *php_pqconn_event_data_init(php_pqconn_object_t *obj TSRMLS_DC)
+{
+ php_pqconn_event_data_t *data = emalloc(sizeof(*data));
+
+ data->obj = obj;
+ TSRMLS_CF(data);
+
+ return data;
+}
+
+static int apply_notice_event(void *p, void *a TSRMLS_DC)
+{
+ zval **evh = p;
+ zval *args = a;
+ zval *retval = NULL;
+
+ zend_call_method_with_1_params(evh, Z_OBJCE_PP(evh), NULL, "trigger", &retval, args);
+ if (retval) {
+ zval_ptr_dtor(&retval);
+ }
+
+ return ZEND_HASH_APPLY_KEEP;
+}
+
+static void php_pqconn_notice_recv(void *p, const PGresult *res)
+{
+ php_pqconn_event_data_t *data = p;
+ zval **evhs;
+ TSRMLS_DF(data);
+
+ if (SUCCESS == zend_hash_find(&data->obj->eventhandlers, ZEND_STRS("notice"), (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);
+ add_next_index_string(args, PQresultErrorMessage(res), 1);
+ zend_hash_apply_with_argument(Z_ARRVAL_PP(evhs), apply_notice_event, args TSRMLS_CC);
+ zval_ptr_dtor(&args);
+ }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct, 0, 0, 1)
+ ZEND_ARG_INFO(0, dsn)
+ ZEND_ARG_INFO(0, async)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, __construct) {
+ zend_error_handling zeh;
+ char *dsn_str;
+ int dsn_len;
+ zend_bool async = 0;
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &dsn_str, &dsn_len, &async)) {
+ 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);
+
+ if (obj->conn) {
+ PQfinish(obj->conn);
+ }
+ if (async) {
+ obj->conn = PQconnectStart(dsn_str);
+ obj->poller = (int (*)(PGconn*)) PQconnectPoll;
+ } else {
+ obj->conn = PQconnectdb(dsn_str);
+ }
+
+ PQsetNoticeReceiver(obj->conn, php_pqconn_notice_recv, data);
+ PQregisterEventProc(obj->conn, php_pqconn_event, "ext-pq", data);
+
+ if (SUCCESS != php_pqconn_update_socket(getThis(), obj TSRMLS_CC)) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connection failed: %s", PQerrorMessage(obj->conn));
+ }
+ }
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, reset) {
+ if (SUCCESS == zend_parse_parameters_none()) {
+ php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->conn) {
+ PQreset(obj->conn);
+
+ if (CONNECTION_OK == PQstatus(obj->conn)) {
+ RETURN_TRUE;
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connection reset failed: %s", PQerrorMessage(obj->conn));
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
+ }
+ RETURN_FALSE;
+ }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset_async, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, resetAsync) {
+ if (SUCCESS == zend_parse_parameters_none()) {
+ php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->conn) {
+ if (PQresetStart(obj->conn)) {
+ obj->poller = (int (*)(PGconn*)) PQresetPoll;
+ RETURN_TRUE;
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
+ }
+ RETURN_FALSE;
+ }
+}
+
+static void php_pqconn_add_listener(php_pqconn_object_t *obj, const char *channel_str, size_t channel_len, php_pq_callback_t *listener TSRMLS_DC)
+{
+ HashTable ht, *existing_listeners;
+
+ php_pq_callback_addref(listener);
+
+ if (SUCCESS == zend_hash_find(&obj->listeners, channel_str, channel_len + 1, (void *) &existing_listeners)) {
+ zend_hash_next_index_insert(existing_listeners, (void *) listener, sizeof(*listener), NULL);
+ } else {
+ zend_hash_init(&ht, 1, NULL, (dtor_func_t) php_pq_callback_dtor, 0);
+ zend_hash_next_index_insert(&ht, (void *) listener, sizeof(*listener), NULL);
+ zend_hash_add(&obj->listeners, channel_str, channel_len + 1, (void *) &ht, sizeof(HashTable), NULL);
+ }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen, 0, 0, 0)
+ ZEND_ARG_INFO(0, channel)
+ ZEND_ARG_INFO(0, callable)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, listen) {
+ 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->poller = PQconsumeInput;
+
+ if (obj->conn) {
+ char *quoted_channel = PQescapeIdentifier(obj->conn, channel_str, channel_len);
+
+ if (quoted_channel) {
+ PGresult *res;
+ char *cmd;
+
+ spprintf(&cmd, 0, "LISTEN %s", channel_str);
+ res = PQexec(obj->conn, cmd);
+
+ efree(cmd);
+ PQfreemem(quoted_channel);
+
+ if (res) {
+ if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
+ php_pqconn_add_listener(obj, channel_str, channel_len, &listener TSRMLS_CC);
+ RETVAL_TRUE;
+ } else {
+ RETVAL_FALSE;
+ }
+ PQclear(res);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not install listener: %s", PQerrorMessage(obj->conn));
+ RETVAL_FALSE;
+ }
+
+ php_pqconn_notify_listeners(getThis(), obj TSRMLS_CC);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not escape channel identifier: %s", PQerrorMessage(obj->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)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, notify) {
+ 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->conn) {
+ PGresult *res;
+ char *params[2] = {channel_str, message_str};
+
+ res = PQexecParams(obj->conn, "select pg_notify($1, $2)", 2, NULL, (const char *const*) params, NULL, NULL, 0);
+
+ if (res) {
+ if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
+ RETVAL_TRUE;
+ } else {
+ RETVAL_FALSE;
+ }
+ PQclear(res);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not notify listeners: %s", PQerrorMessage(obj->conn));
+ RETVAL_FALSE;
+ }
+
+ php_pqconn_notify_listeners(getThis(), 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) {
+ if (SUCCESS == zend_parse_parameters_none()) {
+ php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->conn) {
+ if (obj->poller) {
+ if (obj->poller == PQconsumeInput) {
+ RETVAL_LONG(obj->poller(obj->conn) * PGRES_POLLING_OK);
+ php_pqconn_notify_listeners(getThis(), obj TSRMLS_CC);
+ return;
+ } else {
+ RETURN_LONG(obj->poller(obj->conn));
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "No asynchronous operation active");
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
+ }
+ RETURN_FALSE;
+ }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec, 0, 0, 1)
+ ZEND_ARG_INFO(0, query)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, exec) {
+ zend_error_handling zeh;
+ char *query_str;
+ int query_len;
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &query_str, &query_len)) {
+ php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->conn) {
+ PGresult *res = PQexec(obj->conn, query_str);
+
+ php_pqconn_notify_listeners(getThis(), obj TSRMLS_CC);
+
+ if (res) {
+ if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
+ return_value->type = IS_OBJECT;
+ return_value->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, res, NULL TSRMLS_CC);
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute query: %s", PQerrorMessage(obj->conn));
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
+ }
+ }
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_get_result, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, getResult) {
+ if (SUCCESS == zend_parse_parameters_none()) {
+ php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->conn) {
+ PGresult *res = PQgetResult(obj->conn);
+
+ if (res) {
+ return_value->type = IS_OBJECT;
+ return_value->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, res, NULL TSRMLS_CC);
+ } else {
+ RETVAL_NULL();
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
+ RETVAL_FALSE;
+ }
+ }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_async, 0, 0, 1)
+ ZEND_ARG_INFO(0, query)
+ ZEND_ARG_INFO(0, callable)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, execAsync) {
+ zend_error_handling zeh;
+ php_pq_callback_t resolver = {{0}};
+ char *query_str;
+ int query_len;
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|f", &query_str, &query_len, &resolver.fci, &resolver.fcc)) {
+ php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->conn) {
+ php_pq_callback_dtor(&obj->onevent);
+ if (resolver.fci.size > 0) {
+ obj->onevent = resolver;
+ php_pq_callback_addref(&obj->onevent);
+ }
+
+ obj->poller = PQconsumeInput;
+
+ if (PQsendQuery(obj->conn, query_str)) {
+ if (zend_is_true(zend_read_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("unbuffered"), 0 TSRMLS_CC))) {
+ if (!PQsetSingleRowMode(obj->conn)) {
+ php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not enable unbuffered mode: %s", PQerrorMessage(obj->conn));
+ }
+ }
+ RETVAL_TRUE;
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute query: %s", PQerrorMessage(obj->conn));
+ RETVAL_FALSE;
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
+ RETVAL_FALSE;
+ }
+ }
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+static int apply_to_oid(void *p, void *arg TSRMLS_DC)
+{
+ Oid **types = arg;
+ zval **ztype = p;
+
+ if (Z_TYPE_PP(ztype) != IS_LONG) {
+ convert_to_long_ex(ztype);
+ }
+
+ **types = Z_LVAL_PP(ztype);
+ ++*types;
+
+ if (*ztype != *(zval **)p) {
+ zval_ptr_dtor(ztype);
+ }
+ return ZEND_HASH_APPLY_KEEP;
+}
+
+static int apply_to_param(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
+{
+ char ***params;
+ HashTable *zdtor;
+ zval **zparam = p;
+
+ params = (char ***) va_arg(argv, char ***);
+ zdtor = (HashTable *) va_arg(argv, HashTable *);
+
+ if (Z_TYPE_PP(zparam) == IS_NULL) {
+ **params = NULL;
+ ++*params;
+ } else {
+ if (Z_TYPE_PP(zparam) != IS_STRING) {
+ convert_to_string_ex(zparam);
+ }
+
+ **params = Z_STRVAL_PP(zparam);
+ ++*params;
+
+ if (*zparam != *(zval **)p) {
+ zend_hash_next_index_insert(zdtor, zparam, sizeof(zval *), NULL);
+ }
+ }
+ return ZEND_HASH_APPLY_KEEP;
+}
+
+static int php_pq_types_to_array(HashTable *ht, Oid **types TSRMLS_DC)
+{
+ int count = zend_hash_num_elements(ht);
+
+ *types = NULL;
+
+ if (count) {
+ Oid *tmp;
+
+ /* +1 for when less types than params are specified */
+ *types = tmp = ecalloc(count + 1, sizeof(Oid));
+ zend_hash_apply_with_argument(ht, apply_to_oid, &tmp TSRMLS_CC);
+ }
+
+ return count;
+}
+
+static int php_pq_params_to_array(HashTable *ht, char ***params, HashTable *zdtor TSRMLS_DC)
+{
+ int count = zend_hash_num_elements(ht);
+
+ *params = NULL;
+
+ if (count) {
+ char **tmp;
+
+ *params = tmp = ecalloc(count, sizeof(char *));
+ zend_hash_apply_with_arguments(ht TSRMLS_CC, apply_to_param, 2, &tmp, zdtor);
+ }
+
+ return count;
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params, 0, 0, 2)
+ ZEND_ARG_INFO(0, query)
+ ZEND_ARG_ARRAY_INFO(0, params, 0)
+ ZEND_ARG_ARRAY_INFO(0, types, 1)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, execParams) {
+ zend_error_handling zeh;
+ char *query_str;
+ int query_len;
+ zval *zparams;
+ zval *ztypes = NULL;
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa/|a/!", &query_str, &query_len, &zparams, &ztypes)) {
+ php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->conn) {
+ PGresult *res;
+ int count;
+ Oid *types = NULL;
+ char **params = NULL;
+ HashTable zdtor;
+
+ ZEND_INIT_SYMTABLE(&zdtor);
+ count = php_pq_params_to_array(Z_ARRVAL_P(zparams), ¶ms, &zdtor TSRMLS_CC);
+
+ if (ztypes) {
+ php_pq_types_to_array(Z_ARRVAL_P(ztypes), &types TSRMLS_CC);
+ }
+
+ res = PQexecParams(obj->conn, query_str, count, types, (const char *const*) params, NULL, NULL, 0);
+
+ zend_hash_destroy(&zdtor);
+ if (types) {
+ efree(types);
+ }
+ if (params) {
+ efree(params);
+ }
+
+ php_pqconn_notify_listeners(getThis(), obj TSRMLS_CC);
+
+ if (res) {
+ if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
+ return_value->type = IS_OBJECT;
+ return_value->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, res, NULL TSRMLS_CC);
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute query: %s", PQerrorMessage(obj->conn));
+ RETVAL_FALSE;
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
+ RETVAL_FALSE;
+ }
+ }
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params_async, 0, 0, 2)
+ ZEND_ARG_INFO(0, query)
+ ZEND_ARG_ARRAY_INFO(0, params, 0)
+ ZEND_ARG_ARRAY_INFO(0, types, 1)
+ ZEND_ARG_INFO(0, callable)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, execParamsAsync) {
+ zend_error_handling zeh;
+ php_pq_callback_t resolver = {{0}};
+ char *query_str;
+ int query_len;
+ zval *zparams;
+ zval *ztypes = NULL;
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa/|a/!f", &query_str, &query_len, &zparams, &ztypes, &resolver.fci, &resolver.fcc)) {
+ php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->conn) {
+ int count;
+ Oid *types = NULL;
+ char **params = NULL;
+ HashTable zdtor;
+
+ ZEND_INIT_SYMTABLE(&zdtor);
+ count = php_pq_params_to_array(Z_ARRVAL_P(zparams), ¶ms, &zdtor TSRMLS_CC);
+
+ if (ztypes) {
+ php_pq_types_to_array(Z_ARRVAL_P(ztypes), &types TSRMLS_CC);
+ }
+
+ php_pq_callback_dtor(&obj->onevent);
+ if (resolver.fci.size > 0) {
+ obj->onevent = resolver;
+ php_pq_callback_addref(&obj->onevent);
+ }
+
+ obj->poller = PQconsumeInput;
+
+ if (PQsendQueryParams(obj->conn, query_str, count, types, (const char *const*) params, NULL, NULL, 0)) {
+ if (zend_is_true(zend_read_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("unbuffered"), 0 TSRMLS_CC))) {
+ if (!PQsetSingleRowMode(obj->conn)) {
+ php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not enable unbuffered mode: %s", PQerrorMessage(obj->conn));
+ }
+ }
+ RETVAL_TRUE;
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute query: %s", PQerrorMessage(obj->conn));
+ RETVAL_FALSE;
+ }
+
+ zend_hash_destroy(&zdtor);
+ if (types) {
+ efree(types);
+ }
+ if (params) {
+ efree(params);
+ }
+
+ php_pqconn_notify_listeners(getThis(), obj TSRMLS_CC);
+