vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php line 97

Open in your IDE?
  1. <?php
  2. namespace Doctrine\DBAL\Driver;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\DBALException;
  5. use Doctrine\DBAL\Driver;
  6. use Doctrine\DBAL\Exception;
  7. use Doctrine\DBAL\Platforms\MariaDb1027Platform;
  8. use Doctrine\DBAL\Platforms\MySQL57Platform;
  9. use Doctrine\DBAL\Platforms\MySQL80Platform;
  10. use Doctrine\DBAL\Platforms\MySqlPlatform;
  11. use Doctrine\DBAL\Schema\MySqlSchemaManager;
  12. use Doctrine\DBAL\VersionAwarePlatformDriver;
  13. use function assert;
  14. use function preg_match;
  15. use function stripos;
  16. use function version_compare;
  17. /**
  18.  * Abstract base implementation of the {@link Driver} interface for MySQL based drivers.
  19.  */
  20. abstract class AbstractMySQLDriver implements DriverExceptionConverterDriverVersionAwarePlatformDriver
  21. {
  22.     /**
  23.      * {@inheritdoc}
  24.      *
  25.      * @link https://dev.mysql.com/doc/refman/8.0/en/client-error-reference.html
  26.      * @link https://dev.mysql.com/doc/refman/8.0/en/server-error-reference.html
  27.      */
  28.     public function convertException($messageDriverException $exception)
  29.     {
  30.         switch ($exception->getErrorCode()) {
  31.             case '1213':
  32.                 return new Exception\DeadlockException($message$exception);
  33.             case '1205':
  34.                 return new Exception\LockWaitTimeoutException($message$exception);
  35.             case '1050':
  36.                 return new Exception\TableExistsException($message$exception);
  37.             case '1051':
  38.             case '1146':
  39.                 return new Exception\TableNotFoundException($message$exception);
  40.             case '1216':
  41.             case '1217':
  42.             case '1451':
  43.             case '1452':
  44.             case '1701':
  45.                 return new Exception\ForeignKeyConstraintViolationException($message$exception);
  46.             case '1062':
  47.             case '1557':
  48.             case '1569':
  49.             case '1586':
  50.                 return new Exception\UniqueConstraintViolationException($message$exception);
  51.             case '1054':
  52.             case '1166':
  53.             case '1611':
  54.                 return new Exception\InvalidFieldNameException($message$exception);
  55.             case '1052':
  56.             case '1060':
  57.             case '1110':
  58.                 return new Exception\NonUniqueFieldNameException($message$exception);
  59.             case '1064':
  60.             case '1149':
  61.             case '1287':
  62.             case '1341':
  63.             case '1342':
  64.             case '1343':
  65.             case '1344':
  66.             case '1382':
  67.             case '1479':
  68.             case '1541':
  69.             case '1554':
  70.             case '1626':
  71.                 return new Exception\SyntaxErrorException($message$exception);
  72.             case '1044':
  73.             case '1045':
  74.             case '1046':
  75.             case '1049':
  76.             case '1095':
  77.             case '1142':
  78.             case '1143':
  79.             case '1227':
  80.             case '1370':
  81.             case '1429':
  82.             case '2002':
  83.             case '2005':
  84.                 return new Exception\ConnectionException($message$exception);
  85.             case '1048':
  86.             case '1121':
  87.             case '1138':
  88.             case '1171':
  89.             case '1252':
  90.             case '1263':
  91.             case '1364':
  92.             case '1566':
  93.                 return new Exception\NotNullConstraintViolationException($message$exception);
  94.         }
  95.         return new Exception\DriverException($message$exception);
  96.     }
  97.     /**
  98.      * {@inheritdoc}
  99.      *
  100.      * @throws DBALException
  101.      */
  102.     public function createDatabasePlatformForVersion($version)
  103.     {
  104.         $mariadb stripos($version'mariadb') !== false;
  105.         if ($mariadb && version_compare($this->getMariaDbMysqlVersionNumber($version), '10.2.7''>=')) {
  106.             return new MariaDb1027Platform();
  107.         }
  108.         if (! $mariadb) {
  109.             $oracleMysqlVersion $this->getOracleMysqlVersionNumber($version);
  110.             if (version_compare($oracleMysqlVersion'8''>=')) {
  111.                 return new MySQL80Platform();
  112.             }
  113.             if (version_compare($oracleMysqlVersion'5.7.9''>=')) {
  114.                 return new MySQL57Platform();
  115.             }
  116.         }
  117.         return $this->getDatabasePlatform();
  118.     }
  119.     /**
  120.      * Get a normalized 'version number' from the server string
  121.      * returned by Oracle MySQL servers.
  122.      *
  123.      * @param string $versionString Version string returned by the driver, i.e. '5.7.10'
  124.      *
  125.      * @throws DBALException
  126.      */
  127.     private function getOracleMysqlVersionNumber(string $versionString): string
  128.     {
  129.         if (
  130.             ! preg_match(
  131.                 '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/',
  132.                 $versionString,
  133.                 $versionParts
  134.             )
  135.         ) {
  136.             throw DBALException::invalidPlatformVersionSpecified(
  137.                 $versionString,
  138.                 '<major_version>.<minor_version>.<patch_version>'
  139.             );
  140.         }
  141.         $majorVersion $versionParts['major'];
  142.         $minorVersion $versionParts['minor'] ?? 0;
  143.         $patchVersion $versionParts['patch'] ?? null;
  144.         if ($majorVersion === '5' && $minorVersion === '7' && $patchVersion === null) {
  145.             $patchVersion '9';
  146.         }
  147.         return $majorVersion '.' $minorVersion '.' $patchVersion;
  148.     }
  149.     /**
  150.      * Detect MariaDB server version, including hack for some mariadb distributions
  151.      * that starts with the prefix '5.5.5-'
  152.      *
  153.      * @param string $versionString Version string as returned by mariadb server, i.e. '5.5.5-Mariadb-10.0.8-xenial'
  154.      *
  155.      * @throws DBALException
  156.      */
  157.     private function getMariaDbMysqlVersionNumber(string $versionString): string
  158.     {
  159.         if (
  160.             ! preg_match(
  161.                 '/^(?:5\.5\.5-)?(mariadb-)?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)/i',
  162.                 $versionString,
  163.                 $versionParts
  164.             )
  165.         ) {
  166.             throw DBALException::invalidPlatformVersionSpecified(
  167.                 $versionString,
  168.                 '^(?:5\.5\.5-)?(mariadb-)?<major_version>.<minor_version>.<patch_version>'
  169.             );
  170.         }
  171.         return $versionParts['major'] . '.' $versionParts['minor'] . '.' $versionParts['patch'];
  172.     }
  173.     /**
  174.      * {@inheritdoc}
  175.      */
  176.     public function getDatabase(Connection $conn)
  177.     {
  178.         $params $conn->getParams();
  179.         if (isset($params['dbname'])) {
  180.             return $params['dbname'];
  181.         }
  182.         $database $conn->query('SELECT DATABASE()')->fetchColumn();
  183.         assert($database !== false);
  184.         return $database;
  185.     }
  186.     /**
  187.      * {@inheritdoc}
  188.      *
  189.      * @return MySqlPlatform
  190.      */
  191.     public function getDatabasePlatform()
  192.     {
  193.         return new MySqlPlatform();
  194.     }
  195.     /**
  196.      * {@inheritdoc}
  197.      *
  198.      * @return MySqlSchemaManager
  199.      */
  200.     public function getSchemaManager(Connection $conn)
  201.     {
  202.         return new MySqlSchemaManager($conn);
  203.     }
  204. }