AbstractMySQLDriver.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\DBALException;
  21. use Doctrine\DBAL\Driver;
  22. use Doctrine\DBAL\Exception;
  23. use Doctrine\DBAL\Platforms\MySQL57Platform;
  24. use Doctrine\DBAL\Platforms\MySqlPlatform;
  25. use Doctrine\DBAL\Schema\MySqlSchemaManager;
  26. use Doctrine\DBAL\VersionAwarePlatformDriver;
  27. /**
  28. * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for MySQL based drivers.
  29. *
  30. * @author Steve Müller <st.mueller@dzh-online.de>
  31. * @link www.doctrine-project.org
  32. * @since 2.5
  33. */
  34. abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver
  35. {
  36. /**
  37. * {@inheritdoc}
  38. *
  39. * @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
  40. * @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
  41. */
  42. public function convertException($message, DriverException $exception)
  43. {
  44. switch ($exception->getErrorCode()) {
  45. case '1050':
  46. return new Exception\TableExistsException($message, $exception);
  47. case '1051':
  48. case '1146':
  49. return new Exception\TableNotFoundException($message, $exception);
  50. case '1216':
  51. case '1217':
  52. case '1451':
  53. case '1452':
  54. case '1701':
  55. return new Exception\ForeignKeyConstraintViolationException($message, $exception);
  56. case '1062':
  57. case '1557':
  58. case '1569':
  59. case '1586':
  60. return new Exception\UniqueConstraintViolationException($message, $exception);
  61. case '1054':
  62. case '1166':
  63. case '1611':
  64. return new Exception\InvalidFieldNameException($message, $exception);
  65. case '1052':
  66. case '1060':
  67. case '1110':
  68. return new Exception\NonUniqueFieldNameException($message, $exception);
  69. case '1064':
  70. case '1149':
  71. case '1287':
  72. case '1341':
  73. case '1342':
  74. case '1343':
  75. case '1344':
  76. case '1382':
  77. case '1479':
  78. case '1541':
  79. case '1554':
  80. case '1626':
  81. return new Exception\SyntaxErrorException($message, $exception);
  82. case '1044':
  83. case '1045':
  84. case '1046':
  85. case '1049':
  86. case '1095':
  87. case '1142':
  88. case '1143':
  89. case '1227':
  90. case '1370':
  91. case '2002':
  92. case '2005':
  93. return new Exception\ConnectionException($message, $exception);
  94. case '1048':
  95. case '1121':
  96. case '1138':
  97. case '1171':
  98. case '1252':
  99. case '1263':
  100. case '1566':
  101. return new Exception\NotNullConstraintViolationException($message, $exception);
  102. }
  103. return new Exception\DriverException($message, $exception);
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function createDatabasePlatformForVersion($version)
  109. {
  110. if ( ! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) {
  111. throw DBALException::invalidPlatformVersionSpecified(
  112. $version,
  113. '<major_version>.<minor_version>.<patch_version>'
  114. );
  115. }
  116. if (false !== stripos($version, 'mariadb')) {
  117. return $this->getDatabasePlatform();
  118. }
  119. $majorVersion = $versionParts['major'];
  120. $minorVersion = isset($versionParts['minor']) ? $versionParts['minor'] : 0;
  121. $patchVersion = isset($versionParts['patch']) ? $versionParts['patch'] : 0;
  122. $version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
  123. if (version_compare($version, '5.7', '>=')) {
  124. return new MySQL57Platform();
  125. }
  126. return $this->getDatabasePlatform();
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  132. {
  133. $params = $conn->getParams();
  134. if (isset($params['dbname'])) {
  135. return $params['dbname'];
  136. }
  137. return $conn->query('SELECT DATABASE()')->fetchColumn();
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function getDatabasePlatform()
  143. {
  144. return new MySqlPlatform();
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  150. {
  151. return new MySqlSchemaManager($conn);
  152. }
  153. }