Driver.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Doctrine\DBAL\Driver\SQLAnywhere;
  3. use Doctrine\DBAL\Driver\AbstractSQLAnywhereDriver;
  4. use Doctrine\DBAL\Exception;
  5. use Doctrine\Deprecations\Deprecation;
  6. use function array_keys;
  7. use function array_map;
  8. use function implode;
  9. /**
  10. * A Doctrine DBAL driver for the SAP Sybase SQL Anywhere PHP extension.
  11. */
  12. class Driver extends AbstractSQLAnywhereDriver
  13. {
  14. /**
  15. * {@inheritdoc}
  16. *
  17. * @throws Exception If there was a problem establishing the connection.
  18. */
  19. public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
  20. {
  21. try {
  22. return new SQLAnywhereConnection(
  23. $this->buildDsn(
  24. $params['host'] ?? null,
  25. $params['port'] ?? null,
  26. $params['server'] ?? null,
  27. $params['dbname'] ?? null,
  28. $username,
  29. $password,
  30. $driverOptions
  31. ),
  32. $params['persistent'] ?? false
  33. );
  34. } catch (SQLAnywhereException $e) {
  35. throw Exception::driverException($this, $e);
  36. }
  37. }
  38. /**
  39. * {@inheritdoc}
  40. *
  41. * @deprecated
  42. */
  43. public function getName()
  44. {
  45. Deprecation::trigger(
  46. 'doctrine/dbal',
  47. 'https://github.com/doctrine/dbal/issues/3580',
  48. 'Driver::getName() is deprecated'
  49. );
  50. return 'sqlanywhere';
  51. }
  52. /**
  53. * Build the connection string for given connection parameters and driver options.
  54. *
  55. * @param string|null $host Host address to connect to.
  56. * @param int|null $port Port to use for the connection (default to SQL Anywhere standard port 2638).
  57. * @param string|null $server Database server name on the host to connect to.
  58. * SQL Anywhere allows multiple database server instances on the same host,
  59. * therefore specifying the server instance name to use is mandatory.
  60. * @param string|null $dbname Name of the database on the server instance to connect to.
  61. * @param string $username User name to use for connection authentication.
  62. * @param string $password Password to use for connection authentication.
  63. * @param mixed[] $driverOptions Additional parameters to use for the connection.
  64. *
  65. * @return string
  66. */
  67. private function buildDsn(
  68. $host,
  69. $port,
  70. $server,
  71. $dbname,
  72. $username = null,
  73. $password = null,
  74. array $driverOptions = []
  75. ) {
  76. $host = $host ?: 'localhost';
  77. $port = $port ?: 2638;
  78. if (! empty($server)) {
  79. $server = ';ServerName=' . $server;
  80. }
  81. return 'HOST=' . $host . ':' . $port .
  82. $server .
  83. ';DBN=' . $dbname .
  84. ';UID=' . $username .
  85. ';PWD=' . $password .
  86. ';' . implode(
  87. ';',
  88. array_map(static function ($key, $value) {
  89. return $key . '=' . $value;
  90. }, array_keys($driverOptions), $driverOptions)
  91. );
  92. }
  93. }