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