fix async queries not cleared prior some sync queries
[m6w6/ext-pq] / src / php_pqres.c
index e9608efc8024b1fe5a6dc4c13f5bde97090a8f82..5406ebfd627ed7a8768b6d7dfe132098bc16b8e9 100644 (file)
@@ -227,6 +227,23 @@ zval *php_pqres_typed_zval(php_pqres_t *res, Oid typ, zval *zv)
                break;
 #endif
 
+       case PHP_PQ_OID_BYTEA:
+               if (!(res->auto_convert & PHP_PQRES_CONV_BYTEA)) {
+                       goto noconversion;
+               } else {
+                       size_t to_len;
+                       char *to_str = (char *) PQunescapeBytea((unsigned char *) str->val, &to_len);
+
+                       if (!to_str) {
+                               ZVAL_NULL(zv);
+                               php_error_docref(NULL, E_WARNING, "Failed to unsescape BYTEA: '%s'", str->val);
+                       } else {
+                               ZVAL_STRINGL(zv, to_str, to_len);
+                               PQfreemem(to_str);
+                       }
+               }
+               break;
+
        default:
                if (!(res->auto_convert & PHP_PQRES_CONV_ARRAY)) {
                        goto noconversion;
@@ -383,12 +400,7 @@ static zend_object_iterator_funcs php_pqres_iterator_funcs = {
 #endif
 };
 
-#if PHP_VERSION_ID >= 80000
-# define php_pqres_count_elements php_pqres_count_elements_80
-#else
-# define php_pqres_count_elements php_pqres_count_elements_70
-#endif
-static ZEND_RESULT_CODE php_pqres_count_elements_80(zend_object *object, long *count)
+static inline ZEND_RESULT_CODE php_pqres_count_elements_ex(zend_object *object, long *count)
 {
        php_pqres_object_t *obj = PHP_PQ_OBJ(NULL, object);
 
@@ -399,10 +411,17 @@ static ZEND_RESULT_CODE php_pqres_count_elements_80(zend_object *object, long *c
                return SUCCESS;
        }
 }
-static ZEND_RESULT_CODE php_pqres_count_elements_70(zval *object, long *count)
+#if PHP_VERSION_ID >= 80000
+static ZEND_RESULT_CODE php_pqres_count_elements(zend_object *object, long *count)
 {
-       return php_pqres_count_elements_80(Z_OBJ_P(object), count);
+       return php_pqres_count_elements_ex(object, count);
 }
+#else
+static ZEND_RESULT_CODE php_pqres_count_elements(zval *object, long *count)
+{
+       return php_pqres_count_elements_ex(Z_OBJ_P(object), count);
+}
+#endif
 
 ZEND_RESULT_CODE php_pqres_success(PGresult *res)
 {
@@ -1144,7 +1163,7 @@ static PHP_METHOD(pqres, fetchAll) {
        }
 }
 
-ZEND_BEGIN_ARG_INFO_EX(ai_pqres_count, 0, 0, 0)
+ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(ai_pqres_count, 0, 0, IS_LONG, 0)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(pqres, count) {
        zend_error_handling zeh;
@@ -1157,7 +1176,7 @@ static PHP_METHOD(pqres, count) {
        if (SUCCESS == rv) {
                long count;
 
-               if (SUCCESS != php_pqres_count_elements_70(getThis(), &count)) {
+               if (SUCCESS != php_pqres_count_elements_ex(Z_OBJ_P(getThis()), &count)) {
                        throw_exce(EX_UNINITIALIZED, "pq\\Result not initialized");
                } else {
                        RETVAL_LONG(count);
@@ -1191,7 +1210,8 @@ static PHP_METHOD(pqres, desc) {
        }
 }
 
-ZEND_BEGIN_ARG_INFO_EX(ai_pqres_getIterator, 0, 0, 0)
+#if PHP_VERSION_ID >= 80000
+ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_INFO_EX(ai_pqres_getIterator, 0, 0, Traversable, 0)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(pqres, getIterator)
 {
@@ -1212,6 +1232,7 @@ static PHP_METHOD(pqres, getIterator)
                }
        }
 }
+#endif
 
 static zend_function_entry php_pqres_methods[] = {
        PHP_ME(pqres, bind, ai_pqres_bind, ZEND_ACC_PUBLIC)
@@ -1223,7 +1244,9 @@ static zend_function_entry php_pqres_methods[] = {
        PHP_ME(pqres, count, ai_pqres_count, ZEND_ACC_PUBLIC)
        PHP_ME(pqres, map, ai_pqres_map, ZEND_ACC_PUBLIC)
        PHP_ME(pqres, desc, ai_pqres_desc, ZEND_ACC_PUBLIC)
+#if PHP_VERSION_ID >= 80000
        PHP_ME(pqres, getIterator, ai_pqres_getIterator, ZEND_ACC_PUBLIC)
+#endif
        {0}
 };
 
@@ -1242,7 +1265,11 @@ PHP_MINIT_FUNCTION(pqres)
        php_pqres_class_entry = zend_register_internal_class_ex(&ce, NULL);
        php_pqres_class_entry->create_object = php_pqres_create_object;
        php_pqres_class_entry->get_iterator = php_pqres_iterator_init;
-       zend_class_implements(php_pqres_class_entry, 2, zend_ce_aggregate, spl_ce_Countable);
+#if PHP_VERSION_ID >= 80000
+       zend_class_implements(php_pqres_class_entry, 2, zend_ce_aggregate, zend_ce_countable);
+#else
+       zend_class_implements(php_pqres_class_entry, 2, zend_ce_traversable, zend_ce_countable);
+#endif
 
        memcpy(&php_pqres_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
        php_pqres_object_handlers.offset = XtOffsetOf(php_pqres_object_t, zo);
@@ -1326,6 +1353,7 @@ PHP_MINIT_FUNCTION(pqres)
 #if PHP_PQ_HAVE_PHP_JSON_H
        zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("CONV_JSON"), PHP_PQRES_CONV_JSON);
 #endif
+       zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("CONV_BYTEA"), PHP_PQRES_CONV_BYTEA);
        zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("CONV_ALL"), PHP_PQRES_CONV_ALL);
 
        return SUCCESS;