From d9fef9ff837bb9b466252149d0bf9ed86808230b Mon Sep 17 00:00:00 2001 From: Roel Arents Date: Fri, 10 Jul 2015 15:54:10 +0200 Subject: [PATCH] moved the non-UCS-2 character replacement to PDOStatement#bindValue --- Doctrine/DBAL/Driver/PDODblib/Connection.php | 32 ++++++++-------- .../DBAL/Driver/PDODblib/PDOStatement.php | 38 +++++++++++++++++++ 2 files changed, 54 insertions(+), 16 deletions(-) create mode 100644 Doctrine/DBAL/Driver/PDODblib/PDOStatement.php diff --git a/Doctrine/DBAL/Driver/PDODblib/Connection.php b/Doctrine/DBAL/Driver/PDODblib/Connection.php index c6d0dad..c7be417 100644 --- a/Doctrine/DBAL/Driver/PDODblib/Connection.php +++ b/Doctrine/DBAL/Driver/PDODblib/Connection.php @@ -21,38 +21,38 @@ namespace Lsw\DoctrinePdoDblib\Doctrine\DBAL\Driver\PDODblib; +use Doctrine\DBAL\Driver\Connection; +use Doctrine\DBAL\Driver\PDOConnection; +use PDO; +use PDOException; + /** * PDODblib Connection implementation. * * @since 2.0 */ -class Connection extends \Doctrine\DBAL\Driver\PDOConnection implements \Doctrine\DBAL\Driver\Connection { +class Connection extends PDOConnection implements Connection { protected $_pdoTransactionsSupport = null; protected $_pdoLastInsertIdSupport = null; + + public function __construct($dsn, $user = null, $password = null, $options = null) + { + parent::__construct($dsn, $user, $password, $options); + $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array(__NAMESPACE__ . '\PDOStatement', array())); + } + /** * @override */ - public function quote($value, $type = \PDO::PARAM_STR) { + public function quote($value, $type = PDO::PARAM_STR) { $val = parent::quote($value, $type); // Fix for a driver version terminating all values with null byte $val = rtrim($val, "\0"); - - // Freetds communicates with the server using UCS-2 (since v7.0). - // Freetds claims to convert from any given client charset to UCS-2 using iconv. Which should strip or replace unsupported chars. - // However in my experience, characters like 👍 (THUMBS UP SIGN, \u1F44D) end up in MSSQL shouting 'incorrect syntax' still. - $val = static::replaceNonUcs2Chars($val); return $val; } - - public static function replaceNonUcs2Chars($val) - { - // UCS-2 cannot represent unicode code points outside the BMP. (> 16 bits.) - // Replace those chars with the REPLACEMENT CHARACTER. - return preg_replace('/[^\x{0}-x{FFFF}]/u', "\xFFFD", $val); - } /** * @return bool PDO_DBlib transaction support @@ -66,7 +66,7 @@ private function _pdoTransactionsSupported() { try { $supported = true; parent::beginTransaction(); - } catch (\PDOException $e) { + } catch (PDOException $e) { $supported = false; } if ($supported) { @@ -121,7 +121,7 @@ private function _pdoLastInsertId() { try { $supported = true; parent::lastInsertId(); - } catch (\PDOException $e) { + } catch (PDOException $e) { $supported = false; } diff --git a/Doctrine/DBAL/Driver/PDODblib/PDOStatement.php b/Doctrine/DBAL/Driver/PDODblib/PDOStatement.php new file mode 100644 index 0000000..082745a --- /dev/null +++ b/Doctrine/DBAL/Driver/PDODblib/PDOStatement.php @@ -0,0 +1,38 @@ + 16 bits.) + * This function replaces those characters in a string with the REPLACEMENT CHARACTER. + * @param string $val + * @return string + */ + public static function replaceNonUcs2Chars($val) + { + return is_string($val) ? \preg_replace('/[^\x{0}-x{FFFF}]/u', "�", $val) : $val; + } + + +}