PHP RFC: Extended String Types For PDO
- Version: 0.3
- Date: 2017-02-16
- Author: Adam Baratz email@example.com
- Status: Accepted
- First Published at: https://wiki.php.net/rfc/extended-string-types-for-pdo
The “national character” type was introduced in SQL-92 (section 4.2.1). It's an open-ended type. The spec indicates that its meaning is defined by the implementation. MySQL and Microsoft SQL Server use it to store Unicode data.
There is a different format for literals of this type. Instead of simply surrounding strings with quotes, an N is added as a prefix (e.g., N'string' instead of 'string'). When using emulated prepared statements -- the default behavior for pdo_mysql, the only one for pdo_dblib -- it's not possible to quote parameters using this format. This means that queries involving these columns will trigger implicit casts, which makes them more expensive. This issue affects MySQL and MSSQL.
There aren't many pdo_dblib users who comment regularly on the internals list, but the presence of a feature request and a pull request suggests that this is an impactful omission.
Three constants would be added to the pdo extension:
- PDO::PARAM_STR_NATL. A new type, to be applied as a bitwise-OR to
PDO::PARAM_STR. It would indicate that the value should be quoted with the N-prefix.
- PDO::PARAM_STR_CHAR. A new type, to be applied as a bitwise-OR to
PDO::PARAM_STR. It would indicate that the value should be quoted without the N-prefix. This would be used as an exception for when the
PDO::ATTR_DEFAULT_STR_PARAMattribute is set to
- PDO::ATTR_DEFAULT_STR_PARAM. This driver attribute would indicate a value to bitwise-OR to
The parameter constants are more like
PDO::PARAM_STR. They're flags to be applied to other parameters. This would also mean that code portability would be preserved. Drivers that don't need the hints for true prepared statements would ignore them.
$db->quote('über', PDO::PARAM_STR | PDO::PARAM_STR_NATL); // N'über' $db->quote('A'); // 'A' $db->setAttribute(PDO::ATTR_DEFAULT_STR_PARAM, PDO::PARAM_STR_NATL); $db->quote('über'); // N'über' $db->quote('A', PDO::PARAM_STR | PDO::PARAM_STR_CHAR); // 'A'
Backward Incompatible Changes
This functionality would be strictly additive. Existing code would continue to work as it does. These constants wouldn't affect anything related to the character set used for connections.
Impact To Existing Extensions
Drivers outside of php-src might have to be modified if they make assumptions about the structure of
enum pdo_param_type. They would have to be rebuilt since the
PDO_DRIVER_API macro would be updated.
Proposed PHP Version(s)
Next PHP 7.x.
Proposed Voting Choices
Voting opened on 8 March 2017. It will close on the 17th at 0:00 UTC. This project requires a 50%+1 majority.
This feature was implemented in PHP 7.2 (4afce8ec8c6660ebd9f9eb174d2614361d1c6129).