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

Adding the constant PARAM_ASCII_ARRAY to Doctrine\DBAL\Connection #5120

Merged
merged 1 commit into from
Jan 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class Connection
*/
public const PARAM_STR_ARRAY = ParameterType::STRING + self::ARRAY_PARAM_OFFSET;

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

/**
* Offset by which PARAM_* constants are detected as arrays of the param type.
*/
Expand Down Expand Up @@ -1786,7 +1791,11 @@ private function needsArrayParameterConversion(array $params, array $types): boo
}

foreach ($types as $type) {
if ($type === self::PARAM_INT_ARRAY || $type === self::PARAM_STR_ARRAY) {
if (
$type === self::PARAM_INT_ARRAY
|| $type === self::PARAM_STR_ARRAY
|| $type === self::PARAM_ASCII_STR_ARRAY
) {
return true;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/ExpandArrayParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ private function acceptParameter($key, $value): void

$type = $this->originalTypes[$key];

if ($type !== Connection::PARAM_INT_ARRAY && $type !== Connection::PARAM_STR_ARRAY) {
if (
$type !== Connection::PARAM_INT_ARRAY
&& $type !== Connection::PARAM_STR_ARRAY
&& $type !== Connection::PARAM_ASCII_STR_ARRAY
) {
$this->appendTypedParameter([$value], $type);

return;
Expand Down
25 changes: 20 additions & 5 deletions tests/Connection/ExpandArrayParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,25 @@ 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 = ?',
[1 => ['bar1', 'bar2'], 2 => true, 0 => [1, 2, 3]],
[2 => ParameterType::BOOLEAN, 1 => Connection::PARAM_STR_ARRAY, 0 => Connection::PARAM_INT_ARRAY],
'SELECT * FROM Foo WHERE foo IN (?, ?, ?) AND bar IN (?, ?) AND baz = ?',
[1, 2, 3, 'bar1', 'bar2', true],
'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']],
[
3 => Connection::PARAM_ASCII_STR_ARRAY,
2 => ParameterType::BOOLEAN,
1 => Connection::PARAM_STR_ARRAY,
0 => Connection::PARAM_INT_ARRAY,
],
'SELECT * FROM Foo WHERE foo IN (?, ?, ?) AND bar IN (?, ?) AND baz = ? AND bax IN (?, ?)',
[1, 2, 3, 'bar1', 'bar2', true, 'bax1', 'bax2'],
[
ParameterType::INTEGER,
ParameterType::INTEGER,
ParameterType::INTEGER,
ParameterType::STRING,
ParameterType::STRING,
ParameterType::BOOLEAN,
ParameterType::ASCII,
ParameterType::ASCII,
],
],
'Named: Very simple with param int' => [
Expand Down Expand Up @@ -229,6 +236,14 @@ public static function dataExpandListParameters(): iterable
[],
[],
],
[
'SELECT * FROM Foo WHERE foo IN (:foo) OR bar IN (:bar)',
['foo' => [], 'bar' => []],
['foo' => Connection::PARAM_ASCII_STR_ARRAY, 'bar' => Connection::PARAM_ASCII_STR_ARRAY],
'SELECT * FROM Foo WHERE foo IN (NULL) OR bar IN (NULL)',
[],
[],
],
[
'SELECT * FROM Foo WHERE foo IN (:foo) OR bar = :bar OR baz = :baz',
['foo' => [1, 2], 'bar' => 'bar', 'baz' => 'baz'],
Expand Down