OCI8Connection.php 5.8 KB

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