SchemaException.php 5.79 KB
Newer Older
1 2
<?php

Michael Moravec's avatar
Michael Moravec committed
3 4
declare(strict_types=1);

5 6
namespace Doctrine\DBAL\Schema;

7
use Doctrine\DBAL\DBALException;
8 9 10
use function implode;
use function sprintf;

11
class SchemaException extends DBALException
12
{
13 14 15 16 17 18 19 20 21 22
    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;
23 24
    public const CONSTRAINT_DOESNT_EXIST  = 110;
    public const NAMESPACE_ALREADY_EXISTS = 120;
25 26 27

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

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

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

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

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

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

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

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

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

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

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    /**
     * @param string $constraintName
     * @param string $table
     *
     * @return self
     */
    public static function uniqueConstraintDoesNotExist($constraintName, $table)
    {
        return new self(sprintf(
            'There exists no unique constraint with the name "%s" on table "%s".',
            $constraintName,
            $table
        ), self::CONSTRAINT_DOESNT_EXIST);
    }

163
    /**
Benjamin Morel's avatar
Benjamin Morel committed
164 165 166 167
     * @param string $fkName
     * @param string $table
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
168
     */
169
    public static function foreignKeyDoesNotExist($fkName, $table)
170
    {
171 172 173 174
        return new self(
            sprintf("There exists no foreign key with the name '%s' on table '%s'.", $fkName, $table),
            self::FOREIGNKEY_DOESNT_EXIST
        );
175
    }
176

Benjamin Morel's avatar
Benjamin Morel committed
177 178 179
    /**
     * @return \Doctrine\DBAL\Schema\SchemaException
     */
180
    public static function namedForeignKeyRequired(Table $localTable, ForeignKeyConstraint $foreignKey)
181 182
    {
        return new self(
183 184 185 186
            '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.'
187 188
        );
    }
189

Benjamin Morel's avatar
Benjamin Morel committed
190 191 192 193 194
    /**
     * @param string $changeName
     *
     * @return \Doctrine\DBAL\Schema\SchemaException
     */
195
    public static function alterTableChangeNotSupported($changeName)
Benjamin Morel's avatar
Benjamin Morel committed
196
    {
197 198 199
        return new self(
            sprintf("Alter table change not supported, given '%s'", $changeName)
        );
200
    }
Benjamin Morel's avatar
Benjamin Morel committed
201
}