OCI8Connection.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Driver\OCI8;
  20. use Doctrine\DBAL\Platforms\OraclePlatform;
  21. /**
  22. * OCI8 implementation of the Connection interface.
  23. *
  24. * @since 2.0
  25. */
  26. class OCI8Connection implements \Doctrine\DBAL\Driver\Connection
  27. {
  28. /**
  29. * @var resource
  30. */
  31. protected $dbh;
  32. /**
  33. * @var integer
  34. */
  35. protected $executeMode = OCI_COMMIT_ON_SUCCESS;
  36. /**
  37. * Creates a Connection to an Oracle Database using oci8 extension.
  38. *
  39. * @param string $username
  40. * @param string $password
  41. * @param string $db
  42. * @param string|null $charset
  43. * @param integer $sessionMode
  44. * @param boolean $persistent
  45. *
  46. * @throws OCI8Exception
  47. */
  48. public function __construct($username, $password, $db, $charset = null, $sessionMode = OCI_DEFAULT, $persistent = false)
  49. {
  50. if (!defined('OCI_NO_AUTO_COMMIT')) {
  51. define('OCI_NO_AUTO_COMMIT', 0);
  52. }
  53. $this->dbh = $persistent
  54. ? @oci_pconnect($username, $password, $db, $charset, $sessionMode)
  55. : @oci_connect($username, $password, $db, $charset, $sessionMode);
  56. if ( ! $this->dbh) {
  57. throw OCI8Exception::fromErrorInfo(oci_error());
  58. }
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function prepare($prepareString)
  64. {
  65. return new OCI8Statement($this->dbh, $prepareString, $this);
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function query()
  71. {
  72. $args = func_get_args();
  73. $sql = $args[0];
  74. //$fetchMode = $args[1];
  75. $stmt = $this->prepare($sql);
  76. $stmt->execute();
  77. return $stmt;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function quote($value, $type=\PDO::PARAM_STR)
  83. {
  84. if (is_int($value) || is_float($value)) {
  85. return $value;
  86. }
  87. $value = str_replace("'", "''", $value);
  88. return "'" . addcslashes($value, "\000\n\r\\\032") . "'";
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function exec($statement)
  94. {
  95. $stmt = $this->prepare($statement);
  96. $stmt->execute();
  97. return $stmt->rowCount();
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function lastInsertId($name = null)
  103. {
  104. if ($name === null) {
  105. return false;
  106. }
  107. OraclePlatform::assertValidIdentifier($name);
  108. $sql = 'SELECT ' . $name . '.CURRVAL FROM DUAL';
  109. $stmt = $this->query($sql);
  110. $result = $stmt->fetch(\PDO::FETCH_ASSOC);
  111. if ($result === false || !isset($result['CURRVAL'])) {
  112. throw new OCI8Exception("lastInsertId failed: Query was executed but no result was returned.");
  113. }
  114. return (int) $result['CURRVAL'];
  115. }
  116. /**
  117. * Returns the current execution mode.
  118. *
  119. * @return integer
  120. */
  121. public function getExecuteMode()
  122. {
  123. return $this->executeMode;
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function beginTransaction()
  129. {
  130. $this->executeMode = OCI_NO_AUTO_COMMIT;
  131. return true;
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function commit()
  137. {
  138. if (!oci_commit($this->dbh)) {
  139. throw OCI8Exception::fromErrorInfo($this->errorInfo());
  140. }
  141. $this->executeMode = OCI_COMMIT_ON_SUCCESS;
  142. return true;
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function rollBack()
  148. {
  149. if (!oci_rollback($this->dbh)) {
  150. throw OCI8Exception::fromErrorInfo($this->errorInfo());
  151. }
  152. $this->executeMode = OCI_COMMIT_ON_SUCCESS;
  153. return true;
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function errorCode()
  159. {
  160. $error = oci_error($this->dbh);
  161. if ($error !== false) {
  162. $error = $error['code'];
  163. }
  164. return $error;
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function errorInfo()
  170. {
  171. return oci_error($this->dbh);
  172. }
  173. }