SchemaException.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Schema;
  20. class SchemaException extends \Doctrine\DBAL\DBALException
  21. {
  22. const TABLE_DOESNT_EXIST = 10;
  23. const TABLE_ALREADY_EXISTS = 20;
  24. const COLUMN_DOESNT_EXIST = 30;
  25. const COLUMN_ALREADY_EXISTS = 40;
  26. const INDEX_DOESNT_EXIST = 50;
  27. const INDEX_ALREADY_EXISTS = 60;
  28. const SEQUENCE_DOENST_EXIST = 70;
  29. const SEQUENCE_ALREADY_EXISTS = 80;
  30. const INDEX_INVALID_NAME = 90;
  31. const FOREIGNKEY_DOESNT_EXIST = 100;
  32. const NAMESPACE_ALREADY_EXISTS = 110;
  33. /**
  34. * @param string $tableName
  35. *
  36. * @return \Doctrine\DBAL\Schema\SchemaException
  37. */
  38. static public function tableDoesNotExist($tableName)
  39. {
  40. return new self("There is no table with name '".$tableName."' in the schema.", self::TABLE_DOESNT_EXIST);
  41. }
  42. /**
  43. * @param string $indexName
  44. *
  45. * @return \Doctrine\DBAL\Schema\SchemaException
  46. */
  47. static public function indexNameInvalid($indexName)
  48. {
  49. return new self("Invalid index-name $indexName given, has to be [a-zA-Z0-9_]", self::INDEX_INVALID_NAME);
  50. }
  51. /**
  52. * @param string $indexName
  53. * @param string $table
  54. *
  55. * @return \Doctrine\DBAL\Schema\SchemaException
  56. */
  57. static public function indexDoesNotExist($indexName, $table)
  58. {
  59. return new self("Index '$indexName' does not exist on table '$table'.", self::INDEX_DOESNT_EXIST);
  60. }
  61. /**
  62. * @param string $indexName
  63. * @param string $table
  64. *
  65. * @return \Doctrine\DBAL\Schema\SchemaException
  66. */
  67. static public function indexAlreadyExists($indexName, $table)
  68. {
  69. return new self("An index with name '$indexName' was already defined on table '$table'.", self::INDEX_ALREADY_EXISTS);
  70. }
  71. /**
  72. * @param string $columnName
  73. * @param string $table
  74. *
  75. * @return \Doctrine\DBAL\Schema\SchemaException
  76. */
  77. static public function columnDoesNotExist($columnName, $table)
  78. {
  79. return new self("There is no column with name '$columnName' on table '$table'.", self::COLUMN_DOESNT_EXIST);
  80. }
  81. /**
  82. * @param string $namespaceName
  83. *
  84. * @return \Doctrine\DBAL\Schema\SchemaException
  85. */
  86. static public function namespaceAlreadyExists($namespaceName)
  87. {
  88. return new self(
  89. sprintf("The namespace with name '%s' already exists.", $namespaceName),
  90. self::NAMESPACE_ALREADY_EXISTS
  91. );
  92. }
  93. /**
  94. * @param string $tableName
  95. *
  96. * @return \Doctrine\DBAL\Schema\SchemaException
  97. */
  98. static public function tableAlreadyExists($tableName)
  99. {
  100. return new self("The table with name '".$tableName."' already exists.", self::TABLE_ALREADY_EXISTS);
  101. }
  102. /**
  103. * @param string $tableName
  104. * @param string $columnName
  105. *
  106. * @return \Doctrine\DBAL\Schema\SchemaException
  107. */
  108. static public function columnAlreadyExists($tableName, $columnName)
  109. {
  110. return new self(
  111. "The column '".$columnName."' on table '".$tableName."' already exists.", self::COLUMN_ALREADY_EXISTS
  112. );
  113. }
  114. /**
  115. * @param string $sequenceName
  116. *
  117. * @return \Doctrine\DBAL\Schema\SchemaException
  118. */
  119. static public function sequenceAlreadyExists($sequenceName)
  120. {
  121. return new self("The sequence '".$sequenceName."' already exists.", self::SEQUENCE_ALREADY_EXISTS);
  122. }
  123. /**
  124. * @param string $sequenceName
  125. *
  126. * @return \Doctrine\DBAL\Schema\SchemaException
  127. */
  128. static public function sequenceDoesNotExist($sequenceName)
  129. {
  130. return new self("There exists no sequence with the name '".$sequenceName."'.", self::SEQUENCE_DOENST_EXIST);
  131. }
  132. /**
  133. * @param string $fkName
  134. * @param string $table
  135. *
  136. * @return \Doctrine\DBAL\Schema\SchemaException
  137. */
  138. static public function foreignKeyDoesNotExist($fkName, $table)
  139. {
  140. return new self("There exists no foreign key with the name '$fkName' on table '$table'.", self::FOREIGNKEY_DOESNT_EXIST);
  141. }
  142. /**
  143. * @param \Doctrine\DBAL\Schema\Table $localTable
  144. * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey
  145. *
  146. * @return \Doctrine\DBAL\Schema\SchemaException
  147. */
  148. static public function namedForeignKeyRequired(Table $localTable, ForeignKeyConstraint $foreignKey)
  149. {
  150. return new self(
  151. "The performed schema operation on ".$localTable->getName()." requires a named foreign key, ".
  152. "but the given foreign key from (".implode(", ", $foreignKey->getColumns()).") onto foreign table ".
  153. "'".$foreignKey->getForeignTableName()."' (".implode(", ", $foreignKey->getForeignColumns()).") is currently ".
  154. "unnamed."
  155. );
  156. }
  157. /**
  158. * @param string $changeName
  159. *
  160. * @return \Doctrine\DBAL\Schema\SchemaException
  161. */
  162. static public function alterTableChangeNotSupported($changeName)
  163. {
  164. return new self("Alter table change not supported, given '$changeName'");
  165. }
  166. }