SchemaException.php 5.0 KB

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