In issue #2506005: PostgreSQL incompatible with Storage API, we've run into a problem where binary data is being passed as a value to the ->condition method, and on PostgreSQL, this typically results in a 'invalid byte sequence for encoding "UTF8"' error.

The code involved is

 $file_id = db_select('storage_file')
    ->fields('storage_file', array('file_id'))
    ->condition('whirlpool', $whirlpool)
    ->condition('filename', $filename)
    ->execute()
    ->fetchField();

The whirlpool field is defined as:

      'whirlpool' => array(
        'type' => 'blob', 'length' => 64, 'not null' => TRUE,
        'mysql_type' => 'BINARY(64)',
      ),

And the contents of $whirlpool is definitely binary, being returned from hash_file:


$whirlpool = hash_file('whirlpool', $uri, TRUE);

Is there some way of indicating that the value passed to ->condition is binary, and therefore should be properly escaped or formatted as such if the underlying database requires it? Is there a different way of handling it?

Comments

Ben Coleman created an issue. See original summary.

roderik’s picture

This is still not solved in D9. #2238253: Add bindValue to a PDO::PARAM_* type in database query arguments is trying to solve it by adding an extra optional argument $bindValue to (what in D7 is) DatabaseConnection::query().

So this issue could be the D7 followup once it gets fixed there.

(There's no problem description on #2238253, but it refers to #710940-10: Support for BINARY and VARBINARY in Database Schema which shows the same error.)

Status: Active » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.