SchemaException.php 5.31 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Schema;

5
use Doctrine\DBAL\DBALException;
6 7 8
use function implode;
use function sprintf;

9
class SchemaException extends DBALException
10
{
11 12 13 14 15 16 17 18 19 20 21
    public const TABLE_DOESNT_EXIST       = 10;
    public const TABLE_ALREADY_EXISTS     = 20;
    public const COLUMN_DOESNT_EXIST      = 30;
    public const COLUMN_ALREADY_EXISTS    = 40;
    public const INDEX_DOESNT_EXIST       = 50;
    public const INDEX_ALREADY_EXISTS     = 60;
    public const SEQUENCE_DOENST_EXIST    = 70;
    public const SEQUENCE_ALREADY_EXISTS  = 80;
    public const INDEX_INVALID_NAME       = 90;
    public const FOREIGNKEY_DOESNT_EXIST  = 100;
    public const NAMESPACE_ALREADY_EXISTS = 110;
22 23 24

    /**
     * @param string $tableName
Benjamin Morel's avatar
Benjamin Morel committed
25 26
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
27
     */
28
    public static function tableDoesNotExist($tableName)
29
    {
30
        return new self("There is no table with name '" . $tableName . "' in the schema.", self::TABLE_DOESNT_EXIST);
31 32 33 34
    }

    /**
     * @param string $indexName
Benjamin Morel's avatar
Benjamin Morel committed
35 36
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
37
     */
38
    public static function indexNameInvalid($indexName)
39
    {
40 41 42 43
        return new self(
            sprintf('Invalid index-name %s given, has to be [a-zA-Z0-9_]', $indexName),
            self::INDEX_INVALID_NAME
        );
44 45 46 47
    }

    /**
     * @param string $indexName
Benjamin Morel's avatar
Benjamin Morel committed
48 49 50
     * @param string $table
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
51
     */
52
    public static function indexDoesNotExist($indexName, $table)
53
    {
54 55 56 57
        return new self(
            sprintf("Index '%s' does not exist on table '%s'.", $indexName, $table),
            self::INDEX_DOESNT_EXIST
        );
58 59 60 61
    }

    /**
     * @param string $indexName
Benjamin Morel's avatar
Benjamin Morel committed
62 63 64
     * @param string $table
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
65
     */
66
    public static function indexAlreadyExists($indexName, $table)
67
    {
68 69 70 71
        return new self(
            sprintf("An index with name '%s' was already defined on table '%s'.", $indexName, $table),
            self::INDEX_ALREADY_EXISTS
        );
72 73 74 75
    }

    /**
     * @param string $columnName
Benjamin Morel's avatar
Benjamin Morel committed
76 77 78
     * @param string $table
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
79
     */
80
    public static function columnDoesNotExist($columnName, $table)
81
    {
82 83 84 85
        return new self(
            sprintf("There is no column with name '%s' on table '%s'.", $columnName, $table),
            self::COLUMN_DOESNT_EXIST
        );
86 87
    }

88 89 90 91 92
    /**
     * @param string $namespaceName
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
     */
93
    public static function namespaceAlreadyExists($namespaceName)
94 95 96 97 98 99 100
    {
        return new self(
            sprintf("The namespace with name '%s' already exists.", $namespaceName),
            self::NAMESPACE_ALREADY_EXISTS
        );
    }

101
    /**
Benjamin Morel's avatar
Benjamin Morel committed
102
     * @param string $tableName
103
     *
Benjamin Morel's avatar
Benjamin Morel committed
104
     * @return \Doctrine\DBAL\Schema\SchemaException
105
     */
106
    public static function tableAlreadyExists($tableName)
107
    {
108
        return new self("The table with name '" . $tableName . "' already exists.", self::TABLE_ALREADY_EXISTS);
109 110 111 112 113
    }

    /**
     * @param string $tableName
     * @param string $columnName
Benjamin Morel's avatar
Benjamin Morel committed
114 115
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
116
     */
117
    public static function columnAlreadyExists($tableName, $columnName)
118 119
    {
        return new self(
120 121
            "The column '" . $columnName . "' on table '" . $tableName . "' already exists.",
            self::COLUMN_ALREADY_EXISTS
122 123 124 125 126
        );
    }

    /**
     * @param string $sequenceName
Benjamin Morel's avatar
Benjamin Morel committed
127 128
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
129
     */
130
    public static function sequenceAlreadyExists($sequenceName)
131
    {
132
        return new self("The sequence '" . $sequenceName . "' already exists.", self::SEQUENCE_ALREADY_EXISTS);
133 134 135 136
    }

    /**
     * @param string $sequenceName
Benjamin Morel's avatar
Benjamin Morel committed
137 138
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
139
     */
140
    public static function sequenceDoesNotExist($sequenceName)
141
    {
142
        return new self("There exists no sequence with the name '" . $sequenceName . "'.", self::SEQUENCE_DOENST_EXIST);
143
    }
144 145

    /**
Benjamin Morel's avatar
Benjamin Morel committed
146 147 148 149
     * @param string $fkName
     * @param string $table
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
150
     */
151
    public static function foreignKeyDoesNotExist($fkName, $table)
152
    {
153 154 155 156
        return new self(
            sprintf("There exists no foreign key with the name '%s' on table '%s'.", $fkName, $table),
            self::FOREIGNKEY_DOESNT_EXIST
        );
157
    }
158

Benjamin Morel's avatar
Benjamin Morel committed
159 160 161
    /**
     * @return \Doctrine\DBAL\Schema\SchemaException
     */
162
    public static function namedForeignKeyRequired(Table $localTable, ForeignKeyConstraint $foreignKey)
163 164
    {
        return new self(
165 166 167 168
            'The performed schema operation on ' . $localTable->getName() . ' requires a named foreign key, ' .
            'but the given foreign key from (' . implode(', ', $foreignKey->getColumns()) . ') onto foreign table ' .
            "'" . $foreignKey->getForeignTableName() . "' (" . implode(', ', $foreignKey->getForeignColumns()) . ') is currently ' .
            'unnamed.'
169 170
        );
    }
171

Benjamin Morel's avatar
Benjamin Morel committed
172 173 174 175 176
    /**
     * @param string $changeName
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
     */
177
    public static function alterTableChangeNotSupported($changeName)
Benjamin Morel's avatar
Benjamin Morel committed
178
    {
179 180 181
        return new self(
            sprintf("Alter table change not supported, given '%s'", $changeName)
        );
182
    }
Benjamin Morel's avatar
Benjamin Morel committed
183
}