SchemaException.php 5.3 KB

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