Skip to content
Permalink
Browse files

Replace regex with instanceof check.

The hexadecimal check could get very expensive with big blobs. Instead
we can sniff out the Sqlserver class and change behavior based on that.

In order for the SQLServer driver to be loadable in environments where
pdo_sqlsrv is not installed a constant needed to be replaced with an
integer.
  • Loading branch information...
markstory committed May 14, 2015
1 parent 5bd710f commit 2d6ab89201cbc07e25ecfcb9007fa98873641154
@@ -39,7 +39,8 @@ class Sqlserver extends \Cake\Database\Driver
'username' => '',
'password' => '',
'database' => 'cake',
'encoding' => PDO::SQLSRV_ENCODING_UTF8,
// PDO::SQLSRV_ENCODING_UTF8
'encoding' => 65001,
'flags' => [],
'init' => [],
'settings' => [],
@@ -16,6 +16,7 @@
use Cake\Core\Exception\Exception;
use Cake\Database\Driver;
use Cake\Database\Driver\Sqlserver;
use Cake\Database\Type;
use PDO;
@@ -55,7 +56,7 @@ public function toPHP($value, Driver $driver)
if ($value === null) {
return null;
}
if (is_string($value) && preg_match('/^[a-zA-Z0-9]+$/', $value)) {
if (is_string($value) && $driver instanceof Sqlserver) {
$value = pack('H*', $value);
}
if (is_string($value)) {
@@ -46,10 +46,6 @@ public function testToPHP()
{
$this->assertNull($this->type->toPHP(null, $this->driver));
$result = $this->type->toPHP('536F6D652076616C7565', $this->driver);
$this->assertInternalType('resource', $result);
$this->assertSame('Some value', stream_get_contents($result));
$result = $this->type->toPHP('some data', $this->driver);
$this->assertInternalType('resource', $result);
@@ -59,6 +55,20 @@ public function testToPHP()
fclose($fh);
}
/**
* SQLServer returns binary fields as hexidecimal
* Ensure decoding happens for SQLServer drivers
*
* @return void
*/
public function testToPHPSqlserver()
{
$driver = $this->getMock('Cake\Database\Driver\Sqlserver', [], [], '', false);
$result = $this->type->toPHP('536F6D652076616C7565', $driver);
$this->assertInternalType('resource', $result);
$this->assertSame('Some value', stream_get_contents($result));
}
/**
* Test exceptions on invalid data.
*

0 comments on commit 2d6ab89

Please sign in to comment.
You can’t perform that action at this time.