OCI8Statement.php 10 KB

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