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 d9fef9f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
32 changes: 16 additions & 16 deletions Doctrine/DBAL/Driver/PDODblib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -66,7 +66,7 @@ private function _pdoTransactionsSupported() {
try {
$supported = true;
parent::beginTransaction();
} catch (\PDOException $e) {
} catch (PDOException $e) {
$supported = false;
}
if ($supported) {
Expand Down Expand Up @@ -121,7 +121,7 @@ private function _pdoLastInsertId() {
try {
$supported = true;
parent::lastInsertId();
} catch (\PDOException $e) {
} catch (PDOException $e) {
$supported = false;
}

Expand Down
38 changes: 38 additions & 0 deletions Doctrine/DBAL/Driver/PDODblib/PDOStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

use PDO;

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 d9fef9f

Please sign in to comment.