Driver.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Doctrine\DBAL\Driver\PDOMySql;
  3. use Doctrine\DBAL\Driver\AbstractMySQLDriver;
  4. use Doctrine\DBAL\Driver\PDO;
  5. use Doctrine\DBAL\Exception;
  6. use Doctrine\Deprecations\Deprecation;
  7. use PDOException;
  8. /**
  9. * PDO MySql driver.
  10. *
  11. * @deprecated Use {@link PDO\MySQL\Driver} instead.
  12. */
  13. class Driver extends AbstractMySQLDriver
  14. {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
  19. {
  20. try {
  21. $conn = new PDO\Connection(
  22. $this->constructPdoDsn($params),
  23. $username,
  24. $password,
  25. $driverOptions
  26. );
  27. } catch (PDOException $e) {
  28. throw Exception::driverException($this, $e);
  29. }
  30. return $conn;
  31. }
  32. /**
  33. * Constructs the MySql PDO DSN.
  34. *
  35. * @param mixed[] $params
  36. *
  37. * @return string The DSN.
  38. */
  39. protected function constructPdoDsn(array $params)
  40. {
  41. $dsn = 'mysql:';
  42. if (isset($params['host']) && $params['host'] !== '') {
  43. $dsn .= 'host=' . $params['host'] . ';';
  44. }
  45. if (isset($params['port'])) {
  46. $dsn .= 'port=' . $params['port'] . ';';
  47. }
  48. if (isset($params['dbname'])) {
  49. $dsn .= 'dbname=' . $params['dbname'] . ';';
  50. }
  51. if (isset($params['unix_socket'])) {
  52. $dsn .= 'unix_socket=' . $params['unix_socket'] . ';';
  53. }
  54. if (isset($params['charset'])) {
  55. $dsn .= 'charset=' . $params['charset'] . ';';
  56. }
  57. return $dsn;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. *
  62. * @deprecated
  63. */
  64. public function getName()
  65. {
  66. Deprecation::trigger(
  67. 'doctrine/dbal',
  68. 'https://github.com/doctrine/dbal/issues/3580',
  69. 'Driver::getName() is deprecated'
  70. );
  71. return 'pdo_mysql';
  72. }
  73. }