Driver.php 1.6 KB

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