AbstractSQLiteDriver.php 3.75 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Driver;

5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Driver;
7
use Doctrine\DBAL\Driver\Exception as TheDriverException;
8 9 10 11 12 13 14 15 16 17 18 19
use Doctrine\DBAL\Exception\ConnectionException;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
use Doctrine\DBAL\Exception\InvalidFieldNameException;
use Doctrine\DBAL\Exception\LockWaitTimeoutException;
use Doctrine\DBAL\Exception\NonUniqueFieldNameException;
use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
use Doctrine\DBAL\Exception\ReadOnlyException;
use Doctrine\DBAL\Exception\SyntaxErrorException;
use Doctrine\DBAL\Exception\TableExistsException;
use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
20 21
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\SqliteSchemaManager;
22

23
use function strpos;
24 25 26 27 28 29 30 31 32 33 34

/**
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for SQLite based drivers.
 */
abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver
{
    /**
     * {@inheritdoc}
     *
     * @link http://www.sqlite.org/c3ref/c_abort.html
     */
35
    public function convertException($message, TheDriverException $exception)
36
    {
37
        if (strpos($exception->getMessage(), 'database is locked') !== false) {
38
            return new LockWaitTimeoutException($message, $exception);
39 40
        }

41 42
        if (
            strpos($exception->getMessage(), 'must be unique') !== false ||
43
            strpos($exception->getMessage(), 'is not unique') !== false ||
44
            strpos($exception->getMessage(), 'are not unique') !== false ||
45 46
            strpos($exception->getMessage(), 'UNIQUE constraint failed') !== false
        ) {
47
            return new UniqueConstraintViolationException($message, $exception);
48 49
        }

50 51
        if (
            strpos($exception->getMessage(), 'may not be NULL') !== false ||
52 53
            strpos($exception->getMessage(), 'NOT NULL constraint failed') !== false
        ) {
54
            return new NotNullConstraintViolationException($message, $exception);
55 56 57
        }

        if (strpos($exception->getMessage(), 'no such table:') !== false) {
58
            return new TableNotFoundException($message, $exception);
59 60 61
        }

        if (strpos($exception->getMessage(), 'already exists') !== false) {
62
            return new TableExistsException($message, $exception);
63 64 65
        }

        if (strpos($exception->getMessage(), 'has no column named') !== false) {
66
            return new InvalidFieldNameException($message, $exception);
67 68 69
        }

        if (strpos($exception->getMessage(), 'ambiguous column name') !== false) {
70
            return new NonUniqueFieldNameException($message, $exception);
71 72 73
        }

        if (strpos($exception->getMessage(), 'syntax error') !== false) {
74
            return new SyntaxErrorException($message, $exception);
75 76 77
        }

        if (strpos($exception->getMessage(), 'attempt to write a readonly database') !== false) {
78
            return new ReadOnlyException($message, $exception);
79 80 81
        }

        if (strpos($exception->getMessage(), 'unable to open database file') !== false) {
82
            return new ConnectionException($message, $exception);
83 84
        }

85
        if (strpos($exception->getMessage(), 'FOREIGN KEY constraint failed') !== false) {
86
            return new ForeignKeyConstraintViolationException($message, $exception);
87 88
        }

89
        return new DriverException($message, $exception);
90 91 92 93 94 95 96 97 98 99 100 101 102
    }

    /**
     * {@inheritdoc}
     */
    public function getDatabasePlatform()
    {
        return new SqlitePlatform();
    }

    /**
     * {@inheritdoc}
     */
103
    public function getSchemaManager(Connection $conn)
104 105 106 107
    {
        return new SqliteSchemaManager($conn);
    }
}