Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Driver / OCI8 / OCI8Statement.php
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
20 namespace Doctrine\DBAL\Driver\OCI8;
21
22 use PDO;
23 use IteratorAggregate;
24 use Doctrine\DBAL\Driver\Statement;
25
26 /**
27  * The OCI8 implementation of the Statement interface.
28  *
29  * @since 2.0
30  * @author Roman Borschel <roman@code-factory.org>
31  */
32 class OCI8Statement implements \IteratorAggregate, Statement
33 {
34     /** Statement handle. */
35     protected $_dbh;
36     protected $_sth;
37     protected $_conn;
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,
45     );
46     protected $_defaultFetchMode = PDO::FETCH_BOTH;
47     protected $_paramMap = array();
48
49     /**
50      * Creates a new OCI8Statement that uses the given connection handle and SQL statement.
51      *
52      * @param resource $dbh The connection handle.
53      * @param string $statement The SQL statement.
54      */
55     public function __construct($dbh, $statement, OCI8Connection $conn)
56     {
57         list($statement, $paramMap) = self::convertPositionalToNamedPlaceholders($statement);
58         $this->_sth = oci_parse($dbh, $statement);
59         $this->_dbh = $dbh;
60         $this->_paramMap = $paramMap;
61         $this->_conn = $conn;
62     }
63
64     /**
65      * Convert positional (?) into named placeholders (:param<num>)
66      *
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.
71      *
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.
75      *
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.
79      * @return string
80      */
81     static public function convertPositionalToNamedPlaceholders($statement)
82     {
83         $count = 1;
84         $inLiteral = false; // a valid query never starts with quotes
85         $stmtLen = strlen($statement);
86         $paramMap = array();
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
95                 ++$count;
96             } else if ($statement[$i] == "'" || $statement[$i] == '"') {
97                 $inLiteral = ! $inLiteral; // switch state!
98             }
99         }
100
101         return array($statement, $paramMap);
102     }
103
104     /**
105      * {@inheritdoc}
106      */
107     public function bindValue($param, $value, $type = null)
108     {
109         return $this->bindParam($param, $value, $type, null);
110     }
111
112     /**
113      * {@inheritdoc}
114      */
115     public function bindParam($column, &$variable, $type = null,$length = null)
116     {
117         $column = isset($this->_paramMap[$column]) ? $this->_paramMap[$column] : $column;
118
119         if ($type == \PDO::PARAM_LOB) {
120             $lob = oci_new_descriptor($this->_dbh, OCI_D_LOB);
121             $lob->writeTemporary($variable, OCI_TEMP_BLOB);
122
123             return oci_bind_by_name($this->_sth, $column, $lob, -1, OCI_B_BLOB);
124         } else {
125             return oci_bind_by_name($this->_sth, $column, $variable);
126         }
127     }
128
129     /**
130      * Closes the cursor, enabling the statement to be executed again.
131      *
132      * @return boolean              Returns TRUE on success or FALSE on failure.
133      */
134     public function closeCursor()
135     {
136         return oci_free_statement($this->_sth);
137     }
138
139     /**
140      * {@inheritdoc}
141      */
142     public function columnCount()
143     {
144         return oci_num_fields($this->_sth);
145     }
146
147     /**
148      * {@inheritdoc}
149      */
150     public function errorCode()
151     {
152         $error = oci_error($this->_sth);
153         if ($error !== false) {
154             $error = $error['code'];
155         }
156         return $error;
157     }
158
159     /**
160      * {@inheritdoc}
161      */
162     public function errorInfo()
163     {
164         return oci_error($this->_sth);
165     }
166
167     /**
168      * {@inheritdoc}
169      */
170     public function execute($params = null)
171     {
172         if ($params) {
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);
177                 } else {
178                     $this->bindValue($key, $val);
179                 }
180             }
181         }
182
183         $ret = @oci_execute($this->_sth, $this->_conn->getExecuteMode());
184         if ( ! $ret) {
185             throw OCI8Exception::fromErrorInfo($this->errorInfo());
186         }
187         return $ret;
188     }
189
190     /**
191      * {@inheritdoc}
192      */
193     public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
194     {
195         $this->_defaultFetchMode = $fetchMode;
196     }
197
198     /**
199      * {@inheritdoc}
200      */
201     public function getIterator()
202     {
203         $data = $this->fetchAll();
204         return new \ArrayIterator($data);
205     }
206
207     /**
208      * {@inheritdoc}
209      */
210     public function fetch($fetchMode = null)
211     {
212         $fetchMode = $fetchMode ?: $this->_defaultFetchMode;
213         if ( ! isset(self::$fetchModeMap[$fetchMode])) {
214             throw new \InvalidArgumentException("Invalid fetch style: " . $fetchMode);
215         }
216
217         return oci_fetch_array($this->_sth, self::$fetchModeMap[$fetchMode] | OCI_RETURN_NULLS | OCI_RETURN_LOBS);
218     }
219
220     /**
221      * {@inheritdoc}
222      */
223     public function fetchAll($fetchMode = null)
224     {
225         $fetchMode = $fetchMode ?: $this->_defaultFetchMode;
226         if ( ! isset(self::$fetchModeMap[$fetchMode])) {
227             throw new \InvalidArgumentException("Invalid fetch style: " . $fetchMode);
228         }
229
230         $result = array();
231         if (self::$fetchModeMap[$fetchMode] === OCI_BOTH) {
232             while ($row = $this->fetch($fetchMode)) {
233                 $result[] = $row;
234             }
235         } else {
236             $fetchStructure = OCI_FETCHSTATEMENT_BY_ROW;
237             if ($fetchMode == PDO::FETCH_COLUMN) {
238                 $fetchStructure = OCI_FETCHSTATEMENT_BY_COLUMN;
239             }
240
241             oci_fetch_all($this->_sth, $result, 0, -1,
242                     self::$fetchModeMap[$fetchMode] | OCI_RETURN_NULLS | $fetchStructure | OCI_RETURN_LOBS);
243
244             if ($fetchMode == PDO::FETCH_COLUMN) {
245                 $result = $result[0];
246             }
247         }
248
249         return $result;
250     }
251
252     /**
253      * {@inheritdoc}
254      */
255     public function fetchColumn($columnIndex = 0)
256     {
257         $row = oci_fetch_array($this->_sth, OCI_NUM | OCI_RETURN_NULLS | OCI_RETURN_LOBS);
258         return isset($row[$columnIndex]) ? $row[$columnIndex] : false;
259     }
260
261     /**
262      * {@inheritdoc}
263      */
264     public function rowCount()
265     {
266         return oci_num_rows($this->_sth);
267     }
268 }