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

replace unsupported ucs-2 characters in a string with � #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 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 Down
39 changes: 39 additions & 0 deletions Doctrine/DBAL/Driver/PDODblib/PDOStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

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

class PDOStatement extends \Doctrine\DBAL\Driver\PDOStatement
{
/**
* @var string Regex matching freetds non-supported characters.
*/
const FREETDS_INVALID_CHAR_REGEX = '/[^\x{1}-\x{FFFF}]/u';

/**
* {@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'.
* As does the null character \u0000.
*
* 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::replaceUnsupportedFreetdsChars($value);
}
return parent::bindValue($param, $value, $type);
}

/**
* This function replaces characters in a string unsupported by freetds with the REPLACEMENT CHARACTER.
* @param string $val
* @return string
*/
public static function replaceUnsupportedFreetdsChars($val)
{
return is_string($val) ? \preg_replace(static::FREETDS_INVALID_CHAR_REGEX, "�", $val) : $val;
}
}