#include "php_pqres.h"
#include "php_pqstm.h"
#include "php_pqtxn.h"
+#include "php_pqcur.h"
zend_class_entry *php_pqconn_class_entry;
static zend_object_handlers php_pqconn_object_handlers;
fprintf(stderr, "FREE conn(#%d) %p\n", obj->zv.handle, obj);
#endif
if (obj->intern) {
+ php_pq_callback_dtor(&obj->intern->onevent);
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->converters);
zend_hash_destroy(&obj->intern->eventhandlers);
{
php_pqconn_object_t *obj = o;
- obj->intern->unbuffered = zend_is_true(value);
+ obj->intern->unbuffered = z_is_true(value);
}
static void php_pqconn_object_read_db(zval *object, void *o, zval *return_value TSRMLS_DC)
// FIXME: ping server
}
-static int apply_unlisten(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
+static inline PGresult *unlisten(PGconn *conn, const char *channel_str, size_t channel_len TSRMLS_DC)
{
- php_pqconn_object_t *obj = va_arg(argv, php_pqconn_object_t *);
- char *quoted_channel = PQescapeIdentifier(obj->intern->conn, key->arKey, key->nKeyLength - 1);
+ char *quoted_channel = PQescapeIdentifier(conn, channel_str, channel_len);
+ PGresult *res = NULL;
if (quoted_channel) {
- PGresult *res;
- char *cmd;
+ smart_str cmd = {0};
- spprintf(&cmd, 0, "UNLISTEN %s", quoted_channel);
- if ((res = PQexec(obj->intern->conn, cmd))) {
- PHP_PQclear(res);
- }
+ smart_str_appends(&cmd, "UNLISTEN ");
+ smart_str_appends(&cmd, quoted_channel);
+ smart_str_0(&cmd);
+
+ res = PQexec(conn, cmd.c);
- efree(cmd);
+ smart_str_free(&cmd);
PQfreemem(quoted_channel);
}
+ return res;
+}
+
+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 *);
+ PGresult *res = unlisten(obj->intern->conn, key->arKey, key->nKeyLength - 1);
+
+ if (res) {
+ PHP_PQclear(res);
+ }
+
return ZEND_HASH_APPLY_REMOVE;
}
zend_hash_apply_with_arguments(&evdata->obj->intern->listeners TSRMLS_CC, apply_unlisten, 1, evdata->obj);
/* release instance data */
- memset(evdata, 0, sizeof(*evdata));
+ //memset(evdata, 0, sizeof(*evdata));
efree(evdata);
}
}
}
}
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten, 0, 0, 1)
+ ZEND_ARG_INFO(0, channel)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, unlisten)
+{
+ zend_error_handling zeh;
+ char *channel_str;
+ int channel_len;
+ STATUS rv;
+
+ zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
+ rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &channel_str, &channel_len);
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+
+ if (SUCCESS == rv) {
+ php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (!obj->intern) {
+ throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
+ } else if (SUCCESS == zend_hash_del(&obj->intern->listeners, channel_str, channel_len + 1)) {
+ PGresult *res = unlisten(obj->intern->conn, channel_str, channel_len);
+
+ if (res) {
+ php_pqres_success(res TSRMLS_CC);
+ PHP_PQclear(res);
+ }
+ }
+ }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten_async, 0, 0, 1)
+ ZEND_ARG_INFO(0, channel)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, unlistenAsync) {
+ zend_error_handling zeh;
+ char *channel_str;
+ int channel_len;
+ STATUS rv;
+
+ zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
+ rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &channel_str, &channel_len);
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+
+ if (SUCCESS == rv) {
+ php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (!obj->intern) {
+ throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
+ } else {
+ char *quoted_channel = PQescapeIdentifier(obj->intern->conn, channel_str, channel_len);
+
+ if (!quoted_channel) {
+ throw_exce(EX_ESCAPE TSRMLS_CC, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj->intern->conn));
+ } else {
+ smart_str cmd = {0};
+
+ smart_str_appends(&cmd, "UNLISTEN ");
+ smart_str_appends(&cmd, quoted_channel);
+ smart_str_0(&cmd);
+
+ if (!PQsendQuery(obj->intern->conn, cmd.c)) {
+ throw_exce(EX_IO TSRMLS_CC, "Failed to uninstall listener (%s)", PHP_PQerrorMessage(obj->intern->conn));
+ } else {
+ obj->intern->poller = PQconsumeInput;
+ zend_hash_del(&obj->intern->listeners, channel_str, channel_len + 1);
+ }
+
+ smart_str_free(&cmd);
+ PQfreemem(quoted_channel);
+ php_pqconn_notify_listeners(obj TSRMLS_CC);
+ }
+ }
+ }
+}
+
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;
}
}
-ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen, 0, 0, 0)
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen, 0, 0, 2)
ZEND_ARG_INFO(0, channel)
ZEND_ARG_INFO(0, callable)
ZEND_END_ARG_INFO();
zend_error_handling zeh;
char *channel_str = NULL;
int channel_len = 0;
- php_pq_callback_t listener;
+ php_pq_callback_t listener = {{0}};
STATUS rv;
zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
zend_error_handling zeh;
char *channel_str = NULL;
int channel_len = 0;
- php_pq_callback_t listener;
+ php_pq_callback_t listener = {{0}};
STATUS rv;
zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
} else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
} else {
+ php_pq_callback_recurse(&obj->intern->onevent, &resolver TSRMLS_CC);
obj->intern->poller = PQconsumeInput;
- php_pq_callback_dtor(&obj->intern->onevent);
- if (resolver.fci.size > 0) {
- obj->intern->onevent = resolver;
- php_pq_callback_addref(&obj->intern->onevent);
- }
php_pqconn_notify_listeners(obj TSRMLS_CC);
}
}
} else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
} else {
+ php_pq_callback_recurse(&obj->intern->onevent, &resolver TSRMLS_CC);
obj->intern->poller = PQconsumeInput;
- php_pq_callback_dtor(&obj->intern->onevent);
- if (resolver.fci.size > 0) {
- obj->intern->onevent = resolver;
- php_pq_callback_addref(&obj->intern->onevent);
- }
php_pqconn_notify_listeners(obj TSRMLS_CC);
}
}
}
ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare, 0, 0, 2)
- ZEND_ARG_INFO(0, type)
+ ZEND_ARG_INFO(0, name)
ZEND_ARG_INFO(0, query)
ZEND_ARG_ARRAY_INFO(0, types, 1)
ZEND_END_ARG_INFO();
}
ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare_async, 0, 0, 2)
-ZEND_ARG_INFO(0, type)
+ ZEND_ARG_INFO(0, name)
ZEND_ARG_INFO(0, query)
ZEND_ARG_ARRAY_INFO(0, types, 1)
ZEND_END_ARG_INFO();
}
}
+static inline char *declare_str(const char *name_str, size_t name_len, unsigned flags, const char *query_str, size_t query_len)
+{
+ size_t decl_len = name_len + query_len + sizeof("DECLARE BINARY INSENSITIVE NO SCROLL CURSOR WITHOUT HOLD FOR ");
+ char *decl_str;
+
+ decl_str = emalloc(decl_len);
+ decl_len = slprintf(decl_str, decl_len, "DECLARE %s %s %s %s CURSOR %s FOR %s",
+ name_str,
+ (flags & PHP_PQ_DECLARE_BINARY) ? "BINARY" : "",
+ (flags & PHP_PQ_DECLARE_INSENSITIVE) ? "INSENSITIVE" : "",
+ (flags & PHP_PQ_DECLARE_NO_SCROLL) ? "NO SCROLL" :
+ (flags & PHP_PQ_DECLARE_SCROLL) ? "SCROLL" : "",
+ (flags & PHP_PQ_DECLARE_WITH_HOLD) ? "WITH HOLD" : "",
+ query_str
+ );
+ return decl_str;
+}
+
+STATUS php_pqconn_declare(zval *object, php_pqconn_object_t *obj, const char *decl TSRMLS_DC)
+{
+ PGresult *res;
+ STATUS rv;
+
+ if (!obj) {
+ obj = zend_object_store_get_object(object TSRMLS_CC);
+ }
+
+ res = PQexec(obj->intern->conn, decl);
+
+ if (!res) {
+ rv = FAILURE;
+ throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj->intern->conn));
+ } else {
+ rv = php_pqres_success(res TSRMLS_CC);
+ PHP_PQclear(res);
+ php_pqconn_notify_listeners(obj TSRMLS_CC);
+ }
+
+ return rv;
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare, 0, 0, 3)
+ ZEND_ARG_INFO(0, name)
+ ZEND_ARG_INFO(0, flags)
+ ZEND_ARG_INFO(0, query)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, declare) {
+ zend_error_handling zeh;
+ char *name_str, *query_str;
+ int name_len, query_len;
+ long flags;
+ STATUS rv;
+
+ zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
+ rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sls", &name_str, &name_len, &flags, &query_str, &query_len);
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+
+ if (SUCCESS == rv) {
+ php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (!obj->intern) {
+ throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
+ } else {
+ char *decl = declare_str(name_str, name_len, flags, query_str, query_len);
+
+ if (SUCCESS != php_pqconn_declare(getThis(), obj, decl TSRMLS_CC)) {
+ efree(decl);
+ } else {
+ php_pqcur_t *cur = ecalloc(1, sizeof(*cur));
+
+ php_pq_object_addref(obj TSRMLS_CC);
+ cur->conn = obj;
+ cur->open = 1;
+ cur->name = estrdup(name_str);
+ cur->decl = decl;
+
+ return_value->type = IS_OBJECT;
+ return_value->value.obj = php_pqcur_create_object_ex(php_pqcur_class_entry, cur, NULL TSRMLS_CC);
+ }
+ }
+ }
+}
+
+STATUS php_pqconn_declare_async(zval *object, php_pqconn_object_t *obj, const char *decl TSRMLS_DC)
+{
+ STATUS rv;
+
+ if (!obj) {
+ obj = zend_object_store_get_object(object TSRMLS_CC);
+ }
+
+ if (!PQsendQuery(obj->intern->conn, decl)) {
+ rv = FAILURE;
+ throw_exce(EX_IO TSRMLS_CC, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj->intern->conn));
+ } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
+ rv = FAILURE;
+ throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
+ } else {
+ rv = SUCCESS;
+ obj->intern->poller = PQconsumeInput;
+ php_pqconn_notify_listeners(obj TSRMLS_CC);
+ }
+
+ return rv;
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare_async, 0, 0, 2)
+ ZEND_ARG_INFO(0, name)
+ ZEND_ARG_INFO(0, flags)
+ ZEND_ARG_INFO(0, query)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, declareAsync) {
+ zend_error_handling zeh;
+ char *name_str, *query_str;
+ int name_len, query_len;
+ long flags;
+ STATUS rv;
+
+ zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
+ rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sls", &name_str, &name_len, &flags, &query_str, &query_len);
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+
+ if (SUCCESS == rv) {
+ php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (!obj->intern) {
+ throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
+ } else {
+ char *decl = declare_str(name_str, name_len, flags, query_str, query_len);
+
+ if (SUCCESS != php_pqconn_declare_async(getThis(), obj, decl TSRMLS_CC)) {
+ efree(decl);
+ } else {
+ php_pqcur_t *cur = ecalloc(1, sizeof(*cur));
+
+ php_pq_object_addref(obj TSRMLS_CC);
+ cur->conn = obj;
+ cur->open = 1;
+ cur->name = estrdup(name_str);
+ cur->decl = decl;
+
+ return_value->type = IS_OBJECT;
+ return_value->value.obj = php_pqcur_create_object_ex(php_pqcur_class_entry, cur, NULL TSRMLS_CC);
+ }
+ }
+ }
+}
+
ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote, 0, 0, 1)
ZEND_ARG_INFO(0, string)
ZEND_END_ARG_INFO();
}
}
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_off, 0, 0, 1)
+ ZEND_ARG_INFO(0, type)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, off) {
+ zend_error_handling zeh;
+ char *type_str;
+ int type_len;
+ STATUS rv;
+
+ zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
+ rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &type_str, &type_len);
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+
+ if (SUCCESS == rv) {
+ php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (!obj->intern) {
+ throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
+ } else {
+ RETURN_BOOL(SUCCESS == zend_hash_del(&obj->intern->eventhandlers, type_str, type_len + 1));
+ }
+ }
+}
+
ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_on, 0, 0, 2)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, callable)
zend_error_handling zeh;
char *type_str;
int type_len;
- php_pq_callback_t cb;
+ php_pq_callback_t cb = {{0}};
STATUS rv;
zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
PHP_ME(pqconn, execParamsAsync, ai_pqconn_exec_params_async, 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, declare, ai_pqconn_declare, ZEND_ACC_PUBLIC)
+ PHP_ME(pqconn, declareAsync, ai_pqconn_declare_async, ZEND_ACC_PUBLIC)
+ PHP_ME(pqconn, unlisten, ai_pqconn_unlisten, ZEND_ACC_PUBLIC)
+ PHP_ME(pqconn, unlistenAsync, ai_pqconn_unlisten_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, startTransaction, ai_pqconn_start_transaction, ZEND_ACC_PUBLIC)
PHP_ME(pqconn, startTransactionAsync, ai_pqconn_start_transaction_async, ZEND_ACC_PUBLIC)
PHP_ME(pqconn, trace, ai_pqconn_trace, ZEND_ACC_PUBLIC)
+ PHP_ME(pqconn, off, ai_pqconn_off, ZEND_ACC_PUBLIC)
PHP_ME(pqconn, on, ai_pqconn_on, ZEND_ACC_PUBLIC)
PHP_ME(pqconn, setConverter, ai_pqconn_set_converter, ZEND_ACC_PUBLIC)
{0}