Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Statement.php
1 <?php
2 /*
3  *  $Id$
4  *
5  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16  *
17  * This software consists of voluntary contributions made by many individuals
18  * and is licensed under the MIT license. For more information, see
19  * <http://www.doctrine-project.org>.
20  */
21
22 namespace Doctrine\DBAL;
23
24 use PDO,
25     Doctrine\DBAL\Types\Type,
26     Doctrine\DBAL\Driver\Statement as DriverStatement;
27
28 /**
29  * A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support
30  * for logging, DBAL mapping types, etc.
31  *
32  * @author Roman Borschel <roman@code-factory.org>
33  * @since 2.0
34  */
35 class Statement implements \IteratorAggregate, DriverStatement
36 {
37     /**
38      * @var string The SQL statement.
39      */
40     protected $sql;
41     /**
42      * @var array The bound parameters.
43      */
44     protected $params = array();
45     /**
46      * @var array The parameter types
47      */
48     protected $types = array();
49     /**
50      * @var \Doctrine\DBAL\Driver\Statement The underlying driver statement.
51      */
52     protected $stmt;
53     /**
54      * @var \Doctrine\DBAL\Platforms\AbstractPlatform The underlying database platform.
55      */
56     protected $platform;
57     /**
58      * @var \Doctrine\DBAL\Connection The connection this statement is bound to and executed on.
59      */
60     protected $conn;
61
62     /**
63      * Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>.
64      *
65      * @param string $sql The SQL of the statement.
66      * @param \Doctrine\DBAL\Connection The connection on which the statement should be executed.
67      */
68     public function __construct($sql, Connection $conn)
69     {
70         $this->sql = $sql;
71         $this->stmt = $conn->getWrappedConnection()->prepare($sql);
72         $this->conn = $conn;
73         $this->platform = $conn->getDatabasePlatform();
74     }
75
76     /**
77      * Binds a parameter value to the statement.
78      *
79      * The value can optionally be bound with a PDO binding type or a DBAL mapping type.
80      * If bound with a DBAL mapping type, the binding type is derived from the mapping
81      * type and the value undergoes the conversion routines of the mapping type before
82      * being bound.
83      *
84      * @param string $name The name or position of the parameter.
85      * @param mixed $value The value of the parameter.
86      * @param mixed $type Either a PDO binding type or a DBAL mapping type name or instance.
87      * @return boolean TRUE on success, FALSE on failure.
88      */
89     public function bindValue($name, $value, $type = null)
90     {
91         $this->params[$name] = $value;
92         $this->types[$name] = $type;
93         if ($type !== null) {
94             if (is_string($type)) {
95                 $type = Type::getType($type);
96             }
97             if ($type instanceof Type) {
98                 $value = $type->convertToDatabaseValue($value, $this->platform);
99                 $bindingType = $type->getBindingType();
100             } else {
101                 $bindingType = $type; // PDO::PARAM_* constants
102             }
103             return $this->stmt->bindValue($name, $value, $bindingType);
104         } else {
105             return $this->stmt->bindValue($name, $value);
106         }
107     }
108
109     /**
110      * Binds a parameter to a value by reference.
111      *
112      * Binding a parameter by reference does not support DBAL mapping types.
113      *
114      * @param string $name The name or position of the parameter.
115      * @param mixed $var The reference to the variable to bind
116      * @param integer $type The PDO binding type.
117      * @return boolean TRUE on success, FALSE on failure.
118      */
119     public function bindParam($name, &$var, $type = PDO::PARAM_STR, $length = null)
120     {
121         return $this->stmt->bindParam($name, $var, $type, $length );
122     }
123
124     /**
125      * Executes the statement with the currently bound parameters.
126      *
127      * @param array $params
128      * @return boolean TRUE on success, FALSE on failure.
129      */
130     public function execute($params = null)
131     {
132         $logger = $this->conn->getConfiguration()->getSQLLogger();
133         if ($logger) {
134             $logger->startQuery($this->sql, $this->params, $this->types);
135         }
136
137         try {
138             $stmt = $this->stmt->execute($params);
139         } catch (\Exception $ex) {
140             throw DBALException::driverExceptionDuringQuery($ex, $this->sql, $this->conn->resolveParams($this->params, $this->types));
141         }
142
143         if ($logger) {
144             $logger->stopQuery();
145         }
146         $this->params = array();
147         $this->types = array();
148         return $stmt;
149     }
150
151     /**
152      * Closes the cursor, freeing the database resources used by this statement.
153      *
154      * @return boolean TRUE on success, FALSE on failure.
155      */
156     public function closeCursor()
157     {
158         return $this->stmt->closeCursor();
159     }
160
161     /**
162      * Returns the number of columns in the result set.
163      *
164      * @return integer
165      */
166     public function columnCount()
167     {
168         return $this->stmt->columnCount();
169     }
170
171     /**
172      * Fetches the SQLSTATE associated with the last operation on the statement.
173      *
174      * @return string
175      */
176     public function errorCode()
177     {
178         return $this->stmt->errorCode();
179     }
180
181     /**
182      * Fetches extended error information associated with the last operation on the statement.
183      *
184      * @return array
185      */
186     public function errorInfo()
187     {
188         return $this->stmt->errorInfo();
189     }
190
191     public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
192     {
193         if ($arg2 === null) {
194             return $this->stmt->setFetchMode($fetchMode);
195         } else if ($arg3 === null) {
196             return $this->stmt->setFetchMode($fetchMode, $arg2);
197         }
198
199         return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
200     }
201
202     public function getIterator()
203     {
204         return $this->stmt;
205     }
206
207     /**
208      * Fetches the next row from a result set.
209      *
210      * @param integer $fetchMode
211      * @return mixed The return value of this function on success depends on the fetch type.
212      *               In all cases, FALSE is returned on failure.
213      */
214     public function fetch($fetchMode = null)
215     {
216         return $this->stmt->fetch($fetchMode);
217     }
218
219     /**
220      * Returns an array containing all of the result set rows.
221      *
222      * @param integer $fetchMode
223      * @param mixed $fetchArgument
224      * @return array An array containing all of the remaining rows in the result set.
225      */
226     public function fetchAll($fetchMode = null, $fetchArgument = 0)
227     {
228         if ($fetchArgument !== 0) {
229             return $this->stmt->fetchAll($fetchMode, $fetchArgument);
230         }
231         return $this->stmt->fetchAll($fetchMode);
232     }
233
234     /**
235      * Returns a single column from the next row of a result set.
236      *
237      * @param integer $columnIndex
238      * @return mixed A single column from the next row of a result set or FALSE if there are no more rows.
239      */
240     public function fetchColumn($columnIndex = 0)
241     {
242         return $this->stmt->fetchColumn($columnIndex);
243     }
244
245     /**
246      * Returns the number of rows affected by the last execution of this statement.
247      *
248      * @return integer The number of affected rows.
249      */
250     public function rowCount()
251     {
252         return $this->stmt->rowCount();
253     }
254
255     /**
256      * Gets the wrapped driver statement.
257      *
258      * @return \Doctrine\DBAL\Driver\Statement
259      */
260     public function getWrappedStatement()
261     {
262         return $this->stmt;
263     }
264 }