|
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\Driver\IBMDB2; |
|
23 |
|
24 class DB2Statement implements \Doctrine\DBAL\Driver\Statement |
|
25 { |
|
26 private $_stmt = null; |
|
27 |
|
28 private $_bindParam = array(); |
|
29 |
|
30 /** |
|
31 * DB2_BINARY, DB2_CHAR, DB2_DOUBLE, or DB2_LONG |
|
32 * @var <type> |
|
33 */ |
|
34 static private $_typeMap = array( |
|
35 \PDO::PARAM_INT => DB2_LONG, |
|
36 \PDO::PARAM_STR => DB2_CHAR, |
|
37 ); |
|
38 |
|
39 public function __construct($stmt) |
|
40 { |
|
41 $this->_stmt = $stmt; |
|
42 } |
|
43 |
|
44 /** |
|
45 * Binds a value to a corresponding named or positional |
|
46 * placeholder in the SQL statement that was used to prepare the statement. |
|
47 * |
|
48 * @param mixed $param Parameter identifier. For a prepared statement using named placeholders, |
|
49 * this will be a parameter name of the form :name. For a prepared statement |
|
50 * using question mark placeholders, this will be the 1-indexed position of the parameter |
|
51 * |
|
52 * @param mixed $value The value to bind to the parameter. |
|
53 * @param integer $type Explicit data type for the parameter using the PDO::PARAM_* constants. |
|
54 * |
|
55 * @return boolean Returns TRUE on success or FALSE on failure. |
|
56 */ |
|
57 function bindValue($param, $value, $type = null) |
|
58 { |
|
59 return $this->bindParam($param, $value, $type); |
|
60 } |
|
61 |
|
62 /** |
|
63 * Binds a PHP variable to a corresponding named or question mark placeholder in the |
|
64 * SQL statement that was use to prepare the statement. Unlike PDOStatement->bindValue(), |
|
65 * the variable is bound as a reference and will only be evaluated at the time |
|
66 * that PDOStatement->execute() is called. |
|
67 * |
|
68 * Most parameters are input parameters, that is, parameters that are |
|
69 * used in a read-only fashion to build up the query. Some drivers support the invocation |
|
70 * of stored procedures that return data as output parameters, and some also as input/output |
|
71 * parameters that both send in data and are updated to receive it. |
|
72 * |
|
73 * @param mixed $param Parameter identifier. For a prepared statement using named placeholders, |
|
74 * this will be a parameter name of the form :name. For a prepared statement |
|
75 * using question mark placeholders, this will be the 1-indexed position of the parameter |
|
76 * |
|
77 * @param mixed $variable Name of the PHP variable to bind to the SQL statement parameter. |
|
78 * |
|
79 * @param integer $type Explicit data type for the parameter using the PDO::PARAM_* constants. To return |
|
80 * an INOUT parameter from a stored procedure, use the bitwise OR operator to set the |
|
81 * PDO::PARAM_INPUT_OUTPUT bits for the data_type parameter. |
|
82 * @return boolean Returns TRUE on success or FALSE on failure. |
|
83 */ |
|
84 function bindParam($column, &$variable, $type = null) |
|
85 { |
|
86 $this->_bindParam[$column] =& $variable; |
|
87 |
|
88 if ($type && isset(self::$_typeMap[$type])) { |
|
89 $type = self::$_typeMap[$type]; |
|
90 } else { |
|
91 $type = DB2_CHAR; |
|
92 } |
|
93 |
|
94 if (!db2_bind_param($this->_stmt, $column, "variable", DB2_PARAM_IN, $type)) { |
|
95 throw new DB2Exception(db2_stmt_errormsg()); |
|
96 } |
|
97 return true; |
|
98 } |
|
99 |
|
100 /** |
|
101 * Closes the cursor, enabling the statement to be executed again. |
|
102 * |
|
103 * @return boolean Returns TRUE on success or FALSE on failure. |
|
104 */ |
|
105 function closeCursor() |
|
106 { |
|
107 if (!$this->_stmt) { |
|
108 return false; |
|
109 } |
|
110 |
|
111 $this->_bindParam = array(); |
|
112 db2_free_result($this->_stmt); |
|
113 $ret = db2_free_stmt($this->_stmt); |
|
114 $this->_stmt = false; |
|
115 return $ret; |
|
116 } |
|
117 |
|
118 /** |
|
119 * columnCount |
|
120 * Returns the number of columns in the result set |
|
121 * |
|
122 * @return integer Returns the number of columns in the result set represented |
|
123 * by the PDOStatement object. If there is no result set, |
|
124 * this method should return 0. |
|
125 */ |
|
126 function columnCount() |
|
127 { |
|
128 if (!$this->_stmt) { |
|
129 return false; |
|
130 } |
|
131 return db2_num_fields($this->_stmt); |
|
132 } |
|
133 |
|
134 /** |
|
135 * errorCode |
|
136 * Fetch the SQLSTATE associated with the last operation on the statement handle |
|
137 * |
|
138 * @see Doctrine_Adapter_Interface::errorCode() |
|
139 * @return string error code string |
|
140 */ |
|
141 function errorCode() |
|
142 { |
|
143 return db2_stmt_error(); |
|
144 } |
|
145 |
|
146 /** |
|
147 * errorInfo |
|
148 * Fetch extended error information associated with the last operation on the statement handle |
|
149 * |
|
150 * @see Doctrine_Adapter_Interface::errorInfo() |
|
151 * @return array error info array |
|
152 */ |
|
153 function errorInfo() |
|
154 { |
|
155 return array( |
|
156 0 => db2_stmt_errormsg(), |
|
157 1 => db2_stmt_error(), |
|
158 ); |
|
159 } |
|
160 |
|
161 /** |
|
162 * Executes a prepared statement |
|
163 * |
|
164 * If the prepared statement included parameter markers, you must either: |
|
165 * call PDOStatement->bindParam() to bind PHP variables to the parameter markers: |
|
166 * bound variables pass their value as input and receive the output value, |
|
167 * if any, of their associated parameter markers or pass an array of input-only |
|
168 * parameter values |
|
169 * |
|
170 * |
|
171 * @param array $params An array of values with as many elements as there are |
|
172 * bound parameters in the SQL statement being executed. |
|
173 * @return boolean Returns TRUE on success or FALSE on failure. |
|
174 */ |
|
175 function execute($params = null) |
|
176 { |
|
177 if (!$this->_stmt) { |
|
178 return false; |
|
179 } |
|
180 |
|
181 /*$retval = true; |
|
182 if ($params !== null) { |
|
183 $retval = @db2_execute($this->_stmt, $params); |
|
184 } else { |
|
185 $retval = @db2_execute($this->_stmt); |
|
186 }*/ |
|
187 if ($params === null) { |
|
188 ksort($this->_bindParam); |
|
189 $params = array_values($this->_bindParam); |
|
190 } |
|
191 $retval = @db2_execute($this->_stmt, $params); |
|
192 |
|
193 if ($retval === false) { |
|
194 throw new DB2Exception(db2_stmt_errormsg()); |
|
195 } |
|
196 return $retval; |
|
197 } |
|
198 |
|
199 /** |
|
200 * fetch |
|
201 * |
|
202 * @see Query::HYDRATE_* constants |
|
203 * @param integer $fetchStyle Controls how the next row will be returned to the caller. |
|
204 * This value must be one of the Query::HYDRATE_* constants, |
|
205 * defaulting to Query::HYDRATE_BOTH |
|
206 * |
|
207 * @param integer $cursorOrientation For a PDOStatement object representing a scrollable cursor, |
|
208 * this value determines which row will be returned to the caller. |
|
209 * This value must be one of the Query::HYDRATE_ORI_* constants, defaulting to |
|
210 * Query::HYDRATE_ORI_NEXT. To request a scrollable cursor for your |
|
211 * PDOStatement object, |
|
212 * you must set the PDO::ATTR_CURSOR attribute to Doctrine::CURSOR_SCROLL when you |
|
213 * prepare the SQL statement with Doctrine_Adapter_Interface->prepare(). |
|
214 * |
|
215 * @param integer $cursorOffset For a PDOStatement object representing a scrollable cursor for which the |
|
216 * $cursorOrientation parameter is set to Query::HYDRATE_ORI_ABS, this value specifies |
|
217 * the absolute number of the row in the result set that shall be fetched. |
|
218 * |
|
219 * For a PDOStatement object representing a scrollable cursor for |
|
220 * which the $cursorOrientation parameter is set to Query::HYDRATE_ORI_REL, this value |
|
221 * specifies the row to fetch relative to the cursor position before |
|
222 * PDOStatement->fetch() was called. |
|
223 * |
|
224 * @return mixed |
|
225 */ |
|
226 function fetch($fetchStyle = \PDO::FETCH_BOTH) |
|
227 { |
|
228 switch ($fetchStyle) { |
|
229 case \PDO::FETCH_BOTH: |
|
230 return db2_fetch_both($this->_stmt); |
|
231 case \PDO::FETCH_ASSOC: |
|
232 return db2_fetch_assoc($this->_stmt); |
|
233 case \PDO::FETCH_NUM: |
|
234 return db2_fetch_array($this->_stmt); |
|
235 default: |
|
236 throw new DB2Exception("Given Fetch-Style " . $fetchStyle . " is not supported."); |
|
237 } |
|
238 } |
|
239 |
|
240 /** |
|
241 * Returns an array containing all of the result set rows |
|
242 * |
|
243 * @param integer $fetchStyle Controls how the next row will be returned to the caller. |
|
244 * This value must be one of the Query::HYDRATE_* constants, |
|
245 * defaulting to Query::HYDRATE_BOTH |
|
246 * |
|
247 * @param integer $columnIndex Returns the indicated 0-indexed column when the value of $fetchStyle is |
|
248 * Query::HYDRATE_COLUMN. Defaults to 0. |
|
249 * |
|
250 * @return array |
|
251 */ |
|
252 function fetchAll($fetchStyle = \PDO::FETCH_BOTH) |
|
253 { |
|
254 $rows = array(); |
|
255 while ($row = $this->fetch($fetchStyle)) { |
|
256 $rows[] = $row; |
|
257 } |
|
258 return $rows; |
|
259 } |
|
260 |
|
261 /** |
|
262 * fetchColumn |
|
263 * Returns a single column from the next row of a |
|
264 * result set or FALSE if there are no more rows. |
|
265 * |
|
266 * @param integer $columnIndex 0-indexed number of the column you wish to retrieve from the row. If no |
|
267 * value is supplied, PDOStatement->fetchColumn() |
|
268 * fetches the first column. |
|
269 * |
|
270 * @return string returns a single column in the next row of a result set. |
|
271 */ |
|
272 function fetchColumn($columnIndex = 0) |
|
273 { |
|
274 $row = $this->fetch(\PDO::FETCH_NUM); |
|
275 if ($row && isset($row[$columnIndex])) { |
|
276 return $row[$columnIndex]; |
|
277 } |
|
278 return false; |
|
279 } |
|
280 |
|
281 /** |
|
282 * rowCount |
|
283 * rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement |
|
284 * executed by the corresponding object. |
|
285 * |
|
286 * If the last SQL statement executed by the associated Statement object was a SELECT statement, |
|
287 * some databases may return the number of rows returned by that statement. However, |
|
288 * this behaviour is not guaranteed for all databases and should not be |
|
289 * relied on for portable applications. |
|
290 * |
|
291 * @return integer Returns the number of rows. |
|
292 */ |
|
293 function rowCount() |
|
294 { |
|
295 return (@db2_num_rows($this->_stmt))?:0; |
|
296 } |
|
297 } |