|
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 DriverStatement |
|
36 { |
|
37 /** |
|
38 * @var string The SQL statement. |
|
39 */ |
|
40 private $_sql; |
|
41 /** |
|
42 * @var array The bound parameters. |
|
43 */ |
|
44 private $_params = array(); |
|
45 /** |
|
46 * @var Doctrine\DBAL\Driver\Statement The underlying driver statement. |
|
47 */ |
|
48 private $_stmt; |
|
49 /** |
|
50 * @var Doctrine\DBAL\Platforms\AbstractPlatform The underlying database platform. |
|
51 */ |
|
52 private $_platform; |
|
53 /** |
|
54 * @var Doctrine\DBAL\Connection The connection this statement is bound to and executed on. |
|
55 */ |
|
56 private $_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 /** |
|
181 * Fetches the next row from a result set. |
|
182 * |
|
183 * @param integer $fetchStyle |
|
184 * @return mixed The return value of this function on success depends on the fetch type. |
|
185 * In all cases, FALSE is returned on failure. |
|
186 */ |
|
187 public function fetch($fetchStyle = PDO::FETCH_BOTH) |
|
188 { |
|
189 return $this->_stmt->fetch($fetchStyle); |
|
190 } |
|
191 |
|
192 /** |
|
193 * Returns an array containing all of the result set rows. |
|
194 * |
|
195 * @param integer $fetchStyle |
|
196 * @param integer $columnIndex |
|
197 * @return array An array containing all of the remaining rows in the result set. |
|
198 */ |
|
199 public function fetchAll($fetchStyle = PDO::FETCH_BOTH, $columnIndex = 0) |
|
200 { |
|
201 if ($columnIndex != 0) { |
|
202 return $this->_stmt->fetchAll($fetchStyle, $columnIndex); |
|
203 } |
|
204 return $this->_stmt->fetchAll($fetchStyle); |
|
205 } |
|
206 |
|
207 /** |
|
208 * Returns a single column from the next row of a result set. |
|
209 * |
|
210 * @param integer $columnIndex |
|
211 * @return mixed A single column from the next row of a result set or FALSE if there are no more rows. |
|
212 */ |
|
213 public function fetchColumn($columnIndex = 0) |
|
214 { |
|
215 return $this->_stmt->fetchColumn($columnIndex); |
|
216 } |
|
217 |
|
218 /** |
|
219 * Returns the number of rows affected by the last execution of this statement. |
|
220 * |
|
221 * @return integer The number of affected rows. |
|
222 */ |
|
223 public function rowCount() |
|
224 { |
|
225 return $this->_stmt->rowCount(); |
|
226 } |
|
227 |
|
228 /** |
|
229 * Gets the wrapped driver statement. |
|
230 * |
|
231 * @return Doctrine\DBAL\Driver\Statement |
|
232 */ |
|
233 public function getWrappedStatement() |
|
234 { |
|
235 return $this->_stmt; |
|
236 } |
|
237 } |