OCI8Statement.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. use \PDO;
  21. /**
  22. * The OCI8 implementation of the Statement interface.
  23. *
  24. * @since 2.0
  25. * @author Roman Borschel <roman@code-factory.org>
  26. */
  27. class OCI8Statement implements \Doctrine\DBAL\Driver\Statement
  28. {
  29. /** Statement handle. */
  30. private $_sth;
  31. private $_executeMode;
  32. private static $_PARAM = ':param';
  33. private static $fetchStyleMap = array(
  34. PDO::FETCH_BOTH => OCI_BOTH,
  35. PDO::FETCH_ASSOC => OCI_ASSOC,
  36. PDO::FETCH_NUM => OCI_NUM
  37. );
  38. private $_paramMap = array();
  39. /**
  40. * Creates a new OCI8Statement that uses the given connection handle and SQL statement.
  41. *
  42. * @param resource $dbh The connection handle.
  43. * @param string $statement The SQL statement.
  44. */
  45. public function __construct($dbh, $statement, $executeMode)
  46. {
  47. list($statement, $paramMap) = self::convertPositionalToNamedPlaceholders($statement);
  48. $this->_sth = oci_parse($dbh, $statement);
  49. $this->_paramMap = $paramMap;
  50. $this->_executeMode = $executeMode;
  51. }
  52. /**
  53. * Convert positional (?) into named placeholders (:param<num>)
  54. *
  55. * Oracle does not support positional parameters, hence this method converts all
  56. * positional parameters into artificially named parameters. Note that this conversion
  57. * is not perfect. All question marks (?) in the original statement are treated as
  58. * placeholders and converted to a named parameter.
  59. *
  60. * The algorithm uses a state machine with two possible states: InLiteral and NotInLiteral.
  61. * Question marks inside literal strings are therefore handled correctly by this method.
  62. * This comes at a cost, the whole sql statement has to be looped over.
  63. *
  64. * @todo extract into utility class in Doctrine\DBAL\Util namespace
  65. * @todo review and test for lost spaces. we experienced missing spaces with oci8 in some sql statements.
  66. * @param string $statement The SQL statement to convert.
  67. * @return string
  68. */
  69. static public function convertPositionalToNamedPlaceholders($statement)
  70. {
  71. $count = 1;
  72. $inLiteral = false; // a valid query never starts with quotes
  73. $stmtLen = strlen($statement);
  74. $paramMap = array();
  75. for ($i = 0; $i < $stmtLen; $i++) {
  76. if ($statement[$i] == '?' && !$inLiteral) {
  77. // real positional parameter detected
  78. $paramMap[$count] = ":param$count";
  79. $len = strlen($paramMap[$count]);
  80. $statement = substr_replace($statement, ":param$count", $i, 1);
  81. $i += $len-1; // jump ahead
  82. $stmtLen = strlen($statement); // adjust statement length
  83. ++$count;
  84. } else if ($statement[$i] == "'" || $statement[$i] == '"') {
  85. $inLiteral = ! $inLiteral; // switch state!
  86. }
  87. }
  88. return array($statement, $paramMap);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function bindValue($param, $value, $type = null)
  94. {
  95. return $this->bindParam($param, $value, $type);
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function bindParam($column, &$variable, $type = null)
  101. {
  102. $column = isset($this->_paramMap[$column]) ? $this->_paramMap[$column] : $column;
  103. return oci_bind_by_name($this->_sth, $column, $variable);
  104. }
  105. /**
  106. * Closes the cursor, enabling the statement to be executed again.
  107. *
  108. * @return boolean Returns TRUE on success or FALSE on failure.
  109. */
  110. public function closeCursor()
  111. {
  112. return oci_free_statement($this->_sth);
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function columnCount()
  118. {
  119. return oci_num_fields($this->_sth);
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function errorCode()
  125. {
  126. $error = oci_error($this->_sth);
  127. if ($error !== false) {
  128. $error = $error['code'];
  129. }
  130. return $error;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function errorInfo()
  136. {
  137. return oci_error($this->_sth);
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function execute($params = null)
  143. {
  144. if ($params) {
  145. $hasZeroIndex = array_key_exists(0, $params);
  146. foreach ($params as $key => $val) {
  147. if ($hasZeroIndex && is_numeric($key)) {
  148. $this->bindValue($key + 1, $val);
  149. } else {
  150. $this->bindValue($key, $val);
  151. }
  152. }
  153. }
  154. $ret = @oci_execute($this->_sth, $this->_executeMode);
  155. if ( ! $ret) {
  156. throw OCI8Exception::fromErrorInfo($this->errorInfo());
  157. }
  158. return $ret;
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function fetch($fetchStyle = PDO::FETCH_BOTH)
  164. {
  165. if ( ! isset(self::$fetchStyleMap[$fetchStyle])) {
  166. throw new \InvalidArgumentException("Invalid fetch style: " . $fetchStyle);
  167. }
  168. return oci_fetch_array($this->_sth, self::$fetchStyleMap[$fetchStyle] | OCI_RETURN_NULLS | OCI_RETURN_LOBS);
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function fetchAll($fetchStyle = PDO::FETCH_BOTH)
  174. {
  175. if ( ! isset(self::$fetchStyleMap[$fetchStyle])) {
  176. throw new \InvalidArgumentException("Invalid fetch style: " . $fetchStyle);
  177. }
  178. $result = array();
  179. oci_fetch_all($this->_sth, $result, 0, -1,
  180. self::$fetchStyleMap[$fetchStyle] | OCI_RETURN_NULLS | OCI_FETCHSTATEMENT_BY_ROW | OCI_RETURN_LOBS);
  181. return $result;
  182. }
  183. /**
  184. * {@inheritdoc}
  185. */
  186. public function fetchColumn($columnIndex = 0)
  187. {
  188. $row = oci_fetch_array($this->_sth, OCI_NUM | OCI_RETURN_NULLS | OCI_RETURN_LOBS);
  189. return $row[$columnIndex];
  190. }
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function rowCount()
  195. {
  196. return oci_num_rows($this->_sth);
  197. }
  198. }