DBALException.php 9.0 KB

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