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 LGPL. 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 Doctrine\DBAL\Driver\Statement The underlying driver statement. |
---|
47 | */ |
---|
48 | protected $stmt; |
---|
49 | /** |
---|
50 | * @var Doctrine\DBAL\Platforms\AbstractPlatform The underlying database platform. |
---|
51 | */ |
---|
52 | protected $platform; |
---|
53 | /** |
---|
54 | * @var Doctrine\DBAL\Connection The connection this statement is bound to and executed on. |
---|
55 | */ |
---|
56 | protected $conn; |
---|
57 | |
---|
58 | /** |
---|
59 | * Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>. |
---|
60 | * |
---|
61 | * @param string $sql The SQL of the statement. |
---|
62 | * @param Doctrine\DBAL\Connection The connection on which the statement should be executed. |
---|
63 | */ |
---|
64 | public function __construct($sql, Connection $conn) |
---|
65 | { |
---|
66 | $this->sql = $sql; |
---|
67 | $this->stmt = $conn->getWrappedConnection()->prepare($sql); |
---|
68 | $this->conn = $conn; |
---|
69 | $this->platform = $conn->getDatabasePlatform(); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * Binds a parameter value to the statement. |
---|
74 | * |
---|
75 | * The value can optionally be bound with a PDO binding type or a DBAL mapping type. |
---|
76 | * If bound with a DBAL mapping type, the binding type is derived from the mapping |
---|
77 | * type and the value undergoes the conversion routines of the mapping type before |
---|
78 | * being bound. |
---|
79 | * |
---|
80 | * @param $name The name or position of the parameter. |
---|
81 | * @param $value The value of the parameter. |
---|
82 | * @param mixed $type Either a PDO binding type or a DBAL mapping type name or instance. |
---|
83 | * @return boolean TRUE on success, FALSE on failure. |
---|
84 | */ |
---|
85 | public function bindValue($name, $value, $type = null) |
---|
86 | { |
---|
87 | $this->params[$name] = $value; |
---|
88 | if ($type !== null) { |
---|
89 | if (is_string($type)) { |
---|
90 | $type = Type::getType($type); |
---|
91 | } |
---|
92 | if ($type instanceof Type) { |
---|
93 | $value = $type->convertToDatabaseValue($value, $this->platform); |
---|
94 | $bindingType = $type->getBindingType(); |
---|
95 | } else { |
---|
96 | $bindingType = $type; // PDO::PARAM_* constants |
---|
97 | } |
---|
98 | return $this->stmt->bindValue($name, $value, $bindingType); |
---|
99 | } else { |
---|
100 | return $this->stmt->bindValue($name, $value); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * Binds a parameter to a value by reference. |
---|
106 | * |
---|
107 | * Binding a parameter by reference does not support DBAL mapping types. |
---|
108 | * |
---|
109 | * @param string $name The name or position of the parameter. |
---|
110 | * @param mixed $value The reference to the variable to bind |
---|
111 | * @param integer $type The PDO binding type. |
---|
112 | * @return boolean TRUE on success, FALSE on failure. |
---|
113 | */ |
---|
114 | public function bindParam($name, &$var, $type = PDO::PARAM_STR) |
---|
115 | { |
---|
116 | return $this->stmt->bindParam($name, $var, $type); |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * Executes the statement with the currently bound parameters. |
---|
121 | * |
---|
122 | * @return boolean TRUE on success, FALSE on failure. |
---|
123 | */ |
---|
124 | public function execute($params = null) |
---|
125 | { |
---|
126 | $hasLogger = $this->conn->getConfiguration()->getSQLLogger(); |
---|
127 | if ($hasLogger) { |
---|
128 | $this->conn->getConfiguration()->getSQLLogger()->startQuery($this->sql, $this->params); |
---|
129 | } |
---|
130 | |
---|
131 | $stmt = $this->stmt->execute($params); |
---|
132 | |
---|
133 | if ($hasLogger) { |
---|
134 | $this->conn->getConfiguration()->getSQLLogger()->stopQuery(); |
---|
135 | } |
---|
136 | $this->params = array(); |
---|
137 | return $stmt; |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * Closes the cursor, freeing the database resources used by this statement. |
---|
142 | * |
---|
143 | * @return boolean TRUE on success, FALSE on failure. |
---|
144 | */ |
---|
145 | public function closeCursor() |
---|
146 | { |
---|
147 | return $this->stmt->closeCursor(); |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * Returns the number of columns in the result set. |
---|
152 | * |
---|
153 | * @return integer |
---|
154 | */ |
---|
155 | public function columnCount() |
---|
156 | { |
---|
157 | return $this->stmt->columnCount(); |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * Fetches the SQLSTATE associated with the last operation on the statement. |
---|
162 | * |
---|
163 | * @return string |
---|
164 | */ |
---|
165 | public function errorCode() |
---|
166 | { |
---|
167 | return $this->stmt->errorCode(); |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * Fetches extended error information associated with the last operation on the statement. |
---|
172 | * |
---|
173 | * @return array |
---|
174 | */ |
---|
175 | public function errorInfo() |
---|
176 | { |
---|
177 | return $this->stmt->errorInfo(); |
---|
178 | } |
---|
179 | |
---|
180 | public function setFetchMode($fetchStyle) |
---|
181 | { |
---|
182 | return $this->stmt->setFetchMode($fetchStyle); |
---|
183 | } |
---|
184 | |
---|
185 | public function getIterator() |
---|
186 | { |
---|
187 | return $this->stmt; |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | * Fetches the next row from a result set. |
---|
192 | * |
---|
193 | * @param integer $fetchStyle |
---|
194 | * @return mixed The return value of this function on success depends on the fetch type. |
---|
195 | * In all cases, FALSE is returned on failure. |
---|
196 | */ |
---|
197 | public function fetch($fetchStyle = PDO::FETCH_BOTH) |
---|
198 | { |
---|
199 | return $this->stmt->fetch($fetchStyle); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * Returns an array containing all of the result set rows. |
---|
204 | * |
---|
205 | * @param integer $fetchStyle |
---|
206 | * @param mixed $fetchArgument |
---|
207 | * @return array An array containing all of the remaining rows in the result set. |
---|
208 | */ |
---|
209 | public function fetchAll($fetchStyle = PDO::FETCH_BOTH, $fetchArgument = 0) |
---|
210 | { |
---|
211 | if ($fetchArgument !== 0) { |
---|
212 | return $this->stmt->fetchAll($fetchStyle, $fetchArgument); |
---|
213 | } |
---|
214 | return $this->stmt->fetchAll($fetchStyle); |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | * Returns a single column from the next row of a result set. |
---|
219 | * |
---|
220 | * @param integer $columnIndex |
---|
221 | * @return mixed A single column from the next row of a result set or FALSE if there are no more rows. |
---|
222 | */ |
---|
223 | public function fetchColumn($columnIndex = 0) |
---|
224 | { |
---|
225 | return $this->stmt->fetchColumn($columnIndex); |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * Returns the number of rows affected by the last execution of this statement. |
---|
230 | * |
---|
231 | * @return integer The number of affected rows. |
---|
232 | */ |
---|
233 | public function rowCount() |
---|
234 | { |
---|
235 | return $this->stmt->rowCount(); |
---|
236 | } |
---|
237 | |
---|
238 | /** |
---|
239 | * Gets the wrapped driver statement. |
---|
240 | * |
---|
241 | * @return Doctrine\DBAL\Driver\Statement |
---|
242 | */ |
---|
243 | public function getWrappedStatement() |
---|
244 | { |
---|
245 | return $this->stmt; |
---|
246 | } |
---|
247 | } |
---|