OCI8Connection.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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($value, $type=\PDO::PARAM_STR)
  77. {
  78. if (is_int($value) || is_float($value)) {
  79. return $value;
  80. }
  81. $value = str_replace("'", "''", $value);
  82. return "'" . addcslashes($value, "\000\n\r\\\032") . "'";
  83. }
  84. /**
  85. *
  86. * @param string $statement
  87. * @return int
  88. */
  89. public function exec($statement)
  90. {
  91. $stmt = $this->prepare($statement);
  92. $stmt->execute();
  93. return $stmt->rowCount();
  94. }
  95. public function lastInsertId($name = null)
  96. {
  97. //TODO: throw exception or support sequences?
  98. }
  99. /**
  100. * Start a transactiom
  101. *
  102. * Oracle has to explicitly set the autocommit mode off. That means
  103. * after connection, a commit or rollback there is always automatically
  104. * opened a new transaction.
  105. *
  106. * @return bool
  107. */
  108. public function beginTransaction()
  109. {
  110. $this->_executeMode = OCI_NO_AUTO_COMMIT;
  111. return true;
  112. }
  113. /**
  114. * @throws OCI8Exception
  115. * @return bool
  116. */
  117. public function commit()
  118. {
  119. if (!oci_commit($this->_dbh)) {
  120. throw OCI8Exception::fromErrorInfo($this->errorInfo());
  121. }
  122. $this->_executeMode = OCI_COMMIT_ON_SUCCESS;
  123. return true;
  124. }
  125. /**
  126. * @throws OCI8Exception
  127. * @return bool
  128. */
  129. public function rollBack()
  130. {
  131. if (!oci_rollback($this->_dbh)) {
  132. throw OCI8Exception::fromErrorInfo($this->errorInfo());
  133. }
  134. $this->_executeMode = OCI_COMMIT_ON_SUCCESS;
  135. return true;
  136. }
  137. public function errorCode()
  138. {
  139. $error = oci_error($this->_dbh);
  140. if ($error !== false) {
  141. $error = $error['code'];
  142. }
  143. return $error;
  144. }
  145. public function errorInfo()
  146. {
  147. return oci_error($this->_dbh);
  148. }
  149. }