sql-formatter icon indicating copy to clipboard operation
sql-formatter copied to clipboard

Cannot render SQL queries containing binary strings

Open ostrolucky opened this issue 1 year ago • 4 comments

At the moment, even though valid, these kind of queries would crash doctrine-bundle + twig

$connection->executeQuery("SELECT {$connection->quote((new \Symfony\Component\Uid\Ulid())->toBinary())}");

We are running all of these queries through this formatter, so I think formatter should do something about these, eg. detect non-utf8 parts and replace them with something.

Original issue was https://github.com/doctrine/DoctrineBundle/issues/1315, I'm moving it here

ostrolucky avatar Jul 21 '23 03:07 ostrolucky

I hit this error because we have binary values in our db queries.

The exception was thrown in Resources/views/Collector/db.html.twig where the formatted query or runnable query contents are added to the "Copy" buttons data-clipboard-text attribute which has to be escaped. Eg...

<button class="btn btn-sm hidden" data-clipboard-text="{{ query.sql|doctrine_format_sql(highlight = false)|e('html_attr') }}">Copy</button>

By adding a convert_encoding('UTF-8//TRANSLIT//IGNORE', 'UTF-8') filter before the escape attempt, it fixed the issue and the contents of the clipboard were as expected.

Our project is using version 2.7.2 of the bundle, and the offending lines in db.html.twig were 219 and 226.

Sorry, I don't have time to test this in more depth and do a PR. But I hope this helps.

gargoyle avatar Aug 31 '23 17:08 gargoyle

Just wanted to mention. I'm having the same problem while using Uuids in entities. The solution from @gargoyle seems to work for me as well.

I'm using version 2.11.1 of the bundle. In db.html.twig I had to modify line 287 and 294.

I'm terrible at PR's so I better dont do it. Last time it took me a few days to do one correctly with lots of help. Anyway, hopefully my input helped a bit.

Looking forward to a fix for this.

Nexotap avatar Feb 06 '24 02:02 Nexotap