‪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\AbstractSQLiteDriver;
21 use Doctrine\DBAL\Driver\Connection as DriverConnectionInterface;
22 use Doctrine\DBAL\Driver\PDO\Exception;
23 use Doctrine\DBAL\Platforms\SqlitePlatform as DoctrineSQLitePlatform;
24 use ‪TYPO3\CMS\Core\Database\Driver\DriverConnection as TYPO3DriverConnection;
25 
32 final class ‪Driver extends AbstractSQLiteDriver
33 {
34  private array ‪$userDefinedFunctions = [
35  'sqrt' => ['callback' => [DoctrineSQLitePlatform::class, 'udfSqrt'], 'numArgs' => 1],
36  'mod' => ['callback' => [DoctrineSQLitePlatform::class, 'udfMod'], 'numArgs' => 2],
37  'locate' => ['callback' => [DoctrineSQLitePlatform::class, 'udfLocate'], 'numArgs' => -1],
38  ];
39 
40  public function ‪connect(array $params): DriverConnectionInterface
41  {
42  $driverOptions = $params['driverOptions'] ?? [];
43 
44  if (isset($driverOptions['userDefinedFunctions'])) {
45  $this->userDefinedFunctions = array_merge(
46  $this->userDefinedFunctions,
47  $driverOptions['userDefinedFunctions']
48  );
49  unset($driverOptions['userDefinedFunctions']);
50  }
51 
52  try {
53  $pdo = new \PDO(
54  $this->‪constructPdoDsn($params),
55  $params['user'] ?? '',
56  $params['password'] ?? '',
57  $driverOptions
58  );
59  } catch (\PDOException $exception) {
60  throw Exception::new($exception);
61  }
62 
63  foreach ($this->userDefinedFunctions as $fn => $data) {
64  $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
65  }
66 
67  return new TYPO3DriverConnection($pdo);
68  }
69 
73  private function ‪constructPdoDsn(array $params): string
74  {
75  $dsn = 'sqlite:';
76  if (isset($params['path'])) {
77  $dsn .= $params['path'];
78  } elseif (isset($params['memory'])) {
79  $dsn .= ':memory:';
80  }
81  return $dsn;
82  }
83 }
‪TYPO3\CMS\Core\Database\Driver\PDOSqlite
Definition: Driver.php:18
‪TYPO3\CMS\Core\Database\Driver\DriverConnection
Definition: DriverConnection.php:35
‪TYPO3\CMS\Core\Database\Driver\PDOSqlite\Driver\$userDefinedFunctions
‪array $userDefinedFunctions
Definition: Driver.php:34
‪TYPO3\CMS\Core\Database\Driver\PDOSqlite\Driver\connect
‪connect(array $params)
Definition: Driver.php:40
‪TYPO3\CMS\Core\Database\Driver\PDOSqlite\Driver
Definition: Driver.php:33
‪TYPO3\CMS\Core\Database\Driver\PDOSqlite\Driver\constructPdoDsn
‪string constructPdoDsn(array $params)
Definition: Driver.php:73