AbstractSQLiteDriver.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\Driver;
  20. use Doctrine\DBAL\Driver;
  21. use Doctrine\DBAL\Exception;
  22. use Doctrine\DBAL\Platforms\SqlitePlatform;
  23. use Doctrine\DBAL\Schema\SqliteSchemaManager;
  24. /**
  25. * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for SQLite based drivers.
  26. *
  27. * @author Steve Müller <st.mueller@dzh-online.de>
  28. * @link www.doctrine-project.org
  29. * @since 2.5
  30. */
  31. abstract class AbstractSQLiteDriver implements Driver, ExceptionConverterDriver
  32. {
  33. /**
  34. * {@inheritdoc}
  35. *
  36. * @link http://www.sqlite.org/c3ref/c_abort.html
  37. */
  38. public function convertException($message, DriverException $exception)
  39. {
  40. if (strpos($exception->getMessage(), 'must be unique') !== false ||
  41. strpos($exception->getMessage(), 'is not unique') !== false ||
  42. strpos($exception->getMessage(), 'are not unique') !== false ||
  43. strpos($exception->getMessage(), 'UNIQUE constraint failed') !== false
  44. ) {
  45. return new Exception\UniqueConstraintViolationException($message, $exception);
  46. }
  47. if (strpos($exception->getMessage(), 'may not be NULL') !== false ||
  48. strpos($exception->getMessage(), 'NOT NULL constraint failed') !== false
  49. ) {
  50. return new Exception\NotNullConstraintViolationException($message, $exception);
  51. }
  52. if (strpos($exception->getMessage(), 'no such table:') !== false) {
  53. return new Exception\TableNotFoundException($message, $exception);
  54. }
  55. if (strpos($exception->getMessage(), 'already exists') !== false) {
  56. return new Exception\TableExistsException($message, $exception);
  57. }
  58. if (strpos($exception->getMessage(), 'has no column named') !== false) {
  59. return new Exception\InvalidFieldNameException($message, $exception);
  60. }
  61. if (strpos($exception->getMessage(), 'ambiguous column name') !== false) {
  62. return new Exception\NonUniqueFieldNameException($message, $exception);
  63. }
  64. if (strpos($exception->getMessage(), 'syntax error') !== false) {
  65. return new Exception\SyntaxErrorException($message, $exception);
  66. }
  67. if (strpos($exception->getMessage(), 'attempt to write a readonly database') !== false) {
  68. return new Exception\ReadOnlyException($message, $exception);
  69. }
  70. if (strpos($exception->getMessage(), 'unable to open database file') !== false) {
  71. return new Exception\ConnectionException($message, $exception);
  72. }
  73. return new Exception\DriverException($message, $exception);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  79. {
  80. $params = $conn->getParams();
  81. return isset($params['path']) ? $params['path'] : null;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getDatabasePlatform()
  87. {
  88. return new SqlitePlatform();
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  94. {
  95. return new SqliteSchemaManager($conn);
  96. }
  97. }