prepare v2.2.3
[m6w6/ext-pq] / src / php_pqres.c
index 7e51271a5d8800badf9e7e1c88aae43b8d2eb73c..802fcf4a85484b506ed1c87cc58126869b7704df 100644 (file)
@@ -17,9 +17,7 @@
 #include <php.h>
 
 #include <ext/spl/spl_iterators.h>
-#if PHP_PQ_HAVE_PHP_JSON_H
-#include <php_json.h> /* we've added the include directory to INCLUDES */
-#endif
+#include <ext/json/php_json.h>
 
 #include <libpq-events.h>
 
@@ -215,7 +213,7 @@ zval *php_pqres_typed_zval(php_pqres_t *res, Oid typ, zval *zv)
                php_pqdt_from_string(zv, NULL, str->val, str->len, "Y-m-d H:i:s.uO", NULL);
                break;
 
-#if PHP_PQ_HAVE_PHP_JSON_H && defined(PHP_PQ_OID_JSON)
+#ifdef PHP_PQ_OID_JSON
 # ifdef PHP_PQ_OID_JSONB
        case PHP_PQ_OID_JSONB:
 # endif
@@ -227,6 +225,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,24 +398,24 @@ static zend_object_iterator_funcs php_pqres_iterator_funcs = {
 #endif
 };
 
-static inline ZEND_RESULT_CODE php_pqres_count_elements_ex(zend_object *object, long *count)
+static inline ZEND_RESULT_CODE php_pqres_count_elements_ex(zend_object *object, zend_long *count)
 {
        php_pqres_object_t *obj = PHP_PQ_OBJ(NULL, object);
 
        if (!obj->intern) {
                return FAILURE;
        } else {
-               *count = (long) PQntuples(obj->intern->res);
+               *count = (zend_long) PQntuples(obj->intern->res);
                return SUCCESS;
        }
 }
 #if PHP_VERSION_ID >= 80000
-static ZEND_RESULT_CODE php_pqres_count_elements(zend_object *object, long *count)
+static ZEND_RESULT_CODE php_pqres_count_elements(zend_object *object, zend_long *count)
 {
        return php_pqres_count_elements_ex(object, count);
 }
 #else
-static ZEND_RESULT_CODE php_pqres_count_elements(zval *object, long *count)
+static ZEND_RESULT_CODE php_pqres_count_elements(zval *object, zend_long *count)
 {
        return php_pqres_count_elements_ex(Z_OBJ_P(object), count);
 }
@@ -721,7 +736,7 @@ static ZEND_RESULT_CODE column_nn(php_pqres_object_t *obj, zval *zcol, php_pqres
        }
 
        if (!col->name) {
-               php_error_docref(NULL, E_WARNING, "Failed to find column at index %ld", index);
+               php_error_docref(NULL, E_WARNING, "Failed to find column at index " ZEND_LONG_FMT, index);
                return FAILURE;
        }
        if (col->num == -1) {
@@ -776,7 +791,7 @@ static int apply_bound(zval *zbound, int argc, va_list argv, zend_hash_key *key)
        ZEND_RESULT_CODE *rv = va_arg(argv, ZEND_RESULT_CODE *);
 
        if (!(zvalue = zend_hash_index_find(Z_ARRVAL_P(zrow), key->h))) {
-               php_error_docref(NULL, E_WARNING, "Failed to find column ad index %lu", key->h);
+               php_error_docref(NULL, E_WARNING, "Failed to find column ad index " ZEND_ULONG_FMT, key->h);
                *rv = FAILURE;
                return ZEND_HASH_APPLY_STOP;
        } else {
@@ -1146,7 +1161,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 +1172,7 @@ static PHP_METHOD(pqres, count) {
        zend_restore_error_handling(&zeh);
 
        if (SUCCESS == rv) {
-               long count;
+               zend_long count;
 
                if (SUCCESS != php_pqres_count_elements_ex(Z_OBJ_P(getThis()), &count)) {
                        throw_exce(EX_UNINITIALIZED, "pq\\Result not initialized");
@@ -1194,7 +1209,7 @@ static PHP_METHOD(pqres, desc) {
 }
 
 #if PHP_VERSION_ID >= 80000
-ZEND_BEGIN_ARG_INFO_EX(ai_pqres_getIterator, 0, 0, 0)
+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)
 {
@@ -1249,9 +1264,9 @@ PHP_MINIT_FUNCTION(pqres)
        php_pqres_class_entry->create_object = php_pqres_create_object;
        php_pqres_class_entry->get_iterator = php_pqres_iterator_init;
 #if PHP_VERSION_ID >= 80000
-       zend_class_implements(php_pqres_class_entry, 2, zend_ce_aggregate, spl_ce_Countable);
+       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, spl_ce_Countable);
+       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));
@@ -1333,9 +1348,8 @@ PHP_MINIT_FUNCTION(pqres)
        zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("CONV_SCALAR"), PHP_PQRES_CONV_SCALAR);
        zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("CONV_ARRAY"), PHP_PQRES_CONV_ARRAY);
        zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("CONV_DATETIME"), PHP_PQRES_CONV_DATETIME);
-#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;