Driver.php 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\OCI8;
  22. use Doctrine\DBAL\Platforms;
  23. /**
  24. * A Doctrine DBAL driver for the Oracle OCI8 PHP extensions.
  25. *
  26. * @author Roman Borschel <roman@code-factory.org>
  27. * @since 2.0
  28. */
  29. class Driver implements \Doctrine\DBAL\Driver
  30. {
  31. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  32. {
  33. return new OCI8Connection(
  34. $username,
  35. $password,
  36. $this->_constructDsn($params),
  37. isset($params['charset']) ? $params['charset'] : null,
  38. isset($params['sessionMode']) ? $params['sessionMode'] : OCI_DEFAULT
  39. );
  40. }
  41. /**
  42. * Constructs the Oracle DSN.
  43. *
  44. * @return string The DSN.
  45. */
  46. private function _constructDsn(array $params)
  47. {
  48. $dsn = '';
  49. if (isset($params['host'])) {
  50. $dsn .= '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)' .
  51. '(HOST=' . $params['host'] . ')';
  52. if (isset($params['port'])) {
  53. $dsn .= '(PORT=' . $params['port'] . ')';
  54. } else {
  55. $dsn .= '(PORT=1521)';
  56. }
  57. $dsn .= '))';
  58. if (isset($params['dbname'])) {
  59. $dsn .= '(CONNECT_DATA=(SID=' . $params['dbname'] . ')';
  60. }
  61. $dsn .= '))';
  62. } else {
  63. $dsn .= $params['dbname'];
  64. }
  65. return $dsn;
  66. }
  67. public function getDatabasePlatform()
  68. {
  69. return new \Doctrine\DBAL\Platforms\OraclePlatform();
  70. }
  71. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  72. {
  73. return new \Doctrine\DBAL\Schema\OracleSchemaManager($conn);
  74. }
  75. public function getName()
  76. {
  77. return 'oci8';
  78. }
  79. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  80. {
  81. $params = $conn->getParams();
  82. return $params['user'];
  83. }
  84. }