1<?php
2
3namespace Doctrine\DBAL\Schema;
4
5use Doctrine\DBAL\DBALException;
6use function implode;
7use function sprintf;
8
9class SchemaException extends DBALException
10{
11    public const TABLE_DOESNT_EXIST       = 10;
12    public const TABLE_ALREADY_EXISTS     = 20;
13    public const COLUMN_DOESNT_EXIST      = 30;
14    public const COLUMN_ALREADY_EXISTS    = 40;
15    public const INDEX_DOESNT_EXIST       = 50;
16    public const INDEX_ALREADY_EXISTS     = 60;
17    public const SEQUENCE_DOENST_EXIST    = 70;
18    public const SEQUENCE_ALREADY_EXISTS  = 80;
19    public const INDEX_INVALID_NAME       = 90;
20    public const FOREIGNKEY_DOESNT_EXIST  = 100;
21    public const NAMESPACE_ALREADY_EXISTS = 110;
22
23    /**
24     * @param string $tableName
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
35     *
36     * @return \Doctrine\DBAL\Schema\SchemaException
37     */
38    public static function indexNameInvalid($indexName)
39    {
40        return new self(
41            sprintf('Invalid index-name %s given, has to be [a-zA-Z0-9_]', $indexName),
42            self::INDEX_INVALID_NAME
43        );
44    }
45
46    /**
47     * @param string $indexName
48     * @param string $table
49     *
50     * @return \Doctrine\DBAL\Schema\SchemaException
51     */
52    public static function indexDoesNotExist($indexName, $table)
53    {
54        return new self(
55            sprintf("Index '%s' does not exist on table '%s'.", $indexName, $table),
56            self::INDEX_DOESNT_EXIST
57        );
58    }
59
60    /**
61     * @param string $indexName
62     * @param string $table
63     *
64     * @return \Doctrine\DBAL\Schema\SchemaException
65     */
66    public static function indexAlreadyExists($indexName, $table)
67    {
68        return new self(
69            sprintf("An index with name '%s' was already defined on table '%s'.", $indexName, $table),
70            self::INDEX_ALREADY_EXISTS
71        );
72    }
73
74    /**
75     * @param string $columnName
76     * @param string $table
77     *
78     * @return \Doctrine\DBAL\Schema\SchemaException
79     */
80    public static function columnDoesNotExist($columnName, $table)
81    {
82        return new self(
83            sprintf("There is no column with name '%s' on table '%s'.", $columnName, $table),
84            self::COLUMN_DOESNT_EXIST
85        );
86    }
87
88    /**
89     * @param string $namespaceName
90     *
91     * @return \Doctrine\DBAL\Schema\SchemaException
92     */
93    public static function namespaceAlreadyExists($namespaceName)
94    {
95        return new self(
96            sprintf("The namespace with name '%s' already exists.", $namespaceName),
97            self::NAMESPACE_ALREADY_EXISTS
98        );
99    }
100
101    /**
102     * @param string $tableName
103     *
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     * @param string $tableName
113     * @param string $columnName
114     *
115     * @return \Doctrine\DBAL\Schema\SchemaException
116     */
117    public static function columnAlreadyExists($tableName, $columnName)
118    {
119        return new self(
120            "The column '" . $columnName . "' on table '" . $tableName . "' already exists.",
121            self::COLUMN_ALREADY_EXISTS
122        );
123    }
124
125    /**
126     * @param string $sequenceName
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
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    /**
146     * @param string $fkName
147     * @param string $table
148     *
149     * @return \Doctrine\DBAL\Schema\SchemaException
150     */
151    public static function foreignKeyDoesNotExist($fkName, $table)
152    {
153        return new self(
154            sprintf("There exists no foreign key with the name '%s' on table '%s'.", $fkName, $table),
155            self::FOREIGNKEY_DOESNT_EXIST
156        );
157    }
158
159    /**
160     * @return \Doctrine\DBAL\Schema\SchemaException
161     */
162    public static function namedForeignKeyRequired(Table $localTable, ForeignKeyConstraint $foreignKey)
163    {
164        return new self(
165            'The performed schema operation on ' . $localTable->getName() . ' requires a named foreign key, ' .
166            'but the given foreign key from (' . implode(', ', $foreignKey->getColumns()) . ') onto foreign table ' .
167            "'" . $foreignKey->getForeignTableName() . "' (" . implode(', ', $foreignKey->getForeignColumns()) . ') is currently ' .
168            'unnamed.'
169        );
170    }
171
172    /**
173     * @param string $changeName
174     *
175     * @return \Doctrine\DBAL\Schema\SchemaException
176     */
177    public static function alterTableChangeNotSupported($changeName)
178    {
179        return new self(
180            sprintf("Alter table change not supported, given '%s'", $changeName)
181        );
182    }
183}
184