Add BYTEA unescaping support
authorDaniil Gentili <daniil@daniil.it>
Sun, 25 Oct 2020 18:05:55 +0000 (19:05 +0100)
committerMichael Wallner <mike@php.net>
Wed, 11 Aug 2021 16:46:27 +0000 (18:46 +0200)
src/php_pqres.c
src/php_pqres.h
tests/types002.phpt

index d9b10518b32118ba86b5b3dcd93f33bf47f5f62b..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;
@@ -1336,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;
index cc20ddbbc764e15524f71e0f494b40ac21c84611..79507fdf1fa9ca5b1aac9c77cf930e98786cedfc 100644 (file)
@@ -25,6 +25,7 @@ typedef enum php_pqres_fetch {
 #define PHP_PQRES_CONV_BOOL            0x0001
 #define PHP_PQRES_CONV_INT             0x0002
 #define PHP_PQRES_CONV_FLOAT   0x0004
+#define PHP_PQRES_CONV_BYTEA   0x0008
 #define PHP_PQRES_CONV_SCALAR  0x000f
 #define PHP_PQRES_CONV_ARRAY   0x0010
 #define PHP_PQRES_CONV_DATETIME        0x0020
index 9d36788706311b8a414e5d3f8de8b2fca0288071..c288ec15869ab71cb82227d0ec22b73f11f4705b 100644 (file)
@@ -26,14 +26,16 @@ true as bool,
 '2013-01-01 01:01:01 UTC'::timestamptz as timestamptz,
 array[array[1,2,3],array[4,5,6],array[NULL::int,NULL::int,NULL::int]] as intarray,
 array[box(point(1,2),point(2,3)),box(point(4,5),point(5,6))] as boxarray,
-array[]::text[] as emptyarray
+array[]::text[] as emptyarray,
+'foo\n'::bytea as bytea,
+'foo\n'::bytea::text as bytea_text
 ");
 var_dump($r->fetchRow(pq\Result::FETCH_ASSOC));
 ?>
 DONE
 --EXPECTF--
 Test
-array(13) {
+array(15) {
   ["null"]=>
   NULL
   ["bool"]=>
@@ -121,5 +123,10 @@ array(13) {
   ["emptyarray"]=>
   array(0) {
   }
+  ["bytea"]=>
+  string(4) "foo
+"
+  ["bytea_text"]=>
+  string(10) "\x666f6f0a"
 }
-DONE
\ No newline at end of file
+DONE