‪TYPO3CMS  9.5
Connection.php
Go to the documentation of this file.
1 <?php
2 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 
18 use Doctrine\Common\EventManager;
19 use Doctrine\DBAL\Configuration;
20 use Doctrine\DBAL\Driver;
21 use Doctrine\DBAL\Driver\Statement;
22 use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
23 use Doctrine\DBAL\Platforms\SQLServer2012Platform;
24 use Psr\Log\LoggerAwareInterface;
25 use Psr\Log\LoggerAwareTrait;
29 
30 class ‪Connection extends \Doctrine\DBAL\Connection implements LoggerAwareInterface
31 {
32  use LoggerAwareTrait;
33 
37  const ‪PARAM_NULL = \PDO::PARAM_NULL; // 0
38 
42  const ‪PARAM_INT = \PDO::PARAM_INT; // 1
43 
47  const ‪PARAM_STR = \PDO::PARAM_STR; // 2
48 
52  const ‪PARAM_LOB = \PDO::PARAM_LOB; // 3
53 
57  const ‪PARAM_STMT = \PDO::PARAM_STMT; // 4
58 
62  const ‪PARAM_BOOL = \PDO::PARAM_BOOL; // 5
63 
67  private ‪$prepareConnectionCommands = [];
68 
79  public function ‪__construct(array $params, Driver $driver, Configuration $config = null, EventManager $em = null)
80  {
81  parent::__construct($params, $driver, $config, $em);
82  $this->_expr = GeneralUtility::makeInstance(ExpressionBuilder::class, $this);
83  }
84 
90  public function ‪connect(): bool
91  {
92  // Early return if the connection is already open and custom setup has been done.
93  if (!parent::connect()) {
94  return false;
95  }
96 
97  foreach ($this->prepareConnectionCommands as $command) {
98  if ($this->executeUpdate($command) === false) {
99  $this->logger->critical('Could not initialize DB connection with query "' . $command . '": ' . $this->errorInfo());
100  }
101  }
102 
103  return true;
104  }
105 
111  public function ‪createQueryBuilder(): QueryBuilder
112  {
113  return GeneralUtility::makeInstance(QueryBuilder::class, $this);
114  }
115 
127  public function ‪quoteIdentifier($identifier): string
128  {
129  if ($identifier === '*') {
130  return $identifier;
131  }
132 
133  return parent::quoteIdentifier($identifier);
134  }
135 
145  public function ‪quoteIdentifiers(array $input): array
146  {
147  return array_map([$this, 'quoteIdentifier'], $input);
148  }
149 
160  public function ‪quoteColumnValuePairs(array $input): array
161  {
162  return array_combine($this->‪quoteIdentifiers(array_keys($input)), array_values($input));
163  }
164 
173  protected function ‪quoteColumnTypes(array $input): array
174  {
175  if (!is_string(key($input))) {
176  return $input;
177  }
178 
179  return $this->‪quoteColumnValuePairs($input);
180  }
181 
194  public function ‪insert($tableName, array $data, array $types = []): int
195  {
196  return parent::insert(
197  $this->‪quoteIdentifier($tableName),
198  $this->‪quoteColumnValuePairs($data),
199  $this->‪quoteColumnTypes($types)
200  );
201  }
202 
216  public function ‪bulkInsert(string $tableName, array $data, array $columns = [], array $types = []): int
217  {
218  $query = GeneralUtility::makeInstance(Query\BulkInsertQuery::class, $this, $tableName, $columns);
219  foreach ($data as $values) {
220  $query->addValues($values, $types);
221  }
222 
223  return $query->execute();
224  }
225 
242  public function ‪select(
243  array $columns,
244  string $tableName,
245  array $identifiers = [],
246  array $groupBy = [],
247  array $orderBy = [],
248  int $limit = 0,
249  int $offset = 0
250  ): Statement {
251  $query = $this->‪createQueryBuilder();
252  $query->select(...$columns)
253  ->from($tableName);
254 
255  foreach ($identifiers as $identifier => $value) {
256  $query->andWhere($query->expr()->eq($identifier, $query->createNamedParameter($value)));
257  }
258 
259  foreach ($orderBy as $fieldName => $order) {
260  $query->addOrderBy($fieldName, $order);
261  }
262 
263  if (!empty($groupBy)) {
264  $query->groupBy(...$groupBy);
265  }
266 
267  if ($limit > 0) {
268  $query->setMaxResults($limit);
269  $query->setFirstResult($offset);
270  }
271 
272  return $query->execute();
273  }
274 
288  public function ‪update($tableName, array $data, array $identifier, array $types = []): int
289  {
290  return parent::update(
291  $this->‪quoteIdentifier($tableName),
292  $this->‪quoteColumnValuePairs($data),
293  $this->‪quoteColumnValuePairs($identifier),
294  $this->‪quoteColumnTypes($types)
295  );
296  }
297 
310  public function delete($tableName, array $identifier, array $types = []): int
311  {
312  return parent::delete(
313  $this->‪quoteIdentifier($tableName),
314  $this->‪quoteColumnValuePairs($identifier),
315  $this->‪quoteColumnTypes($types)
316  );
317  }
318 
330  public function ‪truncate(string $tableName, bool $cascade = false): int
331  {
332  return $this->executeUpdate(
333  $this->getDatabasePlatform()->getTruncateTableSQL(
334  $this->‪quoteIdentifier($tableName),
335  $cascade
336  )
337  );
338  }
339 
349  public function ‪count(string $item, string $tableName, array $identifiers): int
350  {
351  $query = $this->‪createQueryBuilder();
352  $query->count($item)
353  ->from($tableName);
354 
355  foreach ($identifiers as $identifier => $value) {
356  $query->andWhere($query->expr()->eq($identifier, $query->createNamedParameter($value)));
357  }
358 
359  return (int)$query->execute()->fetchColumn(0);
360  }
361 
371  public function ‪getServerVersion(): string
372  {
373  $version = $this->getDatabasePlatform()->getName();
374  switch ($version) {
375  case 'mysql':
376  case 'pdo_mysql':
377  case 'drizzle_pdo_mysql':
378  $version = 'MySQL';
379  break;
380  case 'postgresql':
381  case 'pdo_postgresql':
382  $version = 'PostgreSQL';
383  break;
384  case 'oci8':
385  case 'pdo_oracle':
386  $version = 'Oracle';
387  break;
388  case 'sqlsrv':
389  case 'pdo_sqlsrv':
390  $version = 'SQLServer';
391  break;
392  }
393 
394  // Driver does not support version specific platforms.
395  if (!$this->getDriver() instanceof \Doctrine\DBAL\VersionAwarePlatformDriver) {
396  return $version;
397  }
398 
399  if ($this->getWrappedConnection() instanceof \Doctrine\DBAL\Driver\ServerInfoAwareConnection
400  && !$this->getWrappedConnection()->requiresQueryForServerVersion()
401  ) {
402  $version .= ' ' . $this->getWrappedConnection()->getServerVersion();
403  }
404 
405  return $version;
406  }
407 
413  public function ‪prepareConnection(string $commands)
414  {
415  if (empty($commands)) {
416  return;
417  }
418 
419  $this->prepareConnectionCommands = GeneralUtility::trimExplode(
420  LF,
421  str_replace(
422  '\' . LF . \'',
423  LF,
424  $commands
425  ),
426  true
427  );
428  }
429 
440  public function ‪lastInsertId($tableName = null, string $fieldName = 'uid'): string
441  {
442  $databasePlatform = $this->getDatabasePlatform();
443  if ($databasePlatform instanceof PostgreSqlPlatform) {
444  return parent::lastInsertId(trim(implode('_', [$tableName, $fieldName, 'seq']), '_'));
445  }
446  if ($databasePlatform instanceof SQLServer2012Platform) {
447  // lastInsertId() in mssql >2012 takes a sequence name and not the table name as
448  // argument. If no argument is given, last insert id of latest table is returned.
449  // https://docs.microsoft.com/de-de/sql/connect/php/pdo-lastinsertid?view=sql-server-2017
450  return (string)parent::lastInsertId();
451  }
452 
453  return (string)parent::lastInsertId($tableName);
454  }
455 }
‪TYPO3\CMS\Core\Database\Connection\truncate
‪int truncate(string $tableName, bool $cascade=false)
Definition: Connection.php:329
‪TYPO3\CMS\Core\Database\Connection\PARAM_BOOL
‪const PARAM_BOOL
Definition: Connection.php:62
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:42
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:33
‪TYPO3\CMS\Core\Database\Connection\bulkInsert
‪int bulkInsert(string $tableName, array $data, array $columns=[], array $types=[])
Definition: Connection.php:215
‪TYPO3\CMS\Core\Database\Connection\lastInsertId
‪string lastInsertId($tableName=null, string $fieldName='uid')
Definition: Connection.php:439
‪TYPO3\CMS\Core\Database\Connection\insert
‪int insert($tableName, array $data, array $types=[])
Definition: Connection.php:193
‪TYPO3\CMS\Core\Database\Connection\quoteIdentifier
‪string quoteIdentifier($identifier)
Definition: Connection.php:126
‪TYPO3\CMS\Core\Database\Connection\PARAM_STR
‪const PARAM_STR
Definition: Connection.php:47
‪TYPO3\CMS\Core\Database\Connection\update
‪int update($tableName, array $data, array $identifier, array $types=[])
Definition: Connection.php:287
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:47
‪TYPO3\CMS\Core\Database\Connection\select
‪Statement select(array $columns, string $tableName, array $identifiers=[], array $groupBy=[], array $orderBy=[], int $limit=0, int $offset=0)
Definition: Connection.php:241
‪TYPO3\CMS\Core\Database\Connection\quoteColumnValuePairs
‪array quoteColumnValuePairs(array $input)
Definition: Connection.php:159
‪TYPO3\CMS\Core\Database\Connection\PARAM_STMT
‪const PARAM_STMT
Definition: Connection.php:57
‪TYPO3\CMS\Core\Database\Connection\quoteColumnTypes
‪array quoteColumnTypes(array $input)
Definition: Connection.php:172
‪TYPO3\CMS\Core\Database\Connection\getServerVersion
‪string getServerVersion()
Definition: Connection.php:370
‪TYPO3\CMS\Core\Database\Connection\count
‪int count(string $item, string $tableName, array $identifiers)
Definition: Connection.php:348
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:31
‪TYPO3\CMS\Core\Database\Connection\quoteIdentifiers
‪array quoteIdentifiers(array $input)
Definition: Connection.php:144
‪TYPO3\CMS\Core\Database\Connection\prepareConnection
‪prepareConnection(string $commands)
Definition: Connection.php:412
‪TYPO3\CMS\Core\Database\Connection\createQueryBuilder
‪TYPO3 CMS Core Database Query QueryBuilder createQueryBuilder()
Definition: Connection.php:110
‪TYPO3\CMS\Core\Database\Connection\PARAM_NULL
‪const PARAM_NULL
Definition: Connection.php:37
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Database\Connection\__construct
‪__construct(array $params, Driver $driver, Configuration $config=null, EventManager $em=null)
Definition: Connection.php:78
‪TYPO3\CMS\Core\Database\Connection\connect
‪bool connect()
Definition: Connection.php:89
‪TYPO3\CMS\Core\Database
Definition: Connection.php:3
‪TYPO3\CMS\Core\Database\Connection\$prepareConnectionCommands
‪array $prepareConnectionCommands
Definition: Connection.php:66
‪TYPO3\CMS\Core\Database\Connection\PARAM_LOB
‪const PARAM_LOB
Definition: Connection.php:52