OCI8Statement.php 7.9 KB

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