OCI8Statement.php 8.7 KB

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