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.
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>.
22 namespace Doctrine\DBAL;
25 Doctrine\DBAL\Types\Type,
26 Doctrine\DBAL\Driver\Statement as DriverStatement;
29 * A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support
30 * for logging, DBAL mapping types, etc.
32 * @author Roman Borschel <roman@code-factory.org>
35 class Statement implements \IteratorAggregate, DriverStatement
38 * @var string The SQL statement.
42 * @var array The bound parameters.
44 protected $params = array();
46 * @var array The parameter types
48 protected $types = array();
50 * @var \Doctrine\DBAL\Driver\Statement The underlying driver statement.
54 * @var \Doctrine\DBAL\Platforms\AbstractPlatform The underlying database platform.
58 * @var \Doctrine\DBAL\Connection The connection this statement is bound to and executed on.
63 * Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>.
65 * @param string $sql The SQL of the statement.
66 * @param \Doctrine\DBAL\Connection The connection on which the statement should be executed.
68 public function __construct($sql, Connection $conn)
71 $this->stmt = $conn->getWrappedConnection()->prepare($sql);
73 $this->platform = $conn->getDatabasePlatform();
77 * Binds a parameter value to the statement.
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
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.
89 public function bindValue($name, $value, $type = null)
91 $this->params[$name] = $value;
92 $this->types[$name] = $type;
94 if (is_string($type)) {
95 $type = Type::getType($type);
97 if ($type instanceof Type) {
98 $value = $type->convertToDatabaseValue($value, $this->platform);
99 $bindingType = $type->getBindingType();
101 $bindingType = $type; // PDO::PARAM_* constants
103 return $this->stmt->bindValue($name, $value, $bindingType);
105 return $this->stmt->bindValue($name, $value);
110 * Binds a parameter to a value by reference.
112 * Binding a parameter by reference does not support DBAL mapping types.
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.
119 public function bindParam($name, &$var, $type = PDO::PARAM_STR, $length = null)
121 return $this->stmt->bindParam($name, $var, $type, $length );
125 * Executes the statement with the currently bound parameters.
127 * @param array $params
128 * @return boolean TRUE on success, FALSE on failure.
130 public function execute($params = null)
132 $logger = $this->conn->getConfiguration()->getSQLLogger();
134 $logger->startQuery($this->sql, $this->params, $this->types);
138 $stmt = $this->stmt->execute($params);
139 } catch (\Exception $ex) {
140 throw DBALException::driverExceptionDuringQuery($ex, $this->sql, $this->conn->resolveParams($this->params, $this->types));
144 $logger->stopQuery();
146 $this->params = array();
147 $this->types = array();
152 * Closes the cursor, freeing the database resources used by this statement.
154 * @return boolean TRUE on success, FALSE on failure.
156 public function closeCursor()
158 return $this->stmt->closeCursor();
162 * Returns the number of columns in the result set.
166 public function columnCount()
168 return $this->stmt->columnCount();
172 * Fetches the SQLSTATE associated with the last operation on the statement.
176 public function errorCode()
178 return $this->stmt->errorCode();
182 * Fetches extended error information associated with the last operation on the statement.
186 public function errorInfo()
188 return $this->stmt->errorInfo();
191 public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
193 if ($arg2 === null) {
194 return $this->stmt->setFetchMode($fetchMode);
195 } else if ($arg3 === null) {
196 return $this->stmt->setFetchMode($fetchMode, $arg2);
199 return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
202 public function getIterator()
208 * Fetches the next row from a result set.
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.
214 public function fetch($fetchMode = null)
216 return $this->stmt->fetch($fetchMode);
220 * Returns an array containing all of the result set rows.
222 * @param integer $fetchMode
223 * @param mixed $fetchArgument
224 * @return array An array containing all of the remaining rows in the result set.
226 public function fetchAll($fetchMode = null, $fetchArgument = 0)
228 if ($fetchArgument !== 0) {
229 return $this->stmt->fetchAll($fetchMode, $fetchArgument);
231 return $this->stmt->fetchAll($fetchMode);
235 * Returns a single column from the next row of a result set.
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.
240 public function fetchColumn($columnIndex = 0)
242 return $this->stmt->fetchColumn($columnIndex);
246 * Returns the number of rows affected by the last execution of this statement.
248 * @return integer The number of affected rows.
250 public function rowCount()
252 return $this->stmt->rowCount();
256 * Gets the wrapped driver statement.
258 * @return \Doctrine\DBAL\Driver\Statement
260 public function getWrappedStatement()