| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Doctrine\DBAL;
|
|
|
4 |
|
|
|
5 |
use Doctrine\DBAL\ArrayParameters\Exception\MissingNamedParameter;
|
|
|
6 |
use Doctrine\DBAL\ArrayParameters\Exception\MissingPositionalParameter;
|
|
|
7 |
use Doctrine\DBAL\SQL\Parser\Visitor;
|
|
|
8 |
use Doctrine\DBAL\Types\Type;
|
|
|
9 |
|
|
|
10 |
use function array_fill;
|
|
|
11 |
use function array_key_exists;
|
|
|
12 |
use function count;
|
|
|
13 |
use function implode;
|
|
|
14 |
use function substr;
|
|
|
15 |
|
|
|
16 |
final class ExpandArrayParameters implements Visitor
|
|
|
17 |
{
|
|
|
18 |
/** @var array<int,mixed>|array<string,mixed> */
|
|
|
19 |
private array $originalParameters;
|
|
|
20 |
|
|
|
21 |
/** @var array<int,Type|int|string|null>|array<string,Type|int|string|null> */
|
|
|
22 |
private array $originalTypes;
|
|
|
23 |
|
|
|
24 |
private int $originalParameterIndex = 0;
|
|
|
25 |
|
|
|
26 |
/** @var list<string> */
|
|
|
27 |
private array $convertedSQL = [];
|
|
|
28 |
|
|
|
29 |
/** @var list<mixed> */
|
|
|
30 |
private array $convertedParameters = [];
|
|
|
31 |
|
|
|
32 |
/** @var array<int,Type|int|string|null> */
|
|
|
33 |
private array $convertedTypes = [];
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* @param array<int, mixed>|array<string, mixed> $parameters
|
|
|
37 |
* @param array<int,Type|int|string|null>|array<string,Type|int|string|null> $types
|
|
|
38 |
*/
|
|
|
39 |
public function __construct(array $parameters, array $types)
|
|
|
40 |
{
|
|
|
41 |
$this->originalParameters = $parameters;
|
|
|
42 |
$this->originalTypes = $types;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
public function acceptPositionalParameter(string $sql): void
|
|
|
46 |
{
|
|
|
47 |
$index = $this->originalParameterIndex;
|
|
|
48 |
|
|
|
49 |
if (! array_key_exists($index, $this->originalParameters)) {
|
|
|
50 |
throw MissingPositionalParameter::new($index);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
$this->acceptParameter($index, $this->originalParameters[$index]);
|
|
|
54 |
|
|
|
55 |
$this->originalParameterIndex++;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
public function acceptNamedParameter(string $sql): void
|
|
|
59 |
{
|
|
|
60 |
$name = substr($sql, 1);
|
|
|
61 |
|
|
|
62 |
if (! array_key_exists($name, $this->originalParameters)) {
|
|
|
63 |
throw MissingNamedParameter::new($name);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
$this->acceptParameter($name, $this->originalParameters[$name]);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public function acceptOther(string $sql): void
|
|
|
70 |
{
|
|
|
71 |
$this->convertedSQL[] = $sql;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
public function getSQL(): string
|
|
|
75 |
{
|
|
|
76 |
return implode('', $this->convertedSQL);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/** @return list<mixed> */
|
|
|
80 |
public function getParameters(): array
|
|
|
81 |
{
|
|
|
82 |
return $this->convertedParameters;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* @param int|string $key
|
|
|
87 |
* @param mixed $value
|
|
|
88 |
*/
|
|
|
89 |
private function acceptParameter($key, $value): void
|
|
|
90 |
{
|
|
|
91 |
if (! isset($this->originalTypes[$key])) {
|
|
|
92 |
$this->convertedSQL[] = '?';
|
|
|
93 |
$this->convertedParameters[] = $value;
|
|
|
94 |
|
|
|
95 |
return;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
$type = $this->originalTypes[$key];
|
|
|
99 |
|
|
|
100 |
if (
|
|
|
101 |
$type !== Connection::PARAM_INT_ARRAY
|
|
|
102 |
&& $type !== Connection::PARAM_STR_ARRAY
|
|
|
103 |
&& $type !== Connection::PARAM_ASCII_STR_ARRAY
|
|
|
104 |
) {
|
|
|
105 |
$this->appendTypedParameter([$value], $type);
|
|
|
106 |
|
|
|
107 |
return;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
if (count($value) === 0) {
|
|
|
111 |
$this->convertedSQL[] = 'NULL';
|
|
|
112 |
|
|
|
113 |
return;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
$this->appendTypedParameter($value, $type - Connection::ARRAY_PARAM_OFFSET);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/** @return array<int,Type|int|string|null> */
|
|
|
120 |
public function getTypes(): array
|
|
|
121 |
{
|
|
|
122 |
return $this->convertedTypes;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* @param list<mixed> $values
|
|
|
127 |
* @param Type|int|string|null $type
|
|
|
128 |
*/
|
|
|
129 |
private function appendTypedParameter(array $values, $type): void
|
|
|
130 |
{
|
|
|
131 |
$this->convertedSQL[] = implode(', ', array_fill(0, count($values), '?'));
|
|
|
132 |
|
|
|
133 |
$index = count($this->convertedParameters);
|
|
|
134 |
|
|
|
135 |
foreach ($values as $value) {
|
|
|
136 |
$this->convertedParameters[] = $value;
|
|
|
137 |
$this->convertedTypes[$index] = $type;
|
|
|
138 |
|
|
|
139 |
$index++;
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
}
|