VictorWesterlund / php-libinnodb-fk

Abstraction library for resolving InnoDB foreign keys in PHP
GNU General Public License v3.0
0 stars 0 forks source link

Table names with Unicode escape sequences aren't decoded before parsing #4

Open VictorWesterlund opened 11 months ago

VictorWesterlund commented 11 months ago

Table names with certain unicode characters are escaped by MySQL/MariaDB and put into the foreign key tables as @ prefixed Unicode numbers.

For example a database with the name database.my-table becomes database/my@002dtable. This library doesn't decode this sequence and therefor looks for a table with that explicit name.

VictorWesterlund commented 11 months ago

I'm not entirely sure how to fix this. I tried to write a function that escapes any character that isn't in the MySQL "permitted characters in unquoted identifiers" with a regex.

// Escape characters that aren't in the allowed identifiers RegEx
private static function escape_unpermitted_identifiers(string $string): string {
    return preg_replace_callback(
        "/[^\d,a-zA-Z\$_]/u",
        function ($matches) {
            // Return as @ prefixed Unicode sequence
            return '@' . bin2hex(mb_convert_encoding($matches[0], 'UTF-16BE', 'UTF-8'));
        },
        $string
    );
}

It worked well for some characters but not others, and I'm not entirely sure why. I think it might have something with multi-byte strings to do. For example my-table becomes my@002dtable as expected. But with another character "ä" for example, my_ä_table becomes my_@00e4_table which I think makes sense. But the same table gets encoded as my_@0k_table in the foreign key table.