OCI8Statement.php 8.3 KB

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