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.
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>.
20 namespace Doctrine\DBAL\Driver\OCI8;
23 use IteratorAggregate;
24 use Doctrine\DBAL\Driver\Statement;
27 * The OCI8 implementation of the Statement interface.
30 * @author Roman Borschel <roman@code-factory.org>
32 class OCI8Statement implements \IteratorAggregate, Statement
34 /** Statement handle. */
38 protected static $_PARAM = ':param';
39 protected static $fetchModeMap = array(
40 PDO::FETCH_BOTH => OCI_BOTH,
41 PDO::FETCH_ASSOC => OCI_ASSOC,
42 PDO::FETCH_NUM => OCI_NUM,
43 PDO::PARAM_LOB => OCI_B_BLOB,
44 PDO::FETCH_COLUMN => OCI_NUM,
46 protected $_defaultFetchMode = PDO::FETCH_BOTH;
47 protected $_paramMap = array();
50 * Creates a new OCI8Statement that uses the given connection handle and SQL statement.
52 * @param resource $dbh The connection handle.
53 * @param string $statement The SQL statement.
55 public function __construct($dbh, $statement, OCI8Connection $conn)
57 list($statement, $paramMap) = self::convertPositionalToNamedPlaceholders($statement);
58 $this->_sth = oci_parse($dbh, $statement);
60 $this->_paramMap = $paramMap;
65 * Convert positional (?) into named placeholders (:param<num>)
67 * Oracle does not support positional parameters, hence this method converts all
68 * positional parameters into artificially named parameters. Note that this conversion
69 * is not perfect. All question marks (?) in the original statement are treated as
70 * placeholders and converted to a named parameter.
72 * The algorithm uses a state machine with two possible states: InLiteral and NotInLiteral.
73 * Question marks inside literal strings are therefore handled correctly by this method.
74 * This comes at a cost, the whole sql statement has to be looped over.
76 * @todo extract into utility class in Doctrine\DBAL\Util namespace
77 * @todo review and test for lost spaces. we experienced missing spaces with oci8 in some sql statements.
78 * @param string $statement The SQL statement to convert.
81 static public function convertPositionalToNamedPlaceholders($statement)
84 $inLiteral = false; // a valid query never starts with quotes
85 $stmtLen = strlen($statement);
87 for ($i = 0; $i < $stmtLen; $i++) {
88 if ($statement[$i] == '?' && !$inLiteral) {
89 // real positional parameter detected
90 $paramMap[$count] = ":param$count";
91 $len = strlen($paramMap[$count]);
92 $statement = substr_replace($statement, ":param$count", $i, 1);
93 $i += $len-1; // jump ahead
94 $stmtLen = strlen($statement); // adjust statement length
96 } else if ($statement[$i] == "'" || $statement[$i] == '"') {
97 $inLiteral = ! $inLiteral; // switch state!
101 return array($statement, $paramMap);
107 public function bindValue($param, $value, $type = null)
109 return $this->bindParam($param, $value, $type, null);
115 public function bindParam($column, &$variable, $type = null,$length = null)
117 $column = isset($this->_paramMap[$column]) ? $this->_paramMap[$column] : $column;
119 if ($type == \PDO::PARAM_LOB) {
120 $lob = oci_new_descriptor($this->_dbh, OCI_D_LOB);
121 $lob->writeTemporary($variable, OCI_TEMP_BLOB);
123 return oci_bind_by_name($this->_sth, $column, $lob, -1, OCI_B_BLOB);
125 return oci_bind_by_name($this->_sth, $column, $variable);
130 * Closes the cursor, enabling the statement to be executed again.
132 * @return boolean Returns TRUE on success or FALSE on failure.
134 public function closeCursor()
136 return oci_free_statement($this->_sth);
142 public function columnCount()
144 return oci_num_fields($this->_sth);
150 public function errorCode()
152 $error = oci_error($this->_sth);
153 if ($error !== false) {
154 $error = $error['code'];
162 public function errorInfo()
164 return oci_error($this->_sth);
170 public function execute($params = null)
173 $hasZeroIndex = array_key_exists(0, $params);
174 foreach ($params as $key => $val) {
175 if ($hasZeroIndex && is_numeric($key)) {
176 $this->bindValue($key + 1, $val);
178 $this->bindValue($key, $val);
183 $ret = @oci_execute($this->_sth, $this->_conn->getExecuteMode());
185 throw OCI8Exception::fromErrorInfo($this->errorInfo());
193 public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
195 $this->_defaultFetchMode = $fetchMode;
201 public function getIterator()
203 $data = $this->fetchAll();
204 return new \ArrayIterator($data);
210 public function fetch($fetchMode = null)
212 $fetchMode = $fetchMode ?: $this->_defaultFetchMode;
213 if ( ! isset(self::$fetchModeMap[$fetchMode])) {
214 throw new \InvalidArgumentException("Invalid fetch style: " . $fetchMode);
217 return oci_fetch_array($this->_sth, self::$fetchModeMap[$fetchMode] | OCI_RETURN_NULLS | OCI_RETURN_LOBS);
223 public function fetchAll($fetchMode = null)
225 $fetchMode = $fetchMode ?: $this->_defaultFetchMode;
226 if ( ! isset(self::$fetchModeMap[$fetchMode])) {
227 throw new \InvalidArgumentException("Invalid fetch style: " . $fetchMode);
231 if (self::$fetchModeMap[$fetchMode] === OCI_BOTH) {
232 while ($row = $this->fetch($fetchMode)) {
236 $fetchStructure = OCI_FETCHSTATEMENT_BY_ROW;
237 if ($fetchMode == PDO::FETCH_COLUMN) {
238 $fetchStructure = OCI_FETCHSTATEMENT_BY_COLUMN;
241 oci_fetch_all($this->_sth, $result, 0, -1,
242 self::$fetchModeMap[$fetchMode] | OCI_RETURN_NULLS | $fetchStructure | OCI_RETURN_LOBS);
244 if ($fetchMode == PDO::FETCH_COLUMN) {
245 $result = $result[0];
255 public function fetchColumn($columnIndex = 0)
257 $row = oci_fetch_array($this->_sth, OCI_NUM | OCI_RETURN_NULLS | OCI_RETURN_LOBS);
258 return isset($row[$columnIndex]) ? $row[$columnIndex] : false;
264 public function rowCount()
266 return oci_num_rows($this->_sth);