‪TYPO3CMS  ‪main
BulkInsertQuery.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Doctrine\DBAL\Connection;
21 
35 {
39  protected ‪$columns;
40 
44  protected ‪$connection;
45 
49  protected ‪$table;
50 
54  protected ‪$parameters = [];
55 
59  protected ‪$types = [];
60 
64  protected ‪$values = [];
65 
74  public function ‪__construct(‪Connection ‪$connection, string ‪$table, array ‪$columns = [])
75  {
76  $this->connection = ‪$connection;
78  $this->columns = ‪$columns;
79  }
80 
84  public function ‪__toString(): string
85  {
86  return $this->‪getSQL();
87  }
88 
112  public function ‪addValues(array ‪$values, array ‪$types = [])
113  {
114  $valueSet = [];
115 
116  if (empty($this->columns)) {
117  foreach (‪$values as $index => $value) {
118  $this->parameters[] = $value;
119  $this->types[] = ‪$types[$index] ?? null;
120  $valueSet[] = '?';
121  }
122 
123  $this->values[] = $valueSet;
124 
125  return;
126  }
127 
128  foreach ($this->columns as $index => $column) {
129  $namedValue = isset(‪$values[$column]) || array_key_exists($column, ‪$values);
130  $positionalValue = isset(‪$values[$index]) || array_key_exists($index, ‪$values);
131 
132  if (!$namedValue && !$positionalValue) {
133  throw new \InvalidArgumentException(
134  sprintf('No value specified for column %s (index %d).', $column, $index),
135  1476049651
136  );
137  }
138 
139  if ($namedValue && $positionalValue && ‪$values[$column] !== ‪$values[$index]) {
140  throw new \InvalidArgumentException(
141  sprintf('Multiple values specified for column %s (index %d).', $column, $index),
142  1476049652
143  );
144  }
145 
146  $this->parameters[] = $namedValue ? ‪$values[$column] : ‪$values[$index];
147  $valueSet[] = '?';
148 
149  $namedType = isset(‪$types[$column]);
150  $positionalType = isset(‪$types[$index]);
151 
152  if ($namedType && $positionalType && ‪$types[$column] !== ‪$types[$index]) {
153  throw new \InvalidArgumentException(
154  sprintf('Multiple types specified for column %s (index %d).', $column, $index),
155  1476049653
156  );
157  }
158 
159  if ($namedType) {
160  $this->types[] = ‪$types[$column];
161 
162  continue;
163  }
164 
165  if ($positionalType) {
166  $this->types[] = ‪$types[$index];
167 
168  continue;
169  }
170 
171  $this->types[] = null;
172  }
173 
174  $this->values[] = $valueSet;
175  }
176 
185  public function ‪execute(): int
186  {
187  return $this->connection->executeStatement($this->‪getSQL(), $this->parameters, $this->types);
188  }
189 
193  public function ‪getParameters(): array
194  {
196  }
197 
201  public function ‪getParameterTypes(): array
202  {
203  return ‪$this->types;
204  }
205 
212  public function ‪getSQL(): string
213  {
214  if (empty($this->values)) {
215  throw new \LogicException(
216  'You need to add at least one set of values before generating the SQL.',
217  1476049702
218  );
219  }
220 
222  $columnList = '';
223 
224  if (!empty($this->columns)) {
225  $columnList = sprintf(
226  ' (%s)',
227  implode(
228  ', ',
229  array_map(
230  static function (string $column) use (‪$connection): string {
231  return ‪$connection->‪quoteIdentifier($column);
232  },
234  )
235  )
236  );
237  }
238 
239  return sprintf(
240  'INSERT INTO %s%s VALUES (%s)',
241  $this->table,
242  $columnList,
243  implode(
244  '), (',
245  array_map(
246  static function (array $valueSet): string {
247  return implode(', ', $valueSet);
248  },
250  )
251  )
252  );
253  }
254 }
‪TYPO3\CMS\Core\Database\Query\BulkInsertQuery\$columns
‪string[] $columns
Definition: BulkInsertQuery.php:38
‪TYPO3\CMS\Core\Database\Query\BulkInsertQuery\__toString
‪__toString()
Definition: BulkInsertQuery.php:78
‪TYPO3\CMS\Core\Database\Connection\quoteIdentifier
‪string quoteIdentifier($identifier)
Definition: Connection.php:126
‪TYPO3\CMS\Core\Database\Query\BulkInsertQuery\$table
‪string $table
Definition: BulkInsertQuery.php:46
‪TYPO3\CMS\Core\Database\Query\BulkInsertQuery\execute
‪int execute()
Definition: BulkInsertQuery.php:179
‪TYPO3\CMS\Core\Database\Query\BulkInsertQuery\$values
‪array $values
Definition: BulkInsertQuery.php:58
‪TYPO3\CMS\Core\Database\Query\BulkInsertQuery\getParameters
‪getParameters()
Definition: BulkInsertQuery.php:187
‪TYPO3\CMS\Core\Database\Query\BulkInsertQuery\$parameters
‪array $parameters
Definition: BulkInsertQuery.php:50
‪TYPO3\CMS\Core\Database\Query\BulkInsertQuery\getSQL
‪getSQL()
Definition: BulkInsertQuery.php:206
‪TYPO3\CMS\Core\Database\Query\BulkInsertQuery\getParameterTypes
‪getParameterTypes()
Definition: BulkInsertQuery.php:195
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:35
‪TYPO3\CMS\Core\Database\Query
Definition: BulkInsertQuery.php:18
‪TYPO3\CMS\Core\Database\Query\BulkInsertQuery
Definition: BulkInsertQuery.php:35
‪TYPO3\CMS\Core\Database\Query\BulkInsertQuery\$connection
‪Connection $connection
Definition: BulkInsertQuery.php:42
‪TYPO3\CMS\Core\Database\Query\BulkInsertQuery\addValues
‪addValues(array $values, array $types=[])
Definition: BulkInsertQuery.php:106
‪TYPO3\CMS\Core\Database\Query\BulkInsertQuery\$types
‪array $types
Definition: BulkInsertQuery.php:54
‪TYPO3\CMS\Core\Database\Query\BulkInsertQuery\__construct
‪__construct(Connection $connection, string $table, array $columns=[])
Definition: BulkInsertQuery.php:68