X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-pq;a=blobdiff_plain;f=src%2Fphp_pqconn.c;h=0527a7bb43ac85a133a749988fa6466408671c60;hp=2588d42f0fb09d9739f072bce180503b7630bb70;hb=d662d7beb20a15ca8ca95761543f6b9d3b6bff29;hpb=359b3471d6ed843d8ecf9d5fb0b85f06af446800 diff --git a/src/php_pqconn.c b/src/php_pqconn.c index 2588d42..0527a7b 100644 --- a/src/php_pqconn.c +++ b/src/php_pqconn.c @@ -219,7 +219,7 @@ static void php_pqconn_object_write_encoding(zval *object, void *o, zval *value } if (0 > PQsetClientEncoding(obj->intern->conn, Z_STRVAL_P(zenc))) { - zend_error(E_NOTICE, "Unrecognized encoding '%s'", Z_STRVAL_P(zenc)); + php_error(E_NOTICE, "Unrecognized encoding '%s'", Z_STRVAL_P(zenc)); } if (zenc != value) { @@ -372,7 +372,7 @@ static void php_pqconn_object_write_def_fetch_type(zval *object, void *o, zval * } } - obj->intern->default_fetch_type = Z_LVAL_P(zft) & 0x2; /* two bits only */ + obj->intern->default_fetch_type = Z_LVAL_P(zft) & 0x3; /* two bits only */ if (zft != value) { zval_ptr_dtor(&zft); @@ -402,7 +402,7 @@ static void php_pqconn_object_write_def_txn_isolation(zval *object, void *o, zva } } - obj->intern->default_txn_isolation = Z_LVAL_P(zti) & 0x2; /* two bits only */ + obj->intern->default_txn_isolation = Z_LVAL_P(zti) & 0x3; /* two bits only */ if (zti != value) { zval_ptr_dtor(&zti); @@ -458,7 +458,7 @@ static void php_pqconn_object_write_def_auto_conv(zval*object, void *o, zval *va } } - obj->intern->default_auto_convert = Z_LVAL_P(zac) & 0xff; + obj->intern->default_auto_convert = Z_LVAL_P(zac) & PHP_PQRES_CONV_ALL; if (zac != value) { zval_ptr_dtor(&zac); @@ -541,7 +541,12 @@ php_resource_factory_ops_t *php_pqconn_get_resource_factory_ops(void) static void php_pqconn_wakeup(php_persistent_handle_factory_t *f, void **handle TSRMLS_DC) { - // FIXME: ping server + PGresult *res = PQexec(*handle, ""); + PHP_PQclear(res); + + if (CONNECTION_OK != PQstatus(*handle)) { + PQreset(*handle); + } } static inline PGresult *unlisten(PGconn *conn, const char *channel_str, size_t channel_len TSRMLS_DC) @@ -568,7 +573,7 @@ static inline PGresult *unlisten(PGconn *conn, const char *channel_str, size_t c static int apply_unlisten(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key) { php_pqconn_object_t *obj = va_arg(argv, php_pqconn_object_t *); - PGresult *res = unlisten(obj->intern->conn, key->arKey, key->nKeyLength - 1); + PGresult *res = unlisten(obj->intern->conn, key->arKey, key->nKeyLength - 1 TSRMLS_CC); if (res) { PHP_PQclear(res); @@ -753,7 +758,7 @@ static PHP_METHOD(pqconn, unlisten) if (!obj->intern) { throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized"); } else if (SUCCESS == zend_hash_del(&obj->intern->listeners, channel_str, channel_len + 1)) { - PGresult *res = unlisten(obj->intern->conn, channel_str, channel_len); + PGresult *res = unlisten(obj->intern->conn, channel_str, channel_len TSRMLS_CC); if (res) { php_pqres_success(res TSRMLS_CC); @@ -1338,24 +1343,6 @@ static PHP_METHOD(pqconn, prepareAsync) { } } -static inline char *declare_str(const char *name_str, size_t name_len, unsigned flags, const char *query_str, size_t query_len) -{ - size_t decl_len = name_len + query_len + sizeof("DECLARE BINARY INSENSITIVE NO SCROLL CURSOR WITHOUT HOLD FOR "); - char *decl_str; - - decl_str = emalloc(decl_len); - decl_len = slprintf(decl_str, decl_len, "DECLARE %s %s %s %s CURSOR %s FOR %s", - name_str, - (flags & PHP_PQ_DECLARE_BINARY) ? "BINARY" : "", - (flags & PHP_PQ_DECLARE_INSENSITIVE) ? "INSENSITIVE" : "", - (flags & PHP_PQ_DECLARE_NO_SCROLL) ? "NO SCROLL" : - (flags & PHP_PQ_DECLARE_SCROLL) ? "SCROLL" : "", - (flags & PHP_PQ_DECLARE_WITH_HOLD) ? "WITH HOLD" : "", - query_str - ); - return decl_str; -} - STATUS php_pqconn_declare(zval *object, php_pqconn_object_t *obj, const char *decl TSRMLS_DC) { PGresult *res; @@ -1401,7 +1388,7 @@ static PHP_METHOD(pqconn, declare) { if (!obj->intern) { throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized"); } else { - char *decl = declare_str(name_str, name_len, flags, query_str, query_len); + char *decl = php_pqcur_declare_str(name_str, name_len, flags, query_str, query_len); if (SUCCESS != php_pqconn_declare(getThis(), obj, decl TSRMLS_CC)) { efree(decl); @@ -1466,7 +1453,7 @@ static PHP_METHOD(pqconn, declareAsync) { if (!obj->intern) { throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized"); } else { - char *decl = declare_str(name_str, name_len, flags, query_str, query_len); + char *decl = php_pqcur_declare_str(name_str, name_len, flags, query_str, query_len); if (SUCCESS != php_pqconn_declare_async(getThis(), obj, decl TSRMLS_CC)) { efree(decl); @@ -1825,20 +1812,25 @@ static PHP_METHOD(pqconn, on) { } } -static int apply_set_converter(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key) +struct apply_set_converter_arg { + HashTable *ht; + zval **zconv; + unsigned add:1; +}; + +static int apply_set_converter(void *p, void *a TSRMLS_DC) { - zval *tmp, **zoid = p, **zcnv = va_arg(argv, zval **); - HashTable *converters = va_arg(argv, HashTable *); - int add = va_arg(argv, int); + zval *tmp, **zoid = p; + struct apply_set_converter_arg *arg = a; tmp = *zoid; Z_ADDREF_P(tmp); convert_to_long_ex(&tmp); - if (add) { - Z_ADDREF_PP(zcnv); - zend_hash_index_update(converters, Z_LVAL_P(tmp), zcnv, sizeof(zval *), NULL); + if (arg->add) { + Z_ADDREF_PP(arg->zconv); + zend_hash_index_update(arg->ht, Z_LVAL_P(tmp), arg->zconv, sizeof(zval *), NULL); } else { - zend_hash_index_del(converters, Z_LVAL_P(tmp)); + zend_hash_index_del(arg->ht, Z_LVAL_P(tmp)); } zval_ptr_dtor(&tmp); @@ -1846,7 +1838,7 @@ static int apply_set_converter(void *p TSRMLS_DC, int argc, va_list argv, zend_h } ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_set_converter, 0, 0, 1) - ZEND_ARG_OBJ_INFO(0, converter, pq\\ConverterInterface, 0) + ZEND_ARG_OBJ_INFO(0, converter, pq\\Converter, 0) ZEND_END_ARG_INFO(); static PHP_METHOD(pqconn, setConverter) { STATUS rv; @@ -1864,12 +1856,19 @@ static PHP_METHOD(pqconn, setConverter) { throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized"); } else { zval *tmp, *zoids = NULL; + struct apply_set_converter_arg arg = {NULL}; zend_call_method_with_0_params(&zcnv, NULL, NULL, "converttypes", &zoids); tmp = zoids; Z_ADDREF_P(tmp); convert_to_array_ex(&tmp); - zend_hash_apply_with_arguments(Z_ARRVAL_P(tmp) TSRMLS_CC, apply_set_converter, 3, &zcnv, &obj->intern->converters, 1); + + arg.ht = &obj->intern->converters; + arg.zconv = &zcnv; + arg.add = 1; + + zend_hash_apply_with_argument(Z_ARRVAL_P(tmp), apply_set_converter, &arg TSRMLS_CC); + zval_ptr_dtor(&tmp); zval_ptr_dtor(&zoids); } @@ -1877,7 +1876,7 @@ static PHP_METHOD(pqconn, setConverter) { } ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unset_converter, 0, 0, 1) - ZEND_ARG_OBJ_INFO(0, converter, pq\\ConverterInterface, 0) + ZEND_ARG_OBJ_INFO(0, converter, pq\\Converter, 0) ZEND_END_ARG_INFO(); static PHP_METHOD(pqconn, unsetConverter) { STATUS rv; @@ -1895,12 +1894,19 @@ static PHP_METHOD(pqconn, unsetConverter) { throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized"); } else { zval *tmp, *zoids = NULL; + struct apply_set_converter_arg arg = {NULL}; zend_call_method_with_0_params(&zcnv, NULL, NULL, "converttypes", &zoids); tmp = zoids; Z_ADDREF_P(tmp); convert_to_array_ex(&tmp); - zend_hash_apply_with_arguments(Z_ARRVAL_P(tmp) TSRMLS_CC, apply_set_converter, 3, &zcnv, &obj->intern->converters, 0); + + arg.ht = &obj->intern->converters; + arg.zconv = &zcnv; + arg.add = 0; + + zend_hash_apply_with_argument(Z_ARRVAL_P(tmp), apply_set_converter, &arg TSRMLS_CC); + zval_ptr_dtor(&tmp); zval_ptr_dtor(&zoids); } @@ -1965,7 +1971,7 @@ PHP_MINIT_FUNCTION(pqconn) php_pqconn_object_handlers.get_properties = php_pq_object_properties; php_pqconn_object_handlers.get_debug_info = php_pq_object_debug_info; - zend_hash_init(&php_pqconn_object_prophandlers, 14, NULL, NULL, 1); + zend_hash_init(&php_pqconn_object_prophandlers, 19, 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;