Driver.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Driver\SQLAnywhere;
  20. use Doctrine\DBAL\DBALException;
  21. use Doctrine\DBAL\Driver\AbstractSQLAnywhereDriver;
  22. /**
  23. * A Doctrine DBAL driver for the SAP Sybase SQL Anywhere PHP extension.
  24. *
  25. * @author Steve Müller <st.mueller@dzh-online.de>
  26. * @link www.doctrine-project.org
  27. * @since 2.5
  28. */
  29. class Driver extends AbstractSQLAnywhereDriver
  30. {
  31. /**
  32. * {@inheritdoc}
  33. *
  34. * @throws \Doctrine\DBAL\DBALException if there was a problem establishing the connection.
  35. */
  36. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  37. {
  38. try {
  39. return new SQLAnywhereConnection(
  40. $this->buildDsn(
  41. isset($params['host']) ? $params['host'] : null,
  42. isset($params['port']) ? $params['port'] : null,
  43. isset($params['server']) ? $params['server'] : null,
  44. isset($params['dbname']) ? $params['dbname'] : null,
  45. $username,
  46. $password,
  47. $driverOptions
  48. ),
  49. isset($params['persistent']) ? $params['persistent'] : false
  50. );
  51. } catch (SQLAnywhereException $e) {
  52. throw DBALException::driverException($this, $e);
  53. }
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getName()
  59. {
  60. return 'sqlanywhere';
  61. }
  62. /**
  63. * Build the connection string for given connection parameters and driver options.
  64. *
  65. * @param string $host Host address to connect to.
  66. * @param integer $port Port to use for the connection (default to SQL Anywhere standard port 2638).
  67. * @param string $server Database server name on the host to connect to.
  68. * SQL Anywhere allows multiple database server instances on the same host,
  69. * therefore specifying the server instance name to use is mandatory.
  70. * @param string $dbname Name of the database on the server instance to connect to.
  71. * @param string $username User name to use for connection authentication.
  72. * @param string $password Password to use for connection authentication.
  73. * @param array $driverOptions Additional parameters to use for the connection.
  74. *
  75. * @return string
  76. */
  77. private function buildDsn($host, $port, $server, $dbname, $username = null, $password = null, array $driverOptions = array())
  78. {
  79. $host = $host ?: 'localhost';
  80. $port = $port ?: 2638;
  81. if (! empty($server)) {
  82. $server = ';ServerName=' . $server;
  83. }
  84. return
  85. 'HOST=' . $host . ':' . $port .
  86. $server .
  87. ';DBN=' . $dbname .
  88. ';UID=' . $username .
  89. ';PWD=' . $password .
  90. ';' . implode(
  91. ';',
  92. array_map(function ($key, $value) {
  93. return $key . '=' . $value;
  94. }, array_keys($driverOptions), $driverOptions)
  95. );
  96. }
  97. }