From a4fefa8b3887b4f0aea4f4eef16ddf87acdf2d33 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 25 Oct 2020 19:05:55 +0100 Subject: [PATCH] Add BYTEA unescaping support --- src/php_pqres.c | 18 ++++++++++++++++++ src/php_pqres.h | 1 + tests/types002.phpt | 13 ++++++++++--- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/php_pqres.c b/src/php_pqres.c index d9b1051..5406ebf 100644 --- a/src/php_pqres.c +++ b/src/php_pqres.c @@ -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; diff --git a/src/php_pqres.h b/src/php_pqres.h index cc20ddb..79507fd 100644 --- a/src/php_pqres.h +++ b/src/php_pqres.h @@ -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 diff --git a/tests/types002.phpt b/tests/types002.phpt index 9d36788..c288ec1 100644 --- a/tests/types002.phpt +++ b/tests/types002.phpt @@ -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 -- 2.30.2