Driver.php 3.0 KB

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