Completed
Push — master ( 5dd66e...5b5c2c )
by Marco
114:19 queued 111:15
created

lib/Doctrine/DBAL/Driver/AbstractSQLiteDriver.php (1 issue)

1
<?php
2
3
namespace Doctrine\DBAL\Driver;
4
5
use Doctrine\DBAL\Connection;
0 ignored issues
show
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 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