DBALException.php 8.2 KB

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