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

Implement binary array parameter type #5994

Closed
Closed
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
4 changes: 3 additions & 1 deletion docs/en/reference/data-retrieval-and-manipulation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,12 @@ SQL injection possibilities if not handled carefully.
Doctrine DBAL implements a very powerful parsing process that will make this kind of prepared
statement possible natively in the binding type system.
The parsing necessarily comes with a performance overhead, but only if you really use a list of parameters.
There are two special binding types that describe a list of integers or strings:
There are four special binding types that describe a list of integers, regular, ascii or binary strings:

- ``\Doctrine\DBAL\ArrayParameterType::INTEGER``
- ``\Doctrine\DBAL\ArrayParameterType::STRING``
- ``\Doctrine\DBAL\ArrayParameterType::ASCII``
- ``\Doctrine\DBAL\ArrayParameterType::BINARY``

Using one of these constants as a type you can activate the SQLParser inside Doctrine that rewrites
the SQL and flattens the specified values into the set of parameters. Consider our previous example:
Expand Down
9 changes: 7 additions & 2 deletions src/ArrayParameterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ final class ArrayParameterType
*/
public const ASCII = ParameterType::ASCII + Connection::ARRAY_PARAM_OFFSET;

/**
* Represents an array of ascii strings to be expanded by Doctrine SQL parsing.
*/
public const BINARY = ParameterType::BINARY + Connection::ARRAY_PARAM_OFFSET;

/**
* @internal
*
* @psalm-param self::INTEGER|self::STRING|self::ASCII $type
* @psalm-param self::INTEGER|self::STRING|self::ASCII|self::BINARY $type
*
* @psalm-return ParameterType::INTEGER|ParameterType::STRING|ParameterType::ASCII
* @psalm-return ParameterType::INTEGER|ParameterType::STRING|ParameterType::ASCII|ParameterType::BINARY
*/
public static function toElementParameterType(int $type): int
{
Expand Down
1 change: 1 addition & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,7 @@ private function needsArrayParameterConversion(array $params, array $types): boo
$type === ArrayParameterType::INTEGER
|| $type === ArrayParameterType::STRING
|| $type === ArrayParameterType::ASCII
|| $type === ArrayParameterType::BINARY
) {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Driver/IBMDB2/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le
$this->lobs[$param] = &$variable;
break;

case ParameterType::BINARY:
$this->bind($param, $variable, DB2_PARAM_IN, DB2_BINARY);
break;

Comment on lines +111 to +114
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your feature should not require a change in the drivers because array parameter expansion happens before the statement hits the driver. If binary parameters didn't need special treatment before, they shouldn't now.

default:
$this->bind($param, $variable, DB2_PARAM_IN, DB2_CHAR);
break;
Expand Down
1 change: 1 addition & 0 deletions src/ExpandArrayParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private function acceptParameter($key, $value): void
$type !== ArrayParameterType::INTEGER
&& $type !== ArrayParameterType::STRING
&& $type !== ArrayParameterType::ASCII
&& $type !== ArrayParameterType::BINARY
) {
$this->appendTypedParameter([$value], $type);

Expand Down
36 changes: 32 additions & 4 deletions tests/Connection/ExpandArrayParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\TestCase;

use function hex2bin;

class ExpandArrayParametersTest extends TestCase
{
/** @return mixed[][] */
Expand Down Expand Up @@ -94,16 +96,24 @@ public static function dataExpandListParameters(): iterable
[1 => ParameterType::STRING, 2 => ParameterType::STRING],
],
'Positional: explicit keys for array params and array types' => [
'SELECT * FROM Foo WHERE foo IN (?) AND bar IN (?) AND baz = ? AND bax IN (?)',
[1 => ['bar1', 'bar2'], 2 => true, 0 => [1, 2, 3], ['bax1', 'bax2']],
'SELECT * FROM Foo WHERE foo IN (?) AND bar IN (?) AND baz = ? AND bax IN (?) AND bay IN (?)',
[
1 => ['bar1', 'bar2'],
2 => true,
0 => [1, 2, 3],
['bax1', 'bax2'],
4 => [hex2bin('DEADBEEF'), hex2bin('C0DEF00D')],
],
[
4 => ArrayParameterType::BINARY,
3 => ArrayParameterType::ASCII,
2 => ParameterType::BOOLEAN,
1 => ArrayParameterType::STRING,
0 => ArrayParameterType::INTEGER,
],
'SELECT * FROM Foo WHERE foo IN (?, ?, ?) AND bar IN (?, ?) AND baz = ? AND bax IN (?, ?)',
[1, 2, 3, 'bar1', 'bar2', true, 'bax1', 'bax2'],
'SELECT * FROM Foo WHERE foo IN (?, ?, ?) AND bar IN (?, ?) AND baz = ? AND bax IN (?, ?) ' .
'AND bay IN (?, ?)',
[1, 2, 3, 'bar1', 'bar2', true, 'bax1', 'bax2', hex2bin('DEADBEEF'), hex2bin('C0DEF00D')],
[
ParameterType::INTEGER,
ParameterType::INTEGER,
Expand All @@ -113,6 +123,8 @@ public static function dataExpandListParameters(): iterable
ParameterType::BOOLEAN,
ParameterType::ASCII,
ParameterType::ASCII,
ParameterType::BINARY,
ParameterType::BINARY,
],
],
'Named: Very simple with param int' => [
Expand Down Expand Up @@ -310,6 +322,22 @@ public static function dataExpandListParameters(): iterable
['foo', 'bar', 'baz'],
[1 => ParameterType::STRING, ParameterType::STRING],
],
'Named: Binary array with explicit types' => [
'SELECT * FROM Foo WHERE foo IN (:foo) OR bar IN (:bar)',
[
'foo' => [hex2bin('DEADBEEF'), hex2bin('C0DEF00D')],
'bar' => [hex2bin('DEADBEEF'), hex2bin('C0DEF00D')],
],
['foo' => ArrayParameterType::BINARY, 'bar' => ArrayParameterType::BINARY],
'SELECT * FROM Foo WHERE foo IN (?, ?) OR bar IN (?, ?)',
[hex2bin('DEADBEEF'), hex2bin('C0DEF00D'), hex2bin('DEADBEEF'), hex2bin('C0DEF00D')],
[
ParameterType::BINARY,
ParameterType::BINARY,
ParameterType::BINARY,
ParameterType::BINARY,
],
],
];
}

Expand Down
Loading