AbstractSQLiteDriver.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Doctrine\DBAL\Driver;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\Driver;
  5. use Doctrine\DBAL\Exception;
  6. use Doctrine\DBAL\Platforms\SqlitePlatform;
  7. use Doctrine\DBAL\Schema\SqliteSchemaManager;
  8. use function strpos;
  9. /**
  10. * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for SQLite based drivers.
  11. */
  12. abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver
  13. {
  14. /**
  15. * {@inheritdoc}
  16. *
  17. * @link http://www.sqlite.org/c3ref/c_abort.html
  18. */
  19. public function convertException($message, DriverException $exception)
  20. {
  21. if (strpos($exception->getMessage(), 'database is locked') !== false) {
  22. return new Exception\LockWaitTimeoutException($message, $exception);
  23. }
  24. if (strpos($exception->getMessage(), 'must be unique') !== false ||
  25. strpos($exception->getMessage(), 'is not unique') !== false ||
  26. strpos($exception->getMessage(), 'are not unique') !== false ||
  27. strpos($exception->getMessage(), 'UNIQUE constraint failed') !== false
  28. ) {
  29. return new Exception\UniqueConstraintViolationException($message, $exception);
  30. }
  31. if (strpos($exception->getMessage(), 'may not be NULL') !== false ||
  32. strpos($exception->getMessage(), 'NOT NULL constraint failed') !== false
  33. ) {
  34. return new Exception\NotNullConstraintViolationException($message, $exception);
  35. }
  36. if (strpos($exception->getMessage(), 'no such table:') !== false) {
  37. return new Exception\TableNotFoundException($message, $exception);
  38. }
  39. if (strpos($exception->getMessage(), 'already exists') !== false) {
  40. return new Exception\TableExistsException($message, $exception);
  41. }
  42. if (strpos($exception->getMessage(), 'has no column named') !== false) {
  43. return new Exception\InvalidFieldNameException($message, $exception);
  44. }
  45. if (strpos($exception->getMessage(), 'ambiguous column name') !== false) {
  46. return new Exception\NonUniqueFieldNameException($message, $exception);
  47. }
  48. if (strpos($exception->getMessage(), 'syntax error') !== false) {
  49. return new Exception\SyntaxErrorException($message, $exception);
  50. }
  51. if (strpos($exception->getMessage(), 'attempt to write a readonly database') !== false) {
  52. return new Exception\ReadOnlyException($message, $exception);
  53. }
  54. if (strpos($exception->getMessage(), 'unable to open database file') !== false) {
  55. return new Exception\ConnectionException($message, $exception);
  56. }
  57. return new Exception\DriverException($message, $exception);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getDatabase(Connection $conn)
  63. {
  64. $params = $conn->getParams();
  65. return $params['path'] ?? null;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getDatabasePlatform()
  71. {
  72. return new SqlitePlatform();
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function getSchemaManager(Connection $conn)
  78. {
  79. return new SqliteSchemaManager($conn);
  80. }
  81. }