Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ASCII parameter binding #4274

Merged
merged 1 commit into from
Sep 18, 2020
Merged
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
10 changes: 10 additions & 0 deletions docs/en/reference/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ or ``null`` if no data is present.
This can lead to type inconsistencies when reverse engineering the
type from the database.

ascii_string
++++++++++++

Similar to the ``string`` type but for binding non-unicode data. This type
should be used with database vendors where a binding type mismatch
can trigger an implicit cast and lead to performance problems.

text
++++

Expand Down Expand Up @@ -607,6 +614,9 @@ Please also notice the mapping specific footnotes for additional information.
| | | | +----------------------------------------------------------+
| | | | | ``NCHAR(n)`` [4]_ |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **ascii_string** | ``string`` | **SQL Server** | | ``VARCHAR(n)`` |
| | | | | ``CHAR(n)`` |
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
| **text** | ``string`` | **MySQL** | *all* | ``TINYTEXT`` [17]_ |
| | | | +----------------------------------------------------------+
| | | | | ``TEXT`` [18]_ |
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result
{
/** @var string[] */
protected static $_paramTypeMap = [
ParameterType::ASCII => 's',
ParameterType::STRING => 's',
ParameterType::BINARY => 's',
ParameterType::BOOLEAN => 'i',
Expand Down
19 changes: 14 additions & 5 deletions lib/Doctrine/DBAL/Driver/PDOSqlsrv/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ class Statement extends PDO\Statement
*/
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null)
{
if (
($type === ParameterType::LARGE_OBJECT || $type === ParameterType::BINARY)
&& $driverOptions === null
) {
$driverOptions = \PDO::SQLSRV_ENCODING_BINARY;
switch ($type) {
case ParameterType::LARGE_OBJECT:
case ParameterType::BINARY:
if ($driverOptions === null) {
$driverOptions = \PDO::SQLSRV_ENCODING_BINARY;
}

break;

case ParameterType::ASCII:
$type = ParameterType::STRING;
gjdanis marked this conversation as resolved.
Show resolved Hide resolved
$length = 0;
$driverOptions = \PDO::SQLSRV_ENCODING_SYSTEM;
break;
}

return parent::bindParam($param, $variable, $type, $length, $driverOptions);
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/DBAL/Driver/PDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class PDOStatement extends \PDOStatement implements StatementInterface, Result
ParameterType::NULL => PDO::PARAM_NULL,
ParameterType::INTEGER => PDO::PARAM_INT,
ParameterType::STRING => PDO::PARAM_STR,
ParameterType::ASCII => PDO::PARAM_STR,
ParameterType::BINARY => PDO::PARAM_LOB,
ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
ParameterType::BOOLEAN => PDO::PARAM_BOOL,
Expand Down
9 changes: 9 additions & 0 deletions lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use function stripos;

use const SQLSRV_ENC_BINARY;
use const SQLSRV_ENC_CHAR;
use const SQLSRV_ERR_ERRORS;
use const SQLSRV_FETCH_ASSOC;
use const SQLSRV_FETCH_BOTH;
Expand Down Expand Up @@ -306,6 +307,14 @@ private function prepare()
];
break;

case ParameterType::ASCII:
$params[$column - 1] = [
&$variable,
SQLSRV_PARAM_IN,
SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR),
];
break;

default:
$params[$column - 1] =& $variable;
break;
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/DBAL/ParameterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ final class ParameterType
*/
public const BINARY = 16;

/**
* Represents an ASCII string data type
*/
public const ASCII = 17;

/**
* This class cannot be instantiated.
*
Expand Down
11 changes: 11 additions & 0 deletions lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,17 @@ private function initializeAllDoctrineTypeMappings()
}
}

/**
* Returns the SQL snippet used to declare a column that can
* store characters in the ASCII character set
*
* @param mixed[] $column
*/
public function getAsciiStringTypeDeclarationSQL(array $column): string
{
return $this->getVarcharTypeDeclarationSQL($column);
}

/**
* Returns the SQL snippet used to declare a VARCHAR column type.
*
Expand Down
14 changes: 14 additions & 0 deletions lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,20 @@ public function getGuidTypeDeclarationSQL(array $column)
return 'UNIQUEIDENTIFIER';
}

/**
* {@inheritDoc}
*/
public function getAsciiStringTypeDeclarationSQL(array $column): string
{
$length = $column['length'] ?? null;

if (! isset($column['fixed'])) {
return sprintf('VARCHAR(%d)', $length);
}

return sprintf('CHAR(%d)', $length);
}

/**
* {@inheritDoc}
*/
Expand Down
32 changes: 32 additions & 0 deletions lib/Doctrine/DBAL/Types/AsciiStringType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Types;

use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;

final class AsciiStringType extends StringType
{
/**
* {@inheritdoc}
*/
public function getSQLDeclaration(array $column, AbstractPlatform $platform)
{
return $platform->getAsciiStringTypeDeclarationSQL($column);
}

/**
* {@inheritdoc}
*/
public function getBindingType()
{
return ParameterType::ASCII;
}

public function getName(): string
{
return Types::ASCII_STRING;
}
}
1 change: 1 addition & 0 deletions lib/Doctrine/DBAL/Types/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ abstract class Type
*/
private const BUILTIN_TYPES_MAP = [
Types::ARRAY => ArrayType::class,
Types::ASCII_STRING => AsciiStringType::class,
Types::BIGINT => BigIntType::class,
Types::BINARY => BinaryType::class,
Types::BLOB => BlobType::class,
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/DBAL/Types/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
final class Types
{
public const ARRAY = 'array';
public const ASCII_STRING = 'ascii_string';
public const BIGINT = 'bigint';
public const BINARY = 'binary';
public const BLOB = 'blob';
Expand Down
28 changes: 28 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/ParameterTypes/AsciiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\DBAL\Functional\ParameterTypes;

use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
use Doctrine\DBAL\ParameterType;
use Doctrine\Tests\DbalFunctionalTestCase;

class AsciiTest extends DbalFunctionalTestCase
{
public function testAsciiBinding(): void
{
if (! $this->connection->getDriver() instanceof AbstractSQLServerDriver) {
self::markTestSkipped('Driver does not support ascii string binding');
}

$statement = $this->connection->prepare('SELECT sql_variant_property(?, \'BaseType\')');

$statement->bindValue(1, 'test', ParameterType::ASCII);
$statement->execute();
gjdanis marked this conversation as resolved.
Show resolved Hide resolved

$results = $statement->fetchOne();

self::assertEquals('varchar', $results);
}
}
70 changes: 70 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/Types/AsciiStringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\DBAL\Functional\Types;

use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Schema\Table;
use Doctrine\Tests\DbalFunctionalTestCase;

class AsciiStringTest extends DbalFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

$table = new Table('ascii_table');
$table->addColumn('id', 'ascii_string', [
'length' => 3,
'fixed' => true,
]);

$table->addColumn('val', 'ascii_string', ['length' => 4]);
$table->setPrimaryKey(['id']);

$sm = $this->connection->getSchemaManager();
$sm->dropAndCreateTable($table);
}

public function testInsertAndSelect(): void
{
$id1 = 'id1';
$id2 = 'id2';

$value1 = 'val1';
$value2 = 'val2';

$this->insert($id1, $value1);
$this->insert($id2, $value2);

self::assertSame($value1, $this->select($id1));
self::assertSame($value2, $this->select($id2));
}

private function insert(string $id, string $value): void
{
$result = $this->connection->insert('ascii_table', [
'id' => $id,
'val' => $value,
], [
ParameterType::ASCII,
ParameterType::ASCII,
]);

self::assertSame(1, $result);
}

private function select(string $id): string
{
$value = $this->connection->fetchOne(
'SELECT val FROM ascii_table WHERE id = ?',
[$id],
[ParameterType::ASCII]
);

self::assertIsString($value);

return $value;
}
}
22 changes: 22 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,28 @@ public function testZeroOffsetWithoutLimitIsIgnored(): void
$this->platform->modifyLimitQuery($query, null, 0)
);
}

/**
* @param array<string, mixed> $column
*
* @dataProvider asciiStringSqlDeclarationDataProvider
*/
public function testAsciiSQLDeclaration(string $expectedSql, array $column): void
{
$declarationSql = $this->platform->getAsciiStringTypeDeclarationSQL($column);
self::assertEquals($expectedSql, $declarationSql);
}

/**
* @return array<int, array{string, array<string, mixed>}>
*/
public function asciiStringSqlDeclarationDataProvider(): array
{
return [
['VARCHAR(12)', ['length' => 12]],
['CHAR(12)', ['length' => 12, 'fixed' => true]],
];
}
}

interface GetCreateTableSqlDispatchEventListener
Expand Down
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -976,4 +976,15 @@ public function testQuotesDatabaseNameInListTableColumnsSQL(): void
$this->platform->getListTableColumnsSQL('foo_table', "Foo'Bar\\")
);
}

/**
* @return array<int, array{string, array<string, mixed>}>
*/
public function asciiStringSqlDeclarationDataProvider(): array
{
return [
['VARCHAR2(12)', ['length' => 12]],
['CHAR(12)', ['length' => 12, 'fixed' => true]],
];
}
}
43 changes: 43 additions & 0 deletions tests/Doctrine/Tests/DBAL/Types/AsciiStringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\DBAL\Types;

use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\AsciiStringType;
use PHPUnit\Framework\TestCase;

class AsciiStringTest extends TestCase
{
/** @var AsciiStringType */
private $type;

protected function setUp(): void
{
$this->type = new AsciiStringType();
}

public function testReturnCorrectBindingType(): void
{
self::assertEquals($this->type->getBindingType(), ParameterType::ASCII);
}

public function testDelegateToPlatformForSqlDeclaration(): void
{
$columnDefinitions = [
[['length' => 12, 'fixed' => true]],
[['length' => 14]],
];

foreach ($columnDefinitions as $column) {
$platform = $this->createMock(AbstractPlatform::class);
$platform->expects(self::once())
->method('getAsciiStringTypeDeclarationSQL')
->with($column);

$this->type->getSQLDeclaration($column, $platform);
}
}
}