Skip to content

Commit 2f3d9bb

Browse files
authored
Merge pull request #4274 from gjdanis/gdanis_support_varchar_binding_3.0
Support ASCII parameter binding
2 parents cce615a + 7b58fd2 commit 2f3d9bb

File tree

16 files changed

+273
-5
lines changed

16 files changed

+273
-5
lines changed

docs/en/reference/types.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ or ``null`` if no data is present.
157157
This can lead to type inconsistencies when reverse engineering the
158158
type from the database.
159159

160+
ascii_string
161+
++++++++++++
162+
163+
Similar to the ``string`` type but for binding non-unicode data. This type
164+
should be used with database vendors where a binding type mismatch
165+
can trigger an implicit cast and lead to performance problems.
166+
160167
text
161168
++++
162169

@@ -607,6 +614,9 @@ Please also notice the mapping specific footnotes for additional information.
607614
| | | | +----------------------------------------------------------+
608615
| | | | | ``NCHAR(n)`` [4]_ |
609616
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
617+
| **ascii_string** | ``string`` | **SQL Server** | | ``VARCHAR(n)`` |
618+
| | | | | ``CHAR(n)`` |
619+
+-------------------+---------------+--------------------------+---------+----------------------------------------------------------+
610620
| **text** | ``string`` | **MySQL** | *all* | ``TINYTEXT`` [17]_ |
611621
| | | | +----------------------------------------------------------+
612622
| | | | | ``TEXT`` [18]_ |

lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result
3838
{
3939
/** @var string[] */
4040
protected static $_paramTypeMap = [
41+
ParameterType::ASCII => 's',
4142
ParameterType::STRING => 's',
4243
ParameterType::BINARY => 's',
4344
ParameterType::BOOLEAN => 'i',

lib/Doctrine/DBAL/Driver/PDOSqlsrv/Statement.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@ class Statement extends PDO\Statement
1717
*/
1818
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null)
1919
{
20-
if (
21-
($type === ParameterType::LARGE_OBJECT || $type === ParameterType::BINARY)
22-
&& $driverOptions === null
23-
) {
24-
$driverOptions = \PDO::SQLSRV_ENCODING_BINARY;
20+
switch ($type) {
21+
case ParameterType::LARGE_OBJECT:
22+
case ParameterType::BINARY:
23+
if ($driverOptions === null) {
24+
$driverOptions = \PDO::SQLSRV_ENCODING_BINARY;
25+
}
26+
27+
break;
28+
29+
case ParameterType::ASCII:
30+
$type = ParameterType::STRING;
31+
$length = 0;
32+
$driverOptions = \PDO::SQLSRV_ENCODING_SYSTEM;
33+
break;
2534
}
2635

2736
return parent::bindParam($param, $variable, $type, $length, $driverOptions);

lib/Doctrine/DBAL/Driver/PDOStatement.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class PDOStatement extends \PDOStatement implements StatementInterface, Result
3030
ParameterType::NULL => PDO::PARAM_NULL,
3131
ParameterType::INTEGER => PDO::PARAM_INT,
3232
ParameterType::STRING => PDO::PARAM_STR,
33+
ParameterType::ASCII => PDO::PARAM_STR,
3334
ParameterType::BINARY => PDO::PARAM_LOB,
3435
ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
3536
ParameterType::BOOLEAN => PDO::PARAM_BOOL,

lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use function stripos;
3535

3636
use const SQLSRV_ENC_BINARY;
37+
use const SQLSRV_ENC_CHAR;
3738
use const SQLSRV_ERR_ERRORS;
3839
use const SQLSRV_FETCH_ASSOC;
3940
use const SQLSRV_FETCH_BOTH;
@@ -306,6 +307,14 @@ private function prepare()
306307
];
307308
break;
308309

310+
case ParameterType::ASCII:
311+
$params[$column - 1] = [
312+
&$variable,
313+
SQLSRV_PARAM_IN,
314+
SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR),
315+
];
316+
break;
317+
309318
default:
310319
$params[$column - 1] =& $variable;
311320
break;

lib/Doctrine/DBAL/ParameterType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ final class ParameterType
4949
*/
5050
public const BINARY = 16;
5151

52+
/**
53+
* Represents an ASCII string data type
54+
*/
55+
public const ASCII = 17;
56+
5257
/**
5358
* This class cannot be instantiated.
5459
*

lib/Doctrine/DBAL/Platforms/AbstractPlatform.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,17 @@ private function initializeAllDoctrineTypeMappings()
245245
}
246246
}
247247

248+
/**
249+
* Returns the SQL snippet used to declare a column that can
250+
* store characters in the ASCII character set
251+
*
252+
* @param mixed[] $column
253+
*/
254+
public function getAsciiStringTypeDeclarationSQL(array $column): string
255+
{
256+
return $this->getVarcharTypeDeclarationSQL($column);
257+
}
258+
248259
/**
249260
* Returns the SQL snippet used to declare a VARCHAR column type.
250261
*

lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,20 @@ public function getGuidTypeDeclarationSQL(array $column)
11911191
return 'UNIQUEIDENTIFIER';
11921192
}
11931193

1194+
/**
1195+
* {@inheritDoc}
1196+
*/
1197+
public function getAsciiStringTypeDeclarationSQL(array $column): string
1198+
{
1199+
$length = $column['length'] ?? null;
1200+
1201+
if (! isset($column['fixed'])) {
1202+
return sprintf('VARCHAR(%d)', $length);
1203+
}
1204+
1205+
return sprintf('CHAR(%d)', $length);
1206+
}
1207+
11941208
/**
11951209
* {@inheritDoc}
11961210
*/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Types;
6+
7+
use Doctrine\DBAL\ParameterType;
8+
use Doctrine\DBAL\Platforms\AbstractPlatform;
9+
10+
final class AsciiStringType extends StringType
11+
{
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
public function getSQLDeclaration(array $column, AbstractPlatform $platform)
16+
{
17+
return $platform->getAsciiStringTypeDeclarationSQL($column);
18+
}
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function getBindingType()
24+
{
25+
return ParameterType::ASCII;
26+
}
27+
28+
public function getName(): string
29+
{
30+
return Types::ASCII_STRING;
31+
}
32+
}

lib/Doctrine/DBAL/Types/Type.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ abstract class Type
9999
*/
100100
private const BUILTIN_TYPES_MAP = [
101101
Types::ARRAY => ArrayType::class,
102+
Types::ASCII_STRING => AsciiStringType::class,
102103
Types::BIGINT => BigIntType::class,
103104
Types::BINARY => BinaryType::class,
104105
Types::BLOB => BlobType::class,

0 commit comments

Comments
 (0)