OCI8Connection.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Driver\OCI8;
  20. /**
  21. * OCI8 implementation of the Connection interface.
  22. *
  23. * @since 2.0
  24. */
  25. class OCI8Connection implements \Doctrine\DBAL\Driver\Connection
  26. {
  27. private $_dbh;
  28. private $_executeMode = OCI_COMMIT_ON_SUCCESS;
  29. /**
  30. * Create a Connection to an Oracle Database using oci8 extension.
  31. *
  32. * @param string $username
  33. * @param string $password
  34. * @param string $db
  35. */
  36. public function __construct($username, $password, $db, $charset = null, $sessionMode = OCI_DEFAULT)
  37. {
  38. if (!defined('OCI_NO_AUTO_COMMIT')) {
  39. define('OCI_NO_AUTO_COMMIT', 0);
  40. }
  41. $this->_dbh = @oci_connect($username, $password, $db, $charset, $sessionMode);
  42. if (!$this->_dbh) {
  43. throw OCI8Exception::fromErrorInfo(oci_error());
  44. }
  45. }
  46. /**
  47. * Create a non-executed prepared statement.
  48. *
  49. * @param string $prepareString
  50. * @return OCI8Statement
  51. */
  52. public function prepare($prepareString)
  53. {
  54. return new OCI8Statement($this->_dbh, $prepareString, $this->_executeMode);
  55. }
  56. /**
  57. * @param string $sql
  58. * @return OCI8Statement
  59. */
  60. public function query()
  61. {
  62. $args = func_get_args();
  63. $sql = $args[0];
  64. //$fetchMode = $args[1];
  65. $stmt = $this->prepare($sql);
  66. $stmt->execute();
  67. return $stmt;
  68. }
  69. /**
  70. * Quote input value.
  71. *
  72. * @param mixed $input
  73. * @param int $type PDO::PARAM*
  74. * @return mixed
  75. */
  76. public function quote($input, $type=\PDO::PARAM_STR)
  77. {
  78. return is_numeric($input) ? $input : "'$input'";
  79. }
  80. /**
  81. *
  82. * @param string $statement
  83. * @return int
  84. */
  85. public function exec($statement)
  86. {
  87. $stmt = $this->prepare($statement);
  88. $stmt->execute();
  89. return $stmt->rowCount();
  90. }
  91. public function lastInsertId($name = null)
  92. {
  93. //TODO: throw exception or support sequences?
  94. }
  95. /**
  96. * Start a transactiom
  97. *
  98. * Oracle has to explicitly set the autocommit mode off. That means
  99. * after connection, a commit or rollback there is always automatically
  100. * opened a new transaction.
  101. *
  102. * @return bool
  103. */
  104. public function beginTransaction()
  105. {
  106. $this->_executeMode = OCI_NO_AUTO_COMMIT;
  107. return true;
  108. }
  109. /**
  110. * @throws OCI8Exception
  111. * @return bool
  112. */
  113. public function commit()
  114. {
  115. if (!oci_commit($this->_dbh)) {
  116. throw OCI8Exception::fromErrorInfo($this->errorInfo());
  117. }
  118. $this->_executeMode = OCI_COMMIT_ON_SUCCESS;
  119. return true;
  120. }
  121. /**
  122. * @throws OCI8Exception
  123. * @return bool
  124. */
  125. public function rollBack()
  126. {
  127. if (!oci_rollback($this->_dbh)) {
  128. throw OCI8Exception::fromErrorInfo($this->errorInfo());
  129. }
  130. $this->_executeMode = OCI_COMMIT_ON_SUCCESS;
  131. return true;
  132. }
  133. public function errorCode()
  134. {
  135. $error = oci_error($this->_dbh);
  136. if ($error !== false) {
  137. $error = $error['code'];
  138. }
  139. return $error;
  140. }
  141. public function errorInfo()
  142. {
  143. return oci_error($this->_dbh);
  144. }
  145. }