Issues (201)

src/Driver/AbstractSQLiteDriver.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver;
6
7
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...
8
use Doctrine\DBAL\Driver;
9
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
10
use Doctrine\DBAL\Exception;
11
use Doctrine\DBAL\Exception\DriverException;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Doctrine\DBAL\Driver\DriverException. 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...
12
use Doctrine\DBAL\Platforms\AbstractPlatform;
13
use Doctrine\DBAL\Platforms\SqlitePlatform;
14
use Doctrine\DBAL\Schema\AbstractSchemaManager;
15
use Doctrine\DBAL\Schema\SqliteSchemaManager;
16
use function strpos;
17
18
/**
19
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for SQLite based drivers.
20
 */
21
abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver
22
{
23
    /**
24
     * {@inheritdoc}
25
     *
26
     * @link http://www.sqlite.org/c3ref/c_abort.html
27
     */
28 498
    public function convertException(string $message, DriverExceptionInterface $exception) : DriverException
29
    {
30 498
        if (strpos($exception->getMessage(), 'database is locked') !== false) {
31 22
            return new Exception\LockWaitTimeoutException($message, $exception);
32
        }
33
34 476
        if (strpos($exception->getMessage(), 'must be unique') !== false ||
35 475
            strpos($exception->getMessage(), 'is not unique') !== false ||
36 474
            strpos($exception->getMessage(), 'are not unique') !== false ||
37 476
            strpos($exception->getMessage(), 'UNIQUE constraint failed') !== false
38
        ) {
39 68
            return new Exception\UniqueConstraintViolationException($message, $exception);
40
        }
41
42 408
        if (strpos($exception->getMessage(), 'FOREIGN KEY constraint failed') !== false) {
43 26
            return new Exception\ForeignKeyConstraintViolationException($message, $exception);
44
        }
45
46 382
        if (strpos($exception->getMessage(), 'may not be NULL') !== false ||
47 382
            strpos($exception->getMessage(), 'NOT NULL constraint failed') !== false
48
        ) {
49 23
            return new Exception\NotNullConstraintViolationException($message, $exception);
50
        }
51
52 359
        if (strpos($exception->getMessage(), 'no such table:') !== false) {
53 89
            return new Exception\TableNotFoundException($message, $exception);
54
        }
55
56 318
        if (strpos($exception->getMessage(), 'already exists') !== false) {
57 23
            return new Exception\TableExistsException($message, $exception);
58
        }
59
60 295
        if (strpos($exception->getMessage(), 'has no column named') !== false) {
61 23
            return new Exception\InvalidFieldNameException($message, $exception);
62
        }
63
64 272
        if (strpos($exception->getMessage(), 'ambiguous column name') !== false) {
65 23
            return new Exception\NonUniqueFieldNameException($message, $exception);
66
        }
67
68 249
        if (strpos($exception->getMessage(), 'syntax error') !== false) {
69 181
            return new Exception\SyntaxErrorException($message, $exception);
70
        }
71
72 69
        if (strpos($exception->getMessage(), 'attempt to write a readonly database') !== false) {
73 23
            return new Exception\ReadOnlyException($message, $exception);
74
        }
75
76 46
        if (strpos($exception->getMessage(), 'unable to open database file') !== false) {
77 22
            return new Exception\ConnectionException($message, $exception);
78
        }
79
80 24
        return new DriverException($message, $exception);
81
    }
82
83 126
    public function getDatabasePlatform() : AbstractPlatform
84
    {
85 126
        return new SqlitePlatform();
86
    }
87
88 96
    public function getSchemaManager(Connection $conn) : AbstractSchemaManager
89
    {
90 96
        return new SqliteSchemaManager($conn);
91
    }
92
}
93