Driver.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Doctrine\DBAL\Driver\PDOSqlsrv;
  3. use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
  4. use Doctrine\DBAL\Driver\AbstractSQLServerDriver\Exception\PortWithoutHost;
  5. use Doctrine\DBAL\Driver\PDO;
  6. use Doctrine\Deprecations\Deprecation;
  7. use function is_int;
  8. use function sprintf;
  9. /**
  10. * The PDO-based Sqlsrv driver.
  11. *
  12. * @deprecated Use {@link PDO\SQLSrv\Driver} instead.
  13. */
  14. class Driver extends AbstractSQLServerDriver
  15. {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
  20. {
  21. $pdoOptions = $dsnOptions = [];
  22. foreach ($driverOptions as $option => $value) {
  23. if (is_int($option)) {
  24. $pdoOptions[$option] = $value;
  25. } else {
  26. $dsnOptions[$option] = $value;
  27. }
  28. }
  29. return new PDO\SQLSrv\Connection(
  30. $this->_constructPdoDsn($params, $dsnOptions),
  31. $username,
  32. $password,
  33. $pdoOptions
  34. );
  35. }
  36. /**
  37. * Constructs the Sqlsrv PDO DSN.
  38. *
  39. * @param mixed[] $params
  40. * @param string[] $connectionOptions
  41. *
  42. * @return string The DSN.
  43. */
  44. private function _constructPdoDsn(array $params, array $connectionOptions)
  45. {
  46. $dsn = 'sqlsrv:server=';
  47. if (isset($params['host'])) {
  48. $dsn .= $params['host'];
  49. if (isset($params['port'])) {
  50. $dsn .= ',' . $params['port'];
  51. }
  52. } elseif (isset($params['port'])) {
  53. throw PortWithoutHost::new();
  54. }
  55. if (isset($params['dbname'])) {
  56. $connectionOptions['Database'] = $params['dbname'];
  57. }
  58. if (isset($params['MultipleActiveResultSets'])) {
  59. $connectionOptions['MultipleActiveResultSets'] = $params['MultipleActiveResultSets'] ? 'true' : 'false';
  60. }
  61. return $dsn . $this->getConnectionOptionsDsn($connectionOptions);
  62. }
  63. /**
  64. * Converts a connection options array to the DSN
  65. *
  66. * @param string[] $connectionOptions
  67. */
  68. private function getConnectionOptionsDsn(array $connectionOptions): string
  69. {
  70. $connectionOptionsDsn = '';
  71. foreach ($connectionOptions as $paramName => $paramValue) {
  72. $connectionOptionsDsn .= sprintf(';%s=%s', $paramName, $paramValue);
  73. }
  74. return $connectionOptionsDsn;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. *
  79. * @deprecated
  80. */
  81. public function getName()
  82. {
  83. Deprecation::trigger(
  84. 'doctrine/dbal',
  85. 'https://github.com/doctrine/dbal/issues/3580',
  86. 'Driver::getName() is deprecated'
  87. );
  88. return 'pdo_sqlsrv';
  89. }
  90. }