1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Driver; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Driver; |
7
|
|
|
use Doctrine\DBAL\Exception; |
8
|
|
|
use Doctrine\DBAL\Platforms\SqlitePlatform; |
9
|
|
|
use Doctrine\DBAL\Schema\SqliteSchemaManager; |
10
|
|
|
use function strpos; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for SQLite based drivers. |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
* |
20
|
|
|
* @link http://www.sqlite.org/c3ref/c_abort.html |
21
|
|
|
*/ |
22
|
568 |
|
public function convertException($message, DriverException $exception) |
23
|
|
|
{ |
24
|
568 |
|
if (strpos($exception->getMessage(), 'database is locked') !== false) { |
25
|
408 |
|
return new Exception\LockWaitTimeoutException($message, $exception); |
26
|
|
|
} |
27
|
|
|
|
28
|
568 |
|
if (strpos($exception->getMessage(), 'must be unique') !== false || |
29
|
568 |
|
strpos($exception->getMessage(), 'is not unique') !== false || |
30
|
568 |
|
strpos($exception->getMessage(), 'are not unique') !== false || |
31
|
568 |
|
strpos($exception->getMessage(), 'UNIQUE constraint failed') !== false |
32
|
|
|
) { |
33
|
408 |
|
return new Exception\UniqueConstraintViolationException($message, $exception); |
34
|
|
|
} |
35
|
|
|
|
36
|
568 |
|
if (strpos($exception->getMessage(), 'may not be NULL') !== false || |
37
|
568 |
|
strpos($exception->getMessage(), 'NOT NULL constraint failed') !== false |
38
|
|
|
) { |
39
|
408 |
|
return new Exception\NotNullConstraintViolationException($message, $exception); |
40
|
|
|
} |
41
|
|
|
|
42
|
568 |
|
if (strpos($exception->getMessage(), 'no such table:') !== false) { |
43
|
408 |
|
return new Exception\TableNotFoundException($message, $exception); |
44
|
|
|
} |
45
|
|
|
|
46
|
568 |
|
if (strpos($exception->getMessage(), 'already exists') !== false) { |
47
|
408 |
|
return new Exception\TableExistsException($message, $exception); |
48
|
|
|
} |
49
|
|
|
|
50
|
568 |
|
if (strpos($exception->getMessage(), 'has no column named') !== false) { |
51
|
408 |
|
return new Exception\InvalidFieldNameException($message, $exception); |
52
|
|
|
} |
53
|
|
|
|
54
|
568 |
|
if (strpos($exception->getMessage(), 'ambiguous column name') !== false) { |
55
|
408 |
|
return new Exception\NonUniqueFieldNameException($message, $exception); |
56
|
|
|
} |
57
|
|
|
|
58
|
568 |
|
if (strpos($exception->getMessage(), 'syntax error') !== false) { |
59
|
568 |
|
return new Exception\SyntaxErrorException($message, $exception); |
60
|
|
|
} |
61
|
|
|
|
62
|
408 |
|
if (strpos($exception->getMessage(), 'attempt to write a readonly database') !== false) { |
63
|
408 |
|
return new Exception\ReadOnlyException($message, $exception); |
64
|
|
|
} |
65
|
|
|
|
66
|
408 |
|
if (strpos($exception->getMessage(), 'unable to open database file') !== false) { |
67
|
408 |
|
return new Exception\ConnectionException($message, $exception); |
68
|
|
|
} |
69
|
|
|
|
70
|
408 |
|
return new Exception\DriverException($message, $exception); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
439 |
|
public function getDatabase(Connection $conn) |
77
|
|
|
{ |
78
|
439 |
|
$params = $conn->getParams(); |
79
|
|
|
|
80
|
439 |
|
return $params['path'] ?? null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
393 |
|
public function getDatabasePlatform() |
87
|
|
|
{ |
88
|
393 |
|
return new SqlitePlatform(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
364 |
|
public function getSchemaManager(Connection $conn) |
95
|
|
|
{ |
96
|
364 |
|
return new SqliteSchemaManager($conn); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: