Completed
Pull Request — 2.10.x (#3984)
by Craig
61:21 queued 57:56
created

AbstractSQLiteDriver::getDatabasePlatform()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Doctrine\DBAL\Driver;
4
5
use Doctrine\DBAL\Connection;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Doctrine\DBAL\Driver\Connection. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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 1140
    public function convertException($message, DriverException $exception)
23
    {
24 1140
        if (strpos($exception->getMessage(), 'database is locked') !== false) {
25 720
            return new Exception\LockWaitTimeoutException($message, $exception);
26
        }
27
28 1138
        if (strpos($exception->getMessage(), 'must be unique') !== false ||
29 1136
            strpos($exception->getMessage(), 'is not unique') !== false ||
30 1134
            strpos($exception->getMessage(), 'are not unique') !== false ||
31 1138
            strpos($exception->getMessage(), 'UNIQUE constraint failed') !== false
32
        ) {
33 793
            return new Exception\UniqueConstraintViolationException($message, $exception);
34
        }
35
36 1132
        if (strpos($exception->getMessage(), 'may not be NULL') !== false ||
37 1132
            strpos($exception->getMessage(), 'NOT NULL constraint failed') !== false
38
        ) {
39 904
            return new Exception\NotNullConstraintViolationException($message, $exception);
40
        }
41
42 1130
        if (strpos($exception->getMessage(), 'no such table:') !== false) {
43 812
            return new Exception\TableNotFoundException($message, $exception);
44
        }
45
46 1128
        if (strpos($exception->getMessage(), 'already exists') !== false) {
47 835
            return new Exception\TableExistsException($message, $exception);
48
        }
49
50 1126
        if (strpos($exception->getMessage(), 'has no column named') !== false) {
51 950
            return new Exception\InvalidFieldNameException($message, $exception);
52
        }
53
54 1124
        if (strpos($exception->getMessage(), 'ambiguous column name') !== false) {
55 927
            return new Exception\NonUniqueFieldNameException($message, $exception);
56
        }
57
58 1122
        if (strpos($exception->getMessage(), 'syntax error') !== false) {
59 1116
            return new Exception\SyntaxErrorException($message, $exception);
60
        }
61
62 977
        if (strpos($exception->getMessage(), 'attempt to write a readonly database') !== false) {
63 881
            return new Exception\ReadOnlyException($message, $exception);
64
        }
65
66 975
        if (strpos($exception->getMessage(), 'unable to open database file') !== false) {
67 973
            return new Exception\ConnectionException($message, $exception);
68
        }
69
70 697
        if (strpos($exception->getMessage(), 'FOREIGN KEY constraint failed') !== false) {
71 109
            return new Exception\ForeignKeyConstraintViolationException($message, $exception);
72
        }
73
74 697
        return new Exception\DriverException($message, $exception);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 999
    public function getDatabase(Connection $conn)
81
    {
82 999
        $params = $conn->getParams();
83
84 999
        return $params['path'] ?? null;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 680
    public function getDatabasePlatform()
91
    {
92 680
        return new SqlitePlatform();
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 654
    public function getSchemaManager(Connection $conn)
99
    {
100 654
        return new SqliteSchemaManager($conn);
101
    }
102
}
103