DBALException.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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;
  20. class DBALException extends \Exception
  21. {
  22. /**
  23. * @param string $method
  24. *
  25. * @return \Doctrine\DBAL\DBALException
  26. */
  27. public static function notSupported($method)
  28. {
  29. return new self("Operation '$method' is not supported by platform.");
  30. }
  31. /**
  32. * @return \Doctrine\DBAL\DBALException
  33. */
  34. public static function invalidPlatformSpecified()
  35. {
  36. return new self(
  37. "Invalid 'platform' option specified, need to give an instance of ".
  38. "\Doctrine\DBAL\Platforms\AbstractPlatform.");
  39. }
  40. /**
  41. * @return \Doctrine\DBAL\DBALException
  42. */
  43. public static function invalidPdoInstance()
  44. {
  45. return new self(
  46. "The 'pdo' option was used in DriverManager::getConnection() but no ".
  47. "instance of PDO was given."
  48. );
  49. }
  50. /**
  51. * @return \Doctrine\DBAL\DBALException
  52. */
  53. public static function driverRequired()
  54. {
  55. return new self("The options 'driver' or 'driverClass' are mandatory if no PDO ".
  56. "instance is given to DriverManager::getConnection().");
  57. }
  58. /**
  59. * @param string $unknownDriverName
  60. * @param array $knownDrivers
  61. *
  62. * @return \Doctrine\DBAL\DBALException
  63. */
  64. public static function unknownDriver($unknownDriverName, array $knownDrivers)
  65. {
  66. return new self("The given 'driver' ".$unknownDriverName." is unknown, ".
  67. "Doctrine currently supports only the following drivers: ".implode(", ", $knownDrivers));
  68. }
  69. /**
  70. * @param \Exception $driverEx
  71. * @param string $sql
  72. * @param array $params
  73. *
  74. * @return \Doctrine\DBAL\DBALException
  75. */
  76. public static function driverExceptionDuringQuery(\Exception $driverEx, $sql, array $params = array())
  77. {
  78. $msg = "An exception occurred while executing '".$sql."'";
  79. if ($params) {
  80. $msg .= " with params " . self::formatParameters($params);
  81. }
  82. $msg .= ":\n\n".$driverEx->getMessage();
  83. return new self($msg, 0, $driverEx);
  84. }
  85. /**
  86. * Returns a human-readable representation of an array of parameters.
  87. * This properly handles binary data by returning a hex representation.
  88. *
  89. * @param array $params
  90. *
  91. * @return string
  92. */
  93. private static function formatParameters(array $params)
  94. {
  95. return '[' . implode(', ', array_map(function($param) {
  96. $json = @json_encode($param);
  97. if (! is_string($json) || $json == 'null' && is_string($param)) {
  98. // JSON encoding failed, this is not a UTF-8 string.
  99. return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"';
  100. }
  101. return $json;
  102. }, $params)) . ']';
  103. }
  104. /**
  105. * @param string $wrapperClass
  106. *
  107. * @return \Doctrine\DBAL\DBALException
  108. */
  109. public static function invalidWrapperClass($wrapperClass)
  110. {
  111. return new self("The given 'wrapperClass' ".$wrapperClass." has to be a ".
  112. "subtype of \Doctrine\DBAL\Connection.");
  113. }
  114. /**
  115. * @param string $driverClass
  116. *
  117. * @return \Doctrine\DBAL\DBALException
  118. */
  119. public static function invalidDriverClass($driverClass)
  120. {
  121. return new self("The given 'driverClass' ".$driverClass." has to implement the ".
  122. "\Doctrine\DBAL\Driver interface.");
  123. }
  124. /**
  125. * @param string $tableName
  126. *
  127. * @return \Doctrine\DBAL\DBALException
  128. */
  129. public static function invalidTableName($tableName)
  130. {
  131. return new self("Invalid table name specified: ".$tableName);
  132. }
  133. /**
  134. * @param string $tableName
  135. *
  136. * @return \Doctrine\DBAL\DBALException
  137. */
  138. public static function noColumnsSpecifiedForTable($tableName)
  139. {
  140. return new self("No columns specified for table ".$tableName);
  141. }
  142. /**
  143. * @return \Doctrine\DBAL\DBALException
  144. */
  145. public static function limitOffsetInvalid()
  146. {
  147. return new self("Invalid Offset in Limit Query, it has to be larger or equal to 0.");
  148. }
  149. /**
  150. * @param string $name
  151. *
  152. * @return \Doctrine\DBAL\DBALException
  153. */
  154. public static function typeExists($name)
  155. {
  156. return new self('Type '.$name.' already exists.');
  157. }
  158. /**
  159. * @param string $name
  160. *
  161. * @return \Doctrine\DBAL\DBALException
  162. */
  163. public static function unknownColumnType($name)
  164. {
  165. return new self('Unknown column type "'.$name.'" requested. Any Doctrine type that you use has ' .
  166. 'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the ' .
  167. 'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database ' .
  168. 'introspection then you might have forgot to register all database types for a Doctrine Type. Use ' .
  169. 'AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement ' .
  170. 'Type#getMappedDatabaseTypes(). If the type name is empty you might ' .
  171. 'have a problem with the cache or forgot some mapping information.'
  172. );
  173. }
  174. /**
  175. * @param string $name
  176. *
  177. * @return \Doctrine\DBAL\DBALException
  178. */
  179. public static function typeNotFound($name)
  180. {
  181. return new self('Type to be overwritten '.$name.' does not exist.');
  182. }
  183. }