martin-georgiev / postgresql-for-doctrine

PostgreSQL enhancements for Doctrine. Provides support for advanced data types (json, jssnb, arrays), text search, array operators and jsonb specific functions.

Home Page:https://packagist.org/packages/martin-georgiev/postgresql-for-doctrine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Transformation to PostgresTextArray does not escape slash

ksd-krauzand opened this issue · comments

\MartinGeorgiev\Utils\DataStructure::transformPHPArrayToPostgresTextArray does not escape slash in a string. E.g.

        print_r(\MartinGeorgiev\Utils\DataStructure::transformPHPArrayToPostgresTextArray(
            ['"Quotes"', 'Slashes\\', "'SingleQuotes", 'Normal'])
        );

Then PostgreSQL 12.4

select unnest('{"\"Quotes\"","Slashes\","''SingleQuotes","Normal"}'::varchar[]);
ERROR:  malformed array literal: "{"\"Quotes\"","Slashes\","'SingleQuotes","Normal"}"
LINE 1: select unnest('{"\"Quotes\"","Slashes\","''SingleQuotes","No...
                      ^
DETAIL:  Unexpected array element.

Please consider using

$escapedText = '"'.\addcslashes($text, '"\\').'"';

As this would return what's exected:

select unnest('{"\"Quotes\"","Slashes\\","''SingleQuotes","Normal"}'::varchar[]);
    unnest
---------------
 "Quotes"
 Slashes\
 'SingleQuotes
 Normal
(4 rows)

Thanks for reporting this. Do you mind opening a PR with your proposed solution and new tests where applicable?

Cheers for reporting the issue and the PR that fixes it. The changes are included in https://github.com/martin-georgiev/postgresql-for-doctrine/releases/tag/v1.6.0