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

namespace Doctrine\DBAL\Schema;

5
use Doctrine\DBAL\DBALException;
6

7 8 9
use function implode;
use function sprintf;

10 11 12
/**
 * @psalm-immutable
 */
13
class SchemaException extends DBALException
14
{
15 16 17 18 19 20 21 22 23 24 25
    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;
26 27 28

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

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

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

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

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

92 93 94
    /**
     * @param string $namespaceName
     *
Grégoire Paris's avatar
Grégoire Paris committed
95
     * @return SchemaException
96
     */
97
    public static function namespaceAlreadyExists($namespaceName)
98 99 100 101 102 103 104
    {
        return new self(
            sprintf("The namespace with name '%s' already exists.", $namespaceName),
            self::NAMESPACE_ALREADY_EXISTS
        );
    }

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

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

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

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
150 151 152
     * @param string $fkName
     * @param string $table
     *
Grégoire Paris's avatar
Grégoire Paris committed
153
     * @return SchemaException
154
     */
155
    public static function foreignKeyDoesNotExist($fkName, $table)
156
    {
157 158 159 160
        return new self(
            sprintf("There exists no foreign key with the name '%s' on table '%s'.", $fkName, $table),
            self::FOREIGNKEY_DOESNT_EXIST
        );
161
    }
162

Benjamin Morel's avatar
Benjamin Morel committed
163
    /**
Grégoire Paris's avatar
Grégoire Paris committed
164
     * @return SchemaException
Benjamin Morel's avatar
Benjamin Morel committed
165
     */
166
    public static function namedForeignKeyRequired(Table $localTable, ForeignKeyConstraint $foreignKey)
167 168
    {
        return new self(
169 170 171 172
            '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.'
173 174
        );
    }
175

Benjamin Morel's avatar
Benjamin Morel committed
176 177 178
    /**
     * @param string $changeName
     *
Grégoire Paris's avatar
Grégoire Paris committed
179
     * @return SchemaException
Benjamin Morel's avatar
Benjamin Morel committed
180
     */
181
    public static function alterTableChangeNotSupported($changeName)
Benjamin Morel's avatar
Benjamin Morel committed
182
    {
183 184 185
        return new self(
            sprintf("Alter table change not supported, given '%s'", $changeName)
        );
186
    }
Benjamin Morel's avatar
Benjamin Morel committed
187
}