Driver.php 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\DBAL\Driver\PDOSqlsrv;
  22. /**
  23. * The PDO-based Sqlsrv driver.
  24. *
  25. * @since 2.0
  26. */
  27. class Driver implements \Doctrine\DBAL\Driver
  28. {
  29. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  30. {
  31. return new Connection(
  32. $this->_constructPdoDsn($params),
  33. $username,
  34. $password,
  35. $driverOptions
  36. );
  37. }
  38. /**
  39. * Constructs the Sqlsrv PDO DSN.
  40. *
  41. * @return string The DSN.
  42. */
  43. private function _constructPdoDsn(array $params)
  44. {
  45. $dsn = 'sqlsrv:server=';
  46. if (isset($params['host'])) {
  47. $dsn .= $params['host'];
  48. }
  49. if (isset($params['port']) && !empty($params['port'])) {
  50. $dsn .= ',' . $params['port'];
  51. }
  52. if (isset($params['dbname'])) {
  53. $dsn .= ';Database=' . $params['dbname'];
  54. }
  55. return $dsn;
  56. }
  57. public function getDatabasePlatform()
  58. {
  59. return new \Doctrine\DBAL\Platforms\MsSqlPlatform();
  60. }
  61. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  62. {
  63. return new \Doctrine\DBAL\Schema\MsSqlSchemaManager($conn);
  64. }
  65. public function getName()
  66. {
  67. return 'pdo_sqlsrv';
  68. }
  69. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  70. {
  71. $params = $conn->getParams();
  72. return $params['dbname'];
  73. }
  74. }