X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-pq;a=blobdiff_plain;f=src%2Fphp_pq.c;h=01886ce983d67ad0a6c7a5b4204555b4d189568e;hp=180ad0b577e25367d5b9f0245e879872d0e28dc7;hb=1d3a582aeca2b93c221f4fbcf9ae42e5201f1639;hpb=0ab628820a5da3a7226e9631b0f2effa2ff15fe6 diff --git a/src/php_pq.c b/src/php_pq.c index 180ad0b..01886ce 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 | +--------------------------------------------------------------------+ */ @@ -14,23 +14,49 @@ # include "config.h" #endif +#define SMART_STR_PREALLOC 256 + #include #include +#include #include +#include #include +#include +#include #include +#include #include #include "php_pq.h" typedef int STATUS; /* SUCCESS/FAILURE */ +static char *rtrim(char *e) { + size_t l = strlen(e); + + while (l-- > 0 && e[l] == '\n') { + e[l] = '\0'; + } + return e; +} + +#define PHP_PQerrorMessage(c) rtrim(PQerrorMessage((c))) +#define PHP_PQresultErrorMessage(r) rtrim(PQresultErrorMessage((r))) + +static int php_pqconn_event(PGEventId id, void *e, void *data); + +#define PHP_PQclear(_r) \ + do { \ + zval *_resinszv = PQresultInstanceData((_r), php_pqconn_event); \ + if (!_resinszv) PQclear((_r)); \ + } while (0) + /* ZEND_DECLARE_MODULE_GLOBALS(pq) */ - /* {{{ PHP_INI */ /* Remove comments and fill if you need to have entries in php.ini @@ -53,18 +79,54 @@ static void php_pq_init_globals(zend_pq_globals *pq_globals) /* }}} */ static zend_class_entry *php_pqconn_class_entry; +static zend_class_entry *php_pqtypes_class_entry; static zend_class_entry *php_pqres_class_entry; static zend_class_entry *php_pqstm_class_entry; static zend_class_entry *php_pqtxn_class_entry; static zend_class_entry *php_pqcancel_class_entry; static zend_class_entry *php_pqevent_class_entry; +static zend_class_entry *php_pqlob_class_entry; +static zend_class_entry *php_pqcopy_class_entry; + +typedef enum php_pqexc_type { + EX_DEFAULT, + EX_INVALID_ARGUMENT, + EX_RUNTIME, + EX_CONNECTION_FAILED, + EX_BAD_METHODCALL, +} php_pqexc_type_t; + +static zend_class_entry *php_pqexc_interface_class_entry; +static zend_class_entry *php_pqexc_default_class_entry; +static zend_class_entry *php_pqexc_invalid_argument_class_entry; +static zend_class_entry *php_pqexc_runtime_class_entry; +static zend_class_entry *php_pqexc_bad_methodcall_class_entry; + +static zend_class_entry *exce(php_pqexc_type_t type) +{ + switch (type) { + case EX_DEFAULT: + default: + return php_pqexc_default_class_entry; + case EX_INVALID_ARGUMENT: + return php_pqexc_invalid_argument_class_entry; + case EX_RUNTIME: + case EX_CONNECTION_FAILED: + return php_pqexc_runtime_class_entry; + case EX_BAD_METHODCALL: + return php_pqexc_bad_methodcall_class_entry; + } +} static zend_object_handlers php_pqconn_object_handlers; +static zend_object_handlers php_pqtypes_object_handlers; static zend_object_handlers php_pqres_object_handlers; static zend_object_handlers php_pqstm_object_handlers; static zend_object_handlers php_pqtxn_object_handlers; static zend_object_handlers php_pqcancel_object_handlers; static zend_object_handlers php_pqevent_object_handlers; +static zend_object_handlers php_pqlob_object_handlers; +static zend_object_handlers php_pqcopy_object_handlers; typedef struct php_pq_callback { zend_fcall_info fci; @@ -79,9 +141,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; @@ -95,6 +161,18 @@ typedef struct php_pqconn_object { php_pqconn_t *intern; } php_pqconn_object_t; +typedef struct php_pqtypes { + HashTable types; + php_pqconn_object_t *conn; +} php_pqtypes_t; + +typedef struct php_pqtypes_object { + zend_object zo; + zend_object_value zv; + HashTable *prophandler; + php_pqtypes_t *intern; +} php_pqtypes_object_t; + typedef struct php_pqconn_event_data { php_pqconn_object_t *obj; #ifdef ZTS @@ -118,6 +196,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 { @@ -130,6 +209,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 { @@ -148,6 +228,8 @@ typedef enum php_pqtxn_isolation { typedef struct php_pqtxn { php_pqconn_object_t *conn; php_pqtxn_isolation_t isolation; + unsigned savepoint; + unsigned open:1; unsigned readonly:1; unsigned deferrable:1; } php_pqtxn_t; @@ -184,12 +266,53 @@ typedef struct php_pqevent_object { php_pqevent_t *intern; } php_pqevent_object_t; +typedef struct php_pqlob { + int lofd; + Oid loid; + php_pqtxn_object_t *txn; +} php_pqlob_t; + +typedef struct php_pqlob_object { + zend_object zo; + zend_object_value zv; + HashTable *prophandler; + php_pqlob_t *intern; +} php_pqlob_object_t; + +typedef enum php_pqcopy_direction { + PHP_PQCOPY_FROM_STDIN, + PHP_PQCOPY_TO_STDOUT +} php_pqcopy_direction_t; + +typedef enum php_pqcopy_status { + PHP_PQCOPY_FAIL, + PHP_PQCOPY_CONT, + PHP_PQCOPY_DONE +} php_pqcopy_status_t; + +typedef struct php_pqcopy { + php_pqcopy_direction_t direction; + char *expression; + char *options; + php_pqconn_object_t *conn; +} php_pqcopy_t; + +typedef struct php_pqcopy_object { + zend_object zo; + zend_object_value zv; + HashTable *prophandler; + php_pqcopy_t *intern; +} php_pqcopy_object_t; + static HashTable php_pqconn_object_prophandlers; +static HashTable php_pqtypes_object_prophandlers; static HashTable php_pqres_object_prophandlers; static HashTable php_pqstm_object_prophandlers; static HashTable php_pqtxn_object_prophandlers; static HashTable php_pqcancel_object_prophandlers; static HashTable php_pqevent_object_prophandlers; +static HashTable php_pqlob_object_prophandlers; +static HashTable php_pqcopy_object_prophandlers; typedef void (*php_pq_object_prophandler_func_t)(zval *object, void *o, zval *return_value TSRMLS_DC); @@ -255,16 +378,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) { @@ -313,7 +444,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; } @@ -356,13 +487,25 @@ 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) { + return FAILURE; + } else { + *count = (long) PQntuples(obj->intern->res); + return SUCCESS; + } +} + static STATUS php_pqres_success(PGresult *res TSRMLS_DC) { switch (PQresultStatus(res)) { case PGRES_BAD_RESPONSE: case PGRES_NONFATAL_ERROR: case PGRES_FATAL_ERROR: - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", PQresultErrorMessage(res)); + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "%s", PHP_PQresultErrorMessage(res)); return FAILURE; default: return SUCCESS; @@ -419,7 +562,8 @@ static void php_pqconn_object_free(void *o TSRMLS_DC) php_pqconn_object_t *obj = o; if (obj->intern) { - PQfinish(obj->intern->conn); + 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); @@ -430,7 +574,19 @@ static void php_pqconn_object_free(void *o TSRMLS_DC) efree(obj); } -static int php_pqconn_event(PGEventId id, void *e, void *data); +static void php_pqtypes_object_free(void *o TSRMLS_DC) +{ + php_pqtypes_object_t *obj = o; + + if (obj->intern) { + zend_hash_destroy(&obj->intern->types); + php_pq_object_delref(obj->intern->conn TSRMLS_CC); + efree(obj->intern); + obj->intern = NULL; + } + zend_object_std_dtor((zend_object *) o TSRMLS_CC); + efree(obj); +} static void php_pqres_object_free(void *o TSRMLS_DC) { @@ -440,7 +596,9 @@ static void php_pqres_object_free(void *o TSRMLS_DC) if (obj->intern->res) { zval *res = PQresultInstanceData(obj->intern->res, php_pqconn_event); if (res) { - PQresultSetInstanceData(obj->intern->res, php_pqconn_event, NULL); + if (1 == Z_REFCOUNT_P(res)) { + PQresultSetInstanceData(obj->intern->res, php_pqconn_event, NULL); + } zval_ptr_dtor(&res); } else { PQclear(obj->intern->res); @@ -453,6 +611,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; } @@ -465,8 +625,28 @@ static void php_pqstm_object_free(void *o TSRMLS_DC) php_pqstm_object_t *obj = o; if (obj->intern) { + char *quoted_name = PQescapeIdentifier(obj->intern->conn->intern->conn, obj->intern->name, strlen(obj->intern->name)); + + php_pq_callback_dtor(&obj->intern->conn->intern->onevent); + + if (quoted_name) { + PGresult *res; + smart_str cmd = {0}; + + smart_str_appends(&cmd, "DEALLOCATE "); + smart_str_appends(&cmd, quoted_name); + smart_str_0(&cmd); + PQfreemem(quoted_name); + + if ((res = PQexec(obj->intern->conn->intern->conn, cmd.c))) { + PHP_PQclear(res); + } + smart_str_free(&cmd); + } + 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; } @@ -479,6 +659,13 @@ static void php_pqtxn_object_free(void *o TSRMLS_DC) php_pqtxn_object_t *obj = o; if (obj->intern) { + if (obj->intern->open) { + PGresult *res = PQexec(obj->intern->conn->intern->conn, "ROLLBACK"); + + if (res) { + PHP_PQclear(res); + } + } php_pq_object_delref(obj->intern->conn TSRMLS_CC); efree(obj->intern); obj->intern = NULL; @@ -516,6 +703,36 @@ static void php_pqevent_object_free(void *o TSRMLS_DC) efree(obj); } +static void php_pqlob_object_free(void *o TSRMLS_DC) +{ + php_pqlob_object_t *obj = o; + + if (obj->intern) { + if (obj->intern->lofd) { + lo_close(obj->intern->txn->intern->conn->intern->conn, obj->intern->lofd); + } + php_pq_object_delref(obj->intern->txn TSRMLS_CC); + efree(obj->intern); + obj->intern = NULL; + } + zend_object_std_dtor((zend_object *) o TSRMLS_CC); + efree(obj); +} + +static void php_pqcopy_object_free(void *o TSRMLS_DC) +{ + php_pqcopy_object_t *obj = o; + + if (obj->intern) { + efree(obj->intern->expression); + efree(obj->intern->options); + php_pq_object_delref(obj->intern->conn TSRMLS_CC); + efree(obj->intern); + obj->intern = NULL; + } + zend_object_std_dtor((zend_object *) o TSRMLS_CC); + efree(obj); +} static zend_object_value php_pqconn_create_object_ex(zend_class_entry *ce, php_pqconn_t *intern, php_pqconn_object_t **ptr TSRMLS_DC) { @@ -540,6 +757,29 @@ static zend_object_value php_pqconn_create_object_ex(zend_class_entry *ce, php_p return o->zv; } +static zend_object_value php_pqtypes_create_object_ex(zend_class_entry *ce, php_pqtypes_t *intern, php_pqtypes_object_t **ptr TSRMLS_DC) +{ + php_pqtypes_object_t *o; + + o = ecalloc(1, sizeof(*o)); + zend_object_std_init((zend_object *) o, ce TSRMLS_CC); + object_properties_init((zend_object *) o, ce); + o->prophandler = &php_pqtypes_object_prophandlers; + + if (ptr) { + *ptr = o; + } + + if (intern) { + o->intern = intern; + } + + o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqtypes_object_free, NULL TSRMLS_CC); + o->zv.handlers = &php_pqtypes_object_handlers; + + return o->zv; +} + static zend_object_value php_pqres_create_object_ex(zend_class_entry *ce, php_pqres_t *intern, php_pqres_object_t **ptr TSRMLS_DC) { php_pqres_object_t *o; @@ -655,11 +895,62 @@ static zend_object_value php_pqevent_create_object_ex(zend_class_entry *ce, php_ return o->zv; } +static zend_object_value php_pqlob_create_object_ex(zend_class_entry *ce, php_pqlob_t *intern, php_pqlob_object_t **ptr TSRMLS_DC) +{ + php_pqlob_object_t *o; + + o = ecalloc(1, sizeof(*o)); + zend_object_std_init((zend_object *) o, ce TSRMLS_CC); + object_properties_init((zend_object *) o, ce); + o->prophandler = &php_pqlob_object_prophandlers; + + if (ptr) { + *ptr = o; + } + + if (intern) { + o->intern = intern; + } + + o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqlob_object_free, NULL TSRMLS_CC); + o->zv.handlers = &php_pqlob_object_handlers; + + return o->zv; +} + +static zend_object_value php_pqcopy_create_object_ex(zend_class_entry *ce, php_pqcopy_t *intern, php_pqcopy_object_t **ptr TSRMLS_DC) +{ + php_pqcopy_object_t *o; + + o = ecalloc(1, sizeof(*o)); + zend_object_std_init((zend_object *) o, ce TSRMLS_CC); + object_properties_init((zend_object *) o, ce); + o->prophandler = &php_pqcopy_object_prophandlers; + + if (ptr) { + *ptr = o; + } + + if (intern) { + o->intern = intern; + } + + o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqcopy_object_free, NULL TSRMLS_CC); + o->zv.handlers = &php_pqcopy_object_handlers; + + return o->zv; +} + static zend_object_value php_pqconn_create_object(zend_class_entry *class_type TSRMLS_DC) { return php_pqconn_create_object_ex(class_type, NULL, NULL TSRMLS_CC); } +static zend_object_value php_pqtypes_create_object(zend_class_entry *class_type TSRMLS_DC) +{ + return php_pqtypes_create_object_ex(class_type, NULL, NULL TSRMLS_CC); +} + static zend_object_value php_pqres_create_object(zend_class_entry *class_type TSRMLS_DC) { return php_pqres_create_object_ex(class_type, NULL, NULL TSRMLS_CC); @@ -685,6 +976,16 @@ static zend_object_value php_pqevent_create_object(zend_class_entry *class_type return php_pqevent_create_object_ex(class_type, NULL, NULL TSRMLS_CC); } +static zend_object_value php_pqlob_create_object(zend_class_entry *class_type TSRMLS_DC) +{ + return php_pqlob_create_object_ex(class_type, NULL, NULL TSRMLS_CC); +} + +static zend_object_value php_pqcopy_create_object(zend_class_entry *class_type TSRMLS_DC) +{ + return php_pqcopy_create_object_ex(class_type, NULL, NULL TSRMLS_CC); +} + static int apply_ph_to_debug(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key) { php_pq_object_prophandler_t *ph = p; @@ -714,7 +1015,7 @@ static int apply_pi_to_debug(void *p TSRMLS_DC, int argc, va_list argv, zend_has php_pq_object_t *obj = va_arg(argv, php_pq_object_t *); zval *property = zend_read_property(obj->zo.ce, object, pi->name, pi->name_length, 0 TSRMLS_CC); - if (!Z_REFCOUNT_P(property)) { + if (1||!Z_REFCOUNT_P(property)) { Z_ADDREF_P(property); } zend_hash_add(ht, pi->name, pi->name_length + 1, (void *) &property, sizeof(zval *), NULL); @@ -754,7 +1055,7 @@ static void php_pqconn_object_read_transaction_status(zval *object, void *o, zva static void php_pqconn_object_read_error_message(zval *object, void *o, zval *return_value TSRMLS_DC) { php_pqconn_object_t *obj = o; - char *error = PQerrorMessage(obj->intern->conn); + char *error = PHP_PQerrorMessage(obj->intern->conn); if (error) { RETVAL_STRING(error, 1); @@ -808,52 +1109,6 @@ static void php_pqconn_notify_listeners(php_pqconn_object_t *obj TSRMLS_DC) } } -/* FIXME: extend to types->nspname->typname */ -#define PHP_PQ_TYPES_QUERY \ - "select t.oid, t.* " \ - "from pg_type t join pg_namespace n on t.typnamespace=n.oid " \ - "where typisdefined " \ - "and typrelid=0 " \ - "and nspname in ('public', 'pg_catalog')" -static void php_pqconn_object_read_types(zval *object, void *o, zval *return_value TSRMLS_DC) -{ - php_pqconn_object_t *obj = o; - PGresult *res = PQexec(obj->intern->conn, PHP_PQ_TYPES_QUERY); - - php_pqconn_notify_listeners(obj TSRMLS_CC); - - /* FIXME: cache that */ - if (res) { - if (PGRES_TUPLES_OK == PQresultStatus(res)) { - int r, rows; - zval *byoid, *byname; - - MAKE_STD_ZVAL(byoid); - MAKE_STD_ZVAL(byname); - object_init(byoid); - object_init(byname); - object_init(return_value); - for (r = 0, rows = PQntuples(res); r < rows; ++r) { - zval *row = php_pqres_row_to_zval(res, r, PHP_PQRES_FETCH_OBJECT TSRMLS_CC); - - add_property_zval(byoid, PQgetvalue(res, r, 0), row); - add_property_zval(byname, PQgetvalue(res, r, 1), row); - zval_ptr_dtor(&row); - } - - add_property_zval(return_value, "byOid", byoid); - add_property_zval(return_value, "byName", byname); - zval_ptr_dtor(&byoid); - zval_ptr_dtor(&byname); - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch types: %s", PQresultErrorMessage(res)); - } - PQclear(res); - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch types: %s", PQerrorMessage(obj->intern->conn)); - } -} - static void php_pqconn_object_read_busy(zval *object, void *o, zval *return_value TSRMLS_DC) { php_pqconn_object_t *obj = o; @@ -900,94 +1155,272 @@ static void php_pqconn_object_write_unbuffered(zval *object, void *o, zval *valu obj->intern->unbuffered = zend_is_true(value); } -static void php_pqres_object_read_status(zval *object, void *o, zval *return_value TSRMLS_DC) +static void php_pqconn_object_read_db(zval *objec, void *o, zval *return_value TSRMLS_DC) { - php_pqres_object_t *obj = o; + php_pqconn_object_t *obj = o; + char *db = PQdb(obj->intern->conn); - RETVAL_LONG(PQresultStatus(obj->intern->res)); + if (db) { + RETVAL_STRING(db, 1); + } else { + RETVAL_EMPTY_STRING(); + } } -static void php_pqres_object_read_error_message(zval *object, void *o, zval *return_value TSRMLS_DC) +static void php_pqconn_object_read_user(zval *objec, void *o, zval *return_value TSRMLS_DC) { - php_pqres_object_t *obj = o; - char *error = PQresultErrorMessage(obj->intern->res); + php_pqconn_object_t *obj = o; + char *user = PQuser(obj->intern->conn); - if (error) { - RETVAL_STRING(error, 1); + if (user) { + RETVAL_STRING(user, 1); } else { - RETVAL_NULL(); + RETVAL_EMPTY_STRING(); } } -static void php_pqres_object_read_num_rows(zval *object, void *o, zval *return_value TSRMLS_DC) +static void php_pqconn_object_read_pass(zval *objec, void *o, zval *return_value TSRMLS_DC) { - php_pqres_object_t *obj = o; + php_pqconn_object_t *obj = o; + char *pass = PQpass(obj->intern->conn); - RETVAL_LONG(PQntuples(obj->intern->res)); + if (pass) { + RETVAL_STRING(pass, 1); + } else { + RETVAL_EMPTY_STRING(); + } } -static void php_pqres_object_read_num_cols(zval *object, void *o, zval *return_value TSRMLS_DC) +static void php_pqconn_object_read_host(zval *objec, void *o, zval *return_value TSRMLS_DC) { - php_pqres_object_t *obj = o; + php_pqconn_object_t *obj = o; + char *host = PQhost(obj->intern->conn); - RETVAL_LONG(PQnfields(obj->intern->res)); + if (host) { + RETVAL_STRING(host, 1); + } else { + RETVAL_EMPTY_STRING(); + } } -static void php_pqres_object_read_affected_rows(zval *object, void *o, zval *return_value TSRMLS_DC) +static void php_pqconn_object_read_port(zval *objec, void *o, zval *return_value TSRMLS_DC) { - php_pqres_object_t *obj = o; + php_pqconn_object_t *obj = o; + char *port = PQport(obj->intern->conn); - RETVAL_LONG(atoi(PQcmdTuples(obj->intern->res))); + if (port) { + RETVAL_STRING(port, 1); + } else { + RETVAL_EMPTY_STRING(); + } } -static void php_pqres_object_read_fetch_type(zval *object, void *o, zval *return_value TSRMLS_DC) +static void php_pqconn_object_read_options(zval *objec, void *o, zval *return_value TSRMLS_DC) { - php_pqres_object_t *obj = o; + php_pqconn_object_t *obj = o; + char *options = PQoptions(obj->intern->conn); - if (obj->intern->iter) { - RETVAL_LONG(obj->intern->iter->fetch_type); + if (options) { + RETVAL_STRING(options, 1); } else { - RETVAL_LONG(PHP_PQRES_FETCH_ARRAY); + RETVAL_EMPTY_STRING(); } } -static void php_pqres_object_write_fetch_type(zval *object, void *o, zval *value TSRMLS_DC) +static void php_pqtypes_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC) { - php_pqres_object_t *obj = o; - zval *zfetch_type = value; + php_pqtypes_object_t *obj = o; - if (Z_TYPE_P(zfetch_type) != IS_LONG) { - convert_to_long_ex(&zfetch_type); - } + php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC); +} - if (!obj->intern->iter) { - obj->intern->iter = (php_pqres_iterator_t *) php_pqres_iterator_init(Z_OBJCE_P(object), object, 0 TSRMLS_CC); - obj->intern->iter->zi.funcs->rewind((zend_object_iterator *) obj->intern->iter TSRMLS_CC); +static int has_dimension(HashTable *ht, zval *member, char **key_str, int *key_len, long *index TSRMLS_DC) +{ + long lval = 0; + zval *tmp = member; + + switch (Z_TYPE_P(member)) { + default: + convert_to_string_ex(&tmp); + /* no break */ + case IS_STRING: + if (!is_numeric_string(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp), &lval, NULL, 0)) { + if (member != tmp) { + zval_ptr_dtor(&tmp); + } + if (key_str) { + *key_str = estrndup(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp)); + if (key_len) { + *key_len = Z_STRLEN_P(tmp) + 1; + } + } + return zend_hash_exists(ht, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp) + 1); + } + /* no break */ + case IS_LONG: + lval = Z_LVAL_P(member); + break; } - obj->intern->iter->fetch_type = Z_LVAL_P(zfetch_type); - if (zfetch_type != value) { - zval_ptr_dtor(&zfetch_type); + if (member != tmp) { + zval_ptr_dtor(&tmp); + } + if (index) { + *index = lval; } + return zend_hash_index_exists(ht, lval); } -static void php_pqstm_object_read_name(zval *object, void *o, zval *return_value TSRMLS_DC) +static int php_pqtypes_object_has_dimension(zval *object, zval *member, int check_empty TSRMLS_DC) { - php_pqstm_object_t *obj = o; + php_pqtypes_object_t *obj = zend_object_store_get_object(object TSRMLS_CC); + char *key_str = NULL; + int key_len = 0; + long index = 0; + + if (check_empty) { + if (has_dimension(&obj->intern->types, member, &key_str, &key_len, &index TSRMLS_CC)) { + zval **data; + + if (key_str && key_len) { + if (SUCCESS == zend_hash_find(&obj->intern->types, key_str, key_len, (void *) &data)) { + efree(key_str); + return Z_TYPE_PP(data) != IS_NULL; + } + efree(key_str); + } else { + if (SUCCESS == zend_hash_index_find(&obj->intern->types, index, (void *) data)) { + return Z_TYPE_PP(data) != IS_NULL; + } + } + } + } else { + return has_dimension(&obj->intern->types, member, NULL, NULL, NULL TSRMLS_CC); + } - RETVAL_STRING(obj->intern->name, 1); + return 0; } -static void php_pqstm_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC) +static zval *php_pqtypes_object_read_dimension(zval *object, zval *member, int type TSRMLS_DC) { - php_pqstm_object_t *obj = o; + long index = 0; + char *key_str = NULL; + int key_len = 0; + php_pqtypes_object_t *obj = zend_object_store_get_object(object TSRMLS_CC); + + if (has_dimension(&obj->intern->types, member, &key_str, &key_len, &index TSRMLS_CC)) { + zval **data; + + if (key_str && key_len) { + if (SUCCESS == zend_hash_find(&obj->intern->types, key_str, key_len, (void *) &data)) { + efree(key_str); + return *data; + } + } else { + if (SUCCESS == zend_hash_index_find(&obj->intern->types, index, (void *) &data)) { + return *data; + } + } + } - php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC); + return NULL; } -static void php_pqtxn_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC) +static void php_pqres_object_read_status(zval *object, void *o, zval *return_value TSRMLS_DC) { - php_pqtxn_object_t *obj = o; + php_pqres_object_t *obj = o; + + RETVAL_LONG(PQresultStatus(obj->intern->res)); +} + +static void php_pqres_object_read_status_message(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqres_object_t *obj = o; + + RETVAL_STRING(PQresStatus(PQresultStatus(obj->intern->res))+sizeof("PGRES"), 1); +} + +static void php_pqres_object_read_error_message(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqres_object_t *obj = o; + char *error = PHP_PQresultErrorMessage(obj->intern->res); + + if (error) { + RETVAL_STRING(error, 1); + } else { + RETVAL_NULL(); + } +} + +static void php_pqres_object_read_num_rows(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqres_object_t *obj = o; + + RETVAL_LONG(PQntuples(obj->intern->res)); +} + +static void php_pqres_object_read_num_cols(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqres_object_t *obj = o; + + RETVAL_LONG(PQnfields(obj->intern->res)); +} + +static void php_pqres_object_read_affected_rows(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqres_object_t *obj = o; + + RETVAL_LONG(atoi(PQcmdTuples(obj->intern->res))); +} + +static void php_pqres_object_read_fetch_type(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqres_object_t *obj = o; + + if (obj->intern->iter) { + RETVAL_LONG(obj->intern->iter->fetch_type); + } else { + RETVAL_LONG(PHP_PQRES_FETCH_ARRAY); + } +} + +static void php_pqres_object_write_fetch_type(zval *object, void *o, zval *value TSRMLS_DC) +{ + php_pqres_object_t *obj = o; + zval *zfetch_type = value; + + if (Z_TYPE_P(zfetch_type) != IS_LONG) { + convert_to_long_ex(&zfetch_type); + } + + if (!obj->intern->iter) { + obj->intern->iter = (php_pqres_iterator_t *) php_pqres_iterator_init(Z_OBJCE_P(object), object, 0 TSRMLS_CC); + obj->intern->iter->zi.funcs->rewind((zend_object_iterator *) obj->intern->iter TSRMLS_CC); + } + obj->intern->iter->fetch_type = Z_LVAL_P(zfetch_type); + + if (zfetch_type != value) { + zval_ptr_dtor(&zfetch_type); + } +} + +static void php_pqstm_object_read_name(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqstm_object_t *obj = o; + + RETVAL_STRING(obj->intern->name, 1); +} + +static void php_pqstm_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqstm_object_t *obj = o; + + php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC); +} + +static void php_pqtxn_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqtxn_object_t *obj = o; php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC); } @@ -1046,7 +1479,7 @@ static void php_pqtxn_object_write_isolation(zval *object, void *o, zval *value if (res) { php_pqres_success(res TSRMLS_CC); - PQclear(res); + PHP_PQclear(res); } } @@ -1063,7 +1496,7 @@ static void php_pqtxn_object_write_readonly(zval *object, void *o, zval *value T if (res) { php_pqres_success(res TSRMLS_CC); - PQclear(res); + PHP_PQclear(res); } } @@ -1080,7 +1513,7 @@ static void php_pqtxn_object_write_deferrable(zval *object, void *o, zval *value if (res) { php_pqres_success(res TSRMLS_CC); - PQclear(res); + PHP_PQclear(res); } } @@ -1105,6 +1538,48 @@ static void php_pqevent_object_read_type(zval *object, void *o, zval *return_val RETVAL_STRING(obj->intern->type, 1); } +static void php_pqlob_object_read_transaction(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqlob_object_t *obj = o; + + php_pq_object_to_zval(obj->intern->txn, &return_value TSRMLS_CC); +} + +static void php_pqlob_object_read_oid(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqlob_object_t *obj = o; + + RETVAL_LONG(obj->intern->loid); +} + +static void php_pqcopy_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqcopy_object_t *obj = o; + + php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC); +} + +static void php_pqcopy_object_read_direction(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqcopy_object_t *obj = o; + + RETVAL_LONG(obj->intern->direction); +} + +static void php_pqcopy_object_read_expression(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqcopy_object_t *obj = o; + + RETURN_STRING(obj->intern->expression, 1); +} + +static void php_pqcopy_object_read_options(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqcopy_object_t *obj = o; + + RETURN_STRING(obj->intern->options, 1); +} + static zend_class_entry *ancestor(zend_class_entry *ce) { while (ce->parent) { ce = ce->parent; @@ -1170,6 +1645,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 { @@ -1190,48 +1666,101 @@ 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) +static int apply_event(void *p, void *a TSRMLS_DC) { - PQsetInstanceData(event->conn, php_pqconn_event, data); + 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_event_conndestroy(PGEventConnDestroy *event, php_pqconn_event_data_t *data) + +static void php_pqconn_event_connreset(PGEventConnReset *event) { - PQsetInstanceData(event->conn, php_pqconn_event, NULL); - efree(data); + php_pqconn_event_data_t *data = PQinstanceData(event->conn, php_pqconn_event); + + if (data) { + zval **evhs; + TSRMLS_DF(data); + + 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); + } + } } -static void php_pqconn_event_resultcreate(PGEventResultCreate *event, php_pqconn_event_data_t *data) + +static zval *result_instance_zval(PGresult *res TSRMLS_DC) { - TSRMLS_DF(data); + zval *rid = PQresultInstanceData(res, php_pqconn_event); - if (data->obj->intern->onevent.fci.size > 0) { - zval *res; + if (!rid) { php_pqres_t *r = ecalloc(1, sizeof(*r)); - MAKE_STD_ZVAL(res); - r->res = event->result; - res->type = IS_OBJECT; - res->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC); + 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); + + PQresultSetInstanceData(res, php_pqconn_event, rid); + } + + Z_ADDREF_P(rid); + return rid; +} + +static void php_pqconn_event_resultcreate(PGEventResultCreate *event) +{ + 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); + } - Z_ADDREF_P(res); - PQresultSetInstanceData(event->result, php_pqconn_event, res); + /* 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); + case PGEVT_CONNRESET: + php_pqconn_event_connreset(e); break; case PGEVT_RESULTCREATE: - php_pqconn_event_resultcreate(e, data); + php_pqconn_event_resultcreate(e); break; default: break; @@ -1250,36 +1779,144 @@ static php_pqconn_event_data_t *php_pqconn_event_data_init(php_pqconn_object_t * return data; } -static int apply_notice_event(void *p, void *a TSRMLS_DC) +static void php_pqconn_notice_recv(void *p, const PGresult *res) { - zval **evh = p; - zval *args = a; - zval *retval = NULL; + php_pqconn_event_data_t *data = p; - zend_call_method_with_1_params(evh, Z_OBJCE_PP(evh), NULL, "trigger", &retval, args); - if (retval) { - zval_ptr_dtor(&retval); + if (data) { + zval **evhs; + TSRMLS_DF(data); + + if (SUCCESS == zend_hash_find(&data->obj->intern->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, PHP_PQresultErrorMessage(res), 1); + zend_hash_apply_with_argument(Z_ARRVAL_PP(evhs), apply_event, args TSRMLS_CC); + zval_ptr_dtor(&args); + } + } +} + +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; + PGconn *conn = NULL;; + + if (o->flags & PHP_PQCONN_ASYNC) { + conn = PQconnectStart(o->dsn); + } else { + conn = PQconnectdb(o->dsn); } - return ZEND_HASH_APPLY_KEEP; + if (conn) { + PQregisterEventProc(conn, php_pqconn_event, "ext-pq", NULL); + } + + return conn; } -static void php_pqconn_notice_recv(void *p, const PGresult *res) +static void php_pqconn_resource_factory_dtor(void *opaque, void *handle TSRMLS_DC) { - php_pqconn_event_data_t *data = p; - zval **evhs; - TSRMLS_DF(data); + php_pqconn_event_data_t *evdata = PQinstanceData(handle, php_pqconn_event); + + /* we don't care for anything, except free'ing evdata */ + if (evdata) { + PQsetInstanceData(handle, php_pqconn_event, NULL); + memset(evdata, 0, sizeof(*evdata)); + efree(evdata); + } + + 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_wakeup(php_persistent_handle_factory_t *f, void **handle TSRMLS_DC) +{ + // FIXME: ping server +} + +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_retire(php_persistent_handle_factory_t *f, void **handle TSRMLS_DC) +{ + php_pqconn_event_data_t *evdata = PQinstanceData(*handle, php_pqconn_event); + PGcancel *cancel; + PGresult *res; - if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("notice"), (void *) &evhs)) { - zval *args, *connection = NULL; + /* go away */ + PQsetInstanceData(*handle, php_pqconn_event, 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); + /* ignore notices */ + PQsetNoticeReceiver(*handle, php_pqconn_notice_ignore, NULL); + + /* cancel async queries */ + if (PQisBusy(*handle) && (cancel = PQgetCancel(*handle))) { + PQcancel(cancel, ZEND_STRL("retiring persistent connection")); + PQfreeCancel(cancel); + } + /* clean up async results */ + while ((res = PQgetResult(*handle))) { + PHP_PQclear(res); + } + + /* clean up transaction & session */ + switch (PQtransactionStatus(*handle)) { + case PQTRANS_IDLE: + res = PQexec(*handle, "RESET ALL"); + break; + default: + res = PQexec(*handle, "ROLLBACK; RESET ALL"); + break; + } + + if (res) { + PHP_PQclear(res); + } + + if (evdata) { + /* clean up notify listeners */ + zend_hash_apply_with_arguments(&evdata->obj->intern->listeners TSRMLS_CC, apply_unlisten, 1, evdata->obj); + + /* release instance data */ + memset(evdata, 0, sizeof(*evdata)); + efree(evdata); } } @@ -1289,36 +1926,50 @@ ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct, 0, 0, 1) ZEND_END_ARG_INFO(); static PHP_METHOD(pqconn, __construct) { zend_error_handling zeh; - char *dsn_str; - int dsn_len; - zend_bool async = 0; + char *dsn_str = ""; + int dsn_len = 0; + long flags = 0; + STATUS rv; - 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)) { + zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &dsn_str, &dsn_len, &flags); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { 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); - obj->intern = ecalloc(1, sizeof(*obj->intern)); + if (obj->intern) { + zend_throw_exception_ex(exce(EX_BAD_METHODCALL), EX_BAD_METHODCALL TSRMLS_CC, "pq\\Connection already initialized"); + } else { + php_pqconn_event_data_t *evdata = php_pqconn_event_data_init(obj TSRMLS_CC); + php_pqconn_resource_factory_data_t rfdata = {dsn_str, flags}; - 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); + 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); - obj->intern->poller = (int (*)(PGconn*)) PQconnectPoll; - } else { - obj->intern->conn = PQconnectdb(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, php_pqconn_wakeup, php_pqconn_retire TSRMLS_CC); + php_resource_factory_init(&obj->intern->factory, php_persistent_handle_get_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; + } + + obj->intern->conn = php_resource_factory_handle_ctor(&obj->intern->factory, &rfdata TSRMLS_CC); - PQsetNoticeReceiver(obj->intern->conn, php_pqconn_notice_recv, data); - PQregisterEventProc(obj->intern->conn, php_pqconn_event, "ext-pq", 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", PQerrorMessage(obj->intern->conn)); + if (SUCCESS != php_pqconn_update_socket(getThis(), obj TSRMLS_CC)) { + zend_throw_exception_ex(exce(EX_CONNECTION_FAILED), EX_CONNECTION_FAILED TSRMLS_CC, "Connection failed (%s)", PHP_PQerrorMessage(obj->intern->conn)); + } } } - zend_restore_error_handling(&zeh TSRMLS_CC); } ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset, 0, 0, 0) @@ -1333,7 +1984,7 @@ static PHP_METHOD(pqconn, reset) { if (CONNECTION_OK == PQstatus(obj->intern->conn)) { RETURN_TRUE; } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connection reset failed: %s", PQerrorMessage(obj->intern->conn)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connection reset failed: (%s)", PHP_PQerrorMessage(obj->intern->conn)); } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); @@ -1387,79 +2038,150 @@ static PHP_METHOD(pqconn, listen) { 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) { + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else { char *quoted_channel = PQescapeIdentifier(obj->intern->conn, channel_str, channel_len); - if (quoted_channel) { + if (!quoted_channel) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj->intern->conn)); + RETVAL_FALSE; + } else { PGresult *res; - char *cmd; + smart_str cmd = {0}; - spprintf(&cmd, 0, "LISTEN %s", channel_str); - res = PQexec(obj->intern->conn, cmd); + smart_str_appends(&cmd, "LISTEN "); + smart_str_appends(&cmd, quoted_channel); + smart_str_0(&cmd); - efree(cmd); + res = PQexec(obj->intern->conn, cmd.c); + + smart_str_free(&cmd); PQfreemem(quoted_channel); - if (res) { - if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { + if (!res) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to install listener (%s)", PHP_PQerrorMessage(obj->intern->conn)); + RETVAL_FALSE; + } else { + if (SUCCESS != php_pqres_success(res TSRMLS_CC)) { + RETVAL_FALSE; + } else { + obj->intern->poller = PQconsumeInput; 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->intern->conn)); - RETVAL_FALSE; + PHP_PQclear(res); } php_pqconn_notify_listeners(obj TSRMLS_CC); - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not escape channel identifier: %s", 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_listen_async, 0, 0, 0) ZEND_ARG_INFO(0, channel) - ZEND_ARG_INFO(0, message) + ZEND_ARG_INFO(0, callable) ZEND_END_ARG_INFO(); -static PHP_METHOD(pqconn, notify) { - char *channel_str, *message_str; - int channel_len, message_len; +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, "ss", &channel_str, &channel_len, &message_str, &message_len)) { + 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); - if (obj->intern) { - PGresult *res; - char *params[2] = {channel_str, message_str}; - - res = PQexecParams(obj->intern->conn, "select pg_notify($1, $2)", 2, NULL, (const char *const*) params, NULL, NULL, 0); + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else { + char *quoted_channel = PQescapeIdentifier(obj->intern->conn, channel_str, channel_len); - if (res) { - if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { - RETVAL_TRUE; - } else { + if (!quoted_channel) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj->intern->conn)); + } else { + smart_str cmd = {0}; + + smart_str_appends(&cmd, "LISTEN "); + smart_str_appends(&cmd, quoted_channel); + smart_str_0(&cmd); + + if (!PQsendQuery(obj->intern->conn, cmd.c)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to install listener (%s)", PHP_PQerrorMessage(obj->intern->conn)); RETVAL_FALSE; + } else { + obj->intern->poller = PQconsumeInput; + php_pqconn_add_listener(obj, channel_str, channel_len, &listener TSRMLS_CC); + RETVAL_TRUE; } - PQclear(res); - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not notify listeners: %s", PQerrorMessage(obj->intern->conn)); + + smart_str_free(&cmd); + PQfreemem(quoted_channel); + } + } + } +} + +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->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else { + PGresult *res; + char *params[2] = {channel_str, message_str}; + + res = PQexecParams(obj->intern->conn, "select pg_notify($1, $2)", 2, NULL, (const char *const*) params, NULL, NULL, 0); + + if (!res) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj->intern->conn)); RETVAL_FALSE; + } else { + if (SUCCESS != php_pqres_success(res TSRMLS_CC)) { + RETVAL_FALSE; + } else { + RETVAL_TRUE; + } + PHP_PQclear(res); } php_pqconn_notify_listeners(obj TSRMLS_CC); + } + } +} + +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) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); - RETVAL_FALSE; + char *params[2] = {channel_str, message_str}; + + if (!PQsendQueryParams(obj->intern->conn, "select pg_notify($1, $2)", 2, NULL, (const char *const*) params, NULL, NULL, 0)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj->intern->conn)); + RETVAL_FALSE; + } else { + obj->intern->poller = PQconsumeInput; + RETVAL_TRUE; + } + + php_pqconn_notify_listeners(obj TSRMLS_CC); } } } @@ -1470,22 +2192,16 @@ 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->intern) { - if (obj->intern->poller) { - if (obj->intern->poller == PQconsumeInput) { - RETVAL_LONG(obj->intern->poller(obj->intern->conn) * PGRES_POLLING_OK); - php_pqconn_notify_listeners(obj TSRMLS_CC); - return; - } else { - RETURN_LONG(obj->intern->poller(obj->intern->conn)); - } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "No asynchronous operation active"); - } + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else if (!obj->intern->poller) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "No asynchronous operation active"); + } else if (obj->intern->poller == PQconsumeInput) { + RETVAL_LONG(obj->intern->poller(obj->intern->conn) * PGRES_POLLING_OK); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); + RETVAL_LONG(obj->intern->poller(obj->intern->conn)); } - RETURN_FALSE; + php_pqconn_notify_listeners(obj TSRMLS_CC); } } @@ -1496,32 +2212,34 @@ static PHP_METHOD(pqconn, exec) { zend_error_handling zeh; char *query_str; int query_len; + STATUS rv; - 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)) { + zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &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) { + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else { PGresult *res = PQexec(obj->intern->conn, query_str); - php_pqconn_notify_listeners(obj TSRMLS_CC); - - if (res) { - if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { - php_pqres_t *r = ecalloc(1, sizeof(*r)); + if (!res) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn)); + } else if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { + php_pqres_t *r = ecalloc(1, sizeof(*r)); - r->res = res; - return_value->type = IS_OBJECT; - return_value->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC); - } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute query: %s", PQerrorMessage(obj->intern->conn)); + 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 { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); + + php_pqconn_notify_listeners(obj TSRMLS_CC); } } - zend_restore_error_handling(&zeh TSRMLS_CC); } ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_get_result, 0, 0, 0) @@ -1530,21 +2248,23 @@ 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->intern) { + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connectio not initialized"); + } else { PGresult *res = PQgetResult(obj->intern->conn); - if (res) { + if (!res) { + RETVAL_NULL(); + } else { 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 { - RETVAL_NULL(); } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); - RETVAL_FALSE; + + php_pqconn_notify_listeners(obj TSRMLS_CC); } } } @@ -1558,37 +2278,31 @@ static PHP_METHOD(pqconn, execAsync) { php_pq_callback_t resolver = {{0}}; char *query_str; int query_len; + STATUS rv; - 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)) { + zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|f", &query_str, &query_len, &resolver.fci, &resolver.fcc); + 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) { + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else if (!PQsendQuery(obj->intern->conn, query_str)) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn)); + } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to request unbuffered result handling (%s)", PHP_PQerrorMessage(obj->intern->conn)); + } else { + 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); } - - obj->intern->poller = PQconsumeInput; - - if (PQsendQuery(obj->intern->conn, query_str)) { - if (obj->intern->unbuffered) { - if (!PQsetSingleRowMode(obj->intern->conn)) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not enable unbuffered mode: %s", PQerrorMessage(obj->intern->conn)); - } - } - RETVAL_TRUE; - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute query: %s", PQerrorMessage(obj->intern->conn)); - RETVAL_FALSE; - } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); - RETVAL_FALSE; + php_pqconn_notify_listeners(obj TSRMLS_CC); } } - zend_restore_error_handling(&zeh TSRMLS_CC); } static int apply_to_oid(void *p, void *arg TSRMLS_DC) @@ -1646,7 +2360,7 @@ static int php_pq_types_to_array(HashTable *ht, Oid **types TSRMLS_DC) Oid *tmp; /* +1 for when less types than params are specified */ - *types = tmp = ecalloc(count + 1, sizeof(Oid)); + *types = tmp = ecalloc(count + 1, sizeof(**types)); zend_hash_apply_with_argument(ht, apply_to_oid, &tmp TSRMLS_CC); } @@ -1668,7 +2382,26 @@ static int php_pq_params_to_array(HashTable *ht, char ***params, HashTable *zdto return count; } +/* +static Oid *php_pq_ntypes_to_array(zend_bool fill, int argc, ...) +{ + int i; + Oid *oids = ecalloc(argc + 1, sizeof(*oids)); + va_list argv; + + va_start(argv, argc); + for (i = 0; i < argc; ++i) { + if (!fill || !i) { + oids[i] = va_arg(argv, Oid); + } else { + oids[i] = oids[0]; + } + } + va_end(argv); + return oids; +} +*/ ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params, 0, 0, 2) ZEND_ARG_INFO(0, query) ZEND_ARG_ARRAY_INFO(0, params, 0) @@ -1680,12 +2413,18 @@ static PHP_METHOD(pqconn, execParams) { int query_len; zval *zparams; zval *ztypes = NULL; + STATUS rv; - 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)) { + zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa/|a/!", &query_str, &query_len, &zparams, &ztypes); + 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) { + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else { PGresult *res; int count; Oid *types = NULL; @@ -1709,26 +2448,22 @@ static PHP_METHOD(pqconn, execParams) { efree(params); } - php_pqconn_notify_listeners(obj TSRMLS_CC); - - if (res) { + if (!res) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn)); + } else { if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { 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 { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute query: %s", 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_restore_error_handling(&zeh TSRMLS_CC); } ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params_async, 0, 0, 2) @@ -1744,12 +2479,18 @@ static PHP_METHOD(pqconn, execParamsAsync) { int query_len; zval *zparams; zval *ztypes = NULL; + STATUS rv; - 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)) { + zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa/|a/!f", &query_str, &query_len, &zparams, &ztypes, &resolver.fci, &resolver.fcc); + 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) { + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else { int count; Oid *types = NULL; char **params = NULL; @@ -1762,24 +2503,18 @@ static PHP_METHOD(pqconn, execParamsAsync) { php_pq_types_to_array(Z_ARRVAL_P(ztypes), &types TSRMLS_CC); } - php_pq_callback_dtor(&obj->intern->onevent); - if (resolver.fci.size > 0) { - obj->intern->onevent = resolver; - php_pq_callback_addref(&obj->intern->onevent); - } - - obj->intern->poller = PQconsumeInput; - - if (PQsendQueryParams(obj->intern->conn, query_str, count, types, (const char *const*) params, NULL, NULL, 0)) { - if (obj->intern->unbuffered) { - if (!PQsetSingleRowMode(obj->intern->conn)) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not enable unbuffered mode: %s", PQerrorMessage(obj->intern->conn)); - } - } - RETVAL_TRUE; + if (!PQsendQueryParams(obj->intern->conn, query_str, count, types, (const char *const*) params, NULL, NULL, 0)) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn)); + } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn)); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute query: %s", PQerrorMessage(obj->intern->conn)); - RETVAL_FALSE; + 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_hash_destroy(&zdtor); @@ -1789,12 +2524,6 @@ static PHP_METHOD(pqconn, execParamsAsync) { if (params) { efree(params); } - - php_pqconn_notify_listeners(obj TSRMLS_CC); - - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); - RETVAL_FALSE; } } zend_restore_error_handling(&zeh TSRMLS_CC); @@ -1822,12 +2551,13 @@ static STATUS php_pqconn_prepare(zval *object, php_pqconn_object_t *obj, const c efree(types); } - if (res) { - rv = php_pqres_success(res TSRMLS_CC); - PQclear(res); - } else { + if (!res) { rv = FAILURE; - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not prepare statement: %s", PQerrorMessage(obj->intern->conn)); + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to prepare statement (%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; @@ -1843,28 +2573,29 @@ static PHP_METHOD(pqconn, prepare) { zval *ztypes = NULL; char *name_str, *query_str; int name_len, *query_len; + STATUS rv; - zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!", &name_str, &name_len, &query_str, &query_len, &ztypes)) { + zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!", &name_str, &name_len, &query_str, &query_len, &ztypes); + 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) { - if (SUCCESS == php_pqconn_prepare(getThis(), obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC)) { - php_pqstm_t *stm = ecalloc(1, sizeof(*stm)); + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else if (SUCCESS == php_pqconn_prepare(getThis(), obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC)) { + php_pqstm_t *stm = ecalloc(1, sizeof(*stm)); - php_pq_object_addref(obj TSRMLS_CC); - stm->conn = obj; - stm->name = estrdup(name_str); + 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); - } - php_pqconn_notify_listeners(obj TSRMLS_CC); - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); + return_value->type = IS_OBJECT; + return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC); } } - zend_restore_error_handling(&zeh TSRMLS_CC); } static STATUS php_pqconn_prepare_async(zval *object, php_pqconn_object_t *obj, const char *name, const char *query, HashTable *typest TSRMLS_DC) @@ -1881,16 +2612,16 @@ static STATUS php_pqconn_prepare_async(zval *object, php_pqconn_object_t *obj, c count = php_pq_types_to_array(typest, &types TSRMLS_CC); } - if (PQsendPrepare(obj->intern->conn, name, query, count, types)) { - if (obj->intern->unbuffered) { - if (!PQsetSingleRowMode(obj->intern->conn)) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not enable unbuffered mode: %s", PQerrorMessage(obj->intern->conn)); - } - } - rv = SUCCESS; - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not prepare statement: %s", PQerrorMessage(obj->intern->conn)); + if (!PQsendPrepare(obj->intern->conn, name, query, count, types)) { rv = FAILURE; + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj->intern->conn)); + } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) { + rv = FAILURE; + zend_throw_exception_ex(exce(EX_RUNTIME), 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); } if (types) { @@ -1910,29 +2641,29 @@ static PHP_METHOD(pqconn, prepareAsync) { zval *ztypes = NULL; char *name_str, *query_str; int name_len, *query_len; + STATUS rv; - zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!", &name_str, &name_len, &query_str, &query_len, &ztypes)) { + zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!", &name_str, &name_len, &query_str, &query_len, &ztypes); + 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) { - obj->intern->poller = PQconsumeInput; - if (SUCCESS == php_pqconn_prepare_async(getThis(), obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC)) { - php_pqstm_t *stm = ecalloc(1, sizeof(*stm)); + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else if (SUCCESS == php_pqconn_prepare_async(getThis(), obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC)) { + php_pqstm_t *stm = ecalloc(1, sizeof(*stm)); - php_pq_object_addref(obj TSRMLS_CC); - stm->conn = obj; - stm->name = estrdup(name_str); + 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); - } - php_pqconn_notify_listeners(obj TSRMLS_CC); - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); + return_value->type = IS_OBJECT; + return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC); } } - zend_restore_error_handling(&zeh TSRMLS_CC); } ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote, 0, 0, 1) @@ -1945,19 +2676,18 @@ static PHP_METHOD(pqconn, quote) { if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) { php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - if (obj->intern) { + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else { char *quoted = PQescapeLiteral(obj->intern->conn, str, len); - if (quoted) { + if (!quoted) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to quote string (%s)", PHP_PQerrorMessage(obj->intern->conn)); + RETVAL_FALSE; + } else { RETVAL_STRING(quoted, 1); PQfreemem(quoted); - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not quote string: %s", PQerrorMessage(obj->intern->conn)); - RETVAL_FALSE; } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); - RETVAL_FALSE; } } } @@ -1972,19 +2702,18 @@ static PHP_METHOD(pqconn, quoteName) { if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) { php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - if (obj->intern) { + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else { char *quoted = PQescapeIdentifier(obj->intern->conn, str, len); - if (quoted) { + if (!quoted) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to quote name (%s)", PHP_PQerrorMessage(obj->intern->conn)); + RETVAL_FALSE; + } else { RETVAL_STRING(quoted, 1); PQfreemem(quoted); - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not quote name: %s", PQerrorMessage(obj->intern->conn)); - RETVAL_FALSE; } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); - RETVAL_FALSE; } } } @@ -1999,20 +2728,19 @@ static PHP_METHOD(pqconn, escapeBytea) { if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) { php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - if (obj->intern) { + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else { size_t escaped_len; char *escaped_str = (char *) PQescapeByteaConn(obj->intern->conn, (unsigned char *) str, len, &escaped_len); - if (escaped_str) { + if (!escaped_str) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to escape bytea (%s)", PHP_PQerrorMessage(obj->intern->conn)); + RETVAL_FALSE; + } else { RETVAL_STRINGL(escaped_str, escaped_len - 1, 1); PQfreemem(escaped_str); - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not escape bytea: %s", PQerrorMessage(obj->intern->conn)); - RETVAL_FALSE; } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); - RETVAL_FALSE; } } } @@ -2027,20 +2755,19 @@ static PHP_METHOD(pqconn, unescapeBytea) { if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) { php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - if (obj->intern) { + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else { size_t unescaped_len; char *unescaped_str = (char *) PQunescapeBytea((unsigned char *)str, &unescaped_len); - if (unescaped_str) { + if (!unescaped_str) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to unescape bytea (%s)", PHP_PQerrorMessage(obj->intern->conn)); + RETVAL_FALSE; + } else { RETVAL_STRINGL(unescaped_str, unescaped_len, 1); PQfreemem(unescaped_str); - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not unescape bytea: %s", PQerrorMessage(obj->intern->conn)); - RETVAL_FALSE; } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); - RETVAL_FALSE; } } } @@ -2061,61 +2788,79 @@ static const char *isolation_level(long *isolation) { static STATUS php_pqconn_start_transaction(zval *zconn, php_pqconn_object_t *conn_obj, long isolation, zend_bool readonly, zend_bool deferrable TSRMLS_DC) { + STATUS rv = FAILURE; + if (!conn_obj) { conn_obj = zend_object_store_get_object(zconn TSRMLS_CC); } - if (conn_obj->intern) { + if (!conn_obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else { PGresult *res; - char *cmd; - - spprintf(&cmd, 0, "START TRANSACTION ISOLATION LEVEL %s, READ %s, %s DEFERRABLE", - isolation_level(&isolation), readonly ? "ONLY" : "WRITE", deferrable ? "": "NOT"); - - res = PQexec(conn_obj->intern->conn, cmd); - - efree(cmd); - - if (res) { - STATUS rv = php_pqres_success(res TSRMLS_CC); - - PQclear(res); - return rv; + smart_str cmd = {0}; + const char *il = isolation_level(&isolation); + + smart_str_appends(&cmd, "START TRANSACTION ISOLATION LEVEL "); + smart_str_appends(&cmd, il); + smart_str_appends(&cmd, ", READ "); + smart_str_appends(&cmd, readonly ? "ONLY" : "WRITE"); + smart_str_appends(&cmd, ","); + smart_str_appends(&cmd, deferrable ? "" : " NOT"); + smart_str_appends(&cmd, " DEFERRABLE"); + smart_str_0(&cmd); + + res = PQexec(conn_obj->intern->conn, cmd.c); + + if (!res) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj->intern->conn)); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not start transaction: %s", PQerrorMessage(conn_obj->intern->conn)); - return FAILURE; + rv = php_pqres_success(res TSRMLS_CC); + PHP_PQclear(res); + php_pqconn_notify_listeners(conn_obj TSRMLS_CC); } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); - return FAILURE; + + smart_str_free(&cmd); } + + return rv; } static STATUS php_pqconn_start_transaction_async(zval *zconn, php_pqconn_object_t *conn_obj, long isolation, zend_bool readonly, zend_bool deferrable TSRMLS_DC) { + STATUS rv = FAILURE; + if (!conn_obj) { conn_obj = zend_object_store_get_object(zconn TSRMLS_CC); } - if (conn_obj->intern->conn) { - char *cmd; - - spprintf(&cmd, 0, "START TRANSACTION ISOLATION LEVEL %s, READ %s, %s DEFERRABLE", - isolation_level(&isolation), readonly ? "ONLY" : "WRITE", deferrable ? "": "NOT"); - - if (PQsendQuery(conn_obj->intern->conn, cmd)) { - conn_obj->intern->poller = PQconsumeInput; - efree(cmd); - return SUCCESS; + if (!conn_obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else { + smart_str cmd = {0}; + const char *il = isolation_level(&isolation); + + smart_str_appends(&cmd, "START TRANSACTION ISOLATION LEVEL "); + smart_str_appends(&cmd, il); + smart_str_appends(&cmd, ", READ "); + smart_str_appends(&cmd, readonly ? "ONLY" : "WRITE"); + smart_str_appends(&cmd, ","); + smart_str_appends(&cmd, deferrable ? "" : "NOT "); + smart_str_appends(&cmd, " DEFERRABLE"); + smart_str_0(&cmd); + + if (!PQsendQuery(conn_obj->intern->conn, cmd.c)) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj->intern->conn)); } else { - efree(cmd); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not start transaction: %s", PQerrorMessage(conn_obj->intern->conn)); - return FAILURE; + rv = SUCCESS; + conn_obj->intern->poller = PQconsumeInput; + php_pqconn_notify_listeners(conn_obj TSRMLS_CC); } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); - return FAILURE; + + smart_str_free(&cmd); } + + return rv; } ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction, 0, 0, 0) @@ -2127,10 +2872,13 @@ static PHP_METHOD(pqconn, startTransaction) { zend_error_handling zeh; long isolation = PHP_PQTXN_READ_COMMITTED; zend_bool readonly = 0, deferrable = 0; + STATUS rv; - zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lbb", &isolation, &readonly, &deferrable)) { - STATUS rv; + zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lbb", &isolation, &readonly, &deferrable); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); rv = php_pqconn_start_transaction(getThis(), obj, isolation, readonly, deferrable TSRMLS_CC); @@ -2140,6 +2888,7 @@ static PHP_METHOD(pqconn, startTransaction) { php_pq_object_addref(obj TSRMLS_CC); txn->conn = obj; + txn->open = 1; txn->isolation = isolation; txn->readonly = readonly; txn->deferrable = deferrable; @@ -2148,7 +2897,6 @@ static PHP_METHOD(pqconn, startTransaction) { return_value->value.obj = php_pqtxn_create_object_ex(php_pqtxn_class_entry, txn, NULL TSRMLS_CC); } } - zend_restore_error_handling(&zeh TSRMLS_CC); } @@ -2161,10 +2909,12 @@ static PHP_METHOD(pqconn, startTransactionAsync) { zend_error_handling zeh; long isolation = PHP_PQTXN_READ_COMMITTED; zend_bool readonly = 0, deferrable = 0; + STATUS rv; - zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lbb", &isolation, &readonly, &deferrable)) { - STATUS rv; + zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lbb", &isolation, &readonly, &deferrable); + zend_restore_error_handling(&zeh TSRMLS_CC); + if (SUCCESS == rv) { php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); rv = php_pqconn_start_transaction_async(getThis(), obj, isolation, readonly, deferrable TSRMLS_CC); @@ -2182,7 +2932,39 @@ static PHP_METHOD(pqconn, startTransactionAsync) { return_value->value.obj = php_pqtxn_create_object_ex(php_pqtxn_class_entry, txn, NULL TSRMLS_CC); } } - zend_restore_error_handling(&zeh TSRMLS_CC); +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_trace, 0, 0, 0) + ZEND_ARG_INFO(0, stdio_stream) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqconn, trace) { + zval *zstream = NULL; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r!", &zstream)) { + php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else { + if (!zstream) { + PQuntrace(obj->intern->conn); + RETVAL_TRUE; + } else { + FILE *fp; + php_stream *stream = NULL; + + php_stream_from_zval(stream, &zstream); + + if (SUCCESS != php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) { + RETVAL_FALSE; + } else { + stream->flags |= PHP_STREAM_FLAG_NO_CLOSE; + PQtrace(obj->intern->conn, fp); + RETVAL_TRUE; + } + } + } + } } static zend_function_entry php_pqconn_methods[] = { @@ -2197,7 +2979,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) @@ -2205,12 +2989,145 @@ static zend_function_entry php_pqconn_methods[] = { PHP_ME(pqconn, unescapeBytea, ai_pqconn_unescape_bytea, 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) + {0} +}; + +ZEND_BEGIN_ARG_INFO_EX(ai_pqtypes_construct, 0, 0, 1) + ZEND_ARG_OBJ_INFO(0, connection, pq\\Connection, 0) + ZEND_ARG_ARRAY_INFO(0, namespaces, 1) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqtypes, __construct) { + zend_error_handling zeh; + zval *zconn, *znsp = NULL; + STATUS rv; + + zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|a!", &zconn, php_pqconn_class_entry, &znsp); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { + php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC); + + if (!conn_obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else { + php_pqtypes_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + zval *retval = NULL; + + obj->intern = ecalloc(1, sizeof(*obj->intern)); + obj->intern->conn = conn_obj; + php_pq_object_addref(conn_obj TSRMLS_CC); + zend_hash_init(&obj->intern->types, 300, NULL, ZVAL_PTR_DTOR, 0); + + zend_replace_error_handling(EH_THROW, exce(EX_RUNTIME), &zeh TSRMLS_CC); + if (znsp) { + zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "refresh", &retval, znsp); + } else { + zend_call_method_with_0_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "refresh", &retval); + } + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (retval) { + zval_ptr_dtor(&retval); + } + } + } +} + +#define PHP_PQ_TYPES_QUERY \ + "select t.oid, t.* " \ + "from pg_type t join pg_namespace n on t.typnamespace=n.oid " \ + "where typisdefined " \ + "and typrelid=0" +#define PHP_PQ_OID_TEXT 25 + +ZEND_BEGIN_ARG_INFO_EX(ai_pqtypes_refresh, 0, 0, 0) + ZEND_ARG_ARRAY_INFO(0, namespaces, 1) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqtypes, refresh) { + HashTable *nsp = NULL; + zend_error_handling zeh; + STATUS rv; + + zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|H/!", &nsp); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { + php_pqtypes_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Types not initialized"); + } else { + PGresult *res; + + if (!nsp || !zend_hash_num_elements(nsp)) { + res = PQexec(obj->intern->conn->intern->conn, PHP_PQ_TYPES_QUERY " and nspname in ('public', 'pg_catalog')"); + } else { + int i, count; + Oid *oids; + char **params = NULL; + HashTable zdtor; + smart_str str = {0}; + + smart_str_appends(&str, PHP_PQ_TYPES_QUERY " and nspname in("); + zend_hash_init(&zdtor, 0, NULL, ZVAL_PTR_DTOR, 0); + count = php_pq_params_to_array(nsp, ¶ms, &zdtor TSRMLS_CC); + oids = ecalloc(count + 1, sizeof(*oids)); + for (i = 0; i < count; ++i) { + oids[i] = PHP_PQ_OID_TEXT; + if (i) { + smart_str_appendc(&str, ','); + } + smart_str_appendc(&str, '$'); + smart_str_append_unsigned(&str, i+1); + } + smart_str_appendc(&str, ')'); + smart_str_0(&str); + + res = PQexecParams(obj->intern->conn->intern->conn, str.c, count, oids, (const char *const*) params, NULL, NULL, 0); + + smart_str_free(&str); + efree(oids); + efree(params); + zend_hash_destroy(&zdtor); + } + + if (!res) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to fetch types (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } else { + if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { + 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, NULL TSRMLS_CC); + long oid = atol(PQgetvalue(res, r, 0 )); + char *name = PQgetvalue(res, r, 1); + + Z_ADDREF_P(row); + + zend_hash_index_update(&obj->intern->types, oid, (void *) &row, sizeof(zval *), NULL); + zend_hash_add(&obj->intern->types, name, strlen(name) + 1, (void *) &row, sizeof(zval *), NULL); + } + } + + PHP_PQclear(res); + php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC); + } + } + } +} + +static zend_function_entry php_pqtypes_methods[] = { + PHP_ME(pqtypes, __construct, ai_pqtypes_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) + PHP_ME(pqtypes, refresh, ai_pqtypes_refresh, ZEND_ACC_PUBLIC) {0} }; -static zval **php_pqres_iteration(zval *this_ptr, php_pqres_object_t *obj, php_pqres_fetch_t fetch_type 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) { - zval **row = NULL; + STATUS rv; php_pqres_fetch_t orig_fetch; if (!obj) { @@ -2223,13 +3140,159 @@ static zval **php_pqres_iteration(zval *this_ptr, php_pqres_object_t *obj, php_p } 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)) { - obj->intern->iter->zi.funcs->get_current_data((zend_object_iterator *) obj->intern->iter, &row 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) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Result not initialized"); + } else { + php_pqres_col_t col; + + if (SUCCESS != column_nn(obj, zcol, &col TSRMLS_CC)) { + RETVAL_FALSE; + } else { + Z_ADDREF_P(zref); + + if (SUCCESS != zend_hash_index_update(&obj->intern->bound, col.num, (void *) &zref, sizeof(zval *), NULL)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to bind column %s@%d", col.name, col.num); + RETVAL_FALSE; + } else { + zend_hash_sort(&obj->intern->bound, zend_qsort, compare_index, 0 TSRMLS_CC); + RETVAL_TRUE; + } + } + } + } +} + +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 **); + STATUS *rv = va_arg(argv, STATUS *); + + if (SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrow), key->h, (void *) &zvalue)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to find column ad index %lu", key->h); + *rv = FAILURE; + return ZEND_HASH_APPLY_STOP; + } else { + zval_dtor(*zbound); + ZVAL_COPY_VALUE(*zbound, *zvalue); + ZVAL_NULL(*zvalue); + zval_ptr_dtor(zvalue); + Z_ADDREF_P(*zbound); + *zvalue = *zbound; + *rv = SUCCESS; + return ZEND_HASH_APPLY_KEEP; + } +} + +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; + STATUS rv; + + zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); + rv = zend_parse_parameters_none(); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { + php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Result not initialized"); + } else { + zval **row = NULL; + + if (SUCCESS == php_pqres_iteration(getThis(), obj, PHP_PQRES_FETCH_ARRAY, &row TSRMLS_CC) && row) { + zend_replace_error_handling(EH_THROW, exce(EX_RUNTIME), &zeh TSRMLS_CC); + zend_hash_apply_with_arguments(&obj->intern->bound TSRMLS_CC, apply_bound, 2, row, &rv); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS != rv) { + zval_ptr_dtor(row); + } else { + RETVAL_ZVAL(*row, 1, 0); + } + } + } + } } ZEND_BEGIN_ARG_INFO_EX(ai_pqres_fetch_row, 0, 0, 0) @@ -2238,19 +3301,32 @@ 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; + STATUS rv; - 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 = php_pqres_iteration(getThis(), obj, fetch_type TSRMLS_CC); + zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &fetch_type); + zend_restore_error_handling(&zeh TSRMLS_CC); - if (row) { - RETVAL_ZVAL(*row, 1, 0); + if (SUCCESS == rv) { + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Result not initialized"); } else { - RETVAL_FALSE; + zval **row = NULL; + + if (fetch_type == -1) { + fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : PHP_PQRES_FETCH_ARRAY; + } + + zend_replace_error_handling(EH_THROW, exce(EX_RUNTIME), &zeh TSRMLS_CC); + php_pqres_iteration(getThis(), obj, fetch_type, &row TSRMLS_CC); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (row) { + RETVAL_ZVAL(*row, 1, 0); + } } } - zend_restore_error_handling(&zeh TSRMLS_CC); } static zval **column_at(zval *row, int col TSRMLS_DC) @@ -2259,14 +3335,14 @@ static zval **column_at(zval *row, int col TSRMLS_DC) HashTable *ht = HASH_OF(row); int count = zend_hash_num_elements(ht); - if (col < count) { + if (col >= count) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Column index %d exceeds column count %d", col, count); + } else { zend_hash_internal_pointer_reset(ht); while (col-- > 0) { zend_hash_move_forward(ht); } zend_hash_get_current_data(ht, (void *) &data); - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Column index %d exceeds column count %d", col, count); } return data; } @@ -2277,61 +3353,246 @@ ZEND_END_ARG_INFO(); static PHP_METHOD(pqres, fetchCol) { zend_error_handling zeh; long fetch_col = 0; + STATUS rv; - zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &fetch_col)) { + zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &fetch_col); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - zval **row = php_pqres_iteration(getThis(), obj, obj->intern->iter ? obj->intern->iter->fetch_type : 0 TSRMLS_CC); - if (row) { - zval **col = column_at(*row, fetch_col TSRMLS_CC); + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Result not initialized"); + } else { + zval **row = NULL; + + zend_replace_error_handling(EH_THROW, exce(EX_RUNTIME), &zeh TSRMLS_CC); + 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 (col) { - RETVAL_ZVAL(*col, 1, 0); - } else { - RETVAL_FALSE; + if (col) { + RETVAL_ZVAL(*col, 1, 0); + } } - } else { - RETVAL_FALSE; + zend_restore_error_handling(&zeh TSRMLS_CC); } } - 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 = FAILURE; + return ZEND_HASH_APPLY_STOP; + } else { + *rv = SUCCESS; + ++*cols; + return ZEND_HASH_APPLY_KEEP; + } } -static zend_function_entry php_pqres_methods[] = { - PHP_ME(pqres, fetchRow, ai_pqres_fetch_row, ZEND_ACC_PUBLIC) - PHP_ME(pqres, fetchCol, ai_pqres_fetch_col, ZEND_ACC_PUBLIC) - {0} -}; +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; -ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_construct, 0, 0, 3) - ZEND_ARG_OBJ_INFO(0, Connection, pq\\Connection, 0) - ZEND_ARG_INFO(0, type) - ZEND_ARG_INFO(0, query) - ZEND_ARG_ARRAY_INFO(0, types, 1) - ZEND_ARG_INFO(0, async) + 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(pqstm, __construct) { +static PHP_METHOD(pqres, map) { zend_error_handling zeh; - zval *zconn, *ztypes = NULL; - char *name_str, *query_str; - int name_len, *query_len; - zend_bool async = 0; + zval *zkeys = 0, *zvals = 0; + long fetch_type = -1; + STATUS rv; zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oss|a/!b", &zconn, php_pqconn_class_entry, &name_str, &name_len, &query_str, &query_len, &ztypes, &async)) { - php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z/!z/!l", &zkeys, &zvals, &fetch_type); + zend_restore_error_handling(&zeh TSRMLS_CC); - if (conn_obj->intern) { - STATUS rv; + if (SUCCESS == rv) { + php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Result not initialized"); + } else { + 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 { + 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)) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "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); + } + } + } +} + +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)) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Result not initialized"); + } else { + RETVAL_LONG(count); + } + } +} + +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} +}; + +ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_construct, 0, 0, 3) + ZEND_ARG_OBJ_INFO(0, Connection, pq\\Connection, 0) + ZEND_ARG_INFO(0, type) + ZEND_ARG_INFO(0, query) + ZEND_ARG_ARRAY_INFO(0, types, 1) + ZEND_ARG_INFO(0, async) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqstm, __construct) { + zend_error_handling zeh; + zval *zconn, *ztypes = NULL; + char *name_str, *query_str; + int name_len, *query_len; + zend_bool async = 0; + STATUS rv; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oss|a/!b", &zconn, php_pqconn_class_entry, &name_str, &name_len, &query_str, &query_len, &ztypes, &async); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { + php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC); + + if (!conn_obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else { if (async) { - conn_obj->intern->poller = PQconsumeInput; rv = php_pqconn_prepare_async(zconn, conn_obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC); } else { rv = php_pqconn_prepare(zconn, conn_obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC); - php_pqconn_notify_listeners(conn_obj TSRMLS_CC); } if (SUCCESS == rv) { @@ -2340,13 +3601,31 @@ 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; } + } + } +} +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) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Statement not initialized"); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); + 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); } } - zend_restore_error_handling(&zeh TSRMLS_CC); } ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_exec, 0, 0, 0) @@ -2355,53 +3634,52 @@ ZEND_END_ARG_INFO(); static PHP_METHOD(pqstm, exec) { zend_error_handling zeh; zval *zparams = NULL; + STATUS rv; zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &zparams)) { + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &zparams); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - if (obj->intern) { - if (obj->intern->conn->intern) { - int count = 0; - char **params = NULL; - HashTable zdtor; - PGresult *res; + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Statement not initialized"); + } else { + int count = 0; + char **params = NULL; + HashTable zdtor; + PGresult *res; - if (zparams) { - ZEND_INIT_SYMTABLE(&zdtor); - count = php_pq_params_to_array(Z_ARRVAL_P(zparams), ¶ms, &zdtor TSRMLS_CC); - } + ZEND_INIT_SYMTABLE(&zdtor); - res = PQexecPrepared(obj->intern->conn->intern->conn, obj->intern->name, count, (const char *const*) params, NULL, NULL, 0); + if (zparams) { + 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); + } - if (params) { - efree(params); - } - if (zparams) { - zend_hash_destroy(&zdtor); - } + res = PQexecPrepared(obj->intern->conn->intern->conn, obj->intern->name, count, (const char *const*) params, NULL, NULL, 0); - php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC); + if (params) { + efree(params); + } + zend_hash_destroy(&zdtor); - if (res) { - if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { - php_pqres_t *r = ecalloc(1, sizeof(*r)); + if (!res) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to execute statement (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } else if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { + php_pqres_t *r = ecalloc(1, sizeof(*r)); - r->res = res; - return_value->type = IS_OBJECT; - return_value->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC); - } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute statement: %s", PQerrorMessage(obj->intern->conn->intern->conn)); - } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); + 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); + + php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC); } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Statement not initialized"); } } - zend_restore_error_handling(&zeh TSRMLS_CC); } ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_exec_async, 0, 0, 0) @@ -2412,105 +3690,119 @@ static PHP_METHOD(pqstm, execAsync) { zend_error_handling zeh; zval *zparams = NULL; php_pq_callback_t resolver = {{0}}; + STATUS rv; zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!f", &zparams, &resolver.fci, &resolver.fcc)) { + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!f", &zparams, &resolver.fci, &resolver.fcc); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - if (obj->intern) { - if (obj->intern->conn->intern) { - int count; - char **params = NULL; - HashTable zdtor; + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Statement not initialized"); + } else { + int count; + char **params = NULL; + HashTable zdtor; - if (zparams) { - ZEND_INIT_SYMTABLE(&zdtor); - count = php_pq_params_to_array(Z_ARRVAL_P(zparams), ¶ms, &zdtor TSRMLS_CC); - } + if (zparams) { + ZEND_INIT_SYMTABLE(&zdtor); + count = php_pq_params_to_array(Z_ARRVAL_P(zparams), ¶ms, &zdtor TSRMLS_CC); + } + if (!PQsendQueryPrepared(obj->intern->conn->intern->conn, obj->intern->name, count, (const char *const*) params, NULL, NULL, 0)) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } else if (obj->intern->conn->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn->intern->conn)) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } else { php_pq_callback_dtor(&obj->intern->conn->intern->onevent); if (resolver.fci.size > 0) { obj->intern->conn->intern->onevent = resolver; php_pq_callback_addref(&obj->intern->conn->intern->onevent); } - obj->intern->conn->intern->poller = PQconsumeInput; + } - if (PQsendQueryPrepared(obj->intern->conn->intern->conn, obj->intern->name, count, (const char *const*) params, NULL, NULL, 0)) { - if (obj->intern->conn->intern->unbuffered) { - if (!PQsetSingleRowMode(obj->intern->conn->intern->conn)) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not enable unbuffered mode: %s", PQerrorMessage(obj->intern->conn->intern->conn)); - } - } - RETVAL_TRUE; - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute statement: %s", PQerrorMessage(obj->intern->conn->intern->conn)); - RETVAL_FALSE; - } - - if (params) { - efree(params); - } - if (zparams) { - zend_hash_destroy(&zdtor); - } - - php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC); - - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); - RETVAL_FALSE; + if (params) { + efree(params); } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Statement not initialized"); - RETVAL_FALSE; + if (zparams) { + zend_hash_destroy(&zdtor); + } + + php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC); } } - zend_restore_error_handling(&zeh TSRMLS_CC); } ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_desc, 0, 0, 0) ZEND_END_ARG_INFO(); static PHP_METHOD(pqstm, desc) { zend_error_handling zeh; + STATUS rv; zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); - if (SUCCESS == zend_parse_parameters_none()) { - php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + rv = zend_parse_parameters_none(); + zend_restore_error_handling(&zeh TSRMLS_CC); - if (obj->intern) { - if (obj->intern->conn->intern) { - PGresult *res = PQdescribePrepared(obj->intern->conn->intern->conn, obj->intern->name); + if (SUCCESS == rv) { + php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC); + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Statement not initialized"); + } else { + PGresult *res = PQdescribePrepared(obj->intern->conn->intern->conn, obj->intern->name); - if (res) { - if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { - int p, params; + if (!res) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to describe statement (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } else { + if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { + int p, params; - array_init(return_value); - for (p = 0, params = PQnparams(res); p < params; ++p) { - add_next_index_long(return_value, PQparamtype(res, p)); - } + array_init(return_value); + for (p = 0, params = PQnparams(res); p < params; ++p) { + add_next_index_long(return_value, PQparamtype(res, p)); } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not describe statement: %s", PQerrorMessage(obj->intern->conn->intern->conn)); } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); + PHP_PQclear(res); + php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC); } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Statement not initialized"); } } +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_desc_async, 0, 0, 0) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqstm, descAsync) { + zend_error_handling zeh; + STATUS rv; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + rv = zend_parse_parameters_none(); zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { + php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Statement not initialized"); + } else if (!PQsendDescribePrepared(obj->intern->conn->intern->conn, obj->intern->name)) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to describe statement: %s", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } else { + obj->intern->conn->intern->poller = PQconsumeInput; + php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC); + } + } } 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) + PHP_ME(pqstm, descAsync, ai_pqstm_desc_async, ZEND_ACC_PUBLIC) {0} }; @@ -2526,14 +3818,18 @@ static PHP_METHOD(pqtxn, __construct) { zval *zconn; long isolation = PHP_PQTXN_READ_COMMITTED; zend_bool async = 0, readonly = 0, deferrable = 0; + STATUS rv; zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|blbb", &zconn, php_pqconn_class_entry, &async, &isolation, &readonly, &deferrable)) { - STATUS rv; - php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|blbb", &zconn, php_pqconn_class_entry, &async, &isolation, &readonly, &deferrable); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC); - if (conn_obj->intern) { + if (!conn_obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized"); + } else { if (async) { rv = php_pqconn_start_transaction_async(zconn, conn_obj, isolation, readonly, deferrable TSRMLS_CC); } else { @@ -2541,69 +3837,470 @@ static PHP_METHOD(pqtxn, __construct) { } if (SUCCESS == rv) { + php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + obj->intern = ecalloc(1, sizeof(*obj->intern)); php_pq_object_addref(conn_obj TSRMLS_CC); obj->intern->conn = conn_obj; + obj->intern->open = 1; obj->intern->isolation = isolation; obj->intern->readonly = readonly; obj->intern->deferrable = deferrable; } + } + } +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_savepoint, 0, 0, 0) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqtxn, savepoint) { + zend_error_handling zeh; + STATUS rv; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + rv = zend_parse_parameters_none(); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { + php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transaction not initialized"); + } else if (!obj->intern->open) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transaction already closed"); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); + PGresult *res; + smart_str cmd = {0}; + + smart_str_appends(&cmd, "SAVEPOINT \""); + smart_str_append_unsigned(&cmd, ++obj->intern->savepoint); + smart_str_appends(&cmd, "\""); + smart_str_0(&cmd); + + res = PQexec(obj->intern->conn->intern->conn, cmd.c); + + if (!res) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to create %s (%s)", cmd.c, PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } else { + php_pqres_success(res TSRMLS_CC); + PHP_PQclear(res); + } + + smart_str_free(&cmd); } } +} + +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; + STATUS rv; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + rv = zend_parse_parameters_none(); zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { + php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transaction not initialized"); + } else if (!obj->intern->open) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transaction already closed"); + } else { + smart_str cmd = {0}; + + smart_str_appends(&cmd, "SAVEPOINT \""); + smart_str_append_unsigned(&cmd, ++obj->intern->savepoint); + smart_str_appends(&cmd, "\""); + smart_str_0(&cmd); + + if (!PQsendQuery(obj->intern->conn->intern->conn, cmd.c)) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to create %s (%s)", cmd.c, PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } + + smart_str_free(&cmd); + } + } } ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_commit, 0, 0, 0) ZEND_END_ARG_INFO(); static PHP_METHOD(pqtxn, commit) { zend_error_handling zeh; + STATUS rv; zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); - if (SUCCESS == zend_parse_parameters_none()) { + rv = zend_parse_parameters_none(); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - if (obj->intern) { + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transacation not initialized"); + } else if (!obj->intern->open) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transacation already closed"); + } else { + PGresult *res; + smart_str cmd = {0}; - if (obj->intern->conn->intern) { - PGresult *res = PQexec(obj->intern->conn->intern->conn, "COMMIT"); + if (!obj->intern->savepoint) { + res = PQexec(obj->intern->conn->intern->conn, "COMMIT"); + } else { + smart_str_appends(&cmd, "RELEASE SAVEPOINT \""); + smart_str_append_unsigned(&cmd, obj->intern->savepoint--); + smart_str_appends(&cmd, "\""); + smart_str_0(&cmd); - if (res) { - php_pqres_success(res TSRMLS_CC); - PQclear(res); - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not commit transaction: %s", PQerrorMessage(obj->intern->conn->intern->conn)); - } + res = PQexec(obj->intern->conn->intern->conn, cmd.c); + } + + if (!res) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to %s (%s)", cmd.c ? cmd.c : "commit transaction", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not intialized"); + if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { + if (!cmd.c) { + obj->intern->open = 0; + } + } + PHP_PQclear(res); } - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized"); + + smart_str_free(&cmd); + php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC); } } - zend_restore_error_handling(&zeh TSRMLS_CC); } ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_commit_async, 0, 0, 0) ZEND_END_ARG_INFO(); static PHP_METHOD(pqtxn, commitAsync) { zend_error_handling zeh; + STATUS rv; zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); - if (SUCCESS == zend_parse_parameters_none()) { + rv = zend_parse_parameters_none(); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - if (obj->intern) { - if (obj->intern->conn->intern) { + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transaction not initialized"); + } else if (!obj->intern->open) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transaction already closed"); + } else { + int rc; + smart_str cmd = {0}; + + if (!obj->intern->savepoint) { + rc = PQsendQuery(obj->intern->conn->intern->conn, "COMMIT"); + } else { + smart_str_appends(&cmd, "RELEASE SAVEPOINT \""); + smart_str_append_unsigned(&cmd, obj->intern->savepoint--); + smart_str_appends(&cmd, "\""); + smart_str_0(&cmd); + + rc = PQsendQuery(obj->intern->conn->intern->conn, cmd.c); + } + + if (!rc) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to %s (%s)", cmd.c ? cmd.c : "commmit transaction", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } else { + if (!cmd.c) { + obj->intern->open = 0; + } obj->intern->conn->intern->poller = PQconsumeInput; + php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC); + } - if (!PQsendQuery(obj->intern->conn->intern->conn, "COMMIT")) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not commit transaction: %s", PQerrorMessage(obj->intern->conn->intern->conn)); + smart_str_free(&cmd); + } + } +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_rollback, 0, 0, 0) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqtxn, rollback) { + zend_error_handling zeh; + STATUS rv; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + rv = zend_parse_parameters_none(); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { + php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transaction not initialized"); + } else if (!obj->intern->open) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transaction already closed"); + } else { + PGresult *res; + smart_str cmd = {0}; + + if (!obj->intern->savepoint) { + res = PQexec(obj->intern->conn->intern->conn, "ROLLBACK"); + } else { + smart_str_appends(&cmd, "ROLLBACK TO SAVEPOINT \""); + smart_str_append_unsigned(&cmd, obj->intern->savepoint--); + smart_str_appends(&cmd, "\""); + smart_str_0(&cmd); + + res = PQexec(obj->intern->conn->intern->conn, cmd.c); + } + + if (!res) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to %s (%s)", cmd.c ? cmd.c : "rollback transaction", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } else { + if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { + if (!cmd.c) { + obj->intern->open = 0; + } } + PHP_PQclear(res); + } + + smart_str_free(&cmd); + php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC); + } + } +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_rollback_async, 0, 0, 0) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqtxn, rollbackAsync) { + zend_error_handling zeh; + STATUS rv; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + rv = zend_parse_parameters_none(); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { + php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transaction not initialized"); + } else if (!obj->intern->open) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transaction already closed"); + } else { + int rc; + smart_str cmd = {0}; + + if (!obj->intern->savepoint) { + rc = PQsendQuery(obj->intern->conn->intern->conn, "ROLLBACK"); + } else { + smart_str_appends(&cmd, "ROLLBACK TO SAVEPOINT \""); + smart_str_append_unsigned(&cmd, obj->intern->savepoint--); + smart_str_appends(&cmd, "\""); + smart_str_0(&cmd); + + rc = PQsendQuery(obj->intern->conn->intern->conn, cmd.c); + } + + if (!rc) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to %s (%s)", cmd.c ? cmd.c : "rollback transaction", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not intialized"); + if (!cmd.c) { + obj->intern->open = 0; + } + obj->intern->conn->intern->poller = PQconsumeInput; + } + + smart_str_free(&cmd); + php_pqconn_notify_listeners(obj->intern->conn 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; + STATUS rv; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + rv = zend_parse_parameters_none(); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { + php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transaction not initialized"); + } else { + PGresult *res = PQexec(obj->intern->conn->intern->conn, "SELECT pg_export_snapshot()"); + + if (!res) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to export transaction snapshot (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } else { + if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { + RETVAL_STRING(PQgetvalue(res, 0, 0), 1); + } + + PHP_PQclear(res); + } + + php_pqconn_notify_listeners(obj->intern->conn 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; + STATUS rv; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + rv = zend_parse_parameters_none(); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { + php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transaction not initialized"); + } else if (!PQsendQuery(obj->intern->conn->intern->conn, "SELECT pg_export_snapshot()")) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to export transaction snapshot (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } else { + obj->intern->conn->intern->poller = PQconsumeInput; + php_pqconn_notify_listeners(obj->intern->conn 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; + STATUS rv; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &snapshot_str, &snapshot_len); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { + php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transaction not initialized"); + } else if (obj->intern->isolation < PHP_PQTXN_REPEATABLE_READ) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transaction must have at least isolation level REPEATABLE READ to be able to import a snapshot"); + } else { + char *sid = PQescapeLiteral(obj->intern->conn->intern->conn, snapshot_str, snapshot_len); + + if (!sid) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to quote snapshot identifier (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } else { + PGresult *res; + smart_str cmd = {0}; + + smart_str_appends(&cmd, "SET TRANSACTION SNAPSHOT "); + smart_str_appends(&cmd, sid); + smart_str_0(&cmd); + + res = PQexec(obj->intern->conn->intern->conn, cmd.c); + + if (!res) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to import transaction snapshot (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } else { + php_pqres_success(res TSRMLS_CC); + PHP_PQclear(res); + } + + smart_str_free(&cmd); + php_pqconn_notify_listeners(obj->intern->conn 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; + STATUS rv; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &snapshot_str, &snapshot_len); + zend_restore_error_handling(&zeh TSRMLS_CC); + + if (SUCCESS == rv) { + php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (!obj->intern) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transaction not initialized"); + } else if (obj->intern->isolation < PHP_PQTXN_REPEATABLE_READ) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Transaction must have at least isolation level REPEATABLE READ to be able to import a snapshot"); + } else { + char *sid = PQescapeLiteral(obj->intern->conn->intern->conn, snapshot_str, snapshot_len); + + if (!sid) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to quote snapshot identifier (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } else { + smart_str cmd = {0}; + + smart_str_appends(&cmd, "SET TRANSACTION SNAPSHOT "); + smart_str_appends(&cmd, sid); + smart_str_0(&cmd); + + if (!PQsendQuery(obj->intern->conn->intern->conn, cmd.c)) { + zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to %s (%s)", cmd.c, PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } else { + obj->intern->conn->intern->poller = PQconsumeInput; + } + + smart_str_free(&cmd); + php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC); + } + } + } +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_open_lob, 0, 0, 1) + ZEND_ARG_INFO(0, oid) + ZEND_ARG_INFO(0, mode) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqtxn, openLOB) { + zend_error_handling zeh; + long mode = INV_WRITE|INV_READ, loid; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &loid, &mode)) { + php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + int lofd = lo_open(obj->intern->conn->intern->conn, loid, mode); + + if (lofd >= 0) { + php_pqlob_t *lob = ecalloc(1, sizeof(*lob)); + + lob->lofd = lofd; + lob->loid = loid; + php_pq_object_addref(obj TSRMLS_CC); + lob->txn = obj; + + return_value->type = IS_OBJECT; + return_value->value.obj = php_pqlob_create_object_ex(php_pqlob_class_entry, lob, NULL TSRMLS_CC); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to open large object with mode '%s' (%s)", + (mode & (INV_READ|INV_WRITE) ? "rw" : + (mode & INV_READ ? "r" : + (mode & INV_WRITE ? "w" : "-"))), + PHP_PQerrorMessage(obj->intern->conn->intern->conn) + ); } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized"); @@ -2612,27 +4309,47 @@ static PHP_METHOD(pqtxn, commitAsync) { zend_restore_error_handling(&zeh TSRMLS_CC); } -ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_rollback, 0, 0, 0) +ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_create_lob, 0, 0, 0) + ZEND_ARG_INFO(0, mode) ZEND_END_ARG_INFO(); -static PHP_METHOD(pqtxn, rollback) { +static PHP_METHOD(pqtxn, createLOB) { zend_error_handling zeh; + long mode = INV_WRITE|INV_READ; zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); - if (SUCCESS == zend_parse_parameters_none()) { + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &mode)) { 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"); + Oid loid = lo_creat(obj->intern->conn->intern->conn, mode); - if (res) { - php_pqres_success(res TSRMLS_CC); - PQclear(res); + if (loid != InvalidOid) { + int lofd = lo_open(obj->intern->conn->intern->conn, loid, mode); + + if (lofd >= 0) { + php_pqlob_t *lob = ecalloc(1, sizeof(*lob)); + lob->lofd = lofd; + lob->loid = loid; + php_pq_object_addref(obj TSRMLS_CC); + lob->txn = obj; + + return_value->type = IS_OBJECT; + return_value->value.obj = php_pqlob_create_object_ex(php_pqlob_class_entry, lob, NULL TSRMLS_CC); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not rollback transaction: %s", PQerrorMessage(obj->intern->conn->intern->conn)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to open large object with mode '%s': %s", + (mode & (INV_READ|INV_WRITE) ? "rw" : + (mode & INV_READ ? "r" : + (mode & INV_WRITE ? "w" : "-"))), + PHP_PQerrorMessage(obj->intern->conn->intern->conn) + ); } } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not intialized"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create large object with mode '%s': %s", + (mode & (INV_READ|INV_WRITE) ? "rw" : + (mode & INV_READ ? "r" : + (mode & INV_WRITE ? "w" : "-"))), + PHP_PQerrorMessage(obj->intern->conn->intern->conn) + ); } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized"); @@ -2641,29 +4358,27 @@ static PHP_METHOD(pqtxn, rollback) { zend_restore_error_handling(&zeh TSRMLS_CC); } -ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_rollback_async, 0, 0, 0) +ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_unlink_lob, 0, 0, 1) + ZEND_ARG_INFO(0, oid) ZEND_END_ARG_INFO(); -static PHP_METHOD(pqtxn, rollbackAsync) { - zend_error_handling zeh; +static PHP_METHOD(pqtxn, unlinkLOB) { + long loid; - zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); - if (SUCCESS == zend_parse_parameters_none()) { + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &loid)) { 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")) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not rollback transaction: %s", PQerrorMessage(obj->intern->conn->intern->conn)); - } + if (1 == lo_unlink(obj->intern->conn->intern->conn, loid)) { + RETVAL_TRUE; } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not intialized"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to unlink LOB (oid=%ld): %s", loid, PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + RETVAL_FALSE; } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized"); + RETVAL_FALSE; } } - zend_restore_error_handling(&zeh TSRMLS_CC); } static zend_function_entry php_pqtxn_methods[] = { @@ -2672,6 +4387,15 @@ 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) {0} }; @@ -2697,7 +4421,7 @@ static PHP_METHOD(pqcancel, __construct) { php_pq_object_addref(conn_obj TSRMLS_CC); obj->intern->conn = conn_obj; } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not acquire cancel: %s", PQerrorMessage(conn_obj->intern->conn)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to acquire cancel (%s)", PHP_PQerrorMessage(conn_obj->intern->conn)); } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized"); @@ -2719,7 +4443,7 @@ static PHP_METHOD(pqcancel, cancel) { char err[256]; if (!PQcancel(obj->intern->cancel, err, sizeof(err))) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not request cancellation: %s", err); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to request cancellation: %s", err); } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Cancel not initialized"); @@ -2819,6 +4543,402 @@ static zend_function_entry php_pqevent_methods[] = { {0} }; +ZEND_BEGIN_ARG_INFO_EX(ai_pqlob_construct, 0, 0, 1) + ZEND_ARG_OBJ_INFO(0, transaction, pq\\Transaction, 0) + ZEND_ARG_INFO(0, oid) + ZEND_ARG_INFO(0, mode) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqlob, __construct) { + zend_error_handling zeh; + zval *ztxn; + long mode = INV_WRITE|INV_READ, loid = InvalidOid; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|ll", &ztxn, php_pqtxn_class_entry, &loid, &mode)) { + php_pqtxn_object_t *txn_obj = zend_object_store_get_object(ztxn TSRMLS_CC); + + if (txn_obj->intern) { + + if (loid == InvalidOid) { + loid = lo_creat(txn_obj->intern->conn->intern->conn, mode); + } + + if (loid != InvalidOid) { + int lofd = lo_open(txn_obj->intern->conn->intern->conn, loid, mode); + + if (lofd >= 0) { + php_pqlob_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + obj->intern = ecalloc(1, sizeof(*obj->intern)); + obj->intern->lofd = lofd; + obj->intern->loid = loid; + php_pq_object_addref(txn_obj TSRMLS_CC); + obj->intern->txn = txn_obj; + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to open large object with mode '%s' (%s)", + (mode & (INV_READ|INV_WRITE) ? "rw" : + (mode & INV_READ ? "r" : + (mode & INV_WRITE ? "w" : "-"))), + PHP_PQerrorMessage(txn_obj->intern->conn->intern->conn) + ); + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create large object with mode '%s' (%s)", + (mode & (INV_READ|INV_WRITE) ? "rw" : + (mode & INV_READ ? "r" : + (mode & INV_WRITE ? "w" : "-"))), + PHP_PQerrorMessage(txn_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_pqlob_write, 0, 0, 1) + ZEND_ARG_INFO(0, data) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqlob, write) { + char *data_str; + int data_len; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data_str, &data_len)) { + php_pqlob_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + int written = lo_write(obj->intern->txn->intern->conn->intern->conn, obj->intern->lofd, data_str, data_len); + + if (written >= 0) { + RETVAL_LONG(written); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write to LOB, oid=%d (%s)", obj->intern->loid, + PHP_PQerrorMessage(obj->intern->txn->intern->conn->intern->conn)); + RETVAL_FALSE; + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\LOB not initialized"); + RETVAL_FALSE; + } + } +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqlob_read, 0, 0, 0) + ZEND_ARG_INFO(0, length) + ZEND_ARG_INFO(1, read) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqlob, read) { + long length = 0x1000; + zval *zread = NULL; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lz!", &length, &zread)) { + php_pqlob_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + char *buffer = emalloc(length + 1); + int read = lo_read(obj->intern->txn->intern->conn->intern->conn, obj->intern->lofd, buffer, length); + + if (read >= 0) { + if (zread) { + zval_dtor(zread); + ZVAL_LONG(zread, read); + } + buffer[read] = '\0'; + RETVAL_STRINGL(buffer, read, 0); + } else { + efree(buffer); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to read from LOB, oid=%d (%s)", obj->intern->loid, + PHP_PQerrorMessage(obj->intern->txn->intern->conn->intern->conn)); + RETVAL_FALSE; + } + + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\LOB not initialized"); + RETVAL_FALSE; + } + } +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqlob_seek, 0, 0, 1) + ZEND_ARG_INFO(0, offset) + ZEND_ARG_INFO(0, whence) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqlob, seek) { + long offset, whence = SEEK_SET; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &offset, &whence)) { + php_pqlob_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + int position = lo_lseek(obj->intern->txn->intern->conn->intern->conn, obj->intern->lofd, offset, whence); + + if (position >= 0) { + RETVAL_LONG(position); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to seek offset in LOB, oid=%d (%s)", obj->intern->loid, + PHP_PQerrorMessage(obj->intern->txn->intern->conn->intern->conn)); + RETVAL_FALSE; + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\LOB not initialized"); + RETVAL_FALSE; + } + } +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqlob_tell, 0, 0, 0) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqlob, tell) { + if (SUCCESS == zend_parse_parameters_none()) { + php_pqlob_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + int position = lo_tell(obj->intern->txn->intern->conn->intern->conn, obj->intern->lofd); + + if (position >= 0) { + RETVAL_LONG(position); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to tell offset in LOB (oid=%d): %s", obj->intern->loid, + PHP_PQerrorMessage(obj->intern->txn->intern->conn->intern->conn)); + RETVAL_FALSE; + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\LOB not initialized"); + RETVAL_FALSE; + } + } +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqlob_truncate, 0, 0, 0) + ZEND_ARG_INFO(0, length) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqlob, truncate) { + long length = 0; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &length)) { + php_pqlob_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + if (0 == lo_truncate(obj->intern->txn->intern->conn->intern->conn, obj->intern->lofd, length)) { + RETVAL_TRUE; + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to truncate LOB (oid=%d): %s", obj->intern->loid, + PHP_PQerrorMessage(obj->intern->txn->intern->conn->intern->conn)); + RETVAL_FALSE; + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\LOB not initialized"); + RETVAL_FALSE; + } + } +} + +static zend_function_entry php_pqlob_methods[] = { + PHP_ME(pqlob, __construct, ai_pqlob_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) + PHP_ME(pqlob, write, ai_pqlob_write, ZEND_ACC_PUBLIC) + PHP_ME(pqlob, read, ai_pqlob_read, ZEND_ACC_PUBLIC) + PHP_ME(pqlob, seek, ai_pqlob_seek, ZEND_ACC_PUBLIC) + PHP_ME(pqlob, tell, ai_pqlob_tell, ZEND_ACC_PUBLIC) + PHP_ME(pqlob, truncate, ai_pqlob_truncate, ZEND_ACC_PUBLIC) + {0} +}; + +ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_construct, 0, 0, 3) + ZEND_ARG_OBJ_INFO(0, "connection", pq\\Connection, 0) + ZEND_ARG_INFO(0, expression) + ZEND_ARG_INFO(0, direction) + ZEND_ARG_INFO(0, options) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqcopy, __construct) { + zend_error_handling zeh; + zval *zconn; + char *expr_str, *opt_str = ""; + int expr_len, opt_len = 0; + long direction; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Osl|s", &zconn, php_pqconn_class_entry, &expr_str, &expr_len, &direction, &opt_str, &opt_len)) { + php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC); + + if (conn_obj->intern) { + char *copy = NULL; + + switch (direction) { + case PHP_PQCOPY_FROM_STDIN: + spprintf(©, 0, "COPY %s %s %s", expr_str, "FROM STDIN", opt_str); + break; + + case PHP_PQCOPY_TO_STDOUT: + spprintf(©, 0, "COPY %s %s %s", expr_str, "TO STDOUT", opt_str); + break; + + default: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid COPY direction, expected one of FROM_STDIN (%d) TO_STDOUT (%d), got %ld", + PHP_PQCOPY_FROM_STDIN, PHP_PQCOPY_TO_STDOUT, direction); + break; + } + + if (copy) { + php_pqcopy_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + PGresult *res = PQexec(conn_obj->intern->conn, copy); + + efree(copy); + + if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { + obj->intern = ecalloc(1, sizeof(*obj->intern)); + obj->intern->direction = direction; + obj->intern->expression = estrdup(expr_str); + obj->intern->options = estrdup(opt_str); + obj->intern->conn = conn_obj; + php_pq_object_addref(conn_obj TSRMLS_CC); + } + + PHP_PQclear(res); + } + } 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_pqcopy_put, 0, 0, 1) + ZEND_ARG_INFO(0, data) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqcopy, put) { + zend_error_handling zeh; + char *data_str; + int data_len; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data_str, &data_len)) { + php_pqcopy_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + if (PHP_PQCOPY_FROM_STDIN == obj->intern->direction) { + if (1 == PQputCopyData(obj->intern->conn->intern->conn, data_str, data_len)) { + RETVAL_TRUE; + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to send COPY data (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\COPY was not initialized with FROM_STDIN"); + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\COPY not initialized"); + } + } + zend_restore_error_handling(&zeh TSRMLS_CC); +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_end, 0, 0, 0) + ZEND_ARG_INFO(0, error) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqcopy, end) { + zend_error_handling zeh; + char *error_str = NULL; + int error_len = 0; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &error_str, &error_len)) { + php_pqcopy_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->intern) { + if (PHP_PQCOPY_FROM_STDIN == obj->intern->direction) { + if (1 == PQputCopyEnd(obj->intern->conn->intern->conn, error_str)) { + PGresult *res = PQgetResult(obj->intern->conn->intern->conn); + + if (res) { + if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { + RETVAL_TRUE; + } + + PHP_PQclear(res); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to get COPY result (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to end COPY (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\COPY was not initialized with FROM_STDIN"); + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\COPY not initialized"); + } + } + zend_restore_error_handling(&zeh TSRMLS_CC); +} + +ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_get, 0, 0, 1) + ZEND_ARG_INFO(1, data) +ZEND_END_ARG_INFO(); +static PHP_METHOD(pqcopy, get) { + zend_error_handling zeh; + zval *zdata; + + zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata)) { + php_pqcopy_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + char *buffer = NULL; + + if (obj->intern) { + if (PHP_PQCOPY_TO_STDOUT == obj->intern->direction) { + PGresult *res; + int bytes = PQgetCopyData(obj->intern->conn->intern->conn, &buffer, 0); + + switch (bytes) { + case -1: + res = PQgetResult(obj->intern->conn->intern->conn); + + if (res) { + if (SUCCESS == php_pqres_success(res TSRMLS_CC)) { + RETVAL_FALSE; + } + + PHP_PQclear(res); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to get COPY result (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + } + break; + + case -2: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to get COPY data (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn)); + break; + + default: + zval_dtor(zdata); + if (buffer) { + ZVAL_STRINGL(zdata, buffer, bytes, 1); + } else { + ZVAL_EMPTY_STRING(zdata); + } + RETVAL_TRUE; + break; + } + + if (buffer) { + PQfreemem(buffer); + } + } + } + } + zend_restore_error_handling(&zeh TSRMLS_CC); +} + +static zend_function_entry php_pqcopy_methods[] = { + PHP_ME(pqcopy, __construct, ai_pqcopy_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) + PHP_ME(pqcopy, put, ai_pqcopy_put, ZEND_ACC_PUBLIC) + PHP_ME(pqcopy, end, ai_pqcopy_end, ZEND_ACC_PUBLIC) + PHP_ME(pqcopy, get, ai_pqcopy_get, ZEND_ACC_PUBLIC) + {0} +}; + +static zend_function_entry php_pqexc_methods[] = { + {0} +}; + /* {{{ PHP_MINIT_FUNCTION */ static PHP_MINIT_FUNCTION(pq) @@ -2826,6 +4946,30 @@ static PHP_MINIT_FUNCTION(pq) zend_class_entry ce = {0}; php_pq_object_prophandler_t ph = {0}; + INIT_NS_CLASS_ENTRY(ce, "pq", "Exception", php_pqexc_methods); + php_pqexc_interface_class_entry = zend_register_internal_interface(&ce TSRMLS_CC); + + memset(&ce, 0, sizeof(ce)); + INIT_NS_CLASS_ENTRY(ce, "pq\\Exception", "Exception", php_pqexc_methods); + php_pqexc_default_class_entry = zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C), "Exception" TSRMLS_CC); + zend_class_implements(php_pqexc_default_class_entry TSRMLS_CC, 1, php_pqexc_interface_class_entry); + + memset(&ce, 0, sizeof(ce)); + INIT_NS_CLASS_ENTRY(ce, "pq\\Exception", "InvalidArgument", php_pqexc_methods); + php_pqexc_invalid_argument_class_entry = zend_register_internal_class_ex(&ce, spl_ce_InvalidArgumentException, "InvalidArgumentException" TSRMLS_CC); + zend_class_implements(php_pqexc_invalid_argument_class_entry TSRMLS_CC, 1, php_pqexc_interface_class_entry); + + memset(&ce, 0, sizeof(ce)); + INIT_NS_CLASS_ENTRY(ce, "pq\\Exception", "Runtime", php_pqexc_methods); + php_pqexc_runtime_class_entry = zend_register_internal_class_ex(&ce, spl_ce_RuntimeException, "RuntimeException" TSRMLS_CC); + zend_class_implements(php_pqexc_runtime_class_entry TSRMLS_CC, 1, php_pqexc_interface_class_entry); + + memset(&ce, 0, sizeof(ce)); + INIT_NS_CLASS_ENTRY(ce, "pq\\Exception", "BadMethodCall", php_pqexc_methods); + php_pqexc_bad_methodcall_class_entry = zend_register_internal_class_ex(&ce, spl_ce_BadMethodCallException, "BadMethodCallException" TSRMLS_CC); + zend_class_implements(php_pqexc_bad_methodcall_class_entry TSRMLS_CC, 1, php_pqexc_interface_class_entry); + + memset(&ce, 0, sizeof(ce)); INIT_NS_CLASS_ENTRY(ce, "pq", "Connection", php_pqconn_methods); php_pqconn_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC); php_pqconn_class_entry->create_object = php_pqconn_create_object; @@ -2837,7 +4981,7 @@ static PHP_MINIT_FUNCTION(pq) php_pqconn_object_handlers.get_property_ptr_ptr = NULL; php_pqconn_object_handlers.get_debug_info = php_pq_object_debug_info; - zend_hash_init(&php_pqconn_object_prophandlers, 8, NULL, NULL, 1); + zend_hash_init(&php_pqconn_object_prophandlers, 13, NULL, NULL, 1); zend_declare_property_long(php_pqconn_class_entry, ZEND_STRL("status"), CONNECTION_BAD, ZEND_ACC_PUBLIC TSRMLS_CC); ph.read = php_pqconn_object_read_status; @@ -2855,10 +4999,6 @@ static PHP_MINIT_FUNCTION(pq) ph.read = php_pqconn_object_read_error_message; zend_hash_add(&php_pqconn_object_prophandlers, "errorMessage", sizeof("errorMessage"), (void *) &ph, sizeof(ph), NULL); - zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("types"), ZEND_ACC_PUBLIC TSRMLS_CC); - ph.read = php_pqconn_object_read_types; - zend_hash_add(&php_pqconn_object_prophandlers, "types", sizeof("types"), (void *) &ph, sizeof(ph), NULL); - zend_declare_property_bool(php_pqconn_class_entry, ZEND_STRL("busy"), 0, ZEND_ACC_PUBLIC TSRMLS_CC); ph.read = php_pqconn_object_read_busy; zend_hash_add(&php_pqconn_object_prophandlers, "busy", sizeof("busy"), (void *) &ph, sizeof(ph), NULL); @@ -2875,6 +5015,30 @@ static PHP_MINIT_FUNCTION(pq) zend_hash_add(&php_pqconn_object_prophandlers, "unbuffered", sizeof("unbuffered"), (void *) &ph, sizeof(ph), NULL); ph.write = NULL; + zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("db"), ZEND_ACC_PUBLIC TSRMLS_CC); + ph.read = php_pqconn_object_read_db; + zend_hash_add(&php_pqconn_object_prophandlers, "db", sizeof("db"), (void *) &ph, sizeof(ph), NULL); + + zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("user"), ZEND_ACC_PUBLIC TSRMLS_CC); + ph.read = php_pqconn_object_read_user; + zend_hash_add(&php_pqconn_object_prophandlers, "user", sizeof("user"), (void *) &ph, sizeof(ph), NULL); + + zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("pass"), ZEND_ACC_PUBLIC TSRMLS_CC); + ph.read = php_pqconn_object_read_pass; + zend_hash_add(&php_pqconn_object_prophandlers, "pass", sizeof("pass"), (void *) &ph, sizeof(ph), NULL); + + zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("host"), ZEND_ACC_PUBLIC TSRMLS_CC); + ph.read = php_pqconn_object_read_host; + zend_hash_add(&php_pqconn_object_prophandlers, "host", sizeof("host"), (void *) &ph, sizeof(ph), NULL); + + zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("port"), ZEND_ACC_PUBLIC TSRMLS_CC); + ph.read = php_pqconn_object_read_port; + zend_hash_add(&php_pqconn_object_prophandlers, "port", sizeof("port"), (void *) &ph, sizeof(ph), NULL); + + zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("options"), ZEND_ACC_PUBLIC TSRMLS_CC); + ph.read = php_pqconn_object_read_options; + zend_hash_add(&php_pqconn_object_prophandlers, "options", sizeof("options"), (void *) &ph, sizeof(ph), NULL); + zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("OK"), CONNECTION_OK TSRMLS_CC); zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("BAD"), CONNECTION_BAD TSRMLS_CC); zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("STARTED"), CONNECTION_STARTED TSRMLS_CC); @@ -2895,13 +5059,37 @@ 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); + php_pqtypes_class_entry->create_object = php_pqtypes_create_object; + + memcpy(&php_pqtypes_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); + php_pqtypes_object_handlers.read_property = php_pq_object_read_prop; + php_pqtypes_object_handlers.write_property = php_pq_object_write_prop; + php_pqtypes_object_handlers.clone_obj = NULL; + php_pqtypes_object_handlers.get_property_ptr_ptr = NULL; + php_pqtypes_object_handlers.get_debug_info = php_pq_object_debug_info; + php_pqtypes_object_handlers.has_dimension = php_pqtypes_object_has_dimension; + php_pqtypes_object_handlers.read_dimension = php_pqtypes_object_read_dimension; + php_pqtypes_object_handlers.unset_dimension = NULL; + php_pqtypes_object_handlers.write_dimension = NULL; + + zend_hash_init(&php_pqtypes_object_prophandlers, 1, NULL, NULL, 1); + + zend_declare_property_null(php_pqtypes_class_entry, ZEND_STRL("connection"), ZEND_ACC_PUBLIC TSRMLS_CC); + ph.read = php_pqtypes_object_read_connection; + zend_hash_add(&php_pqtypes_object_prophandlers, "connection", sizeof("connection"), (void *) &ph, sizeof(ph), NULL); + memset(&ce, 0, sizeof(ce)); INIT_NS_CLASS_ENTRY(ce, "pq", "Result", php_pqres_methods); php_pqres_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC); 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; @@ -2909,6 +5097,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); @@ -2916,6 +5105,10 @@ static PHP_MINIT_FUNCTION(pq) ph.read = php_pqres_object_read_status; zend_hash_add(&php_pqres_object_prophandlers, "status", sizeof("status"), (void *) &ph, sizeof(ph), NULL); + zend_declare_property_null(php_pqres_class_entry, ZEND_STRL("statusMessage"), ZEND_ACC_PUBLIC TSRMLS_CC); + ph.read = php_pqres_object_read_status_message; + zend_hash_add(&php_pqres_object_prophandlers, "statusMessage", sizeof("statusMessage"), (void *) &ph, sizeof(ph), NULL); + zend_declare_property_null(php_pqres_class_entry, ZEND_STRL("errorMessage"), ZEND_ACC_PUBLIC TSRMLS_CC); ph.read = php_pqres_object_read_error_message; zend_hash_add(&php_pqres_object_prophandlers, "errorMessage", sizeof("errorMessage"), (void *) &ph, sizeof(ph), NULL); @@ -3054,6 +5247,70 @@ static PHP_MINIT_FUNCTION(pq) zend_hash_add(&php_pqevent_object_prophandlers, "type", sizeof("type"), (void *) &ph, sizeof(ph), NULL); zend_declare_class_constant_stringl(php_pqevent_class_entry, ZEND_STRL("NOTICE"), ZEND_STRL("notice") TSRMLS_CC); + zend_declare_class_constant_stringl(php_pqevent_class_entry, ZEND_STRL("RESULT"), ZEND_STRL("result") TSRMLS_CC); + zend_declare_class_constant_stringl(php_pqevent_class_entry, ZEND_STRL("RESET"), ZEND_STRL("reset") TSRMLS_CC); + + memset(&ce, 0, sizeof(ce)); + INIT_NS_CLASS_ENTRY(ce, "pq", "LOB", php_pqlob_methods); + php_pqlob_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC); + php_pqlob_class_entry->create_object = php_pqlob_create_object; + + memcpy(&php_pqlob_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); + php_pqlob_object_handlers.read_property = php_pq_object_read_prop; + php_pqlob_object_handlers.write_property = php_pq_object_write_prop; + php_pqlob_object_handlers.clone_obj = NULL; + php_pqlob_object_handlers.get_property_ptr_ptr = NULL; + php_pqlob_object_handlers.get_debug_info = php_pq_object_debug_info; + + zend_hash_init(&php_pqlob_object_prophandlers, 2, NULL, NULL, 1); + + zend_declare_property_null(php_pqlob_class_entry, ZEND_STRL("transaction"), ZEND_ACC_PUBLIC TSRMLS_CC); + ph.read = php_pqlob_object_read_transaction; + zend_hash_add(&php_pqlob_object_prophandlers, "transaction", sizeof("transaction"), (void *) &ph, sizeof(ph), NULL); + + zend_declare_property_long(php_pqlob_class_entry, ZEND_STRL("oid"), InvalidOid, ZEND_ACC_PUBLIC TSRMLS_CC); + ph.read = php_pqlob_object_read_oid; + zend_hash_add(&php_pqlob_object_prophandlers, "oid", sizeof("oid"), (void *) &ph, sizeof(ph), NULL); + + zend_declare_class_constant_long(php_pqlob_class_entry, ZEND_STRL("INVALID_OID"), InvalidOid TSRMLS_CC); + zend_declare_class_constant_long(php_pqlob_class_entry, ZEND_STRL("R"), INV_READ TSRMLS_CC); + zend_declare_class_constant_long(php_pqlob_class_entry, ZEND_STRL("W"), INV_WRITE TSRMLS_CC); + zend_declare_class_constant_long(php_pqlob_class_entry, ZEND_STRL("RW"), INV_READ|INV_WRITE TSRMLS_CC); + + memset(&ce, 0, sizeof(ce)); + INIT_NS_CLASS_ENTRY(ce, "pq", "COPY", php_pqcopy_methods); + php_pqcopy_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC); + php_pqcopy_class_entry->create_object = php_pqcopy_create_object; + + memcpy(&php_pqcopy_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); + php_pqcopy_object_handlers.read_property = php_pq_object_read_prop; + php_pqcopy_object_handlers.write_property = php_pq_object_write_prop; + php_pqcopy_object_handlers.clone_obj = NULL; + php_pqcopy_object_handlers.get_property_ptr_ptr = NULL; + php_pqcopy_object_handlers.get_debug_info = php_pq_object_debug_info; + + zend_hash_init(&php_pqcopy_object_prophandlers, 4, NULL, NULL, 1); + + zend_declare_property_null(php_pqcopy_class_entry, ZEND_STRL("connection"), ZEND_ACC_PUBLIC TSRMLS_CC); + ph.read = php_pqcopy_object_read_connection; + zend_hash_add(&php_pqcopy_object_prophandlers, "connection", sizeof("connection"), (void *) &ph, sizeof(ph), NULL); + + zend_declare_property_null(php_pqcopy_class_entry, ZEND_STRL("expression"), ZEND_ACC_PUBLIC TSRMLS_CC); + ph.read = php_pqcopy_object_read_expression; + zend_hash_add(&php_pqcopy_object_prophandlers, "expression", sizeof("expression"), (void *) &ph, sizeof(ph), NULL); + + zend_declare_property_null(php_pqcopy_class_entry, ZEND_STRL("direction"), ZEND_ACC_PUBLIC TSRMLS_CC); + ph.read = php_pqcopy_object_read_direction; + zend_hash_add(&php_pqcopy_object_prophandlers, "direction", sizeof("direction"), (void *) &ph, sizeof(ph), NULL); + + zend_declare_property_null(php_pqcopy_class_entry, ZEND_STRL("options"), ZEND_ACC_PUBLIC TSRMLS_CC); + ph.read = php_pqcopy_object_read_options; + zend_hash_add(&php_pqcopy_object_prophandlers, "options", sizeof("options"), (void *) &ph, sizeof(ph), NULL); + + 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 TSRMLS_CC); /* REGISTER_INI_ENTRIES(); @@ -3069,6 +5326,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; } /* }}} */ @@ -3077,8 +5335,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_row(2, "Extension Version", PHP_PQ_EXT_VERSION); + php_info_print_table_end(); + php_info_print_table_start(); - php_info_print_table_header(2, "pq support", "enabled"); + 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 @@ -3091,10 +5364,18 @@ const zend_function_entry pq_functions[] = { {0} }; +static zend_module_dep pq_module_deps[] = { + ZEND_MOD_REQUIRED("raphf") + ZEND_MOD_REQUIRED("spl") + 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),