Table of Contents

PHP RFC: Extended String Types For PDO

Introduction

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.

Proposal

Three constants would be added to the pdo extension:

  1. 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.
  2. 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_PARAM attribute is set to PDO::PARAM_STR_NATL.
  3. PDO::ATTR_DEFAULT_STR_PARAM. This driver attribute would indicate a value to bitwise-OR to PDO::PARAM_STR by default.

The parameter constants are more like PDO::PARAM_INPUT_OUTPUT than 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.

Example:

$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.

extended-string-types-for-pdo
Real name Yes No
adambaratz (adambaratz)  
ashnazg (ashnazg)  
daverandom (daverandom)  
dm (dm)  
galvao (galvao)  
hywan (hywan)  
mariano (mariano)  
mbeccati (mbeccati)  
patrickallaert (patrickallaert)  
Final result: 8 1
This poll has been closed.

Implementation

This feature was implemented in PHP 7.2 (4afce8ec8c6660ebd9f9eb174d2614361d1c6129).