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
|
|
|
|
18
|
|
|
namespace TYPO3\CMS\Core\Database\Driver\PDOSqlite; |
19
|
|
|
|
20
|
|
|
use Doctrine\DBAL\Driver\AbstractSQLiteDriver; |
21
|
|
|
use Doctrine\DBAL\Exception as DBALException; |
22
|
|
|
use Doctrine\DBAL\Platforms\SqlitePlatform; |
23
|
|
|
use PDOException; |
24
|
|
|
use TYPO3\CMS\Core\Database\Driver\PDOConnection; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This is a full "clone" of the class of package doctrine/dbal. Scope is to use the PDOConnection of TYPO3. |
28
|
|
|
* All private methods have to be checked on every release of doctrine/dbal. |
29
|
|
|
*/ |
30
|
|
|
class Driver extends AbstractSQLiteDriver |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var mixed[] |
34
|
|
|
*/ |
35
|
|
|
protected $_userDefinedFunctions = [ |
36
|
|
|
'sqrt' => ['callback' => [SqlitePlatform::class, 'udfSqrt'], 'numArgs' => 1], |
37
|
|
|
'mod' => ['callback' => [SqlitePlatform::class, 'udfMod'], 'numArgs' => 2], |
38
|
|
|
'locate' => ['callback' => [SqlitePlatform::class, 'udfLocate'], 'numArgs' => -1], |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) |
45
|
|
|
{ |
46
|
|
|
if (isset($driverOptions['userDefinedFunctions'])) { |
47
|
|
|
$this->_userDefinedFunctions = array_merge( |
48
|
|
|
$this->_userDefinedFunctions, |
49
|
|
|
$driverOptions['userDefinedFunctions'] |
50
|
|
|
); |
51
|
|
|
unset($driverOptions['userDefinedFunctions']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
$pdo = new PDOConnection( |
56
|
|
|
$this->_constructPdoDsn($params), |
57
|
|
|
$username, |
58
|
|
|
$password, |
59
|
|
|
$driverOptions |
60
|
|
|
); |
61
|
|
|
} catch (PDOException $ex) { |
62
|
|
|
throw DBALException::driverException($this, $ex); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
foreach ($this->_userDefinedFunctions as $fn => $data) { |
66
|
|
|
$pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $pdo; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Constructs the Sqlite PDO DSN. |
74
|
|
|
* |
75
|
|
|
* @param mixed[] $params |
76
|
|
|
* |
77
|
|
|
* @return string The DSN. |
78
|
|
|
*/ |
79
|
|
|
protected function _constructPdoDsn(array $params) |
80
|
|
|
{ |
81
|
|
|
$dsn = 'sqlite:'; |
82
|
|
|
if (isset($params['path'])) { |
83
|
|
|
$dsn .= $params['path']; |
84
|
|
|
} elseif (isset($params['memory'])) { |
85
|
|
|
$dsn .= ':memory:'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $dsn; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
* |
94
|
|
|
* @deprecated |
95
|
|
|
*/ |
96
|
|
|
public function getName() |
97
|
|
|
{ |
98
|
|
|
return 'pdo_sqlite'; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|