Skip to content
This repository has been archived by the owner on Oct 28, 2020. It is now read-only.

Commit

Permalink
moved the non-UCS-2 character replacement to PDOStatement#bindValue
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel Arents committed Jul 10, 2015
1 parent 11a78a8 commit 7ce8343
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
19 changes: 7 additions & 12 deletions Doctrine/DBAL/Driver/PDODblib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class Connection extends \Doctrine\DBAL\Driver\PDOConnection implements \Doctrin

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
*/
Expand All @@ -38,21 +45,9 @@ public function quote($value, $type = \PDO::PARAM_STR) {

// 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
Expand Down
36 changes: 36 additions & 0 deletions Doctrine/DBAL/Driver/PDODblib/PDOStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Lsw\DoctrinePdoDblib\Doctrine\DBAL\Driver\PDODblib;

class PDOStatement extends \Doctrine\DBAL\Driver\PDOStatement
{

/**
* {@inheritdoc}
*
* 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) still end up in Sqlsrv shouting '102 incorrect syntax'.
* Upon binding a value, this function replaces the unsupported characters.
*/
public function bindValue($param, $value, $type = \PDO::PARAM_STR)
{
if ($type == \PDO::PARAM_STR) {
$value = static::replaceNonUcs2Chars($value);
}
return parent::bindValue($param, $value, $type);
}

/**
* UCS-2 cannot represent Unicode code points outside the BMP. (> 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;
}


}

0 comments on commit 7ce8343

Please sign in to comment.