‪TYPO3CMS  ‪main
Driver.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\Driver\AbstractMySQLDriver;
21 use Doctrine\DBAL\Driver\Connection as DriverConnectionInterface;
22 use Doctrine\DBAL\Driver\PDO\Exception;
24 
31 final class ‪Driver extends AbstractMySQLDriver
32 {
33  public function ‪connect(array $params): DriverConnectionInterface
34  {
35  $driverOptions = $params['driverOptions'] ?? [];
36 
37  if (!empty($params['persistent'])) {
38  $driverOptions[\PDO::ATTR_PERSISTENT] = true;
39  }
40 
41  try {
42  $pdo = new \PDO(
43  $this->‪constructPdoDsn($params),
44  $params['user'] ?? '',
45  $params['password'] ?? '',
46  $driverOptions
47  );
48  // use prepared statements for pdo_mysql per default to retrieve native data types
49  if (!isset($driverOptions[\PDO::ATTR_EMULATE_PREPARES])) {
50  $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
51  }
52  } catch (\PDOException $exception) {
53  throw Exception::new($exception);
54  }
55 
56  return new ‪DriverConnection($pdo);
57  }
58 
62  private function ‪constructPdoDsn(array $params): string
63  {
64  $dsn = 'mysql:';
65  if (isset($params['host']) && $params['host'] !== '') {
66  $dsn .= 'host=' . $params['host'] . ';';
67  }
68  if (isset($params['port'])) {
69  $dsn .= 'port=' . $params['port'] . ';';
70  }
71  if (isset($params['dbname'])) {
72  $dsn .= 'dbname=' . $params['dbname'] . ';';
73  }
74  if (isset($params['unix_socket'])) {
75  $dsn .= 'unix_socket=' . $params['unix_socket'] . ';';
76  }
77  if (isset($params['charset'])) {
78  $dsn .= 'charset=' . $params['charset'] . ';';
79  }
80  return $dsn;
81  }
82 }
‪TYPO3\CMS\Core\Database\Driver\PDOMySql\Driver
Definition: Driver.php:32
‪TYPO3\CMS\Core\Database\Driver\DriverConnection
Definition: DriverConnection.php:35
‪TYPO3\CMS\Core\Database\Driver\PDOMySql\Driver\connect
‪connect(array $params)
Definition: Driver.php:33
‪TYPO3\CMS\Core\Database\Driver\PDOMySql\Driver\constructPdoDsn
‪string constructPdoDsn(array $params)
Definition: Driver.php:62
‪TYPO3\CMS\Core\Database\Driver\PDOMySql
Definition: Driver.php:18