Driver.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 MIT license. 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. isset($params['persistent']) ? $params['persistent'] : false
  40. );
  41. }
  42. /**
  43. * Constructs the Oracle DSN.
  44. *
  45. * @return string The DSN.
  46. */
  47. protected function _constructDsn(array $params)
  48. {
  49. $dsn = '';
  50. if (isset($params['host']) && $params['host'] != '') {
  51. $dsn .= '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)' .
  52. '(HOST=' . $params['host'] . ')';
  53. if (isset($params['port'])) {
  54. $dsn .= '(PORT=' . $params['port'] . ')';
  55. } else {
  56. $dsn .= '(PORT=1521)';
  57. }
  58. if (isset($params['service']) && $params['service'] == true) {
  59. $dsn .= '))(CONNECT_DATA=(SERVICE_NAME=' . $params['dbname'] . '))';
  60. } else {
  61. $dsn .= '))(CONNECT_DATA=(SID=' . $params['dbname'] . '))';
  62. }
  63. if (isset($params['pooled']) && $params['pooled'] == true) {
  64. $dsn .= '(SERVER=POOLED)';
  65. }
  66. $dsn .= ')';
  67. } else {
  68. $dsn .= $params['dbname'];
  69. }
  70. return $dsn;
  71. }
  72. public function getDatabasePlatform()
  73. {
  74. return new \Doctrine\DBAL\Platforms\OraclePlatform();
  75. }
  76. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  77. {
  78. return new \Doctrine\DBAL\Schema\OracleSchemaManager($conn);
  79. }
  80. public function getName()
  81. {
  82. return 'oci8';
  83. }
  84. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  85. {
  86. $params = $conn->getParams();
  87. return $params['user'];
  88. }
  89. }