DBALException.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Doctrine\DBAL;
  3. class DBALException extends \Exception
  4. {
  5. public static function notSupported($method)
  6. {
  7. return new self("Operation '$method' is not supported by platform.");
  8. }
  9. public static function invalidPlatformSpecified()
  10. {
  11. return new self(
  12. "Invalid 'platform' option specified, need to give an instance of ".
  13. "\Doctrine\DBAL\Platforms\AbstractPlatform.");
  14. }
  15. public static function invalidPdoInstance()
  16. {
  17. return new self(
  18. "The 'pdo' option was used in DriverManager::getConnection() but no ".
  19. "instance of PDO was given."
  20. );
  21. }
  22. public static function driverRequired()
  23. {
  24. return new self("The options 'driver' or 'driverClass' are mandatory if no PDO ".
  25. "instance is given to DriverManager::getConnection().");
  26. }
  27. public static function unknownDriver($unknownDriverName, array $knownDrivers)
  28. {
  29. return new self("The given 'driver' ".$unknownDriverName." is unknown, ".
  30. "Doctrine currently supports only the following drivers: ".implode(", ", $knownDrivers));
  31. }
  32. public static function driverExceptionDuringQuery(\Exception $driverEx, $sql, array $params = array())
  33. {
  34. $msg = "An exception occurred while executing '".$sql."'";
  35. if ($params) {
  36. $msg .= " with params " . self::formatParameters($params);
  37. }
  38. $msg .= ":\n\n".$driverEx->getMessage();
  39. return new self($msg, 0, $driverEx);
  40. }
  41. /**
  42. * Returns a human-readable representation of an array of parameters.
  43. * This properly handles binary data by returning a hex representation.
  44. *
  45. * @param array $params
  46. *
  47. * @return string
  48. */
  49. private static function formatParameters(array $params)
  50. {
  51. return '[' . implode(', ', array_map(function($param) {
  52. $json = @json_encode($param);
  53. if (! is_string($json) || $json == 'null' && is_string($param)) {
  54. // JSON encoding failed, this is not a UTF-8 string.
  55. return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"';
  56. }
  57. return $json;
  58. }, $params)) . ']';
  59. }
  60. public static function invalidWrapperClass($wrapperClass)
  61. {
  62. return new self("The given 'wrapperClass' ".$wrapperClass." has to be a ".
  63. "subtype of \Doctrine\DBAL\Connection.");
  64. }
  65. public static function invalidDriverClass($driverClass)
  66. {
  67. return new self("The given 'driverClass' ".$driverClass." has to implement the ".
  68. "\Doctrine\DBAL\Driver interface.");
  69. }
  70. /**
  71. * @param string $tableName
  72. * @return DBALException
  73. */
  74. public static function invalidTableName($tableName)
  75. {
  76. return new self("Invalid table name specified: ".$tableName);
  77. }
  78. /**
  79. * @param string $tableName
  80. * @return DBALException
  81. */
  82. public static function noColumnsSpecifiedForTable($tableName)
  83. {
  84. return new self("No columns specified for table ".$tableName);
  85. }
  86. public static function limitOffsetInvalid()
  87. {
  88. return new self("Invalid Offset in Limit Query, it has to be larger or equal to 0.");
  89. }
  90. public static function typeExists($name)
  91. {
  92. return new self('Type '.$name.' already exists.');
  93. }
  94. public static function unknownColumnType($name)
  95. {
  96. return new self('Unknown column type "'.$name.'" requested. Any Doctrine type that you use has ' .
  97. 'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the ' .
  98. 'known types with \Doctrine\DBAL\Types\Type::getTypeMap(). If this error occurs during database ' .
  99. 'introspection then you might have forgot to register all database types for a Doctrine Type. Use ' .
  100. 'AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement ' .
  101. 'Type#getMappedDatabaseTypes(). If the type name is empty you might ' .
  102. 'have a problem with the cache or forgot some mapping information.'
  103. );
  104. }
  105. public static function typeNotFound($name)
  106. {
  107. return new self('Type to be overwritten '.$name.' does not exist.');
  108. }
  109. }